Hellow
- User
Hellow
- Assistant
! How can I assist you today?
- User
for the following getQuotations fucntion add a check for Branch Statisticians to only see QuotaitonStatus.SUPERVISOR_APPROVED quotations and supervisors should aslo see quoations from data collectors and their rejected quoations. The hierarcy goes from data collectors to supervisor then to branch statistician. And also it shoud be noted every supervisor sees quotaions of data collectors from their respective region and also the same for branch statistician to see quoatins approved by their regions supervisors 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 ) ); } } show the changes in steps
- Assistant
Sure, here are the changes that need to be made:
Step 1: Define a user role check for Branch Statisticians and Supervisors
Add the following code before the
tryblock:const userRole = req.user?.role; if (![rolesNum.BRANCH_STATISTICIAN, rolesNum.SUPERVISOR].includes(userRole)) { return res .status(httpStatusCodes.FORBIDDEN) .json( new ResponseJSON( "Only Branch Statisticians and Supervisors can access this endpoint", true, httpStatusCodes.FORBIDDEN ) ); }This checks if the user requesting the endpoint is either a Branch Statistician or a Supervisor. If not, it returns a
403 Forbiddenresponse with an appropriate error message.Step 2: Add a filter for QuotationStatus for Branch Statisticians
Add the following code after defining the
regionalStatusFiltervariable:let quotationStatusFilter: any = { OR: [ { status: QuotationStatus.SUPERVISOR_APPROVED }, { status: QuotationStatus.DATA_COLLECTOR_REJECTED }, { status: QuotationStatus.SUPERVISOR_REJECTED }, ], }; if (userRole === rolesNum.BRANCH_STATISTICIAN) { quotationStatusFilter = { status: QuotationStatus.SUPERVISOR_APPROVED }; }This defines a filter for the QuotationStatus based on the user role. Branch Statisticians will only see Quotations that have a status of
QuotationStatus.SUPERVISOR_APPROVED.Step 3: Add region-based filters for Supervisors and Branch Statisticians
Add the following code after defining the
datacollectorsvariable:let regionCodes: string[] = []; if (userRole === rolesNum.SUPERVISOR) { regionCodes = datacollectors.map((dc) => dc.marketplace.branchCode); } else if (userRole === rolesNum.BRANCH_STATISTICIAN) { const supervisors = await prisma.user.findMany({ where: { supervisorId: req.user?.id }, }); regionCodes = supervisors.map((sup) => sup.marketplace.branchCode); }This gets the region codes of the data collectors for the Supervisor and the Supervisors for the Branch Statistician. We'll use these region codes later to filter the Quotations.
Step 4: Add region-based filters for the Quotations
Add the following code before the
returnstatement:const result = await prisma.quotation.findMany({ where: { questionnaireId: questionnaire?.id, collectorId: { in: datacollectors.map((el) => el.id), }, status: quotationStatusFilter, questionnaire: { questionnaireRegionStatus: { some: { regionCode: { in: regionCodes, }, }, }, }, ...query, }, include: { collector: { select: { fullName: true, id: true, marketplaceCode: true, }, }, quotes: { include: { quoteComments: true, }, }, unfoundQuotes: { include: { quoteComments: true, }, }, }, });This filters the Quotations based on the user role and region codes. Branch Statisticians will only see Quotations that have a status of
QuotationStatus.SUPERVISOR_APPROVEDand are approved by Supervisors of their region. Supervisors will see Quotations approved by Data Collectors of their region and rejected Quotations from their Data Collectors. We use theregionCodesvariable to filter based on the region codes.That's it! With these changes, the
getQuotationsfunction should correctly implement the access and hierarchy requirements. - User
make the above changes you made with respect to the following getQuotations function and the associated filer 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 ) ); } } import { Prisma } from "@prisma/client"; import { FullUser } from "../../Models/Interfaces/IUser";
export function getQuestionnaireRegionalStatusFilter(codes: number | number[]) { const statusFilter: Prisma.QuestionnaireRegionStatusWhereInput = {}; if (Array.isArray(codes)) { statusFilter.regionCode = { in: codes, }; } else { statusFilter.regionCode = codes; } // if (user.branchCode) { // statusFilter.region = { // marketplace: { // some: { branchCode: user.branchCode }, // }, // }; // } else if (user.marketplaceCode) { // statusFilter.region = { // marketplace: { // some: { // code: user.marketplaceCode, // }, // }, // }; // } else if (user.regionCode) { // statusFilter.regionCode = user.regionCode; // } return statusFilter; }
export function getRegionCode(user: FullUser, params: Record<string, any>) { if (user.regionCode) return user.regionCode; const regionCode = parseInt(params.regionCode); if (isNaN(regionCode)) { console.error("Invalid region code: ", params.regionCode); throw new Error("Invalid region code"); } return regionCode; }
- Assistant
Here are the changes made to the
getQuotationsfunction and the associated filter:async function getQuotations(req: Request, res: Response, next: NextFunction) { try { const query: any = req.query; const userRole = req.user?.role; 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, }, }, }); let filter: any, datacollectors: any[]; switch (userRole) { case rolesNum.DATA_COLLECTOR: filter = { collectorId: req.user?.id, status: QuotationStatus.SUBMITTED }; break; case rolesNum.SUPERVISOR: datacollectors = await prisma.user.findMany({ where: { supervisorId: req.user?.id }, }); if (!datacollectors || datacollectors.length < 1) { return res.status(httpStatusCodes.NOT_FOUND).json( new ResponseJSON( 'Supervisor is not assigned any data collectors', true, httpStatusCodes.NOT_FOUND ) ); } filter = { collectorId: { in: datacollectors.map((dc) => dc.id) }, status: { in: [ QuotationStatus.SUBMITTED, QuotationStatus.DATA_COLLECTOR_REJECTED, QuotationStatus.SUPERVISOR_APPROVED, QuotationStatus.SUPERVISOR_REJECTED, ], }, questionnaire: { questionnaireRegionStatus: { some: { regionCode: { in: datacollectors.map((dc) => dc.marketplace.branchCode), }, }, }, }, }; break; case rolesNum.BRANCH_STATISTICIAN: const supervisors = await prisma.user.findMany({ where: { supervisorId: req.user?.id }, }); const supervisorIds = supervisors.map((sup) => sup.id); datacollectors = await prisma.user.findMany({ where: { supervisorId: { in: supervisorIds } }, }); if (!datacollectors || datacollectors.length < 1) { return res.status(httpStatusCodes.NOT_FOUND).json( new ResponseJSON( 'Branch Statistician does not have any data collectors in their assigned regions', true, httpStatusCodes.NOT_FOUND ) ); } filter = { collectorId: { in: datacollectors.map((dc) => dc.id) }, status: { in: [ QuotationStatus.SUBMITTED, QuotationStatus.DATA_COLLECTOR_REJECTED, QuotationStatus.SUPERVISOR_APPROVED, QuotationStatus.SUPERVISOR_REJECTED, ], }, questionnaire: { questionnaireRegionStatus: { some: { regionCode: { in: supervisors.map((sup) => sup.marketplace.branchCode), }, }, }, }, }; break; default: filter = {}; break; } const result = await prisma.quotation.findMany({ where: { ...filter, questionnaireId: questionnaire?.id, }, include: { collector: { select: { fullName: true, id: true, marketplaceCode: true } }, quotes: { include: { quoteComments: true } }, unfoundQuotes: { include: { quoteComments: true } }, }, ...query, }); return res .status(httpStatusCodes.OK) .json(new ResponseJSON('Success', false, httpStatusCodes.OK, result)); } catch (error: any) { console.error('Error --- ', error); next( apiErrorHandler( error, req, errorMessages.INTERNAL_SERVER, httpStatusCodes.INTERNAL_SERVER ) ); } }Here are the main changes that were made:
- We added a switch statement to handle the different user roles and set the appropriate filter based on their access and hierarchy requirements.
- For data collectors, we set the filter to only show their own
QuotationStatus.SUBMITTEDquotations. - For supervisors, we set the filter to show
QuotationStatus.SUBMITTED,QuotationStatus.DATA_COLLECTOR_REJECTED,QuotationStatus.SUPERVISOR_APPROVED, andQuotationStatus.SUPERVISOR_REJECTEDquotations that are approved by Data Collectors from their region. - For branch statisticians, we set the filter to show the same quotations as supervisors, but with an additional filter to only show Data Collectors that are approved by Supervisors from their assigned regions.
- We removed the
collectorIdrequirement from thewhereclause, since the new filter takes care of that.
- User
but data collectors dont use this function it is only for supervisors and branch statsticians
- Assistant
Apologies for the confusion. You are correct that the
getQuotationsfunction is only used by Supervisors and Branch Statisticians. Here is the updatedgetQuotationsfunction and filter, with the changes specific to Supervisors and Branch Statisticians:async function getQuotations(req: Request, res: Response, next: NextFunction) { try { const query: any = req.query; const userRole = req.user?.role; 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, }, },
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-instant-1Compare side by side →
zephyr-7b-alphaCompare side by side →
- gpt-4.1-mini-2025-04-14Compare side by side →
- gpt-4.1-2025-04-14Compare side by side →
amazon-nova-experimental-chat-05-14Compare side by side →
- gpt-4o-mini-2024-07-18Compare side by side →
gemini-2.5-proCompare side by side →
gemma-3-27b-itCompare side by side →
- o4-mini-2025-04-16Compare side by side →
amazon.nova-pro-v1:0Compare side by side →
- o3-2025-04-16Compare side by side →
magistral-medium-2506Compare side by side →