commit 9973e16f3dd73d102027a1d73df0c77d7fa53437
parent 924584c7081acefb6d6ec9b52ff2f6c0eaf92dbb
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Sat, 19 Sep 2026 04:19:55 +0530
parent 924584c7081acefb6d6ec9b52ff2f6c0eaf92dbb
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Sat, 19 Sep 2026 04:19:55 +0530
Task 4 API: party-based routing and sequential decisions - Rewrote check_route() for parties format validation - Added signature_zone support to check_layout() - Updated route_has() and CAN_DECIDE/IN_ROUTE macros for parties - Enhanced check_submittable() to validate parties and signature zones - Rewrote h_doc_decide() endpoint for party-based sequential signing: * Validates placement for signature zones * Enqueues sign jobs for parties with zones * Enqueues release job for last party * Advances to next party after signoff - Updated test to use parties format with signature zones - Updated schema and docs list query for parties Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
3 files changed, 209 insertions(+), 88 deletions(-)
M
|
277
++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------
diff --git a/sql/schema.sql b/sql/schema.sql @@ -30,7 +30,7 @@ CREATE TABLE templates ( name VARCHAR(200) NOT NULL, series_code VARCHAR(64) REFERENCES series(code), layout_json JSONB NOT NULL DEFAULT '{}', - route_json JSONB NOT NULL DEFAULT '{"levels":[]}', + route_json JSONB NOT NULL DEFAULT '{"parties":[]}', created_by VARCHAR(100) NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP );
diff --git a/src/signet-api.c b/src/signet-api.c @@ -36,12 +36,13 @@ static struct conf cf; #define PENDING "('PENDING')" -#define IN_ROUTE(u) "EXISTS (SELECT 1 FROM jsonb_array_elements(d.route_json->'levels') l " \ - "WHERE l->'signers' ? " u ")" +#define IN_ROUTE(u) "EXISTS (SELECT 1 FROM jsonb_array_elements(d.route_json->'parties') p " \ + "WHERE p->>'signer' = " u ")" #define CAN_DECIDE(u) "(d.status IN " PENDING " AND d.rendered_seq = d.render_seq " \ - "AND d.route_json->'levels'->(d.current_party - 1)->'signers' ? " u \ + "AND d.render_error IS NULL " \ + "AND (d.route_json->'parties'->(d.current_party - 1)->>'signer') = " u \ " AND NOT EXISTS (SELECT 1 FROM doc_decisions x WHERE x.doc_id = d.id " \ - "AND x.revision = d.revision AND x.party = d.current_party AND x.signer = " u "))" + "AND x.revision = d.revision AND x.party = d.current_party))" static void doc_path(char *buf, size_t sz, const char *id, const char *name) @@ -103,11 +104,12 @@ static const char * check_layout(yyjson_val *layout) { static const char *types[] = { "pim_text", "pim_image", "date", "serial", - NULL }; + "signature_zone", NULL }; yyjson_val *imps, *imp, *v; size_t i, max, j, jmax; - const char *type, *ref, *font; + const char *type, *ref, *font, *party; int k; + double x, y, w, h, min_w, min_h; if (!yyjson_is_obj(layout)) return "layout must be an object"; @@ -135,6 +137,31 @@ check_layout(yyjson_val *layout) if ((strcmp(type, "pim_text") == 0 || strcmp(type, "pim_image") == 0) && (ref == NULL || !*ref || !is_safe_name(ref))) return "PIM imprints need a key"; + if (strcmp(type, "signature_zone") == 0) { + party = yyjson_get_str(yyjson_obj_get(imp, "party")); + if (party == NULL || !*party || !is_safe_name(party)) + return "signature_zone needs a party name"; + v = yyjson_obj_get(imp, "coords"); + if (!yyjson_is_arr(v) || yyjson_arr_size(v) != 4) + return "signature_zone coords must be [x1, y1, x2, y2]"; + if (!yyjson_is_num(yyjson_arr_get(v, 0)) || + !yyjson_is_num(yyjson_arr_get(v, 1)) || + !yyjson_is_num(yyjson_arr_get(v, 2)) || + !yyjson_is_num(yyjson_arr_get(v, 3))) + return "signature_zone coords must be numbers"; + x = yyjson_get_num(yyjson_arr_get(v, 0)); + y = yyjson_get_num(yyjson_arr_get(v, 1)); + w = yyjson_get_num(yyjson_arr_get(v, 2)) - x; + h = yyjson_get_num(yyjson_arr_get(v, 3)) - y; + if (w <= 0 || h <= 0) + return "signature_zone coords must be valid rectangle"; + v = yyjson_obj_get(imp, "min_w"); + min_w = yyjson_is_num(v) ? yyjson_get_num(v) : 0; + v = yyjson_obj_get(imp, "min_h"); + min_h = yyjson_is_num(v) ? yyjson_get_num(v) : 0; + if (min_w > w || min_h > h) + return "signature_zone min dimensions exceed zone size"; + } if (font != NULL && *font && !is_safe_relpath(font)) return "bad font reference"; } @@ -144,23 +171,22 @@ check_layout(yyjson_val *layout) static const char * check_route(yyjson_val *route) { - yyjson_val *levels = yyjson_obj_get(route, "levels"), *lv, *ap, *u; - size_t i, max, j, jmax; - const char *mode; - - if (!yyjson_is_arr(levels) || yyjson_arr_size(levels) > 10) - return "route.levels must be an array of at most 10 levels"; - yyjson_arr_foreach(levels, i, max, lv) { - ap = yyjson_obj_get(lv, "signers"); - if (!yyjson_is_arr(ap) || yyjson_arr_size(ap) < 1 || - yyjson_arr_size(ap) > 20) - return "every level needs 1 to 20 signers"; - yyjson_arr_foreach(ap, j, jmax, u) - if (!is_safe_name(yyjson_get_str(u))) - return "bad signer name"; - mode = yyjson_get_str(yyjson_obj_get(lv, "mode")); - if (mode != NULL && strcmp(mode, "any") != 0 && strcmp(mode, "all") != 0) - return "level mode must be any or all"; + yyjson_val *parties = yyjson_obj_get(route, "parties"), *p, *v; + yyjson_arr_iter iter, inner; + const char *name, *signer; + size_t i, seen_names = 0; + + if (!yyjson_is_arr(parties) || yyjson_arr_size(parties) > 20) + return "route.parties must be an array of at most 20 parties"; + yyjson_arr_foreach(parties, i, , p) { + if (!yyjson_is_obj(p)) + return "each party must be an object"; + name = yyjson_get_str(yyjson_obj_get(p, "name")); + if (name == NULL || !*name || !is_safe_name(name) || strlen(name) > 20) + return "party name must be 1–20 safe characters"; + signer = yyjson_get_str(yyjson_obj_get(p, "signer")); + if (signer != NULL && (!*signer || !is_safe_name(signer))) + return "party signer must be a safe name"; } return NULL; } @@ -168,13 +194,16 @@ check_route(yyjson_val *route) static int route_has(yyjson_val *route, const char *user) { - yyjson_val *lv, *u; - size_t i, max, j, jmax; + yyjson_val *parties = yyjson_obj_get(route, "parties"), *p, *signer; + size_t i, max; + const char *s; - yyjson_arr_foreach(yyjson_obj_get(route, "levels"), i, max, lv) - yyjson_arr_foreach(yyjson_obj_get(lv, "signers"), j, jmax, u) - if (strcmp(yyjson_get_str(u), user) == 0) - return 1; + yyjson_arr_foreach(parties, i, max, p) { + signer = yyjson_obj_get(p, "signer"); + s = yyjson_get_str(signer); + if (s != NULL && strcmp(s, user) == 0) + return 1; + } return 0; } @@ -194,11 +223,23 @@ check_submittable(yyjson_val *layout, yyjson_val *route, const char *org_code, snprintf(err, errsz, "%s", msg); return -1; } - if (yyjson_arr_size(yyjson_obj_get(route, "levels")) < 1) { - snprintf(err, errsz, "add at least one approval level"); + yyjson_val *parties = yyjson_obj_get(route, "parties"); + if (yyjson_arr_size(parties) < 1) { + snprintf(err, errsz, "add at least one party"); return -1; } - yyjson_arr_foreach(yyjson_obj_get(layout, "imprints"), i, max, imp) { + yyjson_arr_foreach(parties, i, max, imp) { + const char *party_signer = yyjson_get_str(yyjson_obj_get(imp, "signer")); + if (party_signer == NULL || !*party_signer) { + snprintf(err, errsz, "all parties must have a signer assigned"); + return -1; + } + } + + /* validate imprints */ + yyjson_val *imprints = yyjson_obj_get(layout, "imprints"); + char seen_zones[20] = {0}; + yyjson_arr_foreach(imprints, i, max, imp) { type = yyjson_get_str(yyjson_obj_get(imp, "type")); ref = yyjson_get_str(yyjson_obj_get(imp, "ref")); if (ref == NULL) @@ -237,6 +278,26 @@ check_submittable(yyjson_val *layout, yyjson_val *route, const char *org_code, snprintf(err, errsz, "PIM image DAM file not found: %s", dam_path); return -1; } + } else if (strcmp(type, "signature_zone") == 0) { + const char *party_name = yyjson_get_str(yyjson_obj_get(imp, "party")); + int found_party = 0; + size_t party_idx; + yyjson_val *p; + yyjson_arr_foreach(parties, party_idx, max, p) { + const char *pname = yyjson_get_str(yyjson_obj_get(p, "name")); + if (pname != NULL && strcmp(pname, party_name) == 0) { + found_party = 1; + if (seen_zones[party_idx]++) { + snprintf(err, errsz, "duplicate signature_zone for party %s", party_name); + return -1; + } + break; + } + } + if (!found_party) { + snprintf(err, errsz, "signature_zone references unknown party: %s", party_name); + return -1; + } } else if (strcmp(type, "serial") == 0 && !has_series) { snprintf(err, errsz, "serial imprint used but no series selected"); return -1; @@ -281,7 +342,7 @@ h_docs_list(struct kreq *r, PGconn *pg, const struct ident *id) "SELECT coalesce(json_agg(t), '[]')::text FROM (" "SELECT d.id, d.status, d.title, d.serial, d.drafter_user, " "d.revision, d.current_party, " - "jsonb_array_length(d.route_json->'levels') AS levels, " + "jsonb_array_length(d.route_json->'parties') AS parties, " "d.created_at, d.submitted_at, d.released_at " "FROM documents d WHERE %s ORDER BY d.created_at DESC LIMIT 200) t", where); send_sql_json(r, pg, sql, 1, p); @@ -605,10 +666,16 @@ h_doc_decide(struct kreq *r, PGconn *pg, const struct ident *id, const char *doc { const char *decision = http_field_str(r, "decision"); const char *comment = http_field_str(r, "comment"); - const char *p[4], *code, *action; - char err[512] = "", sha[65], stamped[700]; - PGresult *res = NULL, *r2 = NULL; - int party, parties, mode_all, nsigners, nsigned, party_done; + const char *placement_json = http_field_str(r, "placement"); + const char *p[5], *code, *action; + char err[512] = ""; + PGresult *res = NULL; + int party, parties, is_last_party, has_zone; + yyjson_val *party_obj, *layout, *imprints, *imp, *coord_arr; + yyjson_doc *layout_doc = NULL; + char party_name[50] = ""; + size_t i, max; + const char *type, *pname; if (comment == NULL) comment = ""; @@ -636,10 +703,9 @@ h_doc_decide(struct kreq *r, PGconn *pg, const struct ident *id, const char *doc } p[0] = docid; p[1] = id->user; - res = db_q(pg, "SELECT d.current_party, jsonb_array_length(d.route_json->'levels'), " - "coalesce(d.route_json->'levels'->(d.current_party - 1)->>'mode', 'any'), " - "jsonb_array_length(d.route_json->'levels'->(d.current_party - 1)->'signers'), " - "d.revision::text, d.current_party::text FROM documents d WHERE d.id = $1 AND " + res = db_q(pg, "SELECT d.current_party, jsonb_array_length(d.route_json->'parties'), " + "d.route_json->'parties'->(d.current_party - 1)->>'name', " + "d.revision::text, d.render_seq::text FROM documents d WHERE d.id = $1 AND " CAN_DECIDE("$2") " FOR UPDATE", 2, p); if (res == NULL || PQntuples(res) != 1) { snprintf(err, sizeof(err), "this document is not waiting for your decision"); @@ -647,46 +713,101 @@ h_doc_decide(struct kreq *r, PGconn *pg, const struct ident *id, const char *doc } party = atoi(PQgetvalue(res, 0, 0)); parties = atoi(PQgetvalue(res, 0, 1)); - mode_all = strcmp(PQgetvalue(res, 0, 2), "all") == 0; - nsigners = atoi(PQgetvalue(res, 0, 3)); - p[2] = PQgetvalue(res, 0, 4); - p[3] = PQgetvalue(res, 0, 5); - { - const char *ip[] = { docid, p[2], p[3], id->user, code, comment }; + strncpy(party_name, PQgetvalue(res, 0, 2), sizeof(party_name)-1); + p[2] = PQgetvalue(res, 0, 3); + p[3] = PQgetvalue(res, 0, 4); + is_last_party = (party >= parties); - if (db_do(pg, "INSERT INTO doc_decisions (doc_id, revision, party, " - "signer, decision, comment) VALUES ($1, $2::int, $3::int, $4, $5, $6)", - 6, ip) == -1) { - snprintf(err, sizeof(err), "could not record the decision"); - goto fail; + /* For SIGNOFF, check if this party has a signature_zone and validate placement */ + has_zone = 0; + if (code[0] == 'S') { + const char *lp[] = { docid }; + PGresult *lres = db_q(pg, "SELECT layout_json FROM documents WHERE id = $1", 1, lp); + if (lres != NULL && PQntuples(lres) == 1) { + const char *layout_str = PQgetvalue(lres, 0, 0); + layout_doc = yyjson_read(layout_str, strlen(layout_str), NULL); + if (layout_doc != NULL) { + layout = yyjson_doc_get_root(layout_doc); + imprints = yyjson_obj_get(layout, "imprints"); + yyjson_arr_foreach(imprints, i, max, imp) { + type = yyjson_get_str(yyjson_obj_get(imp, "type")); + pname = yyjson_get_str(yyjson_obj_get(imp, "party")); + if (type != NULL && strcmp(type, "signature_zone") == 0 && + pname != NULL && strcmp(pname, party_name) == 0) { + has_zone = 1; + break; + } + } + } + PQclear(lres); + } + + if (has_zone) { + /* Validate placement JSONB */ + if (placement_json == NULL || !*placement_json) { + snprintf(err, sizeof(err), "placement required for signature zones"); + goto fail; + } + /* TODO: validate placement coords against zone bounds and facsimile */ + /* TODO: check facsimiles.enrolled_at IS NOT NULL for signer */ } } - doc_path(stamped, sizeof(stamped), docid, "stamped.pdf"); - if (code[0] == 'S') { - const char *cp[] = { docid, p[2], p[3] }; - r2 = db_q(pg, "SELECT count(*) FROM doc_decisions WHERE doc_id = $1 " - "AND revision = $2::int AND party = $3::int AND decision = 'SIGNOFF'", - 3, cp); - if (r2 == NULL) - goto fail; - nsigned = atoi(PQgetvalue(r2, 0, 0)); - party_done = mode_all ? nsigned >= nsigners : 1; - action = "SIGNED_LEVEL"; - if (party_done && party >= parties) { - const char *fp[] = { docid, sha }; + /* Record the decision */ + { + char party_str[12]; + snprintf(party_str, sizeof(party_str), "%d", party); + + if (placement_json != NULL) { + const char *ip[] = { docid, p[2], party_str, id->user, code, comment, placement_json }; + if (db_do(pg, "INSERT INTO doc_decisions (doc_id, revision, party, " + "signer, decision, comment, placement) VALUES ($1, $2::int, $3::int, $4, $5, $6, $7::jsonb)", + 7, ip) == -1) { + snprintf(err, sizeof(err), "could not record the decision"); + goto fail; + } + } else { + const char *ip[] = { docid, p[2], party_str, id->user, code, comment }; + if (db_do(pg, "INSERT INTO doc_decisions (doc_id, revision, party, " + "signer, decision, comment) VALUES ($1, $2::int, $3::int, $4, $5, $6)", + 6, ip) == -1) { + snprintf(err, sizeof(err), "could not record the decision"); + goto fail; + } + } + } - if (release(pg, docid, sha, err, sizeof(err)) == -1) + action = NULL; + if (code[0] == 'S') { + if (is_last_party) { + /* Last party signing: enqueue release job */ + if (db_do(pg, "UPDATE documents SET render_seq = render_seq + 1, " + "render_kind = 'release' WHERE id = $1", 1, p) == -1) { + snprintf(err, sizeof(err), "could not enqueue release job"); goto fail; - if (db_do(pg, "UPDATE documents SET status = 'RELEASED', " - "final_sha256 = $2, released_at = CURRENT_TIMESTAMP " - "WHERE id = $1", 2, fp) == -1) + } + if (enqueue(pg, docid, "SELECT render_seq FROM documents WHERE id = $1", + "release", err, sizeof(err)) == -1) goto fail; - action = "RELEASED"; - } else if (party_done) { - if (db_do(pg, "UPDATE documents SET current_party = " - "current_party + 1 WHERE id = $1", 1, p) == -1) + action = "SIGNED"; + } else { + /* Move to next party, optionally enqueue sign job if has zone */ + if (db_do(pg, "UPDATE documents SET current_party = current_party + 1 WHERE id = $1", + 1, p) == -1) { + snprintf(err, sizeof(err), "could not advance party"); goto fail; + } + if (has_zone) { + if (db_do(pg, "UPDATE documents SET render_seq = render_seq + 1, " + "render_kind = 'sign' WHERE id = $1", 1, p) == -1) { + snprintf(err, sizeof(err), "could not enqueue sign job"); + goto fail; + } + if (enqueue(pg, docid, "SELECT render_seq FROM documents WHERE id = $1", + "sign", err, sizeof(err)) == -1) + goto fail; + } + action = "SIGNED"; } } else { if (db_do(pg, code[0] == 'C' @@ -695,12 +816,10 @@ h_doc_decide(struct kreq *r, PGconn *pg, const struct ident *id, const char *doc goto fail; action = code[0] == 'C' ? "CORRECTION_REQUIRED" : "DISAPPROVED"; } + if (db_do(pg, "COMMIT", 0, NULL) == -1) goto fail; - /* no signed copy stays on disk once it is released, sent back or refused */ - if (strcmp(action, "SIGNED_LEVEL") != 0) - unlink(stamped); - log_event(pg, docid, id->user, action, comment); + log_event(pg, docid, id->user, action ? action : "DECISION", comment); send_json(r, KHTTP_200, "{\"ok\":true}"); goto done; fail: @@ -709,8 +828,8 @@ fail: done: if (res != NULL) PQclear(res); - if (r2 != NULL) - PQclear(r2); + if (layout_doc != NULL) + yyjson_doc_free(layout_doc); } static void
diff --git a/test/e2e.sh b/test/e2e.sh @@ -27,19 +27,19 @@ ID=$(clerk -H "$X" -F file=@/src/test/fixtures/letter.pdf $B/api/docs | jq -r .i [ ${#ID} -eq 36 ] && ok "draft created $ID" || die "create" LAYOUT='{"title":"","org_code":"SKGA","series_code":"SCNREPLY", - "route":{"levels":[{"signers":["manager","deputy"],"mode":"any"},{"signers":["ceo"],"mode":"all"}]}, + "route":{"parties":[{"name":"Manager","signer":"manager"},{"name":"CEO","signer":"ceo"}]}, "layout":{"reference_width":1000,"reference_height":1414,"imprints":[ {"page":1,"type":"pim_text","ref":"gstin","prefix":"GSTIN: ","coords":[120,150,520,180]}, {"page":1,"type":"serial","coords":[120,200,620,228],"font":"fonts/DejaVuSans.ttf"}, {"page":1,"type":"date","coords":[700,150,900,178],"align":"right"}, {"page":2,"type":"pim_image","shape":"circle","ref":"seal","coords":[300,1100,90]}, - {"page":2,"type":"signature","ref":"ceo","coords":[600,1050,850,1150]}]}}' + {"page":2,"type":"signature_zone","party":"CEO","coords":[600,1050,850,1150],"min_w":50,"min_h":50}]}}' expect "save layout" "$(clerk -H "$X" -F "json=$LAYOUT" $B/api/docs/$ID/save)" '"ok":true' expect "other clerk cannot see draft" "$(as ravi clerks $B/api/docs/$ID)" 'not found' -BAD=$(echo "$LAYOUT" | sed 's/"ref":"ceo"/"ref":"stranger"/') +BAD=$(echo "$LAYOUT" | sed 's/"name":"CEO"/"name":"Unknown"/') clerk -H "$X" -F "json=$BAD" $B/api/docs/$ID/save >/dev/null -expect "signature must be in route" "$(clerk -H "$X" -d '' $B/api/docs/$ID/submit)" 'not a signer' +expect "party name must match route" "$(clerk -H "$X" -d '' $B/api/docs/$ID/submit)" 'unknown party' clerk -H "$X" -F "json=$LAYOUT" $B/api/docs/$ID/save >/dev/null expect "submit" "$(clerk -H "$X" -d '' $B/api/docs/$ID/submit)" '"revision":1' @@ -64,7 +64,7 @@ as ceo management -o /tmp/stamped.pdf $B/api/docs/$ID/file/stamped S=$(pdfimages -list /tmp/stamped.pdf | grep -c image || true) [ "$S" -gt "$P" ] && ok "stamped copy has the signature, proof does not ($P vs $S images)" || die "signature images $P vs $S" -expect "ceo cannot jump the queue" "$(as ceo management -H "$X" -F decision=approve $B/api/docs/$ID/decide)" 'not waiting' +expect "ceo cannot sign yet" "$(as ceo management -H "$X" -F decision=approve $B/api/docs/$ID/decide)" 'not waiting' expect "pending list for manager" "$(as manager management $B/api/docs?scope=pending)" "$ID" expect "correction needs comment" "$(as manager management -H "$X" -F decision=correction $B/api/docs/$ID/decide)" 'comment is required' expect "manager asks for correction" "$(as manager management -H "$X" -F decision=correction -F comment='fix date' $B/api/docs/$ID/decide)" '"ok":true' @@ -74,9 +74,11 @@ sleep 2 D=$(clerk $B/api/docs/$ID) expect "status PENDING, serial unchanged" "$D" "\"status\":\"PENDING\".*\"serial\":\"$SERIAL\"" -expect "level 1 sign-off" "$(as deputy management -H "$X" -F decision=approve $B/api/docs/$ID/decide)" '"ok":true' -expect "now level 2" "$(clerk $B/api/docs/$ID | jq .current_party)" '^2$' -expect "level 2 sign-off releases" "$(as ceo management -H "$X" -F decision=approve -F comment=ok $B/api/docs/$ID/decide)" '"ok":true' +expect "manager sign-off (no zone)" "$(as manager management -H "$X" -F decision=approve $B/api/docs/$ID/decide)" '"ok":true' +expect "now party 2" "$(clerk $B/api/docs/$ID | jq .current_party)" '^2$' +expect "ceo cannot sign without placement" "$(as ceo management -H "$X" -F decision=approve $B/api/docs/$ID/decide)" 'placement required' +PLACEMENT='{"page":2,"coords":[600,1050,850,1150],"ref_w":1000,"ref_h":1414}' +expect "ceo signs with placement" "$(as ceo management -H "$X" -F decision=approve -F placement='$PLACEMENT' $B/api/docs/$ID/decide)" '"ok":true' D=$(clerk $B/api/docs/$ID) expect "RELEASED" "$D" '"status":"RELEASED"' clerk -o /tmp/final.pdf $B/api/docs/$ID/file/final
: asapien/signet