|
| 1 | +import {Flags} from '@oclif/core' |
| 2 | +import {PolymorfaValidationError} from '@polymorfa/sdk' |
| 3 | +import {BaseCommand} from '../../../../base-command.js' |
| 4 | +import { |
| 5 | + updateFrequencyCap, |
| 6 | + renderFrequencyCap, |
| 7 | +} from '../../../../services/campaign-frequency-cap.js' |
| 8 | +export default class FrequencyCapSet extends BaseCommand { |
| 9 | + static override description = |
| 10 | + 'Replace team limits (beta). Omitted windows become unlimited. Stale revisions return 409; no automatic retry.' |
| 11 | + static override flags = { |
| 12 | + revision: Flags.integer({ |
| 13 | + required: true, |
| 14 | + min: 0, |
| 15 | + max: 2147483646, |
| 16 | + description: |
| 17 | + 'Revision from get. On 409, read again and reconcile before retrying.', |
| 18 | + }), |
| 19 | + 'max-per-24-hours': Flags.integer({ |
| 20 | + min: 1, |
| 21 | + max: 50, |
| 22 | + description: 'Rolling 24-hour limit; omitted means no limit.', |
| 23 | + }), |
| 24 | + 'max-per-7-days': Flags.integer({ |
| 25 | + min: 1, |
| 26 | + max: 200, |
| 27 | + description: 'Rolling 7-day limit; omitted means no limit.', |
| 28 | + }), |
| 29 | + } |
| 30 | + async run(): Promise<void> { |
| 31 | + const {flags} = await this.parse(FrequencyCapSet) |
| 32 | + if ( |
| 33 | + flags['max-per-24-hours'] === undefined && |
| 34 | + flags['max-per-7-days'] === undefined |
| 35 | + ) |
| 36 | + throw new PolymorfaValidationError( |
| 37 | + 'Set at least one window, or use remove.', |
| 38 | + {code: 'invalid_frequency_cap'}, |
| 39 | + ) |
| 40 | + if ( |
| 41 | + flags['max-per-24-hours'] !== undefined && |
| 42 | + flags['max-per-7-days'] !== undefined && |
| 43 | + flags['max-per-24-hours'] > flags['max-per-7-days'] |
| 44 | + ) |
| 45 | + throw new PolymorfaValidationError( |
| 46 | + 'The 24-hour limit cannot exceed the 7-day limit.', |
| 47 | + {code: 'invalid_frequency_cap'}, |
| 48 | + ) |
| 49 | + const result = await updateFrequencyCap(await this.platformClient(flags), { |
| 50 | + revision: flags.revision, |
| 51 | + maxPer24Hours: flags['max-per-24-hours'] ?? null, |
| 52 | + maxPer7Days: flags['max-per-7-days'] ?? null, |
| 53 | + }) |
| 54 | + this.writeValue(result, renderFrequencyCap(result.data), flags) |
| 55 | + } |
| 56 | +} |
0 commit comments