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 try block:
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 Forbidden response with an appropriate error message.
Step 2: Add a filter for QuotationStatus for Branch Statisticians
Add the following code after defining the regionalStatusFilter variable:
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 datacollectors variable:
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 return statement:
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_APPROVED and 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 the regionCodes variable to filter based on the region codes.
That's it! With these changes, the getQuotations function should correctly implement the access and hierarchy requirements.