hierarchical-approval 2.1.0 → 2.2.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/dist/nestjs.cjs CHANGED
@@ -927,6 +927,7 @@ var TERMINAL_STATUSES = /* @__PURE__ */ new Set([
927
927
  ]);
928
928
  var CYCLE_TIME_STATUSES = ["approved", "rejected", "cancelled"];
929
929
  var CYCLE_TIME_FETCH_BATCH_SIZE = 500;
930
+ var TEMPLATE_BUNDLE_VERSION = 1;
930
931
  var ApprovalEngine = class _ApprovalEngine {
931
932
  constructor(opts) {
932
933
  this.opts = opts;
@@ -3008,6 +3009,118 @@ var ApprovalEngine = class _ApprovalEngine {
3008
3009
  oldestAgeMs: Number.isFinite(row.oldest) ? now.getTime() - row.oldest : 0
3009
3010
  })).sort((a, b) => b.pending - a.pending || a.approverId.localeCompare(b.approverId));
3010
3011
  }
3012
+ /**
3013
+ * Export templates as a portable bundle.
3014
+ *
3015
+ * Approval configuration is written once and then has to travel — authored in
3016
+ * a sandbox, reviewed, promoted to production. Reading `listTemplates()` and
3017
+ * re-posting the rows carried each environment's own `id`, `tenantId` and
3018
+ * version lineage with it, which either collided on arrival or silently
3019
+ * claimed a history the target never had. This strips all of it.
3020
+ *
3021
+ * @param names - Templates to include; omit for all of them.
3022
+ */
3023
+ async exportTemplates(names) {
3024
+ const all = await this.registry.list();
3025
+ const wanted = names ? all.filter((t) => names.includes(t.name)) : all;
3026
+ if (names) {
3027
+ const missing = names.filter((n) => !all.some((t) => t.name === n));
3028
+ if (missing.length > 0) {
3029
+ throw new ApprovalTemplateNotFoundError(missing.join(", "));
3030
+ }
3031
+ }
3032
+ return {
3033
+ bundleVersion: TEMPLATE_BUNDLE_VERSION,
3034
+ exportedAt: this.clock.now(),
3035
+ templates: wanted.map((t) => {
3036
+ const {
3037
+ id: _id,
3038
+ tenantId: _tenantId,
3039
+ createdAt: _createdAt,
3040
+ version: _version,
3041
+ previousVersionId: _previousVersionId,
3042
+ ...config
3043
+ } = t;
3044
+ return config;
3045
+ })
3046
+ };
3047
+ }
3048
+ /**
3049
+ * Import a bundle produced by {@link exportTemplates}.
3050
+ *
3051
+ * **Every template is validated before any is written.** A bundle that is
3052
+ * half-applied is worse than one rejected outright: the tenant is left in a
3053
+ * state matching neither environment, and the operator has no way to tell
3054
+ * which half landed. Per-template failures during the write phase are still
3055
+ * reported individually, since a storage error can occur after validation
3056
+ * passes.
3057
+ *
3058
+ * @param bundle - The bundle to apply.
3059
+ * @param opts - `mode: 'create'` (default) refuses to touch existing
3060
+ * templates; `'upsert'` updates them. `dryRun` reports without writing.
3061
+ */
3062
+ async importTemplates(bundle, opts = {}) {
3063
+ const mode = opts.mode ?? "create";
3064
+ const dryRun = opts.dryRun ?? false;
3065
+ if (bundle.bundleVersion !== TEMPLATE_BUNDLE_VERSION) {
3066
+ throw new ApprovalValidationError(
3067
+ `Unsupported template bundle version ${bundle.bundleVersion}; this engine reads version ${TEMPLATE_BUNDLE_VERSION}.`
3068
+ );
3069
+ }
3070
+ if (!Array.isArray(bundle.templates) || bundle.templates.length === 0) {
3071
+ throw new ApprovalValidationError("Template bundle contains no templates.");
3072
+ }
3073
+ const duplicates = bundle.templates.map((t) => t.name).filter((name, i, all) => all.indexOf(name) !== i);
3074
+ if (duplicates.length > 0) {
3075
+ throw new ApprovalValidationError(
3076
+ `Template bundle contains duplicate names: ${[...new Set(duplicates)].join(", ")}.`
3077
+ );
3078
+ }
3079
+ const invalid = [];
3080
+ for (const config of bundle.templates) {
3081
+ const result2 = this.validateTemplate(config);
3082
+ if (!result2.valid) {
3083
+ invalid.push({
3084
+ name: config.name,
3085
+ message: result2.errors[0]?.message ?? "unknown validation error"
3086
+ });
3087
+ }
3088
+ }
3089
+ if (invalid.length > 0) {
3090
+ throw new ApprovalValidationError(
3091
+ `Template bundle failed validation and was not applied: ${invalid.map((e) => `${e.name}: ${e.message}`).join("; ")}`
3092
+ );
3093
+ }
3094
+ const result = { created: [], updated: [], skipped: [], errors: [], dryRun };
3095
+ for (const config of bundle.templates) {
3096
+ const existing = await this.opts.adapter.getTemplate(this.tenantId, config.name);
3097
+ try {
3098
+ if (existing && mode === "create") {
3099
+ result.skipped.push(config.name);
3100
+ continue;
3101
+ }
3102
+ if (existing) {
3103
+ if (!dryRun) await this.registry.update(config);
3104
+ result.updated.push(config.name);
3105
+ } else {
3106
+ if (!dryRun) await this.registry.define(config);
3107
+ result.created.push(config.name);
3108
+ }
3109
+ } catch (err) {
3110
+ result.errors.push({ name: config.name, message: err.message });
3111
+ }
3112
+ }
3113
+ this.logger.info("importTemplates: bundle applied", {
3114
+ tenantId: this.tenantId,
3115
+ mode,
3116
+ dryRun,
3117
+ created: result.created.length,
3118
+ updated: result.updated.length,
3119
+ skipped: result.skipped.length,
3120
+ errors: result.errors.length
3121
+ });
3122
+ return result;
3123
+ }
3011
3124
  async getStatistics(filter = {}) {
3012
3125
  const statuses = [
3013
3126
  "pending",