Two things the shop asked for, and one it will notice. **A message on an exception.** A closure or a late opening can now carry a reason, and it reaches the panel. It shows while the door is shut and disappears the moment the shop opens — a notice explaining a late opening is worse than useless once the door is open. It outranks a free message on purpose: "closed this afternoon, back tomorrow" is what someone standing outside needs, and "new collection in store" can wait. Say so if you would rather it were the other way. The first version keyed this off the day's `isOpen`, which means "this day has opening hours" — so a day opening at 14:00 counted as open all morning, exactly when the reason is needed. A test caught it; the rule now reads the state at this minute. **Reusable phrases.** The same handful of notices get written over and over. /admin/modeles keeps them, translated once, and offers them wherever a message is composed — exceptions, closure periods, the banner. Picking one costs no translation at all: the saved English is reused directly, where the service takes the better part of ten seconds. Exception notes and holiday labels are translated too, which they were not before. After the save rather than during it, so a slow service never costs the shop its dates. **Previewing the future.** The dashboard can render the panel at any moment within about a year: "what will the window say while I'm away?" is worth answering before someone is standing in front of a locked door. The whole pipeline was already a function of "now", so this costs passing a different instant. An unparseable or absurd value falls back to the present rather than confidently rendering nonsense. Checked against a three-week holiday, which surfaced something worth knowing: the automatic "opens on…" line stays empty, because the resolver's search is bounded to fourteen days. During a long closure the message is the only thing that tells customers when the shop is back — which is a good reason for this feature to exist. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012cSY9pVhZmJUKNN7wf1Myd
165 lines
5.0 KiB
TypeScript
165 lines
5.0 KiB
TypeScript
'use server';
|
||
|
||
import { revalidatePath } from 'next/cache';
|
||
|
||
import { diffOf, recordAudit } from '@/lib/audit';
|
||
import { requireAdmin } from '@/lib/auth/actions';
|
||
import { prisma } from '@/lib/db';
|
||
import { MESSAGE_MAX_CHARS } from '@/lib/screen/contract';
|
||
import { translateToEnglish } from '@/lib/translation/service';
|
||
|
||
export type ActionResult<T = undefined> = { ok: true; value: T } | { ok: false; error: string };
|
||
|
||
/**
|
||
* Saves a reusable phrase.
|
||
*
|
||
* Templates are translated like any other message, but once rather than at
|
||
* every use: that is most of the point of having them.
|
||
*/
|
||
export async function saveTemplate(input: {
|
||
id?: string;
|
||
textFr: string;
|
||
sortOrder: number;
|
||
}): Promise<ActionResult<{ id: string }>> {
|
||
const gate = await requireAdmin();
|
||
if (!gate.ok) {
|
||
return { ok: false, error: gate.error };
|
||
}
|
||
|
||
const textFr = input.textFr.trim();
|
||
if (textFr.length === 0) {
|
||
return { ok: false, error: 'Le modèle ne peut pas être vide.' };
|
||
}
|
||
if (textFr.length > MESSAGE_MAX_CHARS) {
|
||
return {
|
||
ok: false,
|
||
error: `Le modèle dépasse ${MESSAGE_MAX_CHARS} caractères et ne tiendrait pas sur l’écran.`,
|
||
};
|
||
}
|
||
|
||
const existing = input.id
|
||
? await prisma.messageTemplate.findUnique({ where: { id: input.id } })
|
||
: null;
|
||
|
||
// The French text is the identity of a template: two identical phrases are
|
||
// one phrase, and a duplicate would only ever be half-corrected later.
|
||
const clash = await prisma.messageTemplate.findUnique({ where: { textFr } });
|
||
if (clash && clash.id !== existing?.id) {
|
||
return { ok: false, error: 'Ce modèle existe déjà.' };
|
||
}
|
||
|
||
const textChanged = existing ? existing.textFr !== textFr : true;
|
||
const data = {
|
||
textFr,
|
||
sortOrder: input.sortOrder,
|
||
...(textChanged ? { textEn: null, translationStatus: 'PENDING' as const } : {}),
|
||
};
|
||
|
||
const saved = existing
|
||
? await prisma.messageTemplate.update({ where: { id: existing.id }, data })
|
||
: await prisma.messageTemplate.create({ data: { ...data, createdBy: gate.value.email } });
|
||
|
||
await recordAudit({
|
||
userEmail: gate.value.email,
|
||
action: existing ? 'template.update' : 'template.create',
|
||
entity: 'MessageTemplate',
|
||
entityId: saved.id,
|
||
diff: diffOf(existing ? { texte: existing.textFr } : null, { texte: textFr }),
|
||
});
|
||
|
||
revalidateAll();
|
||
return { ok: true, value: { id: saved.id } };
|
||
}
|
||
|
||
export async function translateTemplate(id: string): Promise<ActionResult<{ textEn: string }>> {
|
||
const gate = await requireAdmin();
|
||
if (!gate.ok) {
|
||
return { ok: false, error: gate.error };
|
||
}
|
||
|
||
const template = await prisma.messageTemplate.findUnique({ where: { id } });
|
||
if (!template) {
|
||
return { ok: false, error: 'Ce modèle a été supprimé.' };
|
||
}
|
||
if (template.translationStatus === 'MANUAL') {
|
||
return { ok: false, error: 'La traduction a été saisie à la main; elle n’est pas écrasée.' };
|
||
}
|
||
|
||
const outcome = await translateToEnglish(template.textFr);
|
||
if (!outcome.ok) {
|
||
await prisma.messageTemplate.update({ where: { id }, data: { translationStatus: 'ERROR' } });
|
||
revalidateAll();
|
||
return { ok: false, error: outcome.error };
|
||
}
|
||
|
||
await prisma.messageTemplate.update({
|
||
where: { id },
|
||
data: { textEn: outcome.text, translationStatus: 'DONE' },
|
||
});
|
||
|
||
revalidateAll();
|
||
return { ok: true, value: { textEn: outcome.text } };
|
||
}
|
||
|
||
export async function setTemplateTranslation(id: string, textEn: string): Promise<ActionResult> {
|
||
const gate = await requireAdmin();
|
||
if (!gate.ok) {
|
||
return { ok: false, error: gate.error };
|
||
}
|
||
|
||
const trimmed = textEn.trim();
|
||
const existing = await prisma.messageTemplate.findUnique({ where: { id } });
|
||
if (!existing) {
|
||
return { ok: false, error: 'Ce modèle a été supprimé.' };
|
||
}
|
||
|
||
await prisma.messageTemplate.update({
|
||
where: { id },
|
||
data: { textEn: trimmed || null, translationStatus: trimmed ? 'MANUAL' : 'PENDING' },
|
||
});
|
||
|
||
await recordAudit({
|
||
userEmail: gate.value.email,
|
||
action: 'template.translate_manual',
|
||
entity: 'MessageTemplate',
|
||
entityId: id,
|
||
diff: diffOf({ anglais: existing.textEn }, { anglais: trimmed || null }),
|
||
});
|
||
|
||
revalidateAll();
|
||
return { ok: true, value: undefined };
|
||
}
|
||
|
||
export async function deleteTemplate(id: string): Promise<ActionResult> {
|
||
const gate = await requireAdmin();
|
||
if (!gate.ok) {
|
||
return { ok: false, error: gate.error };
|
||
}
|
||
|
||
const existing = await prisma.messageTemplate.findUnique({ where: { id } });
|
||
if (!existing) {
|
||
return { ok: false, error: 'Ce modèle a déjà été supprimé.' };
|
||
}
|
||
|
||
await prisma.messageTemplate.delete({ where: { id } });
|
||
|
||
await recordAudit({
|
||
userEmail: gate.value.email,
|
||
action: 'template.delete',
|
||
entity: 'MessageTemplate',
|
||
entityId: id,
|
||
diff: diffOf({ texte: existing.textFr }, null),
|
||
});
|
||
|
||
revalidateAll();
|
||
return { ok: true, value: undefined };
|
||
}
|
||
|
||
function revalidateAll(): void {
|
||
// Templates appear in the pickers on every page that composes a message.
|
||
revalidatePath('/admin/modeles');
|
||
revalidatePath('/admin/exceptions');
|
||
revalidatePath('/admin/vacances');
|
||
revalidatePath('/admin/messages');
|
||
}
|