testnet-laconicd-stack/cli/map-subscribers.ts

111 lines
3.4 KiB
TypeScript

import * as fs from 'fs';
import * as crypto from 'crypto';
import * as path from 'path';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { parse as csvParse } from 'csv-parse';
import * as csvWriter from 'csv-writer';
async function main(): Promise<void> {
const argv = _getArgv();
const participants = readOnboardedParticipants(argv.onboardedJson);
const subscribers = await readSubscribers(argv.subscribersCsv);
processSubscribers(participants, subscribers, argv.output);
}
function readOnboardedParticipants(onboardedJsonPath: string): any {
const jsonData = JSON.parse(fs.readFileSync(onboardedJsonPath, 'utf-8'));
return jsonData.participants;
}
async function readSubscribers(subscribersCsvPath: string): Promise<any> {
const fileContent = fs.readFileSync(subscribersCsvPath, { encoding: 'utf-8' });
const headers = ['subscriber_id', 'email', 'status', 'premium?', 'created_at', 'api_subscription_id'];
return csvParse(fileContent, { delimiter: ',', columns: headers }).toArray();
}
function hashSubscriberId(subscriberId: string): string {
return '0x' + crypto.createHash('sha256').update(subscriberId).digest('hex');
}
async function processSubscribers(participants: any[], subscribers: any[], outputPath: string) {
// Map kyc_id to participant data
const kycMap: Record<string, any> = {};
participants.forEach((participant: any) => {
kycMap[participant.kyc_id] = participant;
});
const onboardedSubscribers: any[] = [];
subscribers.forEach((subscriber) => {
const hashedSubscriberId = hashSubscriberId(subscriber['subscriber_id']);
const participant = kycMap[hashedSubscriberId];
if (!participant) {
return;
}
const onboardedSubscriber = {
subscriber_id: subscriber['subscriber_id'],
email: subscriber['email'],
cosmos_address: participant['cosmos_address'],
nitro_address: participant['nitro_address'],
role: participant['role'],
hashed_subscriber_id: participant['kyc_id'],
status: subscriber['status'],
premium: subscriber['premium?'],
created_at: subscriber['created_at']
};
onboardedSubscribers.push(onboardedSubscriber);
});
const writer = csvWriter.createObjectCsvWriter({
path: path.resolve(outputPath),
header: [
{ id: 'subscriber_id', title: 'subscriber_id' },
{ id: 'email', title: 'email' },
{ id: 'cosmos_address', title: 'cosmos_address' },
{ id: 'nitro_address', title: 'nitro_address' },
{ id: 'role', title: 'role' },
{ id: 'hashed_subscriber_id', title: 'hashed_subscriber_id' },
{ id: 'status', title: 'status' },
{ id: 'premium', title: 'premium' },
{ id: 'created_at', title: 'created_at' }
]
});
await writer.writeRecords(onboardedSubscribers);
console.log(`Data has been written to ${path.resolve(outputPath)}`);
}
function _getArgv (): any {
return yargs(hideBin(process.argv))
.option('onboardedJson', {
alias: 'j',
type: 'string',
demandOption: true,
describe: 'Path to the onboarded JSON file',
})
.option('subscribersCsv', {
alias: 's',
type: 'string',
demandOption: true,
describe: 'Path to the subscribers CSV file',
})
.option('output', {
alias: 'o',
type: 'string',
demandOption: true,
describe: 'Path to the output CSV file',
})
.help()
.argv;
}
main().catch(err => {
console.log(err);
});