added mailing through Brevo and node mailer, also a email editor, with

their custom blocks and renderers
This commit is contained in:
Jakub Dorfman 2026-09-28 19:25:19 +02:00
parent 43db707e34
commit 96828994e1
34 changed files with 2035 additions and 323 deletions

View file

@ -1,4 +1,6 @@
import type { PageServerLoad } from './$types';
import { fail } from '@sveltejs/kit';
import { sendOrderEmails, type EmailKind } from '$lib/server/email';
import type { Actions, PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ locals }) => {
// Sort by id, not 'created' — commerce collections were imported without
@ -9,7 +11,7 @@ export const load: PageServerLoad = async ({ locals }) => {
for (const record of records) {
const lineRecords = await locals.pb
.collection('order_lines')
.getFullList({ filter: `order = "${record.id}"` })
.getFullList({ filter: locals.pb.filter('order = {:id}', { id: record.id }) })
.catch(() => []);
orders.push({
@ -19,6 +21,9 @@ export const load: PageServerLoad = async ({ locals }) => {
total: Number(record.total) || 0,
status: String(record.status || 'pending'),
created: String(record.created || ''),
customerEmailSentAt: String(record.customerEmailSentAt || ''),
clientEmailSentAt: String(record.clientEmailSentAt || ''),
emailError: String(record.emailError || ''),
lines: lineRecords.map((line) => ({
id: line.id,
name: String(line.name || ''),
@ -32,3 +37,25 @@ export const load: PageServerLoad = async ({ locals }) => {
return { orders };
};
const OUTCOME_MESSAGES: Record<string, string> = {
sent: 'sent',
logged: 'not sent — SMTP is not configured (written to the server log)',
disabled: 'not sent — this email is turned off in Emails',
failed: 'failed — see the error on the order'
};
export const actions: Actions = {
resend: async ({ request, locals }) => {
const data = await request.formData();
const orderId = String(data.get('orderId') || '');
const kind = String(data.get('kind') || '');
if (!orderId || (kind !== 'customer' && kind !== 'client')) {
return fail(400, { orderId, ok: false, message: 'Invalid resend request.' });
}
const outcomes = await sendOrderEmails(locals.pb, orderId, [kind as EmailKind]);
const outcome = outcomes[kind as EmailKind] ?? 'failed';
const label = kind === 'customer' ? 'Customer confirmation' : 'Booking alert';
return { orderId, ok: outcome === 'sent', message: `${label} ${OUTCOME_MESSAGES[outcome]}.` };
}
};

View file

@ -1,10 +1,37 @@
<script lang="ts">
let { data } = $props();
import { enhance } from '$app/forms';
let { data, form } = $props();
let orders = $derived(data.orders || []);
let resending = $state('');
const eur = new Intl.NumberFormat(undefined, { style: 'currency', currency: 'EUR' });
</script>
{#snippet emailStatus(order: (typeof orders)[number], kind: 'customer' | 'client', label: string, sentAt: string)}
<form
method="POST"
action="?/resend"
class="flex items-center gap-2"
use:enhance={() => {
resending = `${order.id}-${kind}`;
return async ({ update }) => {
resending = '';
await update();
};
}}
>
<input type="hidden" name="orderId" value={order.id} />
<input type="hidden" name="kind" value={kind} />
<span class="badge badge-sm" class:badge-success={sentAt} class:badge-ghost={!sentAt} title={sentAt ? new Date(sentAt).toLocaleString() : 'Not sent'}>
{label}: {sentAt ? 'sent' : 'not sent'}
</span>
<button class="btn btn-ghost btn-xs" type="submit" disabled={resending !== ''}>
{resending === `${order.id}-${kind}` ? 'Sending…' : sentAt ? 'Resend' : 'Send'}
</button>
</form>
{/snippet}
<svelte:head><title>Orders | CMS</title></svelte:head>
<div class="space-y-6">
@ -53,6 +80,17 @@
{#if order.customerDetails?.notes}
<p class="mt-3 text-sm italic text-base-content/60">"{order.customerDetails.notes}"</p>
{/if}
<div class="mt-4 flex flex-wrap gap-x-6 gap-y-2 border-t border-base-300 pt-3">
{@render emailStatus(order, 'customer', 'Confirmation', order.customerEmailSentAt)}
{@render emailStatus(order, 'client', 'Alert', order.clientEmailSentAt)}
</div>
{#if order.emailError}
<p class="mt-2 whitespace-pre-line text-sm text-error">{order.emailError}</p>
{/if}
{#if form?.orderId === order.id && form.message}
<div class="alert mt-2 py-2 text-sm" class:alert-success={form.ok} class:alert-warning={!form.ok}>{form.message}</div>
{/if}
</div>
{/each}
</div>