-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
152 lines (131 loc) · 6.71 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const authentication = require('./authentication');
const invoiceCreated = require('./triggers/InvoiceCreated.js');
const invoiceExpired = require('./triggers/InvoiceExpired.js');
const invoiceInvalid = require('./triggers/InvoiceInvalid.js');
const invoiceProcessing = require('./triggers/InvoiceProcessing.js');
const invoiceSettled = require('./triggers/InvoiceSettled.js');
const invoicePaymentSettled = require('./triggers/InvoicePaymentSettled.js');
const paymentReceived = require('./triggers/InvoiceReceivedPayment.js');
const createInvoice = require("./creates/CreateInvoice");
const createUser = require("./creates/CreateUser");
const markInvoiceInvalid = require("./creates/MarkInvoiceInvalid");
const markInvoiceSettled = require("./creates/MarkInvoiceSettled");
const generateStoreOnChainWalletAddress = require("./creates/GenerateStoreOnChainWalletAddress");
const generateStoreLightningWalletAddress = require("./creates/GenerateStoreLightningWalletAddress");
const createStoreOnChainWalletTransaction = require("./creates/CreateStoreOnChainWalletTransaction");
const getStoreOnChainBalance = require("./creates/GetStoreOnChainBalance");
const getStoreLightningNodeUri = require("./creates/GetStoreLightningNodeUri");
const openStoreLightningChannel = require("./creates/OpenStoreLightningChannel");
const payLightningInvoice = require("./creates/PayLightningInvoice");
const createPaymentRequest = require("./creates/CreatePaymentRequest");
const custodianAccountDeposit = require("./creates/CustodianAccount/Deposit");
const custodianAccountGetAssetBalance = require("./creates/CustodianAccount/GetAssetBalance");
const custodianAccountMarketTrade = require("./creates/CustodianAccount/MarketTrade");
const custodianAccountWithdrawToStore = require("./creates/CustodianAccount/WithdrawToStore");
const findInvoice = require("./searches/FindInvoice");
const findStore = require("./searches/FindStore");
const findCustodianAccount = require("./searches/FindCustodianAccount");
const Stores = require("./common/Store");
// TODO add deliveryId to all triggers
// TODO add webhookId to all triggers
// TODO add originalDeliveryId to all triggers
// TODO add isRedelivery to all triggers
const beforeRequest = (request, z, bundle) => {
request.headers['Content-Type'] = 'application/json';
request.headers['Accept'] = 'application/json';
request.headers['Authorization'] = 'token ' + bundle.authData.api_key;
request.headers['X-Client'] = 'Zapier ' + require('./package.json').version;
// Avoid double // in the URL if the user entered the server_url with a trailing slash.
if (bundle.authData.server_url.endsWith('/')) {
request.url = request.url.replace(bundle.authData.server_url + '/', bundle.authData.server_url);
}
return request;
};
const afterResponse = (response, z) => {
// TODO refactor error handling for all calls! Start using "HaltedError" and "ExpiredAuthError" instead of always using "Error", which turns off the zap entirely!!
// Error - Stops the current operation, allowing for (auto) replay. Read more on General Errors
// HaltedError - Stops current operation, but will never turn off Zap. Read more on Halting Execution
// ExpiredAuthError - Turns off Zap and emails user to manually reconnect. Read more on Stale Authentication Credentials
if (response.status === 403 && 'missing-permission' === response.json.code) {
throw new z.errors.ExpiredAuthError(response.json.message);
}
return response;
};
module.exports = {
version: require('./package.json').version,
platformVersion: require('zapier-platform-core').version,
authentication: authentication,
beforeRequest: [beforeRequest],
afterResponse: [afterResponse],
resources: {
store: {
key: 'store',
noun: 'Store',
list: {
display: {
label: 'Stores',
description: 'A list of all stores',
hidden: true,
},
operation: {
perform: (z, bundle) => {
// This is called to populate tthe store_id dropdown
const Stores = require('./common/Store.js');
return Stores.getAll(z, bundle);
},
},
},
},
custodian_account: {
key: 'custodian_account',
noun: 'Custodian Account',
list: {
display: {
label: 'Custodian Accounts',
description: 'A list of all custodian accounts',
hidden: true,
},
operation: {
perform: (z, bundle) => {
// This is called to populate the custodian_account_id dropdown
// TODO listing does not work. Probably because this list depends on the store_id which we cannot access. Read the docs about d
const CustodianAccount = require('./common/CustodianAccount.js');
return CustodianAccount.getAll(z, bundle);
},
},
},
},
},
triggers: {
[invoiceCreated.key]: invoiceCreated,
[paymentReceived.key]: paymentReceived,
[invoiceProcessing.key]: invoiceProcessing,
[invoiceSettled.key]: invoiceSettled,
[invoicePaymentSettled.key]: invoicePaymentSettled,
[invoiceExpired.key]: invoiceExpired,
[invoiceInvalid.key]: invoiceInvalid,
},
creates: {
[createInvoice.key]: createInvoice,
[createUser.key]: createUser,
[markInvoiceInvalid.key]: markInvoiceInvalid,
[markInvoiceSettled.key]: markInvoiceSettled,
[generateStoreOnChainWalletAddress.key]: generateStoreOnChainWalletAddress,
[generateStoreLightningWalletAddress.key]: generateStoreLightningWalletAddress,
[createStoreOnChainWalletTransaction.key]: createStoreOnChainWalletTransaction,
[getStoreOnChainBalance.key]: getStoreOnChainBalance,
[getStoreLightningNodeUri.key]: getStoreLightningNodeUri,
[openStoreLightningChannel.key]: openStoreLightningChannel,
[payLightningInvoice.key]: payLightningInvoice,
[createPaymentRequest.key]: createPaymentRequest,
[custodianAccountDeposit.key]: custodianAccountDeposit,
[custodianAccountGetAssetBalance.key]: custodianAccountGetAssetBalance,
[custodianAccountMarketTrade.key]: custodianAccountMarketTrade,
[custodianAccountWithdrawToStore.key]: custodianAccountWithdrawToStore
},
searches: {
[findInvoice.key]: findInvoice,
[findStore.key]: findStore,
[findCustodianAccount.key]: findCustodianAccount
}
};