//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,};
Implement features, document your code, or refactor your projects. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.
Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.
Top comments (0)