-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This trigger fires when a new invoice in created.
- Loading branch information
Showing
11 changed files
with
426 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import { createAppTester } from "zapier-platform-core"; | ||
import * as nock from "nock"; | ||
import App from "../../index"; | ||
import { | ||
prepareBundle, | ||
prepareMocksForWebhookSubscribeTest, | ||
} from "../utils/prepareMocksForWebhookSubscribeTest"; | ||
import triggerInvoiceCreated from "../../triggers/triggerInvoiceCreated"; | ||
import { HookTrigger } from "../../types/trigger"; | ||
import { | ||
UserApiResponse, | ||
InvoiceApiResponse, | ||
BaseInvoiceProperties, | ||
} from "../../types/api-responses"; | ||
|
||
const attributes: BaseInvoiceProperties = { | ||
invoiceDate: "2024-12-20T06:22:29+01:00", | ||
paidStatus: "paid", | ||
dueDate: "2024-12-30T08:22:29+01:00", | ||
number: "1", | ||
sentStatus: "sent", | ||
taxIdName: "taxIdName", | ||
canCharge: true, | ||
recipientAddress: { | ||
company: "company", | ||
name: "name", | ||
fullAddress: "fullAddress", | ||
}, | ||
senderAddress: { | ||
company: "company", | ||
name: "name", | ||
fullAddress: "fullAddress", | ||
}, | ||
items: [ | ||
{ | ||
description: "item 1", | ||
quantity: "1", | ||
paid: true, | ||
accountingCode: null, | ||
amount: { | ||
net: "100", | ||
gross: "100", | ||
currency: "EUR", | ||
taxes: [], | ||
}, | ||
totalAmount: { | ||
net: "100", | ||
gross: "100", | ||
currency: "EUR", | ||
taxes: [], | ||
}, | ||
}, | ||
], | ||
payableAmount: "100", | ||
paidAmount: "100", | ||
totalAmount: { | ||
net: "100", | ||
gross: "100", | ||
currency: "EUR", | ||
taxes: [], | ||
}, | ||
invoiceText: "invoiceText", | ||
paidDate: "2024-12-22T06:22:29+01:00", | ||
taxId: null, | ||
chargeAt: null, | ||
customerNumber: null, | ||
notes: null, | ||
}; | ||
|
||
const invoiceResponse: InvoiceApiResponse = { | ||
id: "1", | ||
attributes, | ||
relationships: { | ||
membership: { | ||
data: { | ||
id: "membership-1", | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const appTester = createAppTester(App); | ||
nock.disableNetConnect(); | ||
const trigger = App.triggers[triggerInvoiceCreated.key] as HookTrigger; | ||
|
||
afterEach(() => nock.cleanAll()); | ||
|
||
describe("triggerInvoiceCreated", () => { | ||
it("creates new webhook through CM API upon subscribe", async () => { | ||
const bundle = prepareMocksForWebhookSubscribeTest( | ||
triggerInvoiceCreated.key, | ||
); | ||
const subscribe = trigger.operation.performSubscribe; | ||
|
||
const result = await appTester(subscribe as any, bundle as any); | ||
|
||
expect(result).toMatchInlineSnapshot(` | ||
{ | ||
"url": "https://trial.cobot.me/api/event/callback", | ||
} | ||
`); | ||
}); | ||
|
||
it("lists recent invoices", async () => { | ||
const bundle = prepareBundle(); | ||
const userResponse: UserApiResponse = { | ||
included: [{ id: "space-1", attributes: { subdomain: "trial" } }], | ||
}; | ||
const scope = nock("https://api.cobot.me"); | ||
scope.get("/user?include=adminOf").reply(200, userResponse); | ||
scope | ||
.get(/\/spaces\/space-1\/invoices/) | ||
.reply(200, { data: [invoiceResponse] }); | ||
|
||
const listRecentEvents = trigger.operation.performList; | ||
|
||
const results = await appTester(listRecentEvents as any, bundle as any); | ||
|
||
expect(results).toStrictEqual([ | ||
{ | ||
...attributes, | ||
id: "1", | ||
membershipId: "membership-1", | ||
}, | ||
]); | ||
}); | ||
|
||
it("triggers on new invoice", async () => { | ||
const bundle = prepareBundle(); | ||
const scope = nock("https://api.cobot.me"); | ||
scope.get("/invoices/12345").reply(200, { data: invoiceResponse }); | ||
|
||
const results = await appTester( | ||
triggerInvoiceCreated.operation.perform as any, | ||
bundle as any, | ||
); | ||
|
||
expect(nock.isDone()).toBe(true); | ||
|
||
expect(results).toStrictEqual([ | ||
{ | ||
...attributes, | ||
id: "1", | ||
membershipId: "membership-1", | ||
}, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { ZObject } from "zapier-platform-core"; | ||
import { KontentBundle } from "../types/kontentBundle"; | ||
import { | ||
listRecentInvoices, | ||
subscribeHook, | ||
unsubscribeHook, | ||
getInvoiceFromApi2, | ||
} from "../utils/api"; | ||
import { SubscribeBundleInputType } from "../types/subscribeType"; | ||
import { getSubdomainField } from "../fields/getSudomainsField"; | ||
import { invoiceSample } from "../utils/samples"; | ||
import { InvoiceApiResponse } from "../types/api-responses"; | ||
import { InvoiceOutput } from "../types/outputs"; | ||
import { HookTrigger } from "../types/trigger"; | ||
import { apiResponseToInvoiceOutput } from "../utils/api-to-output"; | ||
|
||
const hookLabel = "Invoice Created"; | ||
const event = "created_invoice"; | ||
|
||
async function subscribeHookExecute( | ||
z: ZObject, | ||
bundle: KontentBundle<SubscribeBundleInputType>, | ||
) { | ||
return subscribeHook(z, bundle, { | ||
event, | ||
callback_url: bundle.targetUrl ?? "", | ||
}); | ||
} | ||
|
||
async function unsubscribeHookExecute( | ||
z: ZObject, | ||
bundle: KontentBundle<SubscribeBundleInputType>, | ||
) { | ||
const webhook = bundle.subscribeData; | ||
return unsubscribeHook(z, bundle, webhook?.id ?? ""); | ||
} | ||
|
||
async function parsePayload( | ||
z: ZObject, | ||
bundle: KontentBundle<{}>, | ||
): Promise<InvoiceOutput[]> { | ||
const invoiceId = bundle.cleanedRequest.url.split("/").pop(); | ||
const response = await getInvoiceFromApi2(z, invoiceId); | ||
if (response) { | ||
return [apiResponseToInvoiceOutput(response)]; | ||
} else { | ||
return []; | ||
} | ||
} | ||
|
||
const trigger: HookTrigger = { | ||
key: `${event}`, | ||
noun: hookLabel, | ||
display: { | ||
label: hookLabel, | ||
description: "Triggers when an invoice is created.", | ||
}, | ||
operation: { | ||
type: "hook", | ||
|
||
inputFields: [getSubdomainField()], | ||
|
||
performSubscribe: subscribeHookExecute, | ||
performUnsubscribe: unsubscribeHookExecute, | ||
|
||
perform: parsePayload, | ||
performList: async ( | ||
z: ZObject, | ||
bundle: KontentBundle<SubscribeBundleInputType>, | ||
): Promise<InvoiceOutput[]> => { | ||
const invoices = await listRecentInvoices(z, bundle); | ||
return invoices.map((invoice) => apiResponseToInvoiceOutput(invoice)); | ||
}, | ||
|
||
sample: invoiceSample, | ||
}, | ||
}; | ||
export default trigger; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.