Add independent roast plan cloning from the plans table
Test and deploy / test-and-deploy (push) Successful in 1m52s
Test and deploy / test-and-deploy (push) Successful in 1m52s
This commit is contained in:
1 parent
393978195c
commit
a9358048ef
5 files changed
+46
-1
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import { blankPlan } from "../shared/fields.js";
|
||||
import { sanitizeObservations } from "../shared/roast-comparison.js";
|
||||
import express from "express";
|
||||
import path from "node:path";
|
||||
@@ -790,6 +791,21 @@ export function createApp({
|
||||
res.json({ok:true,plan:row});
|
||||
} catch(error) {next(error);}
|
||||
});
|
||||
app.post("/api/plans/:id/clone", requireAuth, csrf, requireUuidParam("id"), async (req, res, next) => {
|
||||
try {
|
||||
const source = (await db.query("SELECT plan,name FROM roast_plans WHERE id=$1 AND user_id=$2", [req.params.id, req.user.id])).rows[0];
|
||||
if (!source) return res.status(404).json({ok:false, code:"not_found"});
|
||||
const plan = structuredClone(source.plan);
|
||||
const defaults = blankPlan();
|
||||
plan.planActual = defaults.planActual;
|
||||
plan.afterRoast = defaults.afterRoast;
|
||||
plan.inventory = {...defaults.inventory, ...plan.inventory, consumed:null};
|
||||
const title = source.name || plan.fields?.["0.1"] || "Untitled plan";
|
||||
const name = `${title.slice(0, 113)} (copy)`;
|
||||
const copy = (await db.query("INSERT INTO roast_plans(user_id,plan,name) VALUES($1,$2,$3) RETURNING id,plan,name,created_at,updated_at", [req.user.id, plan, name])).rows[0];
|
||||
res.status(201).json({ok:true, plan:copy});
|
||||
} catch (error) { next(error); }
|
||||
});
|
||||
app.post("/api/plans", requireAuth, csrf, async (req, res, next) => {
|
||||
try {
|
||||
if (!req.body.plan || typeof req.body.plan !== "object")
|
||||
|
||||
Reference in new issue
Block a user