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; const 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 ) ); } 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 ) ); } } explain the above code
- Assistant
The code is a function
getQuotationsthat handles a GET request for retrieving quotations from the database. It first extracts the query parameters from the request object and gets the region codes associated with the user making the request. It then constructs a query to find the latest approved questionnaire that is not a draft and has not ended more than 5 days ago.If the user making the request is a supervisor, it retrieves all the data collectors assigned to them for further filtering of the quotations. If there are no data collectors assigned, it returns a not found error. If there is no active questionnaire found, it returns a not found error.
Finally, it constructs a query to retrieve quotations that match the questionnaire and data collector filtering criteria, and includes additional information such as the collector details and their comments on the quotes. It then returns the retrieved quotations in a response object. If there is an error, it logs the error and sends an error response.
- User
the following is the route handler for the above function this._router.get( "/quotation", verifyRoles([rolesStr.SUPERVISOR, rolesStr["BRANCH STATISTICIAN"]]), this._controller.getQuotations ); so will this handle or check requests so that only supervisor approved quotations show up to the branch statistician
- Assistant
No, the route handler only checks if the user making the request is a supervisor or a branch statistician using the
verifyRolesmiddleware function. It does not differentiate between supervisor approved quotations and other quotations when calling thegetQuotationsfunction in the controller.If you want to show only supervisor approved quotations to branch statisticians, you would need to add additional logic to filter out other quotations in the
getQuotationsfunction in the controller based on the role of the user making the request. - User
show me how
- Assistant
- User
No it should be quotaions from data collectors to their respective supervisors and then supervisor approved should appear to branch statistians
- Assistant
- User
A bracnh statician should only see quotations approved by a supervisor
- Assistant