41 lines · 1331 bytes
sha256 358ec071a66dee4539f1 hour ago
import { processPayment } from 'fast-pay-utils';
import { Payment } from './types';
import { maskPan, formatAmount, validatePan } from './payment-utils';
/**
* Export a list of payments as CSV.
* Each payment is run through processPayment() before being serialised.
* Columns: id,merchantId,amount,currency,maskedPan,status
*/
export function exportPayouts(payments: Payment[]): string {
const header = 'id,merchantId,amount,currency,maskedPan,status';
const rows = payments.filter((p) => validatePan(p.pan)).map((p) => {
const processed = processPayment(p) as Payment;
return [
processed.id,
processed.merchantId,
formatAmount(processed.amountMinor, processed.currency),
processed.currency,
maskPan(processed.pan),
processed.status,
].join(',');
});
return [header, ...rows].join('\n');
}
/**
* Schedule a recurring export.
* Every `intervalMs` milliseconds the `sink` function is called with the
* CSV produced by exportPayouts for the given payments list.
* Returns a cleanup function that cancels the interval.
*/
export function scheduleExport(
intervalMs: number,
payments: Payment[],
sink: (csv: string) => void,
): () => void {
const id = setInterval(() => {
sink(exportPayouts(payments));
}, intervalMs);
return () => clearInterval(id);
}