83 lines
4.2 KiB
TypeScript
83 lines
4.2 KiB
TypeScript
import "server-only";
|
|
import type { PoolClient } from "pg";
|
|
import { z } from "zod";
|
|
|
|
// Eine Planungsrunde entspricht einem Großeinkauf (Konzept, „Abläufe“). In
|
|
// Paket 5 wird sie nur eröffnet, damit gewünscht werden kann; Vorschlag,
|
|
// Slots und Freigabe folgen mit Paket 6.
|
|
|
|
export type Round = {
|
|
id: string;
|
|
status: "open" | "planned" | "shopping" | "done";
|
|
shoppingDate: string | null;
|
|
slotsCount: number;
|
|
defaultPortions: number;
|
|
/** Bis wann gewünscht werden kann (ISO, UTC), oder null (26.11.53). */
|
|
wishDeadline: string | null;
|
|
};
|
|
|
|
export const roundInput = z.object({
|
|
shoppingDate: z.string().regex(/^\d{4}-\d{2}-\d{2}$/).nullable(),
|
|
slotsCount: z.number().int().min(1).max(14),
|
|
defaultPortions: z.number().int().min(1).max(30),
|
|
// „Wünsche bis“ (26.11.53): ein Zeitpunkt mit Zone; leer heißt keine Frist und keine Erinnerungen.
|
|
wishDeadline: z.string().datetime({ offset: true }).nullable().optional(),
|
|
});
|
|
|
|
const columns = `id, status, to_char(shopping_date, 'YYYY-MM-DD') as "shoppingDate", slots_count as "slotsCount",
|
|
default_portions as "defaultPortions",
|
|
to_char(wish_deadline at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"') as "wishDeadline"`;
|
|
|
|
/** Die laufende Runde (nicht abgeschlossen), oder null. Es gibt höchstens eine. */
|
|
export async function currentRound(client: PoolClient): Promise<Round | null> {
|
|
const { rows } = await client.query(`select ${columns} from rounds where status <> 'done' order by created_at desc limit 1`);
|
|
return rows[0] ?? null;
|
|
}
|
|
|
|
export class RoundExists extends Error {}
|
|
|
|
export async function openRound(client: PoolClient, householdId: string, memberId: string, input: z.infer<typeof roundInput>): Promise<string> {
|
|
// Zwei gleichzeitige Eröffnungen dürfen nicht zwei Runden ergeben.
|
|
await client.query("select pg_advisory_xact_lock(hashtext('round:' || $1))", [householdId]);
|
|
if (await currentRound(client)) throw new RoundExists();
|
|
const store = (await client.query("select id from stores where is_default limit 1")).rows[0]?.id ?? null;
|
|
const { rows } = await client.query(
|
|
`insert into rounds (household_id, shopping_date, slots_count, default_portions, store_id, created_by, wish_deadline)
|
|
values ($1, $2, $3, $4, $5, $6, $7) returning id`,
|
|
[householdId, input.shoppingDate, input.slotsCount, input.defaultPortions, store, memberId, input.wishDeadline ?? null],
|
|
);
|
|
return rows[0].id;
|
|
}
|
|
|
|
// Runde beenden, löschen, ändern (Kai, 8.11.2026, 26.11.45). Bis dahin endete eine
|
|
// Runde nur, wenn alles gekocht war — wer etwas ausließ, kam nie zur nächsten.
|
|
|
|
/** Beenden: Gekochtes und Bewertungen bleiben Geschichte, Ungekochtes verfällt. */
|
|
export async function endRound(client: PoolClient, roundId: string): Promise<boolean> {
|
|
const { rowCount } = await client.query("update rounds set status = 'done' where id = $1 and status <> 'done'", [roundId]);
|
|
return rowCount === 1;
|
|
}
|
|
|
|
export class RoundHasCooked extends Error {}
|
|
|
|
/** Ganz löschen — nur solange nichts gekocht ist, damit keine Bewertung verloren geht. */
|
|
export async function deleteRound(client: PoolClient, roundId: string): Promise<boolean> {
|
|
const cooked = await client.query("select 1 from slots where round_id = $1 and cooked_at is not null limit 1", [roundId]);
|
|
if (cooked.rowCount) throw new RoundHasCooked();
|
|
const { rowCount } = await client.query("delete from rounds where id = $1 and status <> 'done'", [roundId]);
|
|
return rowCount === 1;
|
|
}
|
|
|
|
/** Einkaufstag, Zahl der Mahlzeiten und Standardportionen nachträglich ändern. */
|
|
export async function updateRound(client: PoolClient, roundId: string, input: z.infer<typeof roundInput>): Promise<boolean> {
|
|
const { rowCount } = await client.query(
|
|
// Eine verschobene Frist erinnert neu: Erinnerungen, deren Zeit noch kommt, gehen wieder raus.
|
|
`update rounds set shopping_date = $2, slots_count = $3, default_portions = $4, wish_deadline = $5,
|
|
remind24_sent_at = case when wish_deadline is distinct from $5 then null else remind24_sent_at end,
|
|
remind2_sent_at = case when wish_deadline is distinct from $5 then null else remind2_sent_at end
|
|
where id = $1 and status <> 'done'`,
|
|
[roundId, input.shoppingDate, input.slotsCount, input.defaultPortions, input.wishDeadline ?? null],
|
|
);
|
|
return rowCount === 1;
|
|
}
|