Custom backend for Purrform.
1// packages
2import { writeFile } from 'node:fs/promises';
3
4// helpers
5import getAllBcCustomerAttributeValues from '../helpers/getAllBcCustomerAttributeValues';
6import getAllBcCustomersByCustomerGroup from '../helpers/getAllBcCustomersByCustomerGroup';
7
8interface Ledger {
9 customerId: number;
10 customerName: string;
11 customerEmail: string;
12 loyaltyPoints: string;
13}
14
15export default async function loyaltyPointsLedger(): Promise<void> {
16 console.log('Starting loyalty points ledger');
17
18 const ledger: Ledger[] = [];
19
20 const personalCustomers =
21 await getAllBcCustomersByCustomerGroup('personal');
22
23 for (const personalCustomer of personalCustomers) {
24 let currentLoyaltyPoints = await getAllBcCustomerAttributeValues(
25 1,
26 personalCustomer.id
27 );
28 if (currentLoyaltyPoints === null) {
29 currentLoyaltyPoints = '0';
30 }
31
32 ledger.push({
33 customerId: personalCustomer.id,
34 customerName: `${personalCustomer.first_name} ${personalCustomer.last_name}`,
35 customerEmail: personalCustomer.email,
36 loyaltyPoints: currentLoyaltyPoints,
37 });
38 }
39
40 console.log('Writing ledger to file');
41 await writeFile(
42 'data/loyalty-points-ledger.json',
43 JSON.stringify(ledger, null, 4)
44 );
45}