old extra fee update api service
old extra fee update api serviceYash
javascript
15 days ago
5.7 kB
3
Indexable
async create(data: any): Promise<any> { try { if (data.student_wise) { const { iid, extraFeesIds, userId, branchId, userCourseId, dueDate, initialAmount } = data; const findUserCourse = await this.userCourseService.findOne(userCourseId) if (!findUserCourse) { throw new BadRequestException(`userCourse not found`) } const extraFeeUsers = []; for (const extraFeeId of extraFeesIds) { const findextrafee = await this.extraFeeService.findOne(extraFeeId); if (!findextrafee) { throw new BadRequestException(`Extra fee with ID ${extraFeeId} not found.`); } if (findextrafee.feesType === "ONE_TIME") { const existingRecord = await this.extraFeeUserModel.findOne({ userId, extraFeesId: extraFeeId, status: { $ne: "cancel" }, }); if (existingRecord) { throw new BadRequestException( `Extra fee is type "ONE_TIME" and has already been assigned to the user.` ); } } const newExtraFeeUser = await this.extraFeeUserModel.create({ userId, courseId: findUserCourse.courseId, userCourseId: userCourseId, createdBy: data.createdBy, initialAmount: initialAmount, extraFeesId: extraFeeId, dueDate: dueDate, iid, branchId, }); extraFeeUsers.push(newExtraFeeUser); } return { message: "Extra fees processed successfully.", extraFeeUsers, }; } const { iid, courseId, extraFeesIds, branchId } = data; const existingCourse = await this.courseService.findOneByQuery({ _id: courseId }); if (!existingCourse) { throw new BadRequestException(`Course with ID ${courseId} does not exist.`); } const removedExtraFees = filter(existingCourse.extraFeesIds, (feeId: any) => !includes(extraFeesIds, feeId) ); if (removedExtraFees.length > 0) { // Delete extraFeeUser records with status 'pending' or 'cancel' for removed extraFeeIds await this.extraFeeUserModel.deleteMany({ extraFeesId: { $in: removedExtraFees }, status: { $in: ['pending', 'cancel'] } }); } const updatedCourse = await this.courseService.updateCourseExtraFee( { _id: courseId }, { $set: { extraFeesIds } } ); if (!updatedCourse) { throw new BadRequestException(`Course with ID ${courseId} does not update.`); } const existingRecords = await this.userCourseService.findAll({ iid, courseId }); if (!existingRecords) { throw new BadRequestException(`Record for IID ${iid} and Course ID ${courseId} not exists.`); } const extraFeeUsers = []; for (const extraFeeId of extraFeesIds) { const findextrafee = await this.extraFeeService.findOne(extraFeeId); if (!findextrafee) { throw new BadRequestException(`Extra fee with ID ${extraFeeId} not found.`); } if (findextrafee.feesType === "ONE_TIME") { for (const record of existingRecords) { const existingRecord = await this.extraFeeUserModel.findOne({ userId: record.userId, courseId:record.courseId, extraFeesId: extraFeeId, status: { $ne: "cancel" }, }); if (existingRecord) { // throw new BadRequestException( // `Extra fee with ID ${extraFeeId} is of type "ONE_TIME" and has already been assigned to a user.` // ); continue; } } } for (const record of existingRecords) { const newExtraFeeUser = await this.extraFeeUserModel.create({ userId: record.userId, courseId, userCourseId: record._id, createdBy: data.createdBy, initialAmount: findextrafee.defaultAmount, extraFeesId: extraFeeId, dueDate: findextrafee.dueDate, iid: data.iid, branchId, }); extraFeeUsers.push(newExtraFeeUser); } } return { message: "Extra fees processed successfully.", extraFeeUsers, }; } catch (error) { throw new BadRequestException(error.message || error); } }
Editor is loading...
Leave a Comment