45 lines
1.8 KiB
TypeScript
45 lines
1.8 KiB
TypeScript
// Actions du gérant sur une commande
|
|
export default defineEventHandler(async (event) => {
|
|
const o = findOrder(getRouterParam(event, 'id')!)
|
|
const body = await readBody<{ action: 'accept' | 'refuse' | 'advance' | 'refund', minutes?: number }>(event)
|
|
const now = Date.now()
|
|
const hhmm = (t: number) => new Date(t).toLocaleTimeString('fr-FR', { hour: '2-digit', minute: '2-digit', timeZone: 'Europe/Paris' }).replace(':', 'h')
|
|
|
|
switch (body.action) {
|
|
case 'accept': {
|
|
if (o.status !== 'new') break
|
|
const min = [10, 20, 30, 45].includes(body.minutes!) ? body.minutes! : 20
|
|
o.status = 'prep'
|
|
o.prepMinutes = min
|
|
o.readyBy = now + min * 60000
|
|
o.history.push({ at: now, label: `Acceptée · prête vers ${hhmm(o.readyBy)}` })
|
|
break
|
|
}
|
|
case 'refuse':
|
|
if (o.status !== 'new') break
|
|
o.status = 'refused'
|
|
if (o.paid) { o.refunded = true; o.paid = false }
|
|
o.history.push({ at: now, label: 'Refusée par le restaurant' + (o.refunded ? ' · remboursée' : '') })
|
|
break
|
|
case 'advance':
|
|
if (o.status === 'prep') {
|
|
o.status = 'ready'
|
|
o.history.push({ at: now, label: o.mode === 'livraison' ? 'Prête · en attente du livreur' : 'Prête · en attente du client' })
|
|
} else if (o.status === 'ready') {
|
|
o.status = 'done'
|
|
if (o.payment === 'especes') o.paid = true
|
|
o.history.push({ at: now, label: o.mode === 'livraison' ? 'Remise au livreur' : 'Récupérée par le client' })
|
|
}
|
|
break
|
|
case 'refund':
|
|
if (!o.refunded && o.payment !== 'especes') {
|
|
o.refunded = true
|
|
o.paid = false
|
|
o.history.push({ at: now, label: `Remboursée · ${(o.total / 100).toFixed(2).replace('.', ',')} €` })
|
|
}
|
|
break
|
|
default:
|
|
throw createError({ statusCode: 400, statusMessage: 'Action inconnue' })
|
|
}
|
|
return o
|
|
})
|