Files
lunch/lib/cooking.ts
T
kai 0692914666
release / which version (push) Successful in 1s
release / olares chart · sanity (push) Successful in 4s
release / app (push) Successful in 1m0s
release / tag and release (push) Successful in 3s
Cooking mode with timers and wake lock, ratings and thank-yous (26.11.13) (#14)
2026-11-07 05:06:07 +00:00

109 lines
5.0 KiB
TypeScript

import "server-only";
import type { PoolClient } from "pg";
import { getDish, type DishDetail } from "./dishes";
// Kochen und danach (Konzept, „Kochen“ und „Bewerten“): ein Slot wird gekocht,
// jede Person gibt Daumen hoch oder runter und optional ein Dankeschön an den
// Koch. Bewertungen fließen über dish_popularity in den nächsten Vorschlag.
export type CookingSlot = {
id: string;
portions: number;
cookMemberId: string | null;
cookName: string | null;
cookedAt: string | null;
dish: DishDetail;
};
export async function cookingSlot(client: PoolClient, slotId: string, locale: string): Promise<CookingSlot | null> {
const row = (
await client.query(
`select s.id, s.portions, s.cook_member_id as "cookMemberId", m.display_name as "cookName", s.cooked_at::text as "cookedAt", s.dish_id
from slots s left join members m on m.id = s.cook_member_id where s.id = $1 and s.dish_id is not null`,
[slotId],
)
).rows[0];
if (!row) return null;
const dish = await getDish(client, row.dish_id, locale);
if (!dish) return null;
const { dish_id: _, ...slot } = row;
return { ...slot, dish };
}
/** „Fertig“: Slot ist gekocht; wer kocht, steht fest; ist alles gekocht, endet die Runde. */
export async function markCooked(client: PoolClient, slotId: string, memberId: string): Promise<boolean> {
const { rows } = await client.query(
`update slots set cooked_at = now(), cook_member_id = coalesce(cook_member_id, $2)
where id = $1 and cooked_at is null and dish_id is not null returning round_id`,
[slotId, memberId],
);
if (!rows[0]) return false;
await client.query(
`update rounds set status = 'done' where id = $1 and status <> 'done'
and not exists (select 1 from slots where round_id = $1 and dish_id is not null and cooked_at is null)`,
[rows[0].round_id],
);
return true;
}
export class RatingRefused extends Error {}
async function cookedSlot(client: PoolClient, slotId: string) {
return (await client.query("select cook_member_id from slots where id = $1 and cooked_at is not null", [slotId])).rows[0] as
| { cook_member_id: string | null }
| undefined;
}
export async function rate(client: PoolClient, householdId: string, slotId: string, memberId: string, thumbsUp: boolean, comment: string | null) {
if (!(await cookedSlot(client, slotId))) throw new RatingRefused("notCooked");
await client.query(
`insert into ratings (household_id, slot_id, member_id, thumbs_up, comment) values ($1, $2, $3, $4, $5)
on conflict (slot_id, member_id) do update set thumbs_up = excluded.thumbs_up, comment = excluded.comment`,
[householdId, slotId, memberId, thumbsUp, comment?.trim() || null],
);
}
export async function thank(client: PoolClient, householdId: string, slotId: string, memberId: string) {
const slot = await cookedSlot(client, slotId);
if (!slot) throw new RatingRefused("notCooked");
if (slot.cook_member_id === memberId) throw new RatingRefused("ownDish");
await client.query(
"insert into kudos (household_id, slot_id, from_member_id) values ($1, $2, $3) on conflict do nothing",
[householdId, slotId, memberId],
);
}
export type ToRate = { slotId: string; title: string; cookName: string | null; isCook: boolean; thanked: boolean };
/** Gekochtes der letzten drei Tage, das ich noch nicht bewertet habe. */
export async function pendingRatings(client: PoolClient, memberId: string): Promise<ToRate[]> {
const { rows } = await client.query(
`select s.id as "slotId", d.title, m.display_name as "cookName", s.cook_member_id = $1 as "isCook",
exists (select 1 from kudos k where k.slot_id = s.id and k.from_member_id = $1) as thanked
from slots s join dishes d on d.id = s.dish_id left join members m on m.id = s.cook_member_id
where s.cooked_at > now() - interval '3 days'
and not exists (select 1 from ratings r where r.slot_id = s.id and r.member_id = $1)
order by s.cooked_at desc`,
[memberId],
);
return rows.map((r) => ({ ...r, isCook: !!r.isCook }));
}
export type Thanks = { slotId: string; title: string; kudos: number; up: number; down: number; comments: string[] };
/** Für den Koch: Dankeschöns und Daumen der letzten sieben Tage. */
export async function thanksForCook(client: PoolClient, memberId: string): Promise<Thanks[]> {
const { rows } = await client.query(
`select s.id as "slotId", d.title,
(select count(*)::int from kudos k where k.slot_id = s.id) as kudos,
(select count(*)::int from ratings r where r.slot_id = s.id and r.thumbs_up) as up,
(select count(*)::int from ratings r where r.slot_id = s.id and not r.thumbs_up) as down,
coalesce((select array_agg(r.comment order by r.created_at) from ratings r where r.slot_id = s.id and r.comment is not null), '{}') as comments
from slots s join dishes d on d.id = s.dish_id
where s.cook_member_id = $1 and s.cooked_at > now() - interval '7 days'
order by s.cooked_at desc`,
[memberId],
);
return rows.filter((r) => r.kudos + r.up + r.down > 0);
}