Custom backend for Purrform.
at main 69 lines 2.0 kB view raw
1import fs from 'node:fs/promises'; 2 3// lib 4import supabase from '../lib/supabase'; 5 6interface TraderJson { 7 bc_customer_id: number; 8 first_name: string; 9 last_name: string; 10 email: string; 11 company: string; 12 eligible_for_credit: boolean; 13 credit_amount: number; 14} 15 16export default async function populateCreditSystemTraders(): Promise<boolean> { 17 console.log('Populating credit system traders...'); 18 19 try { 20 const traders = await fs.readFile( 21 './data/trade-customers-for-credit-system.json', 22 'utf8' 23 ); 24 25 const tradersData = JSON.parse(traders) as TraderJson[]; 26 27 for (const trader of tradersData) { 28 if (!trader.eligible_for_credit) { 29 console.log( 30 `Skipping trader ${trader.first_name} ${trader.last_name} as they are not eligible for credit.` 31 ); 32 continue; 33 } 34 35 const { 36 bc_customer_id, 37 first_name, 38 last_name, 39 email, 40 company, 41 credit_amount, 42 } = trader; 43 44 const result = await supabase.from('credit_system_traders').insert([ 45 { 46 bc_customer_id, 47 bc_customer_first_name: first_name, 48 bc_customer_last_name: last_name, 49 bc_customer_email: email, 50 bc_customer_company: company, 51 credit_ceiling: credit_amount, 52 current_balance: credit_amount, 53 }, 54 ]); 55 56 if (result.error) { 57 console.error( 58 `Error adding trader ${first_name} ${last_name}: ${result.error.message}` 59 ); 60 } 61 } 62 63 console.log('Credit system traders populated!'); 64 return true; 65 } catch (error) { 66 console.error(error); 67 return false; 68 } 69}