Based on the following prisma schemas model Quote { id Int @id @default(autoincrement())…
- User
Based on the following prisma schemas 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]) } model UnfoundQuote { id Int @id @default(autoincrement()) reason String quotationId Int quotation Quotation @relation(fields: [quotationId], references: [id]) quoteComments QuotationComment[] updatedAt DateTime @updatedAt createdAt DateTime @default(now()) } update the following function //---------- Edit Quote reason ---------- async editQuoteReason(req: Request, res: Response, next: NextFunction) { try { const updatedQuote: UnfoundQuote = req.body; const quotationId = parseInt(req.params.quotaionId); const quotation = await prisma.quotation.findFirst({ where: { id: quotationId, status: QuotationStatus.PENDING, }, include: { quotes: true, questionnaireItem: true, unfoundQuotes: true, }, }); const questionnaire = await prisma.questionnaire.findUnique({ where: { id: quotation?.questionnaireId }, }); if ( !quotation || quotation.collectorId !== req.user?.id || !questionnaire ) { console.log('User is: ', req.user, ' Quotation is: ', quotation); return res .status(httpStatusCodes.NOT_FOUND) .json( new ResponseJSON('Quote not found', true, httpStatusCodes.NOT_FOUND) ); } if ( questionnaire.endDate.getTime() <= addDays(new Date(), -10).getTime() || ([ QuotationStatus.BRANCH_APPROVED, QuotationStatus.STATISTICIAN_APPROVED, QuotationStatus.SUPERVISOR_APPROVED, ].includes(quotation.status as any) && req.user.role === rolesNum['DATA COLLECTOR']) ) { return res .status(httpStatusCodes.UNAUTHORIZED) .json( new ResponseJSON( 'Quote is Already Approved', true, httpStatusCodes.UNAUTHORIZED ) ); } if (quotation.collectorId != req.user.id) { return res .status(httpStatusCodes.UNAUTHORIZED) .json( new ResponseJSON( errorMessages.UNAUTHORIZED, true, httpStatusCodes.UNAUTHORIZED ) ); } updatedQuote.updatedAt = new Date(); const result = await prisma.$transaction([ prisma.quote.update({ where: { id: quotationId, }, data: updatedQuote, }), prisma.quotation.update({ where: { id: quotationId }, data: { status: QuotationStatus.PENDING, }, }), // prisma.quotationComment.deleteMany({ // where: { // commenterId: req.user.id, // quotationId: quotationId, // }, // }), ]); return res .status(httpStatusCodes.OK) .json( new ResponseJSON( 'Quote reason edited', false, httpStatusCodes.OK, result[0] ) ); } catch (error) { console.error('Error--- ', error); next( apiErrorHandler( error, req, errorMessages.INTERNAL_SERVER, httpStatusCodes.INTERNAL_SERVER ) ); } } so to do an update on a quotation only with status QuoatiionStatus.REJECTED and update the field reason. And change the status to QuotationStatus.PENDING on update. Show the changes in steps
- Assistant
Step 1: Add a check for QuotationStatus.REJECTED in the if condition that checks if the user is authorized to edit the quote reason. Also, add the updated quote reason to the data object for quote update.
if ( questionnaire.endDate.getTime() <= addDays(new Date(), -10).getTime() || ([ QuotationStatus.BRANCH_APPROVED, QuotationStatus.STATISTICIAN_APPROVED, QuotationStatus.SUPERVISOR_APPROVED, ].includes(quotation.status as any) && req.user.role === rolesNum['DATA COLLECTOR']) ) { return res.status(httpStatusCodes.UNAUTHORIZED).json( new ResponseJSON( 'Quote is Already Approved', true, httpStatusCodes.UNAUTHORIZED ) ); } if ( quotation.status !== QuotationStatus.REJECTED && quotation.collectorId !== req.user?.id ) { return res.status(httpStatusCodes.UNAUTHORIZED).json( new ResponseJSON(errorMessages.UNAUTHORIZED, true, httpStatusCodes.UNAUTHORIZED) ); } updatedQuote.updatedAt = new Date(); if (quotation.status === QuotationStatus.REJECTED) { const updatedFields = { ...updatedQuote, status: QuotationStatus.PENDING }; await prisma.quotation.update({ where: { id: quotationId }, data: updatedFields, }); } else { await prisma.quote.update({ where: { id: quotationId }, data: updatedQuote, }); } - User
is that it ?
- Assistant
Yes, those are the changes needed to update the quote reason for quotations with status QuotationStatus.REJECTED and change the status to QuotationStatus.PENDING on update.
- User
Why the if else ?? if (quotation.status === QuotationStatus.REJECTED) { const updatedFields = { …updatedQuote, status: QuotationStatus.PENDING }; await prisma.quotation.update({ where: { id: quotationId }, data: updatedFields, }); } else { await prisma.quote.update({ where: { id: quotationId }, data: updatedQuote, }); }
- Assistant
This if-else statement is used to handle the case where the quotation status is REJECTED.
If the quotation status is REJECTED, we need to update the quotation status to PENDING in addition to updating the quote reason. Therefore, we create a new object
updatedFieldsthat includes the updated quote reason as well as the new status value of PENDING. Then we call theprisma.quotation.updatemethod to update the quotation with the newupdatedFieldsobject.If the quotation status is not REJECTED, we can simply update the quote with the new
updatedQuoteobject using theprisma.quote.updatemethod.That's why we use this if-else statement to check whether we need to update the quotation status or not, and choose the appropriate update method accordingly.
- User
but if the status is different from Rejected updating should not be allowed
- Assistant
You are correct. The original code already includes a check to only allow data collectors to edit quotes with the
QuotationStatus.PENDINGstatus, but we need to add a similar check to only allow editing of quotes with theQuotationStatus.REJECTEDstatus:if (quotation.status !== QuotationStatus.REJECTED && quotation.collectorId !== req.user?.id) { return res.status(httpStatusCodes.UNAUTHORIZED).json( new ResponseJSON(errorMessages.UNAUTHORIZED, true, httpStatusCodes.UNAUTHORIZED) ); }This check ensures that only the data collector who created the quotation can edit the quote reason if the quotation status is
QuotationStatus.PENDINGorQuotationStatus.REJECTED, and that no one else can edit the quote reason. - User
But again a data collector should only edit the reason on quotations that have a Rejected status and then change their status to pending upon a successful update
- Assistant
Yes, you are correct. We only want to allow data collectors to edit the reason on quotations that have a
QuotationStatus.REJECTEDstatus and then change their status toQuotationStatus.PENDINGupon a successful update.Here's the updated code:
if (quotation.status !== QuotationStatus.REJECTED) { return res.status(httpStatusCodes.UNAUTHORIZED).json( new ResponseJSON( 'You can only edit rejected quotes', true, httpStatusCodes.UNAUTHORIZED ) ); } if (quotation.collectorId !== req.user?.id) { return res.status(httpStatusCodes.UNAUTHORIZED).json( new ResponseJSON( 'You are not authorized to edit this quote', true, httpStatusCodes.UNAUTHORIZED ) ); } updatedQuote.updatedAt = new Date(); const updatedFields = { ...updatedQuote, status: QuotationStatus.PENDING }; const result = await prisma.$transaction([ prisma.quote.update({ where: { id: quotationId, }, data: updatedQuote, }), prisma.quotation.update({ where: { id: quotationId }, data: updatedFields, }), ]); return res.status(httpStatusCodes.OK).json( new ResponseJSON('Quote reason edited', false, httpStatusCodes.OK, result[0]) ); - User
reformat the last updated code its not visible
- Assistant
I apologize for the inconvenience. Here is the updated code reformatted for better readability:
if (quotation.status !== QuotationStatus.REJECTED) { return res.status(httpStatusCodes.UNAUTHORIZED).json( new ResponseJSON( 'You can only edit rejected quotes', true, httpStatusCodes.UNAUTHORIZED ) ); } if (quotation.collectorId !== req.user?.id) { return res.status(httpStatusCodes.UNAUTHORIZED).json( new ResponseJSON( 'You are not authorized to edit this quote', true, httpStatusCodes.UNAUTHORIZED ) ); } updatedQuote.updatedAt = new Date(); const updatedFields = { ...updatedQuote, status: QuotationStatus.PENDING }; const result = await prisma.$transaction([ prisma.quote.update({ where: { id: quotationId, }, data: updatedQuote, }), prisma.quotation.update({ where: { id: quotationId }, data: updatedFields, }), ]); return res.status(httpStatusCodes.OK).json( new ResponseJSON('Quote reason edited', false, httpStatusCodes.OK, result[0]) );