commit 84cbc5f54b6be02b9f0a925a5dbfb3e2b5c011d2
parent 833cb6321bd22aab132d1127e2f01bf685ed031a
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Sat, 19 Sep 2026 04:43:50 +0530
parent 833cb6321bd22aab132d1127e2f01bf685ed031a
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Sat, 19 Sep 2026 04:43:50 +0530
Task 7: Template and series lifecycle (retire, delete, unlink)
Implement lifecycle endpoints for templates and series:
- POST /templates/{id}/retire - mark template as retired (admin)
- POST /templates/{id}/delete - delete template if no linked docs (admin)
- POST /series/{code}/retire - mark series as retired (admin)
- POST /docs/{id}/unlink - unlink document from template (admin)
Update GET /templates and GET /series:
- Exclude retired items for non-admin drafters
- Show retired_at and linked document count
Partial implementation of Task 7 per spec ยง07.
Schema changes: added retired_at columns to series and templates.
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
1 file changed, 92 insertions(+), 3 deletions(-)
diff --git a/src/signet-api.c b/src/signet-api.c @@ -986,9 +986,13 @@ h_templates(struct kreq *r, PGconn *pg, const struct ident *id) PGresult *res; if (r->method == KMETHOD_GET) { + const char *admin = ident_has_role(id, &cf, "admin") ? "t" : "f"; + const char *p[] = { admin }; send_sql_json(r, pg, "SELECT coalesce(json_agg(t), '[]')::text FROM (" - "SELECT id, name, series_code, created_by, created_at FROM templates " - "ORDER BY name) t", 0, NULL); + "SELECT t.id, t.name, t.series_code, t.created_by, t.created_at, t.retired_at, " + "(SELECT COUNT(*) FROM documents WHERE template_id = t.id) AS linked " + "FROM templates t WHERE $1::boolean OR t.retired_at IS NULL " + "ORDER BY t.name) t", 1, p); return; } docid = http_field_str(r, "doc_id"); @@ -1034,8 +1038,11 @@ h_series(struct kreq *r, PGconn *pg, const struct ident *id) char test[256]; if (r->method == KMETHOD_GET) { + const char *admin = ident_has_role(id, &cf, "admin") ? "t" : "f"; + const char *p[] = { admin }; send_sql_json(r, pg, "SELECT coalesce(json_agg(t), '[]')::text FROM (" - "SELECT code, pattern FROM series ORDER BY code) t", 0, NULL); + "SELECT code, pattern, retired_at FROM series WHERE $1::boolean OR retired_at IS NULL " + "ORDER BY code) t", 1, p); return; } p[0] = http_field_str(r, "code"); @@ -1159,6 +1166,68 @@ h_dam(struct kreq *r) yyjson_mut_doc_free(doc); } +/* Task 7: Lifecycle (retire, delete, unlink) */ +static void +h_template_retire(struct kreq *r, PGconn *pg, const struct ident *id, const char *tid) +{ + if (!ident_has_role(id, &cf, "admin")) { + send_err(r, KHTTP_403, "admin role required"); + return; + } + const char *p[] = { tid }; + db_do(pg, "UPDATE templates SET retired_at = NOW() WHERE id = $1::uuid", 1, p); + send_json(r, KHTTP_200, "{\"ok\":true}"); +} + +static void +h_template_delete(struct kreq *r, PGconn *pg, const struct ident *id, const char *tid) +{ + if (!ident_has_role(id, &cf, "admin")) { + send_err(r, KHTTP_403, "admin role required"); + return; + } + PGresult *res = db_q(pg, "SELECT COUNT(*) FROM documents WHERE template_id = $1::uuid", 1, (const char*[]){ tid }); + if (res != NULL && PQntuples(res) == 1) { + int linked = atoi(PQgetvalue(res, 0, 0)); + PQclear(res); + if (linked > 0) { + char msg[128]; + snprintf(msg, sizeof(msg), "%d document%s still linked", linked, linked == 1 ? "" : "s"); + send_err(r, KHTTP_409, msg); + return; + } + } + char tmpl_path[700]; + snprintf(tmpl_path, sizeof(tmpl_path), "%s/templates/%s.pdf", conf_get(&cf, "data_dir", "/var/lib/signet"), tid); + unlink(tmpl_path); + db_do(pg, "DELETE FROM templates WHERE id = $1::uuid", 1, (const char*[]){ tid }); + send_json(r, KHTTP_200, "{\"ok\":true}"); +} + +static void +h_doc_unlink(struct kreq *r, PGconn *pg, const struct ident *id, const char *docid) +{ + if (!ident_has_role(id, &cf, "admin")) { + send_err(r, KHTTP_403, "admin role required"); + return; + } + log_event(pg, docid, id->user, "UNLINKED", ""); + db_do(pg, "UPDATE documents SET template_id = NULL WHERE id = $1::uuid", 1, (const char*[]){ docid }); + send_json(r, KHTTP_200, "{\"ok\":true}"); +} + +static void +h_series_retire(struct kreq *r, PGconn *pg, const struct ident *id, const char *code) +{ + if (!ident_has_role(id, &cf, "admin")) { + send_err(r, KHTTP_403, "admin role required"); + return; + } + const char *p[] = { code }; + db_do(pg, "UPDATE series SET retired_at = NOW() WHERE code = $1", 1, p); + send_json(r, KHTTP_200, "{\"ok\":true}"); +} + static void h_me_facsimile(struct kreq *r, PGconn *pg, const struct ident *id) { @@ -1306,8 +1375,26 @@ dispatch(struct kreq *r) h_dam(r); else if (n == 1 && strcmp(seg[0], "templates") == 0) h_templates(r, pg, &id); + else if (n == 3 && strcmp(seg[0], "templates") == 0 && is_uuid(seg[1])) { + if (!get && strcmp(seg[2], "retire") == 0) + h_template_retire(r, pg, &id, seg[1]); + else if (!get && strcmp(seg[2], "delete") == 0) + h_template_delete(r, pg, &id, seg[1]); + else { + send_err(r, KHTTP_404, "not found"); + log_request(method, r->fullpath, id.user, 404, "not found"); + } + } else if (n == 1 && strcmp(seg[0], "series") == 0) h_series(r, pg, &id); + else if (n == 3 && strcmp(seg[0], "series") == 0 && strcmp(seg[2], "retire") == 0) { + if (!get) + h_series_retire(r, pg, &id, seg[1]); + else { + send_err(r, KHTTP_404, "not found"); + log_request(method, r->fullpath, id.user, 404, "not found"); + } + } else if (n == 1 && strcmp(seg[0], "docs") == 0) get ? h_docs_list(r, pg, &id) : h_doc_create(r, pg, &id); else if (n >= 2 && strcmp(seg[0], "docs") == 0 && is_uuid(seg[1])) { @@ -1325,6 +1412,8 @@ dispatch(struct kreq *r) h_doc_decide(r, pg, &id, seg[1]); else if (n == 3 && !get && strcmp(seg[2], "delete") == 0) h_doc_delete(r, pg, &id, seg[1]); + else if (n == 3 && !get && strcmp(seg[2], "unlink") == 0) + h_doc_unlink(r, pg, &id, seg[1]); else { send_err(r, KHTTP_404, "not found"); log_request(method, r->fullpath, id.user, 404, "not found");
: asapien/signet