@heyhru/business-dms-approval 0.6.5 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@ export declare function listApprovals(filters?: {
3
3
  submittedBy?: string;
4
4
  }): Promise<Record<string, unknown>[]>;
5
5
  export declare function getApprovalById(id: string): Promise<Record<string, unknown> | undefined>;
6
- export declare function insertApproval(dataSourceId: string, sqlText: string, submittedBy: string): Promise<Record<string, unknown> | undefined>;
6
+ export declare function insertApproval(dataSourceId: string, dbName: string | null, sqlText: string, submittedBy: string): Promise<Record<string, unknown> | undefined>;
7
7
  export declare function updateReview(id: string, status: string, reviewedBy: string, rejectReason: string | null): Promise<Record<string, unknown> | undefined>;
8
8
  export declare function setExecuting(id: string): Promise<{
9
9
  changes: number;
@@ -1 +1 @@
1
- {"version":3,"file":"approvals.model.d.ts","sourceRoot":"","sources":["../src/approvals.model.ts"],"names":[],"mappings":"AAGA,wBAAsB,aAAa,CAAC,OAAO,CAAC,EAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAA;CAAE,sCAatF;AAED,wBAAgB,eAAe,CAAC,EAAE,EAAE,MAAM,gDAEzC;AAED,wBAAgB,cAAc,CAAC,YAAY,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,gDAExF;AAED,wBAAgB,YAAY,CAC1B,EAAE,EAAE,MAAM,EACV,MAAM,EAAE,MAAM,EACd,UAAU,EAAE,MAAM,EAClB,YAAY,EAAE,MAAM,GAAG,IAAI,gDAG5B;AAED,wBAAgB,YAAY,CAAC,EAAE,EAAE,MAAM;;GAEtC;AAED,wBAAgB,gBAAgB,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;;GAE1E"}
1
+ {"version":3,"file":"approvals.model.d.ts","sourceRoot":"","sources":["../src/approvals.model.ts"],"names":[],"mappings":"AAGA,wBAAsB,aAAa,CAAC,OAAO,CAAC,EAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAA;CAAE,sCAgBtF;AAED,wBAAgB,eAAe,CAAC,EAAE,EAAE,MAAM,gDAEzC;AAED,wBAAgB,cAAc,CAAC,YAAY,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,gDAE/G;AAED,wBAAgB,YAAY,CAC1B,EAAE,EAAE,MAAM,EACV,MAAM,EAAE,MAAM,EACd,UAAU,EAAE,MAAM,EAClB,YAAY,EAAE,MAAM,GAAG,IAAI,gDAG5B;AAED,wBAAgB,YAAY,CAAC,EAAE,EAAE,MAAM;;GAEtC;AAED,wBAAgB,gBAAgB,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;;GAE1E"}
@@ -1,5 +1,5 @@
1
1
  export declare const FIND_BY_ID = "\nSELECT *\nFROM approvals\nWHERE id = ?";
2
- export declare const CREATE = "\nINSERT INTO approvals (data_source_id, sql_text, submitted_by)\nVALUES (?, ?, ?)\nRETURNING *";
2
+ export declare const CREATE = "\nINSERT INTO approvals (data_source_id, db_name, sql_text, submitted_by)\nVALUES (?, ?, ?, ?)\nRETURNING *";
3
3
  export declare const UPDATE_REVIEW: () => string;
4
4
  export declare const UPDATE_EXECUTING: () => string;
5
5
  export declare const UPDATE_RESULT: () => string;
@@ -1 +1 @@
1
- {"version":3,"file":"approvals.sql.d.ts","sourceRoot":"","sources":["../src/approvals.sql.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,UAAU,6CAGV,CAAC;AAEd,eAAO,MAAM,MAAM,oGAGP,CAAC;AAEb,eAAO,MAAM,aAAa,cAId,CAAC;AAEb,eAAO,MAAM,gBAAgB,cAGhB,CAAC;AAEd,eAAO,MAAM,aAAa,cAGb,CAAC"}
1
+ {"version":3,"file":"approvals.sql.d.ts","sourceRoot":"","sources":["../src/approvals.sql.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,UAAU,6CAGV,CAAC;AAEd,eAAO,MAAM,MAAM,gHAGP,CAAC;AAEb,eAAO,MAAM,aAAa,cAId,CAAC;AAEb,eAAO,MAAM,gBAAgB,cAGhB,CAAC;AAEd,eAAO,MAAM,aAAa,cAGb,CAAC"}
package/dist/index.js CHANGED
@@ -42,8 +42,8 @@ SELECT *
42
42
  FROM approvals
43
43
  WHERE id = ?`;
44
44
  var CREATE = `
45
- INSERT INTO approvals (data_source_id, sql_text, submitted_by)
46
- VALUES (?, ?, ?)
45
+ INSERT INTO approvals (data_source_id, db_name, sql_text, submitted_by)
46
+ VALUES (?, ?, ?, ?)
47
47
  RETURNING *`;
48
48
  var UPDATE_REVIEW = () => `
49
49
  UPDATE approvals
@@ -61,24 +61,27 @@ WHERE id = ?`;
61
61
 
62
62
  // src/approvals.model.ts
63
63
  async function listApprovals(filters) {
64
- let query = "SELECT * FROM approvals WHERE 1=1";
64
+ let query = `SELECT a.*, u.username AS submitter_name
65
+ FROM approvals a
66
+ LEFT JOIN users u ON u.id = a.submitted_by
67
+ WHERE 1=1`;
65
68
  const params = [];
66
69
  if (filters?.status) {
67
- query += " AND status = ?";
70
+ query += " AND a.status = ?";
68
71
  params.push(filters.status);
69
72
  }
70
73
  if (filters?.submittedBy) {
71
- query += " AND submitted_by = ?";
74
+ query += " AND a.submitted_by = ?";
72
75
  params.push(filters.submittedBy);
73
76
  }
74
- query += " ORDER BY created_at DESC";
77
+ query += " ORDER BY a.created_at DESC";
75
78
  return (0, import_server_plugin_pg.getPgDb)().query(query, params);
76
79
  }
77
80
  function getApprovalById(id) {
78
81
  return (0, import_server_plugin_pg.getPgDb)().queryOne(FIND_BY_ID, [id]);
79
82
  }
80
- function insertApproval(dataSourceId, sqlText, submittedBy) {
81
- return (0, import_server_plugin_pg.getPgDb)().queryOne(CREATE, [dataSourceId, sqlText, submittedBy]);
83
+ function insertApproval(dataSourceId, dbName, sqlText, submittedBy) {
84
+ return (0, import_server_plugin_pg.getPgDb)().queryOne(CREATE, [dataSourceId, dbName, sqlText, submittedBy]);
82
85
  }
83
86
  function updateReview(id, status, reviewedBy, rejectReason) {
84
87
  return (0, import_server_plugin_pg.getPgDb)().queryOne(UPDATE_REVIEW(), [status, reviewedBy, rejectReason, id]);
@@ -107,11 +110,11 @@ async function approvalGet(req, reply) {
107
110
  return reply.send(approval);
108
111
  }
109
112
  async function approvalCreate(req, reply) {
110
- const { dataSourceId, sql } = req.body ?? {};
113
+ const { dataSourceId, database, sql } = req.body ?? {};
111
114
  if (!dataSourceId || !sql) {
112
115
  return reply.code(400).send({ error: "Data source ID and SQL are required" });
113
116
  }
114
- const approval = await insertApproval(dataSourceId, sql, req.user.id);
117
+ const approval = await insertApproval(dataSourceId, database ?? null, sql, req.user.id);
115
118
  req.log.info("Approval submitted (user=%s)", req.user.id);
116
119
  return reply.code(201).send(approval);
117
120
  }
@@ -167,19 +170,21 @@ async function doExecuteApproval({ id, userId, ip, encryptionKey }, log) {
167
170
  }
168
171
  await setExecuting(id);
169
172
  try {
170
- const ds = await (0, import_business_dms_datasource.getDataSourceWithPassword)(
171
- approval["data_source_id"],
172
- encryptionKey
173
- );
174
- if (!ds) throw new Error("Data source not found");
175
- const pool = (0, import_business_dms_datasource.getPool)({ ...ds, database: ds.database ?? "" });
173
+ const dataSourceId = approval["data_source_id"];
174
+ const dbName = approval["db_name"];
175
+ const pool = dbName ? await (0, import_business_dms_datasource.getPoolForDatabase)(dataSourceId, dbName, encryptionKey) : await (async () => {
176
+ const ds = await (0, import_business_dms_datasource.getDataSourceWithPassword)(dataSourceId, encryptionKey);
177
+ if (!ds) throw new Error("Data source not found");
178
+ return (0, import_business_dms_datasource.getPool)({ ...ds, database: ds.database ?? "" });
179
+ })();
180
+ if (!pool) throw new Error("Data source not found");
176
181
  const rows = await pool.execute(approval["sql_text"]);
177
182
  const result = `${rows.length} rows affected`;
178
183
  await setExecuteResult(id, "executed", result);
179
184
  log.info("Approval executed (id=%s, user=%s)", id, userId);
180
185
  await (0, import_business_dms_audit.writeAuditLog)({
181
186
  userId,
182
- dataSourceId: approval["data_source_id"],
187
+ dataSourceId,
183
188
  action: "DML_EXECUTE",
184
189
  sqlText: approval["sql_text"],
185
190
  resultSummary: result,
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/approvals.service.ts
2
- import { getDataSourceWithPassword, getPool } from "@heyhru/business-dms-datasource";
2
+ import { getDataSourceWithPassword, getPool, getPoolForDatabase } from "@heyhru/business-dms-datasource";
3
3
  import { writeAuditLog } from "@heyhru/business-dms-audit";
4
4
 
5
5
  // src/approvals.model.ts
@@ -11,8 +11,8 @@ SELECT *
11
11
  FROM approvals
12
12
  WHERE id = ?`;
13
13
  var CREATE = `
14
- INSERT INTO approvals (data_source_id, sql_text, submitted_by)
15
- VALUES (?, ?, ?)
14
+ INSERT INTO approvals (data_source_id, db_name, sql_text, submitted_by)
15
+ VALUES (?, ?, ?, ?)
16
16
  RETURNING *`;
17
17
  var UPDATE_REVIEW = () => `
18
18
  UPDATE approvals
@@ -30,24 +30,27 @@ WHERE id = ?`;
30
30
 
31
31
  // src/approvals.model.ts
32
32
  async function listApprovals(filters) {
33
- let query = "SELECT * FROM approvals WHERE 1=1";
33
+ let query = `SELECT a.*, u.username AS submitter_name
34
+ FROM approvals a
35
+ LEFT JOIN users u ON u.id = a.submitted_by
36
+ WHERE 1=1`;
34
37
  const params = [];
35
38
  if (filters?.status) {
36
- query += " AND status = ?";
39
+ query += " AND a.status = ?";
37
40
  params.push(filters.status);
38
41
  }
39
42
  if (filters?.submittedBy) {
40
- query += " AND submitted_by = ?";
43
+ query += " AND a.submitted_by = ?";
41
44
  params.push(filters.submittedBy);
42
45
  }
43
- query += " ORDER BY created_at DESC";
46
+ query += " ORDER BY a.created_at DESC";
44
47
  return getPgDb().query(query, params);
45
48
  }
46
49
  function getApprovalById(id) {
47
50
  return getPgDb().queryOne(FIND_BY_ID, [id]);
48
51
  }
49
- function insertApproval(dataSourceId, sqlText, submittedBy) {
50
- return getPgDb().queryOne(CREATE, [dataSourceId, sqlText, submittedBy]);
52
+ function insertApproval(dataSourceId, dbName, sqlText, submittedBy) {
53
+ return getPgDb().queryOne(CREATE, [dataSourceId, dbName, sqlText, submittedBy]);
51
54
  }
52
55
  function updateReview(id, status, reviewedBy, rejectReason) {
53
56
  return getPgDb().queryOne(UPDATE_REVIEW(), [status, reviewedBy, rejectReason, id]);
@@ -76,11 +79,11 @@ async function approvalGet(req, reply) {
76
79
  return reply.send(approval);
77
80
  }
78
81
  async function approvalCreate(req, reply) {
79
- const { dataSourceId, sql } = req.body ?? {};
82
+ const { dataSourceId, database, sql } = req.body ?? {};
80
83
  if (!dataSourceId || !sql) {
81
84
  return reply.code(400).send({ error: "Data source ID and SQL are required" });
82
85
  }
83
- const approval = await insertApproval(dataSourceId, sql, req.user.id);
86
+ const approval = await insertApproval(dataSourceId, database ?? null, sql, req.user.id);
84
87
  req.log.info("Approval submitted (user=%s)", req.user.id);
85
88
  return reply.code(201).send(approval);
86
89
  }
@@ -136,19 +139,21 @@ async function doExecuteApproval({ id, userId, ip, encryptionKey }, log) {
136
139
  }
137
140
  await setExecuting(id);
138
141
  try {
139
- const ds = await getDataSourceWithPassword(
140
- approval["data_source_id"],
141
- encryptionKey
142
- );
143
- if (!ds) throw new Error("Data source not found");
144
- const pool = getPool({ ...ds, database: ds.database ?? "" });
142
+ const dataSourceId = approval["data_source_id"];
143
+ const dbName = approval["db_name"];
144
+ const pool = dbName ? await getPoolForDatabase(dataSourceId, dbName, encryptionKey) : await (async () => {
145
+ const ds = await getDataSourceWithPassword(dataSourceId, encryptionKey);
146
+ if (!ds) throw new Error("Data source not found");
147
+ return getPool({ ...ds, database: ds.database ?? "" });
148
+ })();
149
+ if (!pool) throw new Error("Data source not found");
145
150
  const rows = await pool.execute(approval["sql_text"]);
146
151
  const result = `${rows.length} rows affected`;
147
152
  await setExecuteResult(id, "executed", result);
148
153
  log.info("Approval executed (id=%s, user=%s)", id, userId);
149
154
  await writeAuditLog({
150
155
  userId,
151
- dataSourceId: approval["data_source_id"],
156
+ dataSourceId,
152
157
  action: "DML_EXECUTE",
153
158
  sqlText: approval["sql_text"],
154
159
  resultSummary: result,
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.6.5",
6
+ "version": "0.7.1",
7
7
  "description": "DMS approval workflow domain logic: service, model, sql",
8
8
  "main": "./dist/index.js",
9
9
  "module": "./dist/index.mjs",
@@ -36,5 +36,5 @@
36
36
  "typescript": "^6.0.2",
37
37
  "vitest": "^4.1.4"
38
38
  },
39
- "gitHead": "8e00760abe8e7497a79f25f559dd353a9746bcaa"
39
+ "gitHead": "6bd2092d7f1dd2bc23d5252e2be4136792ddec4f"
40
40
  }