send email on claim requests
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import { json, error } from '@sveltejs/kit';
|
||||
import type { RequestEvent } from '@sveltejs/kit';
|
||||
import { createPbClient } from '$lib/server/pocketbase';
|
||||
import { createPbClient, pbAdmin } from '$lib/server/pocketbase';
|
||||
import { createServices } from '$lib/server/services';
|
||||
import { sendClaimRequestEmail } from '$lib/server/email';
|
||||
import { handleOf } from '@shared/slugify';
|
||||
|
||||
export async function POST(event: RequestEvent) {
|
||||
const u = event.locals.user;
|
||||
@@ -10,8 +12,36 @@ export async function POST(event: RequestEvent) {
|
||||
const id = event.params.id!;
|
||||
try {
|
||||
const s = createServices(pb, { id: u.id, role: u.role });
|
||||
return json(await s.rewards.claim(u.famId, id));
|
||||
const record = await s.rewards.claim(u.famId, id);
|
||||
|
||||
// Tell the parent(s) a claim was requested — best-effort, so a mail
|
||||
// outage can never fail the claim itself.
|
||||
try {
|
||||
const [parents, fam] = await Promise.all([
|
||||
pbAdmin.getList('users', `famId = '${u.famId}' && role = 'parent'`),
|
||||
pbAdmin.getOne('fams', u.famId)
|
||||
]);
|
||||
const label = record?.label || 'a reward';
|
||||
const idx = label.indexOf(' – ');
|
||||
const rewardName = idx === -1 ? label : label.slice(0, idx);
|
||||
const rewardValue = idx === -1 ? '' : label.slice(idx + 3);
|
||||
for (const parent of parents) {
|
||||
if (!parent.email) continue;
|
||||
await sendClaimRequestEmail({
|
||||
to: parent.email,
|
||||
parentName: parent.name || 'there',
|
||||
childName: u.name || 'Someone',
|
||||
rewardName,
|
||||
rewardValue,
|
||||
dashboardUrl: `${event.url.origin}/${fam?.slug || u.famId}/${handleOf(parent.username)}`
|
||||
});
|
||||
}
|
||||
} catch {
|
||||
// Best-effort only — the claim already succeeded.
|
||||
}
|
||||
|
||||
return json(record);
|
||||
} catch (e) {
|
||||
return json({ error: e instanceof Error ? e.message : 'claim failed' }, { status: 400 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user