Motdde
05/15/2022, 11:29 PMasync function handler(req: NextApiRequest, res: NextApiResponse<Response>) {
await verifyUserToken(req, res)
const automobileQuery = req.query.automobile.toString().split(',')
if (automobileQuery.length > 2) {
throw new CustomError(
400,
'automobile URL parameter expects a maximum of two values'
)
}
let values: VehicleType[] | undefined
if (automobileQuery.includes('BIKE')) {
if (values === undefined) values = []
values.push(VehicleType.BIKE)
}
if (automobileQuery.includes('CAR')) {
if (values === undefined) values = []
values.push(VehicleType.CAR)
}
let vehicleType = undefined
let orders = await prisma.order.findMany({
where: {
vendorId: null,
trackingStatus: 'READY_FOR_PICKUP',
vehicleType: { in: values },
},
})
return res.json({
status: true,
data: { orders },
})
}Jason Kleinberg
05/16/2022, 12:14 PMconst validVehiclesTypes: Record<string, VehicleType> = {
BIKE: VehicleType.BIKE,
CAR: VehicleType.CAR,
}
const values: VehicleType[] = automobileQuery
.map((vehicle) => validVehicleTypes[vehicle])
.filter(Boolean)Jason Kleinberg
05/16/2022, 12:17 PMreduce or flatMap, but this seemed a little more clear.Motdde
05/16/2022, 2:45 PM.filter(Boolean) .
Prisma expects undefined in order to omit a condition in the where object. Can I use a ternary operator to check for the size of values and return undefined if its zero?Jason Kleinberg
05/16/2022, 4:51 PMreduce or flatMap you would use the ternary With filter when the mapped value is undefined, it will be turned into false and filtered out.