53 lines
1.1 KiB
TypeScript
53 lines
1.1 KiB
TypeScript
/**
|
|
* CSV export for referral clawback audit records.
|
|
*/
|
|
|
|
import * as referralRepository from '../repositories/referralRepository';
|
|
|
|
function csvEscape(value: string): string {
|
|
if (/[",\n\r]/.test(value)) {
|
|
return `"${value.replace(/"/g, '""')}"`;
|
|
}
|
|
return value;
|
|
}
|
|
|
|
function row(values: string[]): string {
|
|
return values.map(csvEscape).join(',');
|
|
}
|
|
|
|
export async function buildClawbacksCsv(opts?: { from?: Date; to?: Date }): Promise<string> {
|
|
const records = await referralRepository.listClawbacksForExport(opts);
|
|
|
|
const header = row([
|
|
'clawback_id',
|
|
'payout_id',
|
|
'beneficiary_user_id',
|
|
'referred_user_id',
|
|
'level_number',
|
|
'payout_commission_amount_raw',
|
|
'clawback_amount_raw',
|
|
'payout_status',
|
|
'reason',
|
|
'clawed_by',
|
|
'created_at',
|
|
]);
|
|
|
|
const lines = records.map((r) =>
|
|
row([
|
|
r.id,
|
|
r.payout_id,
|
|
r.beneficiary_user_id,
|
|
r.referred_user_id,
|
|
String(r.level_number),
|
|
r.payout_commission_amount,
|
|
r.amount,
|
|
r.payout_status,
|
|
r.reason,
|
|
r.clawed_by,
|
|
r.created_at.toISOString(),
|
|
])
|
|
);
|
|
|
|
return [header, ...lines].join('\n');
|
|
}
|