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