Skip to content

Commit

Permalink
Add sql import for splitwise + docker stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Ankit Bahl committed Jan 19, 2024
1 parent f9040f8 commit b69b824
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ RUN apt update && \
apt autoclean && \
apt autoremove && \
npm install --ignore-scripts && \
npm install -g prisma
npm run build

ENTRYPOINT ["/bin/bash", "-c", "scripts/image-startup.sh"]
CMD ["npm", "run", "start"]
62 changes: 62 additions & 0 deletions generateSQLImport.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { nanoid } from 'nanoid';
import csv from 'csv-parser';
import fs from 'fs';
const results = [];

const csv_file = 'ankit-b-and-ankita_2024-01-10_export.csv'
fs.createReadStream(csv_file)
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', () => {
generate(results);
// [
// { NAME: 'Daffy Duck', AGE: '24' },
// { NAME: 'Bugs Bunny', AGE: '22' }
// ]
});
function generate(results) {
const out = [];
const ankitId = '_UDudHPzVb2dAzgL_-JzC';
const ankitaId = 'xvRgcH6r4ep7YzF09KSA6';
const groupId = 'mV9UrER7TP3yTQJa1tNAA';
results.forEach(expense => {
const expenses = [];
if (expense.Currency !== 'USD') {
return;
}

if (expense['Ankit Bahl'] === '0.00' && expense['Ankita'] === '0.00') {
return;
}
const paidBy = expense['Ankit Bahl'] > 0 ? ankitId : ankitaId;
const isReimbursement = expense.Category === 'Payment' ? "TRUE" : "FALSE";
let splitMode = 'EVENLY';
const cost = parseInt(expense.Cost.replace('.',''));
const expenseId = nanoid();
if (Math.round(cost / 2) === Math.abs(parseInt(expense['Ankit Bahl'].replace('.','')))) {
expenses.push(`INSERT INTO "ExpensePaidFor"("expenseId", "participantId", "shares") VALUES ('${expenseId}', '${ankitId}', 100)`)
expenses.push(`INSERT INTO "ExpensePaidFor"("expenseId", "participantId", "shares") VALUES ('${expenseId}', '${ankitaId}', 100)`)
} else {
if (isReimbursement === 'TRUE') {
const participant = expense['Ankit Bahl'] > 0 ? ankitaId : ankitId;
expenses.push(`INSERT INTO "ExpensePaidFor"("expenseId", "participantId", "shares") VALUES ('${expenseId}', '${participant}', 1)`)
} else {
splitMode = 'BY_AMOUNT';
// subtract cost from abs of expense + who is paying vs participant
if (paidBy === ankitId) {
const ankitOwed = parseInt(expense['Ankit Bahl'].replace('.',''));
expenses.push(`INSERT INTO "ExpensePaidFor"("expenseId", "participantId", "shares") VALUES ('${expenseId}', '${ankitId}', ${cost - ankitOwed})`);
expenses.push(`INSERT INTO "ExpensePaidFor"("expenseId", "participantId", "shares") VALUES ('${expenseId}', '${ankitaId}', ${ankitOwed})`);
} else {
const ankitaOwed = parseInt(expense['Ankita'].replace('.',''));
expenses.push(`INSERT INTO "ExpensePaidFor"("expenseId", "participantId", "shares") VALUES ('${expenseId}', '${ankitId}', ${ankitaOwed})`);
expenses.push(`INSERT INTO "ExpensePaidFor"("expenseId", "participantId", "shares") VALUES ('${expenseId}', '${ankitaId}', ${cost - ankitaOwed})`);
}
}
}
out.push(`INSERT INTO "Expense"(id, title, amount, "paidById", "groupId", "isReimbursement", "splitMode", "expenseDate") VALUES
('${expenseId}','${expense.Description.replace(/'/g,"''")}',${cost},'${paidBy}','${groupId}','${isReimbursement}','${splitMode}','${expense.Date}')`.replace(/\n/g, ''));
out.push(...expenses);
});
console.log(out.join(";\n"));
}
16 changes: 15 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"clsx": "^2.0.0",
"cmdk": "^0.2.0",
"dayjs": "^1.11.10",
"csv-parser": "^3.0.0",
"lucide-react": "^0.290.0",
"nanoid": "^5.0.4",
"next": "^14.0.4",
Expand Down

0 comments on commit b69b824

Please # to comment.