import * as w from "wretched"; import Table from "cli-table3"; import { getOneInvoice, markInvoicePaid, updateInvoice, } from "../services/pancakeApi.js"; import { doubleBoxWithTitle } from "./helpers/boxBorders.js"; import { isNaN, omit, values } from "lodash-es"; import { fromUnixTime, format } from "date-fns"; import { InvoiceDetailsResponse } from "../services/invoiceResponse.js"; export const renderInvoiceDetails = async (invoiceId: string | number) => { const { invoice } = await getOneInvoice(invoiceId.toString()); if (process.env.DEBUG) { console.log("🚀 ~ renderInvoiceDetails ~ details:", invoice); } const mainWindow = new w.Scrollable({ children: [], }); const mainTable = new Table(); for (const key in omit(invoice, "items", "partial_payments")) { const rawValue = invoice[key as keyof InvoiceDetailsResponse]; if (typeof rawValue !== "object") { let value: Table.Cell = rawValue; if (typeof rawValue !== "boolean") { if (key.indexOf("date") >= 0) { value = Number(rawValue); if (!isNaN(value)) { value = format(fromUnixTime(value), "y-M-d"); } } } mainTable.push({ [key]: value, } as Table.VerticalTableRow); } } mainWindow.addAll([ new w.Flow({ direction: "topToBottom", children: [ new w.Text({ text: `Unpaid: $${invoice.unpaid_amount}` }), new w.Button({ text: `Mark as ${invoice.unpaid_amount > 0 ? "Paid" : "Unpaid"}`, onClick: async () => { const isPaid = invoice.unpaid_amount > 0; if (process.env.DEBUG) { console.log("Switching paid status from", { isPaid, invoiceId, }); } if (isPaid) { mainWindow.removeAllChildren(); mainWindow.addAll([ new w.Flow({ direction: "leftToRight", children: [ new w.Text({ text: "Paid Amount" }), new w.Input({ text: "Paid Amount" }), ], }), ]); } else { if (!process.env.DEBUG) { await markInvoicePaid(invoiceId); } } }, }), new w.Text({ text: mainTable.toString() }), ], }), ]); return new w.Box({ border: doubleBoxWithTitle( `${invoice.client_name} 💸 Invoice: ${invoice.id}`, ), child: mainWindow, }); };