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