hierarchical-approval 2.2.0 → 2.3.0
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.
- package/CHANGELOG.md +36 -0
- package/README.md +25 -0
- package/dist/{ApprovalEngine-CbFBAMlO.d.cts → ApprovalEngine-C1dUPnLM.d.cts} +45 -2
- package/dist/{ApprovalEngine-B3ChZxS7.d.ts → ApprovalEngine-Ci9urTDI.d.ts} +45 -2
- package/dist/{IStorageAdapter-DjRvHUF0.d.ts → IStorageAdapter-BU3sau5W.d.ts} +14 -0
- package/dist/{IStorageAdapter-Bk7ybd3z.d.cts → IStorageAdapter-DdHO4Rf1.d.cts} +14 -0
- package/dist/adapters/MemoryAdapter.cjs +4 -0
- package/dist/adapters/MemoryAdapter.cjs.map +1 -1
- package/dist/adapters/MemoryAdapter.d.cts +2 -1
- package/dist/adapters/MemoryAdapter.d.ts +2 -1
- package/dist/adapters/MemoryAdapter.js +4 -0
- package/dist/adapters/MemoryAdapter.js.map +1 -1
- package/dist/adapters/PostgresAdapter.cjs +12 -0
- package/dist/adapters/PostgresAdapter.cjs.map +1 -1
- package/dist/adapters/PostgresAdapter.d.cts +2 -1
- package/dist/adapters/PostgresAdapter.d.ts +2 -1
- package/dist/adapters/PostgresAdapter.js +12 -0
- package/dist/adapters/PostgresAdapter.js.map +1 -1
- package/dist/index.cjs +81 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +81 -0
- package/dist/index.js.map +1 -1
- package/dist/nestjs.cjs +77 -0
- package/dist/nestjs.cjs.map +1 -1
- package/dist/nestjs.d.cts +2 -2
- package/dist/nestjs.d.ts +2 -2
- package/dist/nestjs.js +77 -0
- package/dist/nestjs.js.map +1 -1
- package/dist/testing.cjs +81 -0
- package/dist/testing.cjs.map +1 -1
- package/dist/testing.d.cts +2 -2
- package/dist/testing.d.ts +2 -2
- package/dist/testing.js +81 -0
- package/dist/testing.js.map +1 -1
- package/package.json +1 -1
package/dist/nestjs.cjs
CHANGED
|
@@ -916,6 +916,7 @@ function computeTimingStats(samples) {
|
|
|
916
916
|
|
|
917
917
|
// src/engine/ApprovalEngine.ts
|
|
918
918
|
var MAX_SUBWORKFLOW_DEPTH = 5;
|
|
919
|
+
var DEFAULT_PURGE_LIMIT = 1e3;
|
|
919
920
|
var DEFAULT_MAX_REMINDERS = 3;
|
|
920
921
|
var DEFAULT_MAX_ATTEMPTS = 3;
|
|
921
922
|
var DEFAULT_BASE_DELAY_MS = 50;
|
|
@@ -3121,6 +3122,82 @@ var ApprovalEngine = class _ApprovalEngine {
|
|
|
3121
3122
|
});
|
|
3122
3123
|
return result;
|
|
3123
3124
|
}
|
|
3125
|
+
/**
|
|
3126
|
+
* Permanently remove finished approvals older than a cut-off.
|
|
3127
|
+
*
|
|
3128
|
+
* Approval tables only grow, and data-minimisation rules eventually require
|
|
3129
|
+
* old records to go. There was no way to remove one, so operators reached
|
|
3130
|
+
* around the library and deleted rows directly — which is exactly where
|
|
3131
|
+
* orphaned audit rows and half-deleted instances come from.
|
|
3132
|
+
*
|
|
3133
|
+
* **Only terminal instances are eligible.** A pending approval is live work;
|
|
3134
|
+
* deleting one would strand a document with no way to finish and no record of
|
|
3135
|
+
* why. Passing a non-terminal status is rejected rather than quietly ignored,
|
|
3136
|
+
* because a caller who asked to purge pending work has misunderstood
|
|
3137
|
+
* something and should hear about it.
|
|
3138
|
+
*
|
|
3139
|
+
* This is irreversible and removes the audit trail with the instance. In many
|
|
3140
|
+
* deployments that trail *is* the compliance record, which is why the
|
|
3141
|
+
* underlying `deleteInstance` is an optional adapter method: an adapter that
|
|
3142
|
+
* does not implement it makes the whole operation unavailable, and this
|
|
3143
|
+
* throws rather than reporting a successful purge of nothing.
|
|
3144
|
+
*
|
|
3145
|
+
* @param opts - Cut-off, optional status/type scoping, safety limit, dry run.
|
|
3146
|
+
* @returns What was removed, or would be under `dryRun`.
|
|
3147
|
+
*/
|
|
3148
|
+
async purgeInstances(opts) {
|
|
3149
|
+
const dryRun = opts.dryRun ?? false;
|
|
3150
|
+
const limit = opts.limit ?? DEFAULT_PURGE_LIMIT;
|
|
3151
|
+
if (!(opts.olderThan instanceof Date) || Number.isNaN(opts.olderThan.getTime())) {
|
|
3152
|
+
throw new ApprovalValidationError("purgeInstances requires a valid olderThan date.");
|
|
3153
|
+
}
|
|
3154
|
+
const requested = opts.statuses ?? [...TERMINAL_STATUSES];
|
|
3155
|
+
const nonTerminal = requested.filter((st) => !TERMINAL_STATUSES.has(st));
|
|
3156
|
+
if (nonTerminal.length > 0) {
|
|
3157
|
+
throw new ApprovalValidationError(
|
|
3158
|
+
`purgeInstances refuses non-terminal statuses (${nonTerminal.join(", ")}): a pending approval is live work, and removing it would strand the document it belongs to.`
|
|
3159
|
+
);
|
|
3160
|
+
}
|
|
3161
|
+
const deleteInstance = this.opts.adapter.deleteInstance?.bind(this.opts.adapter);
|
|
3162
|
+
if (!deleteInstance && !dryRun) {
|
|
3163
|
+
throw new ApprovalValidationError(
|
|
3164
|
+
"The configured storage adapter does not implement deleteInstance(), so instances cannot be purged."
|
|
3165
|
+
);
|
|
3166
|
+
}
|
|
3167
|
+
const result = { purged: [], scanned: 0, dryRun };
|
|
3168
|
+
for (const status of requested) {
|
|
3169
|
+
if (result.purged.length >= limit) break;
|
|
3170
|
+
const page = await this.opts.adapter.getInstancesByFilter(
|
|
3171
|
+
this.tenantId,
|
|
3172
|
+
{
|
|
3173
|
+
status,
|
|
3174
|
+
toDate: opts.olderThan,
|
|
3175
|
+
...opts.documentType ? { documentType: opts.documentType } : {}
|
|
3176
|
+
},
|
|
3177
|
+
{ limit, offset: 0 }
|
|
3178
|
+
);
|
|
3179
|
+
for (const instance of page.items) {
|
|
3180
|
+
if (result.purged.length >= limit) break;
|
|
3181
|
+
result.scanned++;
|
|
3182
|
+
if (!TERMINAL_STATUSES.has(instance.status)) continue;
|
|
3183
|
+
if (new Date(instance.createdAt) > opts.olderThan) continue;
|
|
3184
|
+
if (!dryRun) await deleteInstance(this.tenantId, instance.id);
|
|
3185
|
+
result.purged.push({
|
|
3186
|
+
instanceId: instance.id,
|
|
3187
|
+
documentId: instance.documentId,
|
|
3188
|
+
status: instance.status
|
|
3189
|
+
});
|
|
3190
|
+
}
|
|
3191
|
+
}
|
|
3192
|
+
this.logger.info("purgeInstances: sweep complete", {
|
|
3193
|
+
tenantId: this.tenantId,
|
|
3194
|
+
olderThan: opts.olderThan.toISOString(),
|
|
3195
|
+
scanned: result.scanned,
|
|
3196
|
+
purged: result.purged.length,
|
|
3197
|
+
dryRun
|
|
3198
|
+
});
|
|
3199
|
+
return result;
|
|
3200
|
+
}
|
|
3124
3201
|
async getStatistics(filter = {}) {
|
|
3125
3202
|
const statuses = [
|
|
3126
3203
|
"pending",
|