added base examples and drills

This commit is contained in:
2026-05-20 17:42:01 -06:00
parent 9bf788d9ef
commit 4fc0d851e7
40 changed files with 802 additions and 1 deletions
+3
View File
@@ -0,0 +1,3 @@
# billing original
Messy invoice finalization logic mixing pricing rules, time, logging, mutation, and file I/O.
+39
View File
@@ -0,0 +1,39 @@
const fs = require("fs");
function finalizeInvoice(filePath, invoiceId, customerType) {
const raw = fs.readFileSync(filePath, "utf8");
const state = JSON.parse(raw);
const invoice = state.invoices.find((item) => item.id === invoiceId);
if (!invoice) {
throw new Error("Invoice not found");
}
let subtotal = 0;
for (const line of invoice.lines) {
subtotal = subtotal + line.quantity * line.unitPrice;
}
let discount = 0;
if (customerType === "vip") {
discount = subtotal * 0.15;
}
if (subtotal > 200) {
discount = discount + 10;
}
const taxed = (subtotal - discount) * 1.1;
invoice.status = "finalized";
invoice.total = Math.round(taxed * 100) / 100;
invoice.finalizedAt = new Date().toISOString();
fs.writeFileSync(filePath, JSON.stringify(state, null, 2));
console.log("Invoice finalized", invoiceId);
return invoice;
}
module.exports = { finalizeInvoice };
+14
View File
@@ -0,0 +1,14 @@
{
"invoices": [
{
"id": "inv-1",
"status": "draft",
"total": 0,
"finalizedAt": null,
"lines": [
{ "description": "Hosting", "quantity": 2, "unitPrice": 60 },
{ "description": "Support", "quantity": 1, "unitPrice": 95 }
]
}
]
}