Allow editing SKU value from the Edit SKU modal
Build and push image / build (push) Successful in 54s
Build and push image / build (push) Successful in 54s
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -91,6 +91,7 @@ productsRouter.post("/products", (req, res) => {
|
||||
});
|
||||
|
||||
type UpdateBody = Partial<{
|
||||
sku: string;
|
||||
type: string;
|
||||
kind: "bulk" | "discrete";
|
||||
strainId: string | null;
|
||||
@@ -105,13 +106,21 @@ productsRouter.patch("/products/:id", (req, res) => {
|
||||
const existing = db
|
||||
.prepare<
|
||||
[string],
|
||||
{ id: string; type: string; kind: string; strain_id: string; brand_id: string | null }
|
||||
{ id: string; sku: string; type: string; kind: string; strain_id: string; brand_id: string | null }
|
||||
>(
|
||||
`SELECT id, type, kind, strain_id, brand_id FROM products WHERE id = ?`,
|
||||
`SELECT id, sku, type, kind, strain_id, brand_id FROM products WHERE id = ?`,
|
||||
)
|
||||
.get(id);
|
||||
if (!existing) return res.status(404).json({ error: "product not found" });
|
||||
|
||||
const nextSku = typeof body.sku === "string" && body.sku.trim() ? body.sku.trim() : existing.sku;
|
||||
if (nextSku !== existing.sku) {
|
||||
const duplicate = db
|
||||
.prepare<[string, string], { id: string }>("SELECT id FROM products WHERE sku = ? AND id != ?")
|
||||
.get(nextSku, id);
|
||||
if (duplicate) return res.status(409).json({ error: "sku already exists" });
|
||||
}
|
||||
|
||||
const nextType = typeof body.type === "string" && body.type ? body.type : existing.type;
|
||||
const nextKind: "bulk" | "discrete" =
|
||||
body.kind === "bulk" || body.kind === "discrete"
|
||||
@@ -144,8 +153,8 @@ productsRouter.patch("/products/:id", (req, res) => {
|
||||
}
|
||||
|
||||
db.prepare(
|
||||
`UPDATE products SET type = ?, kind = ?, strain_id = ?, brand_id = ? WHERE id = ?`,
|
||||
).run(nextType, nextKind, nextStrainId, nextBrandId, id);
|
||||
`UPDATE products SET sku = ?, type = ?, kind = ?, strain_id = ?, brand_id = ? WHERE id = ?`,
|
||||
).run(nextSku, nextType, nextKind, nextStrainId, nextBrandId, id);
|
||||
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user