@unboundcx/sdk 4.13.127 → 4.13.129

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/index.js CHANGED
@@ -25,6 +25,7 @@ import { RingGroupsService } from './services/ringGroups.js';
25
25
  import { LineGroupsService } from './services/lineGroups.js';
26
26
  import { SchedulesService } from './services/schedules/SchedulesService.js';
27
27
  import { JourneysService } from './services/journeys/JourneysService.js';
28
+ import { WorkflowTemplatesService } from './services/workflowTemplates/WorkflowTemplatesService.js';
28
29
  import { EmergencyService } from './services/emergency.js';
29
30
  import { WebchatService } from './services/webchat.js';
30
31
  import { CobrowseService } from './services/cobrowse.js';
@@ -129,6 +130,7 @@ class UnboundSDK extends BaseSDK {
129
130
  this.lineGroups = new LineGroupsService(this);
130
131
  this.schedules = new SchedulesService(this);
131
132
  this.journeys = new JourneysService(this);
133
+ this.workflowTemplates = new WorkflowTemplatesService(this);
132
134
  this.emergency = new EmergencyService(this);
133
135
  this.webchat = new WebchatService(this);
134
136
  this.cobrowse = new CobrowseService(this);
@@ -336,6 +338,9 @@ export { SchedulesService } from './services/schedules/SchedulesService.js';
336
338
  export { TimeOffService } from './services/schedules/TimeOffService.js';
337
339
  export { JourneysService } from './services/journeys/JourneysService.js';
338
340
  export { JourneyMembersService } from './services/journeys/JourneyMembersService.js';
341
+ export { JourneyDraftService } from './services/journeys/JourneysService.js';
342
+ export { JourneyWorklistService } from './services/journeys/JourneyWorklistService.js';
343
+ export { WorkflowTemplatesService } from './services/workflowTemplates/WorkflowTemplatesService.js';
339
344
  export { EmergencyService } from './services/emergency.js';
340
345
  export { WebchatService, WebchatWidgetsService } from './services/webchat.js';
341
346
  export { CobrowseService, CobrowseVisitorService } from './services/cobrowse.js';
@@ -376,3 +381,4 @@ export {
376
381
  DataExportService,
377
382
  } from './services/dataImport.js';
378
383
  export { BaseSDK } from './base.js';
384
+ export { layoutJourney, MAX_LANES as JOURNEY_LAYOUT_MAX_LANES } from './lib/journeyLayout.js';
@@ -0,0 +1,132 @@
1
+ // Shared journey step-list layout (journeys-plan.md §7.3). Pure function,
2
+ // no DOM/Svelte — consumed by the API compiler (positions written into
3
+ // workflowItems.position) and the client's step-list / read-only canvas
4
+ // preview, so geometry never drifts between the two.
5
+ //
6
+ // Node width = max(240, pad + portCount * portWidth) so a step with more
7
+ // outcome ports (e.g. journeySms with several expected-reply rows) renders
8
+ // wide enough for all of them. Main path sits in column 0, top to bottom in
9
+ // doc order. A side path (one outcome's target.kind === 'side') renders as
10
+ // a linear vertical stack in the next column to the right, in outcome
11
+ // order; the row the branch step occupies grows tall enough to hold the
12
+ // tallest concurrent side path. Forward jumps (an outcome's target.kind ===
13
+ // 'step' pointing at a later main-path step) get a lane in a left-gutter
14
+ // reserved for wire routing, bounded by MAX_LANES — the same bound
15
+ // app1-client's wireLayout.js uses for the advanced canvas, kept in sync
16
+ // here as a literal with this comment rather than a cross-package import.
17
+ export const MAX_LANES = 6;
18
+
19
+ const DEFAULTS = {
20
+ portWidth: 56,
21
+ pad: 48,
22
+ nodeHeight: 90,
23
+ rowGap: 40,
24
+ colGap: 80,
25
+ };
26
+
27
+ function portCount(step) {
28
+ return Math.max(1, (step?.outcomes || []).length);
29
+ }
30
+
31
+ function widthForStep(step, { pad, portWidth }) {
32
+ return Math.max(240, pad + portCount(step) * portWidth);
33
+ }
34
+
35
+ // Depth-first walk collecting every step (main + nested side-path steps) so
36
+ // widths can be computed for all of them in one pass.
37
+ function collectAllSteps(steps, opts, widths, acc = []) {
38
+ for (const step of steps || []) {
39
+ if (!step?.stepKey) continue;
40
+ acc.push(step);
41
+ widths[step.stepKey] = widthForStep(step, opts);
42
+ for (const outcome of step.outcomes || []) {
43
+ if (outcome?.target?.kind === 'side' && Array.isArray(outcome.target.steps)) {
44
+ collectAllSteps(outcome.target.steps, opts, widths, acc);
45
+ }
46
+ }
47
+ }
48
+ return acc;
49
+ }
50
+
51
+ export function layoutJourney(doc, opts = {}) {
52
+ const options = { ...DEFAULTS, ...opts };
53
+ const { pad, nodeHeight, rowGap, colGap } = options;
54
+ const steps = Array.isArray(doc?.steps) ? doc.steps : [];
55
+
56
+ const widths = {};
57
+ const positions = {};
58
+ const lanes = {};
59
+
60
+ collectAllSteps(steps, options, widths);
61
+
62
+ const mainIndex = new Map();
63
+ steps.forEach((step, i) => mainIndex.set(step.stepKey, i));
64
+
65
+ // --- positions: main path column 0, side paths to the right ---
66
+ const colWidths = [];
67
+ colWidths[0] = Math.max(240, ...steps.map((s) => widths[s.stepKey] || 240));
68
+
69
+ let y = pad;
70
+ for (const step of steps) {
71
+ positions[step.stepKey] = { x: pad, y };
72
+ let rowHeight = nodeHeight;
73
+ let col = 1;
74
+
75
+ for (const outcome of step.outcomes || []) {
76
+ if (outcome?.target?.kind === 'side' && Array.isArray(outcome.target.steps)) {
77
+ const sideSteps = outcome.target.steps.filter((s) => s?.stepKey);
78
+ if (!sideSteps.length) {
79
+ col += 1;
80
+ continue;
81
+ }
82
+ colWidths[col] = Math.max(
83
+ colWidths[col] || 240,
84
+ ...sideSteps.map((s) => widths[s.stepKey] || 240),
85
+ );
86
+ const xOffset =
87
+ pad + colWidths.slice(0, col).reduce((sum, w) => sum + w + colGap, 0);
88
+ let sideY = y;
89
+ sideSteps.forEach((sideStep) => {
90
+ positions[sideStep.stepKey] = { x: xOffset, y: sideY };
91
+ sideY += nodeHeight + rowGap;
92
+ });
93
+ const sideHeight =
94
+ sideSteps.length * nodeHeight + (sideSteps.length - 1) * rowGap;
95
+ rowHeight = Math.max(rowHeight, sideHeight);
96
+ col += 1;
97
+ }
98
+ }
99
+
100
+ y += rowHeight + rowGap;
101
+ }
102
+
103
+ // --- lanes: forward-jump wires (target.kind === 'step', later index) ---
104
+ const forwardJumps = [];
105
+ steps.forEach((step, i) => {
106
+ for (const outcome of step.outcomes || []) {
107
+ if (outcome?.target?.kind === 'step') {
108
+ const j = mainIndex.get(outcome.target.stepKey);
109
+ if (j != null && j > i) {
110
+ forwardJumps.push({ from: i, to: j, stepKey: step.stepKey, port: outcome.port });
111
+ }
112
+ }
113
+ }
114
+ });
115
+ forwardJumps.sort((a, b) => a.from - b.from || a.to - b.to);
116
+
117
+ const laneEndsAt = [];
118
+ for (const jump of forwardJumps) {
119
+ let lane = laneEndsAt.findIndex(
120
+ (endsAt) => endsAt === undefined || endsAt <= jump.from,
121
+ );
122
+ if (lane === -1) {
123
+ lane = Math.min(laneEndsAt.length, MAX_LANES - 1);
124
+ }
125
+ laneEndsAt[lane] = jump.to;
126
+ lanes[`${jump.stepKey}:${jump.port}`] = lane;
127
+ }
128
+
129
+ return { positions, widths, lanes };
130
+ }
131
+
132
+ export default { layoutJourney, MAX_LANES };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unboundcx/sdk",
3
- "version": "4.13.127",
3
+ "version": "4.13.129",
4
4
  "description": "Official JavaScript SDK for the Unbound API - A comprehensive toolkit for integrating with Unbound's communication, AI, and data management services",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -44,6 +44,7 @@
44
44
  "proto/**/*.proto",
45
45
  "schemas/**/*.js",
46
46
  "schemas/**/*.md",
47
+ "lib/**/*.js",
47
48
  "README.md",
48
49
  "LICENSE"
49
50
  ],
@@ -63,6 +64,9 @@
63
64
  },
64
65
  "./schemas/*": {
65
66
  "import": "./schemas/*.js"
67
+ },
68
+ "./lib/*": {
69
+ "import": "./lib/*.js"
66
70
  }
67
71
  },
68
72
  "scripts": {
@@ -0,0 +1,92 @@
1
+ import { internalRequest } from '../../base.js';
2
+
3
+ /**
4
+ * Simple-builder draft surface -- `sdk.journeys.draft.*` (journeys-plan.md
5
+ * §7.3/§7.4, P4 compile area). Custom routes mounted at
6
+ * `/journeys/:id/draft*`, `/journeys/:id/publish-check`,
7
+ * `/journeys/:id/convert-to-advanced`.
8
+ *
9
+ * @see app1-api src/services/journeys/routes.js
10
+ * @see app1-api src/services/journeys/controllers/compile.js
11
+ */
12
+ export class JourneyDraftService {
13
+ constructor(sdk) {
14
+ this.sdk = sdk;
15
+ }
16
+
17
+ /**
18
+ * @param {string} journeyId
19
+ * @returns {Promise<Object>} { version, doc, builderMode }
20
+ */
21
+ async get(journeyId) {
22
+ this.sdk.validateParams(
23
+ { journeyId },
24
+ { journeyId: { type: 'string', required: true } },
25
+ );
26
+ return await internalRequest(
27
+ this.sdk,
28
+ `/journeys/${journeyId}/draft`,
29
+ 'GET',
30
+ );
31
+ }
32
+
33
+ /**
34
+ * Compile the simple-builder step doc into workflowItems/ports/
35
+ * connections/positions in one transaction. 409s if the journey's
36
+ * `builderMode` is `'advanced'` (doc is ignored once converted).
37
+ *
38
+ * @param {string} journeyId
39
+ * @param {Object} doc - journeyDoc shape (journeys-plan.md §7.2)
40
+ * @returns {Promise<Object>} { doc (with dayOffset), itemsByStepKey }
41
+ */
42
+ async saveSteps(journeyId, doc) {
43
+ this.sdk.validateParams(
44
+ { journeyId, doc },
45
+ {
46
+ journeyId: { type: 'string', required: true },
47
+ doc: { type: 'object', required: true },
48
+ },
49
+ );
50
+ return await internalRequest(
51
+ this.sdk,
52
+ `/journeys/${journeyId}/draft/steps`,
53
+ 'PUT',
54
+ { body: { doc } },
55
+ );
56
+ }
57
+
58
+ /**
59
+ * One-way: `simple` -> `advanced` only. Once advanced, the journeyDoc is
60
+ * ignored and compile is unavailable (`saveSteps` 409s).
61
+ *
62
+ * @param {string} journeyId
63
+ * @returns {Promise<Object>} Updated journey
64
+ */
65
+ async convertToAdvanced(journeyId) {
66
+ this.sdk.validateParams(
67
+ { journeyId },
68
+ { journeyId: { type: 'string', required: true } },
69
+ );
70
+ return await internalRequest(
71
+ this.sdk,
72
+ `/journeys/${journeyId}/convert-to-advanced`,
73
+ 'POST',
74
+ );
75
+ }
76
+
77
+ /**
78
+ * @param {string} journeyId
79
+ * @returns {Promise<Object>} { blocking, warnings }
80
+ */
81
+ async publishCheck(journeyId) {
82
+ this.sdk.validateParams(
83
+ { journeyId },
84
+ { journeyId: { type: 'string', required: true } },
85
+ );
86
+ return await internalRequest(
87
+ this.sdk,
88
+ `/journeys/${journeyId}/publish-check`,
89
+ 'GET',
90
+ );
91
+ }
92
+ }
@@ -114,9 +114,12 @@ export class JourneyMembersService {
114
114
  * @param {Object} body
115
115
  * @param {'pause'|'resume'|'remove'|'reassign'|'changeChannel'} body.action
116
116
  * @param {string[]} [body.ids] - Either ids or filter is required
117
- * @param {Object} [body.filter]
117
+ * @param {Object} [body.filter] - e.g. `{ ownerUserId }` to target every
118
+ * member currently owned by one rep (P5 "reassign-all-owned-by" is this
119
+ * `action:'reassign'` + `filter:{ownerUserId}` combination -- no
120
+ * dedicated endpoint/method was added).
118
121
  * @param {string} [body.reason]
119
- * @param {string} [body.ownerUserId]
122
+ * @param {string} [body.ownerUserId] - New owner for `action:'reassign'`
120
123
  * @param {Object} [body.channels]
121
124
  * @returns {Promise<Object>} { results }
122
125
  */
@@ -186,4 +189,31 @@ export class JourneyMembersService {
186
189
  {},
187
190
  );
188
191
  }
192
+
193
+ /**
194
+ * Context-aware AI draft for a human touch-task step (record, step
195
+ * intent, prior conversation) -- journeys-plan.md §9 P5. Not the final
196
+ * send content; the rep edits/sends from the task itself.
197
+ *
198
+ * @param {string} memberId
199
+ * @param {Object} body
200
+ * @param {string} [body.stepKey]
201
+ * @param {'email'|'sms'} body.channel
202
+ * @returns {Promise<{data: {draft: string, channel: string, stepKey: string}}>}
203
+ */
204
+ async draft(memberId, body = {}) {
205
+ this.sdk.validateParams(
206
+ { memberId, channel: body?.channel },
207
+ {
208
+ memberId: { type: 'string', required: true },
209
+ channel: { type: 'string', required: true },
210
+ },
211
+ );
212
+ return await internalRequest(
213
+ this.sdk,
214
+ `/journeys/members/${memberId}/draft`,
215
+ 'POST',
216
+ { body: pickDefined(body) },
217
+ );
218
+ }
189
219
  }
@@ -0,0 +1,73 @@
1
+ import { internalRequest } from '../../base.js';
2
+
3
+ function pickDefined(fields) {
4
+ const body = {};
5
+ for (const [key, value] of Object.entries(fields)) {
6
+ if (value !== undefined) body[key] = value;
7
+ }
8
+ return body;
9
+ }
10
+
11
+ /**
12
+ * @typedef {Object} JourneyWorklistTask
13
+ * @property {string} id
14
+ * @property {'journey'} type
15
+ * @property {'worklist'} deliveryMode
16
+ * @property {string|null} dueAt
17
+ * @property {string} journeyMemberId
18
+ * @property {string} journeyId
19
+ * @property {string} journeyStepKey
20
+ * @property {'email'|'sms'|'call'|'other'} journeyStepChannel
21
+ * @property {string|null} preferredWorkerId - Owner, or null for a pool task
22
+ * @property {string|null} engagementSessionId
23
+ */
24
+
25
+ /**
26
+ * Sales work list -- `sdk.journeys.worklist.*` (journeys-plan.md §9, P5).
27
+ * Cross-journey view of a sales rep's (or the pool's) open touch tasks
28
+ * (`tasks.type='journey'`), plus upcoming members from
29
+ * `journeyMembers.nextActionAt`. Requires `users_acct.salesEnabled` for
30
+ * `takeNext` (`list` stays open to ordinary object-read permission, per
31
+ * the P5 contract).
32
+ *
33
+ * @see app1-api src/services/journeys/controllers/worklist.js
34
+ */
35
+ export class JourneyWorklistService {
36
+ constructor(sdk) {
37
+ this.sdk = sdk;
38
+ }
39
+
40
+ /**
41
+ * @param {Object} [opts]
42
+ * @param {'due'|'overdue'|'upcoming'} [opts.bucket]
43
+ * @param {string} [opts.stepType]
44
+ * @param {string} [opts.journeyId]
45
+ * @param {number} [opts.page]
46
+ * @param {number} [opts.pageSize]
47
+ * @returns {Promise<{data: JourneyWorklistTask[]}>}
48
+ */
49
+ async list(opts = {}) {
50
+ return await internalRequest(this.sdk, '/journeys/worklist', 'GET', {
51
+ query: pickDefined(opts),
52
+ });
53
+ }
54
+
55
+ /**
56
+ * Takes the next due task for the caller (owner-or-pool), via the same
57
+ * `transitionTask` path as a manual take (`PUT /tasks/:id
58
+ * {status:'connected', workerId}`) -- not a separate silent-assign path.
59
+ *
60
+ * @param {Object} [filter]
61
+ * @param {string} [filter.stepType]
62
+ * @param {string} [filter.journeyId]
63
+ * @returns {Promise<{data: JourneyWorklistTask|null}>} null when nothing is due
64
+ */
65
+ async takeNext(filter = {}) {
66
+ return await internalRequest(
67
+ this.sdk,
68
+ '/journeys/worklist/take-next',
69
+ 'POST',
70
+ { body: pickDefined(filter) },
71
+ );
72
+ }
73
+ }
@@ -1,5 +1,15 @@
1
1
  import { internalRequest } from '../../base.js';
2
2
  import { JourneyMembersService } from './JourneyMembersService.js';
3
+ import { JourneyDraftService } from './JourneyDraftService.js';
4
+ import { JourneyWorklistService } from './JourneyWorklistService.js';
5
+
6
+ function pickDefined(fields) {
7
+ const body = {};
8
+ for (const [key, value] of Object.entries(fields)) {
9
+ if (value !== undefined) body[key] = value;
10
+ }
11
+ return body;
12
+ }
3
13
 
4
14
  /**
5
15
  * Journeys (journeys-plan.md §2/§4) -- `sdk.journeys.*`.
@@ -13,6 +23,30 @@ import { JourneyMembersService } from './JourneyMembersService.js';
13
23
  * and the member-grid/enrol/action/fix/events/stats endpoints are custom
14
24
  * routes under `/journeys/`, covered by `members` and `stats`.
15
25
  *
26
+ * P5 (journeys-plan.md §8, sales queues + human steps) added fields to
27
+ * several generic objects rather than new endpoints -- no dedicated SDK
28
+ * service exists for these, so they are documented here instead:
29
+ * - `sdk.objects` object `'queues'`: `queueType` ('support'|'sales',
30
+ * default 'support'), `deliveryMode` ('push'|'worklist', default
31
+ * 'push'). A sales queue is auto-created with a journey type
32
+ * (`queueType:'sales', deliveryMode:'worklist'`) and its id is set on
33
+ * `journeyTypes.queueId`.
34
+ * - `sdk.objects` object `'queueDispositions'`: `outcome`
35
+ * ('positive'|'neutral'|'negative'|null), `countsAsConversion`
36
+ * (boolean), `flagsBadContact` (boolean) -- drive a journey member's
37
+ * `converted` / `needsAttention:badContact` transitions when a touch
38
+ * task is given that disposition.
39
+ * - `sdk.objects` object `'users'` (`users_acct`): `salesEnabled`
40
+ * (boolean, default false) -- required (alongside normal object
41
+ * permissions) for journeys build/enrol/member-action/worklist-take
42
+ * endpoints; member-grid reads stay open to object-read permission.
43
+ * - Task rows (`sdk.taskRouter.task.*`, `sdk.journeys.worklist.*`) gain
44
+ * `deliveryMode` ('push'|'worklist'), `dueAt` (ISO datetime|null),
45
+ * `journeyMemberId`, `journeyId`, `journeyStepKey`, `journeyStepChannel`
46
+ * ('email'|'sms'|'call'|'other') on a journey touch task
47
+ * (`type:'journey'`). See `JourneyWorklistService`'s
48
+ * `JourneyWorklistTask` typedef for the full shape.
49
+ *
16
50
  * @see app1-api src/services/journeys/routes.js
17
51
  */
18
52
  export class JourneysService {
@@ -21,6 +55,8 @@ export class JourneysService {
21
55
  this.members = new JourneyMembersService(sdk);
22
56
  this.types = new JourneyTypesService(sdk);
23
57
  this.goals = new JourneyGoalsService(sdk);
58
+ this.draft = new JourneyDraftService(sdk);
59
+ this.worklist = new JourneyWorklistService(sdk);
24
60
  }
25
61
 
26
62
  /**
@@ -103,6 +139,95 @@ export class JourneysService {
103
139
  {},
104
140
  );
105
141
  }
142
+
143
+ /**
144
+ * No-send dry run: per step, the resolved channel (override -> primary),
145
+ * rendered message, and landing date for a given person (journeys-plan.md
146
+ * §7.6, P4 templates area).
147
+ *
148
+ * @param {string} id
149
+ * @param {Object} params
150
+ * @param {string} params.peopleId
151
+ * @returns {Promise<Object>} { steps: [{ stepKey, channel, message, landingAt }] }
152
+ */
153
+ async preview(id, { peopleId } = {}) {
154
+ this.sdk.validateParams(
155
+ { id, peopleId },
156
+ {
157
+ id: { type: 'string', required: true },
158
+ peopleId: { type: 'string', required: true },
159
+ },
160
+ );
161
+ return await internalRequest(this.sdk, `/journeys/${id}/preview`, 'GET', {
162
+ query: { peopleId },
163
+ });
164
+ }
165
+
166
+ /**
167
+ * Enrols the caller as an `isTest` member: sends redirect to the caller's
168
+ * own email/number, waits compress to `TEST_RUN_WAIT_SECONDS`, and the
169
+ * member is excluded from stats/event counts.
170
+ *
171
+ * @param {string} id
172
+ * @param {Object} [params]
173
+ * @param {string} [params.peopleId]
174
+ * @param {string} [params.testEmail] - override destination
175
+ * @param {string} [params.testPhone] - override destination
176
+ * @returns {Promise<Object>} Enrolled test member
177
+ */
178
+ async testRun(id, { peopleId, testEmail, testPhone } = {}) {
179
+ this.sdk.validateParams({ id }, { id: { type: 'string', required: true } });
180
+ return await internalRequest(
181
+ this.sdk,
182
+ `/journeys/${id}/test-run`,
183
+ 'POST',
184
+ { body: pickDefined({ peopleId, testEmail, testPhone }) },
185
+ );
186
+ }
187
+
188
+ /**
189
+ * Export + install into the same account, keeping template refs
190
+ * (journeys-plan.md §7.7).
191
+ *
192
+ * @param {string} id
193
+ * @param {Object} [params]
194
+ * @param {string} [params.name]
195
+ * @returns {Promise<Object>} New journey + workflow + draft version
196
+ */
197
+ async clone(id, { name } = {}) {
198
+ this.sdk.validateParams({ id }, { id: { type: 'string', required: true } });
199
+ return await internalRequest(this.sdk, `/journeys/${id}/clone`, 'POST', {
200
+ body: pickDefined({ name }),
201
+ });
202
+ }
203
+
204
+ /**
205
+ * Save this journey into the account's template library
206
+ * (`workflowTemplates`, journeys-plan.md §7.7).
207
+ *
208
+ * @param {string} id
209
+ * @param {Object} params
210
+ * @param {string} params.name
211
+ * @param {string} [params.summary]
212
+ * @param {string} [params.category]
213
+ * @param {'platform'|'account'} [params.visibility]
214
+ * @returns {Promise<Object>} Created workflowTemplates row
215
+ */
216
+ async saveAsTemplate(id, { name, summary, category, visibility } = {}) {
217
+ this.sdk.validateParams(
218
+ { id, name },
219
+ {
220
+ id: { type: 'string', required: true },
221
+ name: { type: 'string', required: true },
222
+ },
223
+ );
224
+ return await internalRequest(
225
+ this.sdk,
226
+ `/journeys/${id}/save-as-template`,
227
+ 'POST',
228
+ { body: pickDefined({ name, summary, category, visibility }) },
229
+ );
230
+ }
106
231
  }
107
232
 
108
233
  /**
@@ -213,4 +338,9 @@ class JourneyGoalsService {
213
338
  }
214
339
  }
215
340
 
216
- export { JourneyTypesService, JourneyGoalsService };
341
+ export {
342
+ JourneyTypesService,
343
+ JourneyGoalsService,
344
+ JourneyDraftService,
345
+ JourneyWorklistService,
346
+ };
@@ -0,0 +1,68 @@
1
+ import { internalRequest } from '../../base.js';
2
+
3
+ function pickDefined(fields) {
4
+ const body = {};
5
+ for (const [key, value] of Object.entries(fields)) {
6
+ if (value !== undefined) body[key] = value;
7
+ }
8
+ return body;
9
+ }
10
+
11
+ /**
12
+ * Account-visible workflow template library -- `sdk.workflowTemplates.*`
13
+ * (journeys-plan.md §7.7, P4 templates area). `visibility`/access is
14
+ * service-only (accountId IS NULL for platform starters, or = caller);
15
+ * platform authoring (upsert) is INTERNAL-only (`/internal/journeys/
16
+ * templates`) with no customer-facing SDK surface -- there is no first-party
17
+ * INTERNAL SDK extension surface in this package to hang it on, so it is
18
+ * intentionally not wrapped here.
19
+ *
20
+ * @see app1-api src/services/journeys/routes.js (`/journeys/templates*`)
21
+ */
22
+ export class WorkflowTemplatesService {
23
+ constructor(sdk) {
24
+ this.sdk = sdk;
25
+ }
26
+
27
+ /**
28
+ * @param {Object} [params]
29
+ * @param {string} [params.workflowType] - defaults to 'journey' on the API
30
+ * @returns {Promise<Object[]>} Templates visible to the caller's account
31
+ */
32
+ async list({ workflowType } = {}) {
33
+ return await internalRequest(
34
+ this.sdk,
35
+ '/journeys/templates',
36
+ 'GET',
37
+ { query: pickDefined({ workflowType }) },
38
+ );
39
+ }
40
+
41
+ /**
42
+ * Installs a template into the caller's account as a new journey +
43
+ * workflow + draft version (+ journeyDoc when present) in one
44
+ * transaction, validated again at install time.
45
+ *
46
+ * @param {string} templateId
47
+ * @param {Object} params
48
+ * @param {string} params.journeyTypeId
49
+ * @param {Object} [params.answers] - answers to the template's `requirements`
50
+ * @param {string} [params.name] - override the template's default name
51
+ * @returns {Promise<Object>} New journey + workflow + draft version
52
+ */
53
+ async install(templateId, { journeyTypeId, answers, name } = {}) {
54
+ this.sdk.validateParams(
55
+ { templateId, journeyTypeId },
56
+ {
57
+ templateId: { type: 'string', required: true },
58
+ journeyTypeId: { type: 'string', required: true },
59
+ },
60
+ );
61
+ return await internalRequest(
62
+ this.sdk,
63
+ `/journeys/templates/${templateId}/install`,
64
+ 'POST',
65
+ { body: pickDefined({ journeyTypeId, answers, name }) },
66
+ );
67
+ }
68
+ }