feat: lunch recap aggregation + sendRecap with forceUpdate gate

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Matt
2026-04-29 02:38:00 +02:00
parent 05b0412534
commit 829a7e457a
2 changed files with 181 additions and 1 deletions

View File

@@ -2,7 +2,8 @@ import { z } from 'zod'
import { TRPCError } from '@trpc/server'
import { router, adminProcedure, protectedProcedure } from '../trpc'
import { logAudit } from '../utils/audit'
import { buildManifest } from '../services/lunch-recap'
import { buildManifest, buildRecapPayload } from '../services/lunch-recap'
import { sendLunchRecapEmail } from '@/lib/email'
// ─── Shared zod schemas ──────────────────────────────────────────────────────
@@ -268,6 +269,67 @@ export const lunchRouter = router({
return lines.join('\n')
}),
// ─── Recap preview + send ────────────────────────────────────────────────
getRecapPreview: adminProcedure
.input(z.object({ programId: z.string() }))
.query(({ ctx, input }) => buildRecapPayload(ctx.prisma, input.programId)),
sendRecap: adminProcedure
.input(
z.object({
programId: z.string(),
forceUpdate: z.boolean().optional(),
}),
)
.mutation(async ({ ctx, input }) => {
const event = await ctx.prisma.lunchEvent.findUnique({
where: { programId: input.programId },
})
if (!event) throw new TRPCError({ code: 'NOT_FOUND', message: 'Lunch event not found' })
if (event.recapSentAt && !input.forceUpdate) {
throw new TRPCError({
code: 'PRECONDITION_FAILED',
message: 'Recap already sent. Pass forceUpdate=true to resend.',
})
}
const payload = await buildRecapPayload(ctx.prisma, input.programId)
const adminUsers = await ctx.prisma.user.findMany({
where: {
role: { in: ['SUPER_ADMIN', 'PROGRAM_ADMIN'] },
email: { not: '' },
},
select: { email: true },
})
const recipients = [
...adminUsers.map((u) => u.email).filter(Boolean),
...event.extraRecipients,
]
try {
await sendLunchRecapEmail(recipients, payload)
} catch (e) {
console.error('[lunch.sendRecap] email send failed', e)
// Continue — we still stamp recapSentAt and audit so admins see what happened.
}
const updated = await ctx.prisma.lunchEvent.update({
where: { programId: input.programId },
data: { recapSentAt: new Date() },
})
await logAudit({
prisma: ctx.prisma,
userId: ctx.user.id,
action: 'LUNCH_RECAP_SENT',
entityType: 'LunchEvent',
entityId: event.id,
detailsJson: {
recipientCount: recipients.length,
forceUpdate: !!input.forceUpdate,
source: 'manual',
},
})
return updated
}),
// ─── Member reads ────────────────────────────────────────────────────────
/**