Skip to content

Commit 87924bc

Browse files
authored
Merge pull request #462 from tcet-opensource/461_addingsessionstofaculty
added sessions and transactions to employee addition
2 parents 16aa476 + aee3bdd commit 87924bc

File tree

13 files changed

+696
-289
lines changed

13 files changed

+696
-289
lines changed

controller/faculty.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,34 @@
1+
import mongoose from "mongoose";
12
import {
23
createFaculty,
34
facultyList,
45
deleteFacultyById,
56
updateFacultyById,
67
} from "#services/faculty";
8+
import {
9+
createEmployeeBank,
10+
// employeeBankList,
11+
// deleteEmployeeBankById,
12+
// updateEmployeeBankById,
13+
} from "#services/employee/empBank";
14+
import {
15+
addNewEmployeeCurrent,
16+
// getEmployeeCurrent,
17+
// deleteEmployeeCurrentById,
18+
// updateEmployeeCurrentById,
19+
} from "#services/employee/empCurrentDetail";
20+
import {
21+
createEmployeeEducationHistory,
22+
// employeeEducationHistoryList,
23+
// deleteEmployeeEducationHistoryById,
24+
// updateEmployeeEducationHistoryById,
25+
} from "#services/employee/empEduHistory";
26+
import {
27+
addNewEmployeePersonal,
28+
// getEmployeePersonal,
29+
// deleteEmployeePersonalById,
30+
// updateEmployeePersonalById,
31+
} from "#services/employee/empPersonal";
732
import { logger } from "#util";
833

934
async function addFaculty(req, res) {
@@ -23,7 +48,13 @@ async function addFaculty(req, res) {
2348
designation,
2449
natureOfAssociation,
2550
additionalResponsibilities,
51+
employeePersonalDetails,
52+
employeeBankDetails,
53+
employeeCurrentDetails,
54+
employeeEducationDetails,
2655
} = req.body;
56+
const session = await mongoose.startSession();
57+
session.startTransaction();
2758
try {
2859
const newFaculty = await createFaculty(
2960
ERPID,
@@ -41,9 +72,23 @@ async function addFaculty(req, res) {
4172
designation,
4273
natureOfAssociation,
4374
additionalResponsibilities,
75+
session,
4476
);
45-
res.json({ res: `added faculty ${newFaculty.ERPID}` });
77+
await Promise.all([
78+
addNewEmployeePersonal(employeePersonalDetails, session),
79+
createEmployeeEducationHistory(employeeEducationDetails, session),
80+
addNewEmployeeCurrent(employeeCurrentDetails, session),
81+
createEmployeeBank(employeeBankDetails, session),
82+
]);
83+
res.json({
84+
res: `added faculty ${newFaculty.ERPID}`,
85+
id: newFaculty.ERPID,
86+
});
87+
await session.commitTransaction();
88+
session.endSession();
4689
} catch (error) {
90+
await session.abortTransaction();
91+
session.endSession();
4792
logger.error("Error while inserting", error);
4893
res.status(500);
4994
res.json({ err: "Error while inserting in DB" });

models/employee/empBank.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,39 @@ const employeeBankSchema = {
66
required: true,
77
unique: true,
88
},
9-
bank_name: {
9+
bankName: {
1010
type: String,
1111
required: true,
1212
minLength: 7,
1313
},
14-
bank_acc: {
14+
bankAcc: {
1515
type: String,
1616
required: true,
1717
unique: true,
1818
},
19-
bank_branch: {
19+
bankBranch: {
2020
type: String,
2121
required: true,
2222
},
23-
bank_ifsc: {
23+
bankIfsc: {
2424
type: String,
2525
required: true,
2626
maxLength: 11,
2727
minLength: 11,
2828
},
29-
bank_micr: {
29+
bankMicr: {
3030
type: String,
3131
required: true,
3232
maxLength: 9,
3333
minLength: 9,
3434
},
35-
appointment_approve_sg_dte: {
35+
appointmentApproveSgDte: {
3636
type: String,
3737
},
3838
};
3939

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

4343
/// crud operation///
4444

@@ -64,7 +64,7 @@ async function create(employeeBankData) {
6464
appointmentApproveSgDte,
6565
});
6666

67-
const empBankDoc = await empBank.save();
67+
const empBankDoc = await empBank.save({ session: employeeBankData.session });
6868
return empBankDoc;
6969
}
7070

models/employee/empCurrentDetail.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import connector from "#models/databaseUtil";
22

33
const employeeCurrentEmploymentSchema = {
44
uid: { type: String, require: true },
5-
date_of_joining: { type: Date, required: true },
6-
department_name: { type: String, required: true },
5+
dateOfJoining: { type: Date, required: true },
6+
departmentName: { type: String, required: true },
77
designation: { type: String, required: true },
8-
job_status: { type: String, required: true },
9-
job_profile: { type: String, required: true },
10-
current_ctc: { type: Number, required: true },
8+
jobStatus: { type: String, required: true },
9+
jobProfile: { type: String, required: true },
10+
currentCtc: { type: Number, required: true },
1111
};
1212

1313
// eslint-disable-next-line no-unused-vars
1414
const EmployeeCurrentEmployment = connector.model(
15-
"EmployeeCurrentEmployement",
15+
"Employee Employment",
1616
employeeCurrentEmploymentSchema,
1717
);
1818

@@ -26,7 +26,6 @@ async function create(employeeCurrentEmploymentData) {
2626
jobProfile,
2727
currentCtc,
2828
} = employeeCurrentEmploymentData;
29-
3029
const empCurEmp = new EmployeeCurrentEmployment({
3130
uid,
3231
dateOfJoining,
@@ -37,7 +36,9 @@ async function create(employeeCurrentEmploymentData) {
3736
currentCtc,
3837
});
3938

40-
const empCurrentEmploymentDoc = await empCurEmp.save();
39+
const empCurrentEmploymentDoc = await empCurEmp.save({
40+
session: employeeCurrentEmploymentData.session,
41+
});
4142
return empCurrentEmploymentDoc;
4243
}
4344

models/employee/empEduHistory.js

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -31,56 +31,17 @@ const employeeEducationHistorySchema = {
3131

3232
// eslint-disable-next-line no-unused-vars
3333
const EmployeeEducationHistory = connector.model(
34-
"Employee education history",
34+
"Employee Education",
3535
employeeEducationHistorySchema,
3636
);
3737

3838
// CRUD Operations
3939

4040
async function create(employeeEducationHistoryData) {
41-
const {
42-
educationType,
43-
educationName,
44-
specialization,
45-
period,
46-
institutionName,
47-
university,
48-
passingDivision,
49-
fromYear,
50-
uptoYear,
51-
registrationNumber,
52-
aggregatePct,
53-
finalYearPct,
54-
numberOfAttempts,
55-
rank,
56-
passingYear,
57-
uid,
58-
ssc,
59-
hsc,
60-
dip,
61-
iti,
62-
deg,
63-
pgd,
64-
phd,
65-
pdoc,
66-
} = employeeEducationHistoryData;
41+
const { uid, ssc, hsc, dip, iti, deg, pgd, phd, pdoc } =
42+
employeeEducationHistoryData;
6743

6844
const empEduHistory = new EmployeeEducationHistory({
69-
educationType,
70-
educationName,
71-
specialization,
72-
period,
73-
institutionName,
74-
university,
75-
passingDivision,
76-
fromYear,
77-
uptoYear,
78-
registrationNumber,
79-
aggregatePct,
80-
finalYearPct,
81-
numberOfAttempts,
82-
rank,
83-
passingYear,
8445
uid,
8546
ssc,
8647
hsc,
@@ -92,7 +53,9 @@ async function create(employeeEducationHistoryData) {
9253
pdoc,
9354
});
9455

95-
const empEduHistoryDoc = await empEduHistory.save();
56+
const empEduHistoryDoc = await empEduHistory.save({
57+
session: employeeEducationHistoryData.session,
58+
});
9659
return empEduHistoryDoc;
9760
}
9861

models/employee/empPersonal.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const employeePersonalSchema = {
1212
motherTongue: { type: String, required: true },
1313
gender: { type: String, required: true },
1414
religion: { type: String, required: true },
15-
numOfChildren: { type: Number },
15+
numOfChildren: { type: Number, required: true },
1616
originalCastCategory: { type: String, required: true },
1717
caste: { type: String, required: true },
1818
subCaste: { type: String, required: true },
@@ -61,14 +61,16 @@ const employeePersonalSchema = {
6161
previousLastName: { type: String },
6262
};
6363
const EmployeePersonal = connector.model(
64-
"EmplyeePersonalData",
64+
"Employee Personal",
6565
employeePersonalSchema,
6666
);
6767
/// CRUD operations ///
6868

6969
async function create(employeePersonalData) {
7070
const employeePersonal = new EmployeePersonal(employeePersonalData);
71-
const employeePersonalDoc = await employeePersonal.save();
71+
const employeePersonalDoc = await employeePersonal.save({
72+
session: employeePersonalData.session,
73+
});
7274
return employeePersonalDoc;
7375
}
7476

models/faculty.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ async function remove(filter) {
5252

5353
async function create(facultyData) {
5454
const faculty = new Faculty(facultyData);
55-
const facultyDoc = await faculty.save();
55+
const facultyDoc = await faculty.save({ session: facultyData.session });
5656
return facultyDoc;
5757
}
5858

services/employee/empBank.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import EmployeeBank from "#models/employee/empBank";
22
import databaseError from "#error/database";
33

4-
export async function createEmployeeBank(
5-
uid,
6-
bankName,
7-
bankAcc,
8-
bankBranch,
9-
bankIfsc,
10-
bankMicr,
11-
appointmentApproveSgDte,
12-
) {
4+
export async function createEmployeeBank(empBankDetails, session) {
5+
const {
6+
uid,
7+
bankName,
8+
bankAcc,
9+
bankBranch,
10+
bankIfsc,
11+
bankMicr,
12+
appointmentApproveSgDte,
13+
} = empBankDetails;
1314
const newEmployeeBank = await EmployeeBank.create({
1415
uid,
1516
bankName,
@@ -18,6 +19,7 @@ export async function createEmployeeBank(
1819
bankIfsc,
1920
bankMicr,
2021
appointmentApproveSgDte,
22+
session,
2123
});
2224
if (newEmployeeBank.uid === uid) {
2325
return newEmployeeBank;

services/employee/empCurrentDetail.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import databaseError from "#error/database";
2-
import EmployeeCurrent from "#models/employee/empPersonal";
2+
import EmployeeCurrent from "#models/employee/empCurrentDetail";
33

4-
export async function addNewEmployeeCurrent(
5-
uid,
6-
dateOfJoining,
7-
departmentName,
8-
designation,
9-
jobStatus,
10-
jobProfile,
11-
currentCtc,
12-
) {
4+
export async function addNewEmployeeCurrent(basicEmpDetails, session) {
5+
const {
6+
uid,
7+
dateOfJoining,
8+
departmentName,
9+
designation,
10+
jobStatus,
11+
jobProfile,
12+
currentCtc,
13+
} = basicEmpDetails;
1314
const newEmployeeCurrent = await EmployeeCurrent.create({
1415
uid,
1516
dateOfJoining,
@@ -18,6 +19,7 @@ export async function addNewEmployeeCurrent(
1819
jobStatus,
1920
jobProfile,
2021
currentCtc,
22+
session,
2123
});
2224
if (newEmployeeCurrent.uid === uid) {
2325
return newEmployeeCurrent;

0 commit comments

Comments
 (0)