async getQuotations(req: Request, res: Response, next: NextFunction) { try { const…
- User
async getQuotations(req: Request, res: Response, next: NextFunction) { try { const query: any = req.query; let regionCodeOrCodes = await getRegionCodeOrCodes(req.user!); const regionalStatusFilter = getQuestionnaireRegionalStatusFilter(regionCodeOrCodes); const questionnaire = await prisma.questionnaire.findFirst({ orderBy: [{ startDate: "desc" }], where: { designatedRole: rolesNum["DATA COLLECTOR"], approved: true, disabled: false, endDate: { gte: addDays(new Date(), -5), }, startDate: { lte: new Date(), }, isDraft: false, }, include: { questionnaireItems: { where: { disabled: false, }, include: { item: { include: { translation: true, itemMeasurements: true, }, }, }, }, questionnaireRegionStatus: { where: regionalStatusFilter, }, }, }); const datacollectors = await prisma.user.findMany({ where: req.user?.role === rolesNum.SUPERVISOR ? { supervisorId: req.user?.id, } : { marketplace: { branchCode: req.user?.branchCode } }, }); if ( req.user?.role === rolesNum.SUPERVISOR && (!datacollectors || datacollectors.length < 1) ) { return res .status(httpStatusCodes.NOT_FOUND) .json( new ResponseJSON( "Supervisor is not assigned any data collectors", true, httpStatusCodes.NOT_FOUND ) ); } if (!questionnaire) { return res .status(httpStatusCodes.NOT_FOUND) .json( new ResponseJSON( "Not Questionnaires active right now", true, httpStatusCodes.NOT_FOUND ) ); } if (req.user?.role === rolesNum.SUPERVISOR) { regionCodeOrCodes = datacollectors.map((dc) => dc.id); } else if (req.user?.role === rolesNum["BRANCH STATISTICIAN"]) { const supervisors = await prisma.user.findMany({ where: { supervisorId: req.user?.id, }, }); regionCodeOrCodes = supervisors.map((sup) => sup.id); } const result = await prisma.quotation.findMany({ where: { questionnaireId: questionnaire?.id, collectorId: { in: datacollectors.map((el) => el.id), }, ...query, }, include: { collector: { select: { fullName: true, id: true, marketplaceCode: true, }, }, quotes: { include: { quoteComments: true, }, }, unfoundQuotes: { include: { quoteComments: true, }, }, }, }); return res .status(httpStatusCodes.OK) .json(new ResponseJSON("Success", false, httpStatusCodes.OK, result)); } catch (error: any) { console.log("Error --- ", error); next( apiErrorHandler( error, req, errorMessages.INTERNAL_SERVER, httpStatusCodes.INTERNAL_SERVER ) ); } } assuming the above function returns the following response { "msg": "Success", "error": false, "statusCode": 200, "data": [ { "id": 816, "status": "REJECTED", "creationStatus": "COLLECTED", "approverId": 5, "marketplaceCode": 1402201, "collectorId": 16, "questionnaireId": 3, "itemCode": 101010101, "branchApproverId": null, "updatedAt": "2023-05-26T16:08:01.602Z", "createdAt": "2023-05-26T14:29:00.996Z", "collector": { "fullName": "kera dc", "id": 16, "marketplaceCode": 1402201 }, "quotes": [ { "id": 1209, "price": "2.57", "quantity": 2, "measurementUnit": "g", "shopContactName": null, "shopContactPhone": null, "shopLocation": null, "shopLatitude": null, "shopLongitude": null, "quotationId": 816, "updateNotes": [], "deleteNotes": [], "updatedAt": "2023-05-26T16:08:01.602Z", "createdAt": "2023-05-26T14:29:00.996Z", "quoteComments": [ { "id": 2, "quoteId": 1209, "unfoundQuoteId": null, "comment": "New comment", "commenterId": 5, "commenterName": "addis sup", "createdAt": "2023-05-26T16:08:01.605Z", "updatedAt": "2023-05-26T16:08:01.606Z" } ] } ], "unfoundQuotes": [] } ] } modify getQuotations so that branch statisticians only see responce when the QuotationStatus is SUPERVISOR_APPROVED
- Assistant