import { z } from "zod"; import { clientShotCode } from "@/lib/shot-code"; /** * The handoff after a package is built: upload it, then share the link. * * Kept free of database imports so the deliveries page can use the same stage * logic and share message the server does. */ export type HandoffStage = "AWAITING_UPLOAD" | "AWAITING_SHARE"; /** * What a built EXR package is still waiting on, or null when it is waiting on * nothing — not built yet, already delivered, or a review (which has no * delivered state at all). */ export function handoffStage(pkg: { kind: string; status: string; uploadedAt: Date | string | null; }): HandoffStage | null { if (pkg.kind !== "EXR_DELIVERY" || pkg.status !== "READY") return null; return pkg.uploadedAt ? "AWAITING_SHARE" : "AWAITING_UPLOAD"; } export const recordUploadSchema = z.object({ uploadUrl: z .string() .trim() .max(2000) .url("Paste the full link to the upload folder") .refine((value) => /^https?:\/\//i.test(value), "The upload link must start with http:// or https://"), /** The person looked at the upload and its contents match the package. */ contentsConfirmed: z.literal(true, { errorMap: () => ({ message: "Confirm the uploaded files match the package" }), }), }); export type RecordUploadInput = z.infer; export const recordShareSchema = z.object({ sharedWith: z.string().trim().min(1, "Say who the link was sent to").max(200), note: z.string().trim().max(1000).optional().nullable(), }); export type RecordShareInput = z.infer; /** * The message sent to the client with the link. Generated rather than typed so * the shots and versions the client is told about are the ones in the package. */ export function buildShareMessage(pkg: { projectCode: string; episode: string; name: string; uploadUrl: string; items: { shotCode: string; clientCode?: string | null; versionString: string }[]; }): string { const shots = pkg.items .map((item) => `${clientShotCode(item)} ${item.versionString}`) .sort((a, b) => a.localeCompare(b)); const heading = [pkg.projectCode, pkg.episode ? `episode ${pkg.episode}` : "", pkg.name] .filter(Boolean) .join(" · "); return [ `Delivery: ${heading}`, `${shots.length} shot${shots.length === 1 ? "" : "s"}`, pkg.uploadUrl, "", ...shots, ].join("\n"); }