DEV Community

Kazi Abdur Rakib
Kazi Abdur Rakib

Posted on

backend generateStudent() id automatically

12-11 Complete generateStudent() utility

//user.utlis.ts
import { TAcademicSemester } from '../academicSemester/academicSemester.interface';
import User from './user.model';

//findlast student ID
const findLastStudentId = async () => {
  const lastStudent = await User.findOne(
    {
      role: 'student',
    },
    {
      id: 1,
      _id: 0,
    },
  )
    .sort({
      createdAt: -1,
    })
    .lean();

  // eslint-disable-next-line no-undefined
  return lastStudent?.id.substring(6) ?? undefined; // Assuming id is a property of lastStudent
};

//generate student ID
export const generateStudentId = async (payload: TAcademicSemester) => {
  const currentId = (await findLastStudentId()) || (0).toString();
  let incrementId = (Number(currentId) + 1).toString().padStart(4, '0');

  incrementId = `${payload.year}${payload.code}${incrementId}`;
  return incrementId;
};

Enter fullscreen mode Exit fullscreen mode
//user.service.ts
  //set student role
  userData.role = 'student';

  //find academic semester info
  const admissionSemesterID = await AcademicSemester.findById(
    payload.admissionSemester,
  );

  // //set manually generated id
  if (admissionSemesterID != null) {
    userData.id = await generateStudentId(admissionSemesterID);
  }
  // userData.id = '202310002';
  //create a user
  const newUser = await User.create(userData);

Enter fullscreen mode Exit fullscreen mode
//user.service.ts=> full code
import config from '../../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';
import User from './user.model';
import { generateStudentId } from './user.utils';

const createStudentIntoDB = async (password: string, payload: TStudent) => {
  //create a user object
  const userData: Partial<TUser> = {};

  userData.password = password || (config.default_pass as string);

  //set student role
  userData.role = 'student';

  //find academic semester info
  const admissionSemesterID = await AcademicSemester.findById(
    payload.admissionSemester,
  );

  // //set manually generated id
  if (admissionSemesterID != null) {
    userData.id = await generateStudentId(admissionSemesterID);
  }
  // userData.id = '202310002';
  //create a user
  const newUser = await User.create(userData);

  //crate a student
  if (Object.keys(newUser).length) {
    //set id, _id as a user
    payload.id = newUser.id;
    payload.user = newUser._id; //reference _id

    //create a new student
    const newStudent = await Student.create(payload);
    return newStudent;
  }
  //   return newUser;
};

export const UserServices = {
  createStudentIntoDB,
};


Enter fullscreen mode Exit fullscreen mode

Top comments (0)