Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/commands/db/trigger/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Flags} from '@oclif/core'

import {BaseCommand} from '../../../base.js'
import {readJsonObject, SCHEMA_TRIGGER} from '../../../lib/database.js'
import {triggerFromFile} from '../../../lib/triggers.js'

export default class DbTriggerCreate extends BaseCommand {
static description = `Create or update a schema trigger from a JSON file
Expand Down Expand Up @@ -45,10 +46,8 @@ action fails (--no-break-on-error turns it off). Both override the file.`

const source = flags.file === '-' ? '-' : `@${flags.file}`
const parsed = await readJsonObject(source, 'file')
// Accept the bare trigger or the whole request body ({"trigger": {...}}).
const fromFile = (
parsed.trigger && typeof parsed.trigger === 'object' ? parsed.trigger : parsed
) as Record<string, unknown>
// The bare trigger, the request body or a `trigger <id> --json` answer.
const fromFile = triggerFromFile(parsed)

const trigger: Record<string, unknown> = {type: SCHEMA_TRIGGER, ...fromFile}
if (flags.schema) trigger.schemaId = flags.schema
Expand Down
7 changes: 3 additions & 4 deletions src/commands/payments/trigger/save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Flags} from '@oclif/core'

import {BaseCommand} from '../../../base.js'
import {ON_WEBHOOK_CALL_RECEIVED, PAYMENTS_TRIGGER, readJsonObject} from '../../../lib/payments.js'
import {triggerFromFile} from '../../../lib/triggers.js'

export default class PaymentsTriggerSave extends BaseCommand {
static description = `Create or update a payment trigger from a JSON file
Expand Down Expand Up @@ -50,10 +51,8 @@ trigger's place in the queue of the same event.`

const source = flags.file === '-' ? '-' : `@${flags.file}`
const parsed = await readJsonObject(source, 'file')
// Accept the bare trigger or the whole request body ({"trigger": {...}}).
const fromFile = (
parsed.trigger && typeof parsed.trigger === 'object' ? parsed.trigger : parsed
) as Record<string, unknown>
// The bare trigger, the request body or a `trigger <id> --json` answer.
const fromFile = triggerFromFile(parsed)

const trigger: Record<string, unknown> = {type: PAYMENTS_TRIGGER, when: ON_WEBHOOK_CALL_RECEIVED, ...fromFile}
if (flags.id) trigger.triggerId = flags.id
Expand Down
20 changes: 20 additions & 0 deletions src/lib/triggers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* The trigger of a `--file` for `db trigger create` / `payments trigger save`.
*
* Accepts the bare trigger, the whole save request (`{"trigger": {...}}`) or
* the read answer of `… trigger <id> --json` (`{"item": {...}}`). A read
* answer names the action `thenAction`; the save request names it `action`,
* so a file saved from a read is renamed here — without it the round trip
* saved a trigger with no action.
*/
export function triggerFromFile(parsed: Record<string, unknown>): Record<string, unknown> {
const inner = [parsed.trigger, parsed.item].find((v) => v && typeof v === 'object' && !Array.isArray(v))
const trigger = {...((inner ?? parsed) as Record<string, unknown>)}

if (trigger.action === undefined && trigger.thenAction !== undefined) {
trigger.action = trigger.thenAction
}

delete trigger.thenAction
return trigger
}
9 changes: 9 additions & 0 deletions tests/db-routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,15 @@ describe('db trigger', () => {
})
})

it('create takes a `db trigger <id> --json` answer: item unwrapped, thenAction saved as action', async () => {
const {action, ...rest} = trigger as Record<string, unknown>
const read = join(dir, 'read-trigger.json')
writeFileSync(read, JSON.stringify({item: {...rest, thenAction: action}}))
expect(await bodyOf(['trigger', 'create', '--file', read])).toEqual({
trigger: {type: 'Schema', ...rest, action},
})
})

it('create refuses a negative --order and sends nothing', async () => {
const {error} = await runCommand(['db', 'trigger', 'create', '--file', TRIGGER_FILE, '--order', '-1', ...globalArgs])
expect(error?.message).toMatch(/order/)
Expand Down
13 changes: 13 additions & 0 deletions tests/payments-routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,19 @@ describe('payments trigger save', () => {
expect(body?.trigger).toMatchObject({type: 'Payments', name: 'Paid'})
})

it('takes a `payments trigger <id> --json` answer: item unwrapped, thenAction saved as action', async () => {
const file = join(dir, 'read.json')
writeFileSync(file, JSON.stringify({item: {name: 'Paid', thenAction: {type: 'WebhookCall'}, events: ['invoice.paid']}}))
const {body} = await callOf(['trigger', 'save', '--file', file])
expect(body?.trigger).toEqual({
type: 'Payments',
when: 'OnWebhookCallReceived',
name: 'Paid',
action: {type: 'WebhookCall'},
events: ['invoice.paid'],
})
})

it('lets the flags override the file', async () => {
const {body} = await callOf([
'trigger', 'save', '--file', triggerFile({order: 5}), '--id', ID,
Expand Down
Loading