import { HttpError } from "wasp/server"; import { SECTION_TEMPLATES } from "./sections.js"; export const createSpec = async ({ title }, context) => { if (!context.user) { throw new HttpError(401); } const name = (title || "tech-spec-untitled").trim() || "tech-spec-untitled"; return context.entities.Spec.create({ data: { title: name, user: { connect: { id: context.user.id } }, sections: { create: SECTION_TEMPLATES.map((s, i) => ({ key: s.key, title: s.title, hint: s.hint, content: s.starter, order: i, })), }, }, include: { sections: { orderBy: { order: "asc" } } }, }); }; export const updateSection = async ({ specId, key, content }, context) => { if (!context.user) { throw new HttpError(401); } const count = await context.entities.Section.updateMany({ where: { specId, key, spec: { userId: context.user.id } }, data: { content }, }); if (count === 0) { throw new HttpError(404, "Section not found"); } return { ok: true }; }; export const resetSection = async ({ specId, key }, context) => { if (!context.user) { throw new HttpError(401); } const template = SECTION_TEMPLATES.find((s) => s.key === key); if (!template) { throw new HttpError(400, "Unknown section key"); } const count = await context.entities.Section.updateMany({ where: { specId, key, spec: { userId: context.user.id } }, data: { content: template.starter }, }); if (count === 0) { throw new HttpError(404, "Section not found"); } return { ok: true }; }; export const deleteSpec = async ({ id }, context) => { if (!context.user) { throw new HttpError(401); } await context.entities.Spec.deleteMany({ where: { id, userId: context.user.id }, }); return { ok: true }; };