async importQuotation(req: Request, res: Response, next: NextFunction) {
try {
console.log(“Importing quotations…”);
const body: ImportCleanQuotations = req.body;
const regionCode = getRegionCode(req.user!, req.query);
console.log(“User”, req.user!);
console.log("RegionCode: ", regionCode);
if (!regionCode) {
console.log(“Region code is required for this operation”);
return res
.status(httpStatusCodes.BAD_REQUEST)
.json(
new ResponseJSON(
“Region code is required for this operation”,
true,
httpStatusCodes.BAD_REQUEST
)
);
}
const items = await Promise.all(
body.quotations.map((quotation) =>
prisma.item
.findUnique({
where: {
code: quotation.itemCode,
},
select: {
UoMCode: true,
SmUCode: true,
},
})
// .filter((item) => item)
.catch(() => {
throw new Error(Item '${quotation.itemCode}' not found);
})
)
);


// ); //filter out null items
console.log("Items: ", items);


const createMany = await prisma.$transaction(async (tx) => {
const quotations = await Promise.all(
body.quotations.map((quotation, index) => {
if (!quotation.itemCode || !items[index]) return null; //skip if item code is not found
(quotation as any).status = QuotationStatus.BRANCH_APPROVED;
(quotation as any).creationStatus =
QuotationCreationStatus.IMPORTED;
if (!quotation.collectorId) quotation.collectorId = req.user!.id;
return tx.quotation.create({
data: {
questionnaireId: quotation.questionnaireId,
collectorId: req.user!.id,
itemCode: quotation!.itemCode,
marketplaceCode: quotation.marketplaceCode!,
quotes: {
createMany: {
data: quotation.quotes!.map((quote) => ({
…quote,
shopContactName: “Imported”,
shopContactPhone: “Imported”,
shopLatitude: “Imputated”,
shopLongitude: “Imputated”,
measurementUnit: items?.[index]!?.UoMCode,
})),
},
// quotation.quotes?.map((quote) => { return {…quote,quantity: items[index]?.measurementQuantity,
// measurmentId: items[index]?.measurement,};}),
},
},
select: {
id: true,
questionnaireId: true,
itemCode: true,
quotes: true,
},
});
})
);


await Promise.all(
quotations.reduce((acc: any, quotation, index) => {
if (!quotation) return acc; //skip if quotation is not found


quotation.quotes.forEach((quote) => {
if (!quote) return; //skip if quote is not found


acc.push(
tx.interpolatedQuote.create({
data: {
quoteId: quote.id,
quotationId: quotation.id,
price: quote.price,
measurementUnit: items[index]!.SmUCode,
quantity: quote.quantity,
},
})
);


acc.push(
tx.cleanedQuote.create({
data: {
quoteId: quote.id,
quotationId: quotation.id,
price: quote.price,
measurementUnit: items[index]!.SmUCode,
quantity: quote.quantity,
questionnaireId: quotation.questionnaireId,
itemCode: quotation.itemCode,
},
})
);
});


return acc;
}, [])


// quotations.reduce((acc: any, quotation, index) => {
// if (!quotation || !quotation.quotes[index]) return acc; //skip if quotation or quote is not found
// acc.push(
// tx.interpolatedQuote.create({
// data: {
// quoteId: quotation.quotes[index].id,
// quotationId: quotation.id,
// price: quotation.quotes[index].price,
// measurementUnit: items[index]!.SmUCode,
// quantity: quotation.quotes[index].quantity,
// },
// })
// );
// acc.push(
// tx.cleanedQuote.create({
// data: {
// quoteId: quotation.quotes[index].id,
// quotationId: quotation.id,
// price: quotation.quotes[index].price,
// measurementUnit: items[index]!.SmUCode,
// quantity: quotation.quotes[index].quantity,
// questionnaireId: quotation.questionnaireId,
// itemCode: quotation.itemCode,
// },
// })
// );
// return acc;
// }, [])
);


await tx.itemRegionalMean.createMany({
data: body.geomeans.map((mean) => ({
itemCode: mean.itemCode,
variation: mean.variation,
stdev: mean.stdev,
geomean: mean.geomean,
min: mean.min,
max: mean.max,
questionnaireId: mean.questionnaireId,
regionCode,
})),
});


return quotations.filter((quotation) => !!quotation); //Filter out null/undefined quotations
});


console.log("Quotations created: ", createMany);
return res
.status(httpStatusCodes.OK)
.json(
new ResponseJSON(
“Quotations Created”,
false,
httpStatusCodes.OK,
createMany
)
);
} catch (err) {
// console.log("Error message: ", );
console.log("Error — ", err);
next(
apiErrorHandler(
err,
req,
errorMessages.INTERNAL_SERVER,
httpStatusCodes.INTERNAL_SERVER
)
);
}
} based on the above fucntion i want imported quotations to have a status of branch_approved and creationstatus of Imported here is the schemas that are related with this model Quote {
id Int @id @default(autoincrement())
price Decimal @db.Decimal(10, 2)
quantity Float
// UoMCode String
// measurment ItemMeasurement @relation(fields: [UoMCode], references: [UoMCode])
measurementUnit String
measurement UnitOfMeasurement @relation(fields: [measurementUnit], references: [code])
shopContactName String?
shopContactPhone String?
shopLocation String?
shopLatitude String?
shopLongitude String?
quotationId Int
quotation Quotation @relation(fields: [quotationId], references: [id])
quoteComments QuotationComment[]
cleanedQuote CleanedQuote?
interpolatedQuote InterpolatedQuote?
updateNotes Json[]
deleteNotes Json[]
updatedAt DateTime @updatedAt
createdAt DateTime @default(now())
}model Quotation {
id Int @id @default(autoincrement())
status QuotationStatus @default(PENDING)
creationStatus QuotationCreationStatus @default(COLLECTED)
approverId Int?
approver User? @relation(“approver”, fields: [approverId], references: [id])
marketplaceCode Int
marketplace Marketplace @relation(fields: [marketplaceCode], references: [code])
collectorId Int
collector User @relation(“collector”, fields: [collectorId], references: [id])
questionnaireId Int
itemCode Int
questionnaireItem QuestionnaireItem @relation(fields: [questionnaireId, itemCode], references: [questionnaireId, itemCode])
branchApproverId Int?
branchApprover User? @relation(“branchApprover”, fields: [branchApproverId], references: [id])
quotes Quote[]
unfoundQuotes UnfoundQuote[]
cleanedQuotes CleanedQuote[]
interpolatedQuotes InterpolatedQuote[]
// quoteMean QuotesMean?
updatedAt DateTime @updatedAt
createdAt DateTime @default(now())


@@unique([questionnaireId, itemCode, marketplaceCode])
}enum QuotationStatus {
PENDING
SUPERVISOR_APPROVED
STATISTICIAN_APPROVED
BRANCH_APPROVED
REJECTED
}


enum QuotationCreationStatus {
COLLECTED
IMPUTATION
IMPORTED
} are the statuses being set when quotations are imported? if not correct the code by following the same code flow