Skip to content

Commit

Permalink
Merge pull request #462 from tcet-opensource/461_addingsessionstofaculty
Browse files Browse the repository at this point in the history
added sessions and transactions to employee addition
  • Loading branch information
Aastha-S-Rai authored Dec 27, 2023
2 parents 16aa476 + aee3bdd commit 87924bc
Show file tree
Hide file tree
Showing 13 changed files with 696 additions and 289 deletions.
47 changes: 46 additions & 1 deletion controller/faculty.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
import mongoose from "mongoose";
import {
createFaculty,
facultyList,
deleteFacultyById,
updateFacultyById,
} from "#services/faculty";
import {
createEmployeeBank,
// employeeBankList,
// deleteEmployeeBankById,
// updateEmployeeBankById,
} from "#services/employee/empBank";
import {
addNewEmployeeCurrent,
// getEmployeeCurrent,
// deleteEmployeeCurrentById,
// updateEmployeeCurrentById,
} from "#services/employee/empCurrentDetail";
import {
createEmployeeEducationHistory,
// employeeEducationHistoryList,
// deleteEmployeeEducationHistoryById,
// updateEmployeeEducationHistoryById,
} from "#services/employee/empEduHistory";
import {
addNewEmployeePersonal,
// getEmployeePersonal,
// deleteEmployeePersonalById,
// updateEmployeePersonalById,
} from "#services/employee/empPersonal";
import { logger } from "#util";

async function addFaculty(req, res) {
Expand All @@ -23,7 +48,13 @@ async function addFaculty(req, res) {
designation,
natureOfAssociation,
additionalResponsibilities,
employeePersonalDetails,
employeeBankDetails,
employeeCurrentDetails,
employeeEducationDetails,
} = req.body;
const session = await mongoose.startSession();
session.startTransaction();
try {
const newFaculty = await createFaculty(
ERPID,
Expand All @@ -41,9 +72,23 @@ async function addFaculty(req, res) {
designation,
natureOfAssociation,
additionalResponsibilities,
session,
);
res.json({ res: `added faculty ${newFaculty.ERPID}` });
await Promise.all([
addNewEmployeePersonal(employeePersonalDetails, session),
createEmployeeEducationHistory(employeeEducationDetails, session),
addNewEmployeeCurrent(employeeCurrentDetails, session),
createEmployeeBank(employeeBankDetails, session),
]);
res.json({
res: `added faculty ${newFaculty.ERPID}`,
id: newFaculty.ERPID,
});
await session.commitTransaction();
session.endSession();
} catch (error) {
await session.abortTransaction();
session.endSession();
logger.error("Error while inserting", error);
res.status(500);
res.json({ err: "Error while inserting in DB" });
Expand Down
16 changes: 8 additions & 8 deletions models/employee/empBank.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,39 @@ const employeeBankSchema = {
required: true,
unique: true,
},
bank_name: {
bankName: {
type: String,
required: true,
minLength: 7,
},
bank_acc: {
bankAcc: {
type: String,
required: true,
unique: true,
},
bank_branch: {
bankBranch: {
type: String,
required: true,
},
bank_ifsc: {
bankIfsc: {
type: String,
required: true,
maxLength: 11,
minLength: 11,
},
bank_micr: {
bankMicr: {
type: String,
required: true,
maxLength: 9,
minLength: 9,
},
appointment_approve_sg_dte: {
appointmentApproveSgDte: {
type: String,
},
};

// eslint-disable-next-line no-unused-vars
const EmployeeBank = connector.model("Employee bank", employeeBankSchema);
const EmployeeBank = connector.model("Employee Bank", employeeBankSchema);

/// crud operation///

Expand All @@ -64,7 +64,7 @@ async function create(employeeBankData) {
appointmentApproveSgDte,
});

const empBankDoc = await empBank.save();
const empBankDoc = await empBank.save({ session: employeeBankData.session });
return empBankDoc;
}

Expand Down
17 changes: 9 additions & 8 deletions models/employee/empCurrentDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import connector from "#models/databaseUtil";

const employeeCurrentEmploymentSchema = {
uid: { type: String, require: true },
date_of_joining: { type: Date, required: true },
department_name: { type: String, required: true },
dateOfJoining: { type: Date, required: true },
departmentName: { type: String, required: true },
designation: { type: String, required: true },
job_status: { type: String, required: true },
job_profile: { type: String, required: true },
current_ctc: { type: Number, required: true },
jobStatus: { type: String, required: true },
jobProfile: { type: String, required: true },
currentCtc: { type: Number, required: true },
};

// eslint-disable-next-line no-unused-vars
const EmployeeCurrentEmployment = connector.model(
"EmployeeCurrentEmployement",
"Employee Employment",
employeeCurrentEmploymentSchema,
);

Expand All @@ -26,7 +26,6 @@ async function create(employeeCurrentEmploymentData) {
jobProfile,
currentCtc,
} = employeeCurrentEmploymentData;

const empCurEmp = new EmployeeCurrentEmployment({
uid,
dateOfJoining,
Expand All @@ -37,7 +36,9 @@ async function create(employeeCurrentEmploymentData) {
currentCtc,
});

const empCurrentEmploymentDoc = await empCurEmp.save();
const empCurrentEmploymentDoc = await empCurEmp.save({
session: employeeCurrentEmploymentData.session,
});
return empCurrentEmploymentDoc;
}

Expand Down
49 changes: 6 additions & 43 deletions models/employee/empEduHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,56 +31,17 @@ const employeeEducationHistorySchema = {

// eslint-disable-next-line no-unused-vars
const EmployeeEducationHistory = connector.model(
"Employee education history",
"Employee Education",
employeeEducationHistorySchema,
);

// CRUD Operations

async function create(employeeEducationHistoryData) {
const {
educationType,
educationName,
specialization,
period,
institutionName,
university,
passingDivision,
fromYear,
uptoYear,
registrationNumber,
aggregatePct,
finalYearPct,
numberOfAttempts,
rank,
passingYear,
uid,
ssc,
hsc,
dip,
iti,
deg,
pgd,
phd,
pdoc,
} = employeeEducationHistoryData;
const { uid, ssc, hsc, dip, iti, deg, pgd, phd, pdoc } =
employeeEducationHistoryData;

const empEduHistory = new EmployeeEducationHistory({
educationType,
educationName,
specialization,
period,
institutionName,
university,
passingDivision,
fromYear,
uptoYear,
registrationNumber,
aggregatePct,
finalYearPct,
numberOfAttempts,
rank,
passingYear,
uid,
ssc,
hsc,
Expand All @@ -92,7 +53,9 @@ async function create(employeeEducationHistoryData) {
pdoc,
});

const empEduHistoryDoc = await empEduHistory.save();
const empEduHistoryDoc = await empEduHistory.save({
session: employeeEducationHistoryData.session,
});
return empEduHistoryDoc;
}

Expand Down
8 changes: 5 additions & 3 deletions models/employee/empPersonal.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const employeePersonalSchema = {
motherTongue: { type: String, required: true },
gender: { type: String, required: true },
religion: { type: String, required: true },
numOfChildren: { type: Number },
numOfChildren: { type: Number, required: true },
originalCastCategory: { type: String, required: true },
caste: { type: String, required: true },
subCaste: { type: String, required: true },
Expand Down Expand Up @@ -61,14 +61,16 @@ const employeePersonalSchema = {
previousLastName: { type: String },
};
const EmployeePersonal = connector.model(
"EmplyeePersonalData",
"Employee Personal",
employeePersonalSchema,
);
/// CRUD operations ///

async function create(employeePersonalData) {
const employeePersonal = new EmployeePersonal(employeePersonalData);
const employeePersonalDoc = await employeePersonal.save();
const employeePersonalDoc = await employeePersonal.save({
session: employeePersonalData.session,
});
return employeePersonalDoc;
}

Expand Down
2 changes: 1 addition & 1 deletion models/faculty.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async function remove(filter) {

async function create(facultyData) {
const faculty = new Faculty(facultyData);
const facultyDoc = await faculty.save();
const facultyDoc = await faculty.save({ session: facultyData.session });
return facultyDoc;
}

Expand Down
20 changes: 11 additions & 9 deletions services/employee/empBank.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import EmployeeBank from "#models/employee/empBank";
import databaseError from "#error/database";

export async function createEmployeeBank(
uid,
bankName,
bankAcc,
bankBranch,
bankIfsc,
bankMicr,
appointmentApproveSgDte,
) {
export async function createEmployeeBank(empBankDetails, session) {
const {
uid,
bankName,
bankAcc,
bankBranch,
bankIfsc,
bankMicr,
appointmentApproveSgDte,
} = empBankDetails;
const newEmployeeBank = await EmployeeBank.create({
uid,
bankName,
Expand All @@ -18,6 +19,7 @@ export async function createEmployeeBank(
bankIfsc,
bankMicr,
appointmentApproveSgDte,
session,
});
if (newEmployeeBank.uid === uid) {
return newEmployeeBank;
Expand Down
22 changes: 12 additions & 10 deletions services/employee/empCurrentDetail.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import databaseError from "#error/database";
import EmployeeCurrent from "#models/employee/empPersonal";
import EmployeeCurrent from "#models/employee/empCurrentDetail";

export async function addNewEmployeeCurrent(
uid,
dateOfJoining,
departmentName,
designation,
jobStatus,
jobProfile,
currentCtc,
) {
export async function addNewEmployeeCurrent(basicEmpDetails, session) {
const {
uid,
dateOfJoining,
departmentName,
designation,
jobStatus,
jobProfile,
currentCtc,
} = basicEmpDetails;
const newEmployeeCurrent = await EmployeeCurrent.create({
uid,
dateOfJoining,
Expand All @@ -18,6 +19,7 @@ export async function addNewEmployeeCurrent(
jobStatus,
jobProfile,
currentCtc,
session,
});
if (newEmployeeCurrent.uid === uid) {
return newEmployeeCurrent;
Expand Down
Loading

0 comments on commit 87924bc

Please sign in to comment.