refactor: register student with excel
This commit is contained in:
parent
851d218540
commit
cbde854051
|
|
@ -621,7 +621,7 @@ export const registerStudentForAdminAndTeacher = async (req, res) => {
|
||||||
return response(400, null, "NISN is required for students!", res);
|
return response(400, null, "NISN is required for students!", res);
|
||||||
}
|
}
|
||||||
|
|
||||||
const EMAIL = `${NISN}@gmail.com`;
|
const EMAIL = `${NISN}@student.seals.com`;
|
||||||
const PASSWORD = "12345678";
|
const PASSWORD = "12345678";
|
||||||
|
|
||||||
const transaction = await models.db.transaction();
|
const transaction = await models.db.transaction();
|
||||||
|
|
@ -768,17 +768,35 @@ export const registerStudentCSV = async (req, res) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const transaction = await models.db.transaction();
|
const transaction = await models.db.transaction();
|
||||||
|
const processed = [];
|
||||||
|
const duplicates = [];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
for (const student of students) {
|
for (const student of students) {
|
||||||
const { NISN, NAME_USERS } = student;
|
const { NISN, NAME_USERS } = student;
|
||||||
|
|
||||||
const EMAIL = `${NISN}@gmail.com`;
|
const EMAIL = `${NISN}@student.seals.com`;
|
||||||
const PASSWORD = "12345678";
|
const PASSWORD = "12345678";
|
||||||
|
|
||||||
const salt = await bcrypt.genSalt(10);
|
const salt = await bcrypt.genSalt(10);
|
||||||
const hashedPassword = await bcrypt.hash(PASSWORD, salt);
|
const hashedPassword = await bcrypt.hash(PASSWORD, salt);
|
||||||
|
|
||||||
|
const existingUser = await models.User.findOne({ where: { EMAIL } });
|
||||||
|
|
||||||
|
if (existingUser) {
|
||||||
|
duplicates.push(student);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const existingStudent = await models.Student.findOne({
|
||||||
|
where: { NISN },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (existingStudent) {
|
||||||
|
duplicates.push(student);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
const newUser = await models.User.create(
|
const newUser = await models.User.create(
|
||||||
{
|
{
|
||||||
NAME_USERS,
|
NAME_USERS,
|
||||||
|
|
@ -797,27 +815,26 @@ export const registerStudentCSV = async (req, res) => {
|
||||||
},
|
},
|
||||||
{ transaction }
|
{ transaction }
|
||||||
);
|
);
|
||||||
|
|
||||||
|
processed.push(student);
|
||||||
}
|
}
|
||||||
|
|
||||||
await transaction.commit();
|
await transaction.commit();
|
||||||
response(200, { success: true }, "Students registered successfully!", res);
|
|
||||||
|
response(
|
||||||
|
200,
|
||||||
|
{
|
||||||
|
success: true,
|
||||||
|
message: "Students registered successfully!",
|
||||||
|
processed,
|
||||||
|
duplicates,
|
||||||
|
},
|
||||||
|
"Operation complete with some duplicates skipped.",
|
||||||
|
res
|
||||||
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|
||||||
await transaction.rollback();
|
await transaction.rollback();
|
||||||
|
|
||||||
if (error.name === "SequelizeUniqueConstraintError") {
|
|
||||||
const field = error.original.sqlMessage.match(/for key '(.+)'/)[1];
|
|
||||||
|
|
||||||
if (field === "student_unique_nisn") {
|
|
||||||
return response(400, null, "Duplicate NISN found in file!", res);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (field === "user_unique_email") {
|
|
||||||
return response(400, null, "Duplicate email found in file!", res);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
response(500, null, "Internal Server Error", res);
|
response(500, null, "Internal Server Error", res);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user