incorporate customer data into payment dump

This commit is contained in:
Kiara Grouwstra 2021-10-05 22:37:23 +02:00
parent 7c017ec432
commit 81c7c6f346
1 changed files with 8 additions and 5 deletions

View File

@ -7,23 +7,26 @@ import { mollieKeys, listAll } from './mollie';
async function main() {
const mollie = createMollieClient({ apiKey: mollieKeys.leden });
exportMollieCustomers(mollie);
exportMolliePayments(mollie);
const customers = await exportMollieCustomers(mollie);
const customerObj = Object.fromEntries(customers.map(({ id, name, email, createdAt }) => ([id, { name, email, createdAt }])));
const payments = await exportMolliePayments(mollie, customerObj);
}
async function exportMollieCustomers(mollie: MollieClient) {
async function exportMollieCustomers(mollie: MollieClient): Promise<{[k: string]: any}[]> {
const page$ = mollie.customers.list();
const list = (await listAll(page$, 500))
.map(({ id, name, email, createdAt }) => ({ id, name, email, createdAt }));
writeFileSync('./data/customers.csv', unparse(list));
return list;
}
async function exportMolliePayments(mollie: MollieClient) {
async function exportMolliePayments(mollie: MollieClient, customerObj: { [id: string]: { name: string, email: string, createdAt: string } }): Promise<{[k: string]: any}[]> {
const page$ = mollie.payments.list();
const list = (await listAll(page$, 500))
// const list = Array.from(await page$)
.map(({ id, createdAt, paidAt, description, method, status, isCancelable, sequenceType, profileId, customerId, mandateId, details, amount, settlementAmount, amountRefunded, amountRemaining }) => ({ id, createdAt, paidAt, description, method, status, isCancelable, sequenceType, profileId, customerId, mandateId, ...mapKeys(prepend('details.'))(details), ...mapKeys(prepend('amount.'))(amount), ...mapKeys(prepend('settlementAmount.'))(settlementAmount), ...mapKeys(prepend('amountRefunded.'))(amountRefunded), ...mapKeys(prepend('amountRemaining.'))(amountRemaining) }));
.map(({ resource, id, createdAt, paidAt, description, method, status, isCancelable, sequenceType, profileId, customerId, mandateId, details, amount, settlementAmount, amountRefunded, amountRemaining }) => ({ resource, id, createdAt, paidAt, description, method, status, isCancelable, sequenceType, profileId, customerId, mandateId, ...mapKeys(prepend('details.'))(details), ...mapKeys(prepend('amount.'))(amount), ...mapKeys(prepend('settlementAmount.'))(settlementAmount), ...mapKeys(prepend('amountRefunded.'))(amountRefunded), ...mapKeys(prepend('amountRemaining.'))(amountRemaining), ...mapKeys(prepend('customer.'))(customerObj[customerId || '']) }));
writeFileSync('./data/payments.csv', unparse(list));
return list;
}
main();