|
1 | 1 | import type { Command } from 'commander'; |
2 | | -import { apolloRequest } from '../api.js'; |
| 2 | +import { apolloGet, apolloRequest } from '../api.js'; |
3 | 3 | import { print, FORMAT_OPTION } from '../output.js'; |
4 | 4 | import { parsePageOptions } from '../utils.js'; |
5 | 5 |
|
@@ -38,8 +38,85 @@ interface SequenceRemoveContactsOptions { |
38 | 38 | format?: string; |
39 | 39 | } |
40 | 40 |
|
| 41 | +interface SequenceCreateOptions { |
| 42 | + name: string; |
| 43 | + stepsFile: string; |
| 44 | + scheduleId?: string; |
| 45 | + permissions?: string; |
| 46 | + exactDaytime?: boolean; |
| 47 | + active?: boolean; |
| 48 | + label?: string[]; |
| 49 | + format?: string; |
| 50 | +} |
| 51 | + |
| 52 | +interface SequenceUpdateOptions { |
| 53 | + id: string; |
| 54 | + stepsFile: string; |
| 55 | + name?: string; |
| 56 | + scheduleId?: string; |
| 57 | + permissions?: string; |
| 58 | + exactDaytime?: boolean; |
| 59 | + active?: boolean; |
| 60 | + inactive?: boolean; |
| 61 | + label?: string[]; |
| 62 | + format?: string; |
| 63 | +} |
| 64 | + |
| 65 | +interface SequenceApproveOptions { |
| 66 | + id: string; |
| 67 | + format?: string; |
| 68 | +} |
| 69 | + |
| 70 | +interface SequenceSchedulesOptions { |
| 71 | + format?: string; |
| 72 | +} |
| 73 | + |
| 74 | +// Reads emailer_steps from a JSON file (an array, or { "emailer_steps": [...] }). |
| 75 | +async function readSteps(path: string): Promise<unknown[]> { |
| 76 | + const fs = await import('node:fs/promises'); |
| 77 | + const parsed: unknown = JSON.parse(await fs.readFile(path, 'utf8')); |
| 78 | + const arr = Array.isArray(parsed) |
| 79 | + ? parsed |
| 80 | + : (parsed as { emailer_steps?: unknown }).emailer_steps; |
| 81 | + if (!Array.isArray(arr)) { |
| 82 | + console.error('Error: steps file must contain a JSON array (or { "emailer_steps": [...] })'); |
| 83 | + process.exit(1); |
| 84 | + } |
| 85 | + return arr; |
| 86 | +} |
| 87 | + |
| 88 | +// Maps sequence create options + parsed steps to the /sequences request body. Pure. |
| 89 | +export function buildSequenceCreateBody( |
| 90 | + opts: SequenceCreateOptions, |
| 91 | + steps: unknown[], |
| 92 | +): Record<string, unknown> { |
| 93 | + const body: Record<string, unknown> = { name: opts.name, emailer_steps: steps }; |
| 94 | + if (opts.permissions) body.permissions = opts.permissions; |
| 95 | + if (opts.scheduleId) body.emailer_schedule_id = opts.scheduleId; |
| 96 | + if (opts.exactDaytime) body.sequence_by_exact_daytime = true; |
| 97 | + if (opts.active) body.active = true; |
| 98 | + if (opts.label) body.label_names = opts.label; |
| 99 | + return body; |
| 100 | +} |
| 101 | + |
| 102 | +// Maps sequence update options + parsed steps to the /sequences/:id request body. Pure. |
| 103 | +export function buildSequenceUpdateBody( |
| 104 | + opts: SequenceUpdateOptions, |
| 105 | + steps: unknown[], |
| 106 | +): Record<string, unknown> { |
| 107 | + const body: Record<string, unknown> = { emailer_steps: steps }; |
| 108 | + if (opts.name) body.name = opts.name; |
| 109 | + if (opts.permissions) body.permissions = opts.permissions; |
| 110 | + if (opts.scheduleId) body.emailer_schedule_id = opts.scheduleId; |
| 111 | + if (opts.exactDaytime) body.sequence_by_exact_daytime = true; |
| 112 | + if (opts.active) body.active = true; |
| 113 | + if (opts.inactive) body.active = false; |
| 114 | + if (opts.label) body.label_names = opts.label; |
| 115 | + return body; |
| 116 | +} |
| 117 | + |
41 | 118 | export function registerSequences(program: Command): void { |
42 | | - const seq = program.command('sequences').description('Search sequences and add or remove contacts'); |
| 119 | + const seq = program.command('sequences').description('Manage sequences: search, create/update, approve, schedules, and add/remove contacts'); |
43 | 120 |
|
44 | 121 | seq |
45 | 122 | .command('search') |
@@ -123,4 +200,63 @@ export function registerSequences(program: Command): void { |
123 | 200 | const data = await apolloRequest('/emailer_campaigns/remove_or_stop_contact_ids', body); |
124 | 201 | print(data, opts.format); |
125 | 202 | }); |
| 203 | + |
| 204 | + seq |
| 205 | + .command('create') |
| 206 | + .description('Create a sequence from a steps JSON file') |
| 207 | + .requiredOption('--name <name>', 'Sequence name') |
| 208 | + .requiredOption('--steps-file <path>', 'Path to JSON file with the ordered emailer_steps (array, or { "emailer_steps": [...] })') |
| 209 | + .option('--schedule-id <id>', 'EmailerSchedule ID controlling send-time windows (see "sequences schedules")') |
| 210 | + .option('--permissions <perm>', 'Who can use/view the sequence (defaults to team_can_use)') |
| 211 | + .option('--exact-daytime', 'Each step has a fixed exact datetime (one-off campaign)') |
| 212 | + .option('--active', 'Activate the sequence on creation (contacts added start sending)') |
| 213 | + .option('--label <names...>', 'Label name(s) to apply to the sequence') |
| 214 | + .option(...FORMAT_OPTION) |
| 215 | + .action(async (opts: SequenceCreateOptions) => { |
| 216 | + const steps = await readSteps(opts.stepsFile); |
| 217 | + const data = await apolloRequest('/sequences', buildSequenceCreateBody(opts, steps)); |
| 218 | + print(data, opts.format); |
| 219 | + }); |
| 220 | + |
| 221 | + seq |
| 222 | + .command('update') |
| 223 | + .description('Update a sequence (replaces its full set of steps)') |
| 224 | + .requiredOption('--id <id>', 'Sequence ID') |
| 225 | + .requiredOption('--steps-file <path>', 'Path to JSON file with the FULL ordered emailer_steps after the update') |
| 226 | + .option('--name <name>', 'New sequence name') |
| 227 | + .option('--schedule-id <id>', 'New EmailerSchedule ID') |
| 228 | + .option('--permissions <perm>', 'New sharing permission') |
| 229 | + .option('--exact-daytime', 'Switch to exact-datetime mode') |
| 230 | + .option('--active', 'Activate the sequence as part of the update') |
| 231 | + .option('--inactive', 'Deactivate the sequence as part of the update') |
| 232 | + .option('--label <names...>', 'Replace the label set on the sequence') |
| 233 | + .option(...FORMAT_OPTION) |
| 234 | + .action(async (opts: SequenceUpdateOptions) => { |
| 235 | + if (opts.active && opts.inactive) { |
| 236 | + console.error('Error: pass only one of --active or --inactive'); |
| 237 | + process.exit(1); |
| 238 | + } |
| 239 | + const steps = await readSteps(opts.stepsFile); |
| 240 | + const data = await apolloRequest(`/sequences/${opts.id}`, buildSequenceUpdateBody(opts, steps), 'PATCH'); |
| 241 | + print(data, opts.format); |
| 242 | + }); |
| 243 | + |
| 244 | + seq |
| 245 | + .command('approve') |
| 246 | + .description('Approve a sequence pending review') |
| 247 | + .requiredOption('--id <id>', 'Sequence (emailer_campaign) ID') |
| 248 | + .option(...FORMAT_OPTION) |
| 249 | + .action(async (opts: SequenceApproveOptions) => { |
| 250 | + const data = await apolloRequest(`/emailer_campaigns/${opts.id}/approve`, {}); |
| 251 | + print(data, opts.format); |
| 252 | + }); |
| 253 | + |
| 254 | + seq |
| 255 | + .command('schedules') |
| 256 | + .description('List sending schedules (for --schedule-id on create/update)') |
| 257 | + .option(...FORMAT_OPTION) |
| 258 | + .action(async (opts: SequenceSchedulesOptions) => { |
| 259 | + const data = await apolloGet('/emailer_schedules'); |
| 260 | + print(data, opts.format); |
| 261 | + }); |
126 | 262 | } |
0 commit comments