clear error deploy prod
This commit is contained in:
@@ -61,19 +61,20 @@ router.get('/:id', async (req, res, next) => {
|
||||
|
||||
router.patch('/:id', requireRole('MANAGER'), async (req, res, next) => {
|
||||
try {
|
||||
const { id } = z.object({ id: z.string() }).parse(req.params)
|
||||
const { vehicleIds, ...body } = offerSchema.partial().parse(req.body)
|
||||
const existing = await prisma.offer.findFirst({ where: { id: req.params.id, companyId: req.companyId } })
|
||||
const existing = await prisma.offer.findFirst({ where: { id, companyId: req.companyId } })
|
||||
if (!existing) return res.status(404).json({ error: 'not_found', message: 'Offer not found', statusCode: 404 })
|
||||
await prisma.$transaction(async (tx) => {
|
||||
await tx.offer.update({ where: { id: req.params.id }, data: { ...body, validFrom: body.validFrom ? new Date(body.validFrom) : undefined, validUntil: body.validUntil ? new Date(body.validUntil) : undefined } })
|
||||
await tx.offer.update({ where: { id }, data: { ...body, validFrom: body.validFrom ? new Date(body.validFrom) : undefined, validUntil: body.validUntil ? new Date(body.validUntil) : undefined } })
|
||||
if (vehicleIds !== undefined) {
|
||||
await tx.offerVehicle.deleteMany({ where: { offerId: req.params.id } })
|
||||
await tx.offerVehicle.deleteMany({ where: { offerId: id } })
|
||||
if (vehicleIds.length > 0) {
|
||||
await tx.offerVehicle.createMany({ data: vehicleIds.map((vehicleId) => ({ offerId: req.params.id, vehicleId })) })
|
||||
await tx.offerVehicle.createMany({ data: vehicleIds.map((vehicleId) => ({ offerId: id, vehicleId })) })
|
||||
}
|
||||
}
|
||||
})
|
||||
const updated = await prisma.offer.findUniqueOrThrow({ where: { id: req.params.id }, include: { vehicles: true } })
|
||||
const updated = await prisma.offer.findUniqueOrThrow({ where: { id }, include: { vehicles: true } })
|
||||
res.json({ data: updated })
|
||||
} catch (err) { next(err) }
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user