async approveQuestionnaire(req: Request, res: Response, next: NextFunction) { try {…
- User
async approveQuestionnaire(req: Request, res: Response, next: NextFunction) { try { const questionnaireId = parseInt(req.params.id); const { approve }: { approve: boolean } = req.body; if (approve === false) { const count = await prisma.quotation.count({ where: { questionnaireId, }, }); if (count > 0) { return res .status(httpStatusCodes.BAD_REQUEST) .json( new ResponseJSON( "Questionnaire is already being collected", true, httpStatusCodes.BAD_REQUEST ) ); } } const questionnaire = await prisma.questionnaire.findUnique({ where: { id: questionnaireId, }, include: { questionnaireItems: true, nationalCPIBasket: { select: { regionalCpiBaskets: { select: { regionCode: true }, }, }, }, }, }); if (!questionnaire) { return res .status(httpStatusCodes.NOT_FOUND) .json( new ResponseJSON( errorMessages.NOT_FOUND, true, httpStatusCodes.NOT_FOUND ) ); } if ( !questionnaire.isDraft && questionnaire.endDate.getTime() > Date.now() ) { const eachMonthDates = isSameMonth(questionnaire.startDate, questionnaire.dateRange[1]) || !approve ? [] : eachMonthOfInterval({ start: addMonths(questionnaire.dateRange[0], 1), end: questionnaire.dateRange[1], }); const [result] = await prisma.$transaction([ prisma.questionnaire.update({ where: { id: questionnaireId, }, data: { approved: approve, approvedBy: req.user?.id, updatedAt: new Date(), }, }), ...eachMonthDates.map((date) => prisma.questionnaire.create({ data: { nationalCPIBasketId: questionnaire.nationalCPIBasketId, designatedRole: questionnaire.designatedRole, maxQuotations: questionnaire.maxQuotations, minQuotations: questionnaire.minQuotations, name: questionnaire.name, dateRange: questionnaire.dateRange, requiredDatacollectors: questionnaire.requiredDatacollectors, createdBy: questionnaire.createdBy, isDraft: questionnaire.isDraft, // quotationsStatus: questionnaire.quotationsStatus,
questionnaireRegionStatus: { createMany: { data: questionnaire.nationalCPIBasket.regionalCpiBaskets.map( (el) => ({ regionCode: el.regionCode, status: QuestionnaireQuotationsStatus.PENDING, }) ), }, }, introduction: questionnaire.introduction, regionalMonthInflationDescription: questionnaire.regionalMonthInflationDescription, regionalYearInflationDescription: questionnaire.regionalYearInflationDescription, regionalMovingInflationDescription: questionnaire.regionalMovingInflationDescription, nationalMonthInflationDescription: questionnaire.nationalMonthInflationDescription, nationalYearInflationDescription: questionnaire.nationalYearInflationDescription, nationalMovingInflationDescription: questionnaire.nationalMovingInflationDescription, parentQuestionnaireId: questionnaire.id, monthYear: format(set(date, { hours: 4 }), "MM-yyyy"), approved: approve, approvedBy: req.user?.id, startDate: set(date, { date: getDate(questionnaire.startDate), hours: 4, minutes: 0, seconds: 0, }), endDate: endOfDay( set(date, { date: getDate(questionnaire.endDate), hours: 20, minutes: 0, seconds: 0, }) ), questionnaireItems: { createMany: { data: questionnaire.questionnaireItems.map((item) => ({ itemCode: item.itemCode, maxPrice: item.maxPrice, minPrice: item.minPrice, })), }, }, }, }) ), ]); return res .status(httpStatusCodes.OK) .json( new ResponseJSON( "Questionnaire Updated", false, httpStatusCodes.OK, result ) ); } else { return res .status(httpStatusCodes.BAD_REQUEST) .json( new ResponseJSON( `Questionnaire ${questionnaire.isDraft ? "is still in a draft!" : "collection date has passed!" }`, true, httpStatusCodes.BAD_REQUEST ) ); } } catch (error) { console.log("Error --- ", error); next( apiErrorHandler( error, req, errorMessages.INTERNAL_SERVER, httpStatusCodes.INTERNAL_SERVER ) ); } - Assistant
The code creates an array
eachMonthDatescontaining the start date of each month within the date range of the questionnaire, except for the start month (unlessapproveisfalse). It then creates a new questionnaire for each of these months with the same properties as the original questionnaire, but with different start and end dates that correspond to the month being created, and sets theparentQuestionnaireIdfield to the ID of the original questionnaire. TheapprovedandapprovedByfields are also set to the values in theapproveparameter and the ID of the user making the request, respectively. These new questionnaires are created as part of a larger Prisma transaction that also updates theapprovedandapprovedByfields of the original questionnaire. The result of the transaction is returned in the response.