//user.utlis.tsimport{TAcademicSemester}from'../academicSemester/academicSemester.interface';importUserfrom'./user.model';//findlast student IDconstfindLastStudentId=async ()=>{constlastStudent=awaitUser.findOne({role:'student',},{id:1,_id:0,},).sort({createdAt:-1,}).lean();// eslint-disable-next-line no-undefinedreturnlastStudent?.id.substring(6)??undefined;// Assuming id is a property of lastStudent};//generate student IDexportconstgenerateStudentId=async (payload:TAcademicSemester)=>{constcurrentId=(awaitfindLastStudentId())||(0).toString();letincrementId=(Number(currentId)+1).toString().padStart(4,'0');incrementId=`${payload.year}${payload.code}${incrementId}`;returnincrementId;};
//user.service.ts=> full codeimportconfigfrom'../../config';import{AcademicSemester}from'../academicSemester/academicSemester.model';// import { TAcademicSemester } from '../academicSemester/academicSemester.interface';import{TStudent}from'../student/student.interface';import{Student}from'../student/student.model';import{TUser}from'./user.interface';importUserfrom'./user.model';import{generateStudentId}from'./user.utils';constcreateStudentIntoDB=async (password:string,payload:TStudent)=>{//create a user objectconstuserData:Partial<TUser>={};userData.password=password||(config.default_passasstring);//set student roleuserData.role='student';//find academic semester infoconstadmissionSemesterID=awaitAcademicSemester.findById(payload.admissionSemester,);// //set manually generated idif (admissionSemesterID!=null){userData.id=awaitgenerateStudentId(admissionSemesterID);}// userData.id = '202310002';//create a userconstnewUser=awaitUser.create(userData);//crate a studentif (Object.keys(newUser).length){//set id, _id as a userpayload.id=newUser.id;payload.user=newUser._id;//reference _id//create a new studentconstnewStudent=awaitStudent.create(payload);returnnewStudent;}// return newUser;};exportconstUserServices={createStudentIntoDB,};
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)