-
-
Notifications
You must be signed in to change notification settings - Fork 260
/
privacy-resync-card.js
executable file
·57 lines (50 loc) · 1.94 KB
/
privacy-resync-card.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
#!/usr/bin/env ./node_modules/.bin/babel-node
/* eslint-disable camelcase */
import '../server/env';
import moment from 'moment';
import { Service as ConnectedAccountServices } from '../server/constants/connected_account';
import * as privacyLib from '../server/lib/privacy';
import models, { sequelize } from '../server/models';
import privacy from '../server/paymentProviders/privacy';
const run = async cardId => {
const card = await models.VirtualCard.findByPk(cardId, { include: ['host'] });
const connectedAccount = await models.ConnectedAccount.findOne({
where: { service: ConnectedAccountServices.PRIVACY, CollectiveId: card.HostCollectiveId },
});
const expenses = await models.Expense.findAll({ where: { VirtualCardId: card.id } });
const { data: privacyTransaction } = await privacyLib.listTransactions(
connectedAccount.token,
card.id,
{
begin: moment(process.env.BEGIN || card.createdAt).toISOString(),
// eslint-disable-next-line camelcase
page_size: 500,
},
'approvals',
);
for (const pt of privacyTransaction) {
const expense = expenses.find(c => c.data.token === pt.token);
if (expense) {
console.log(`Charge ${pt.token} from ${pt.created} synced as expense ${expense.id}`);
} else {
const transaction = await models.Transaction.findOne({ where: { data: { token: pt.token } } });
if (transaction) {
console.log(`Charge ${pt.token} from ${pt.created} synced in transaction ${transaction.id}`);
} else {
console.log(`Charge ${pt.token} from ${pt.created} not synced`);
console.log(pt);
await privacy.processTransaction(pt);
}
}
}
await sequelize.close();
process.exit(0);
};
if (!module.parent) {
if (process.argv.length < 3 || process.argv[2] === 'help') {
console.log('\nUsage:\n npm run script ./scripts/privacy-resync-card.js cardId\n');
process.exit(0);
}
const [, , cardId] = process.argv;
run(cardId);
}