This commit is contained in:
2026-05-12 17:35:38 +03:00
parent d9ffcb4b92
commit e3f3e62482
51 changed files with 882 additions and 3413 deletions
+33 -32
View File
@@ -1,15 +1,16 @@
import postgres from 'postgres';
import {
import postgres from "postgres";
import type {
CustomerField,
CustomersTable,
InvoiceForm,
InvoicesTable,
LatestInvoiceRaw,
User,
Revenue,
} from './definitions';
import { formatCurrency } from './utils';
const sql = postgres(process.env.POSTGRES_URL!, { ssl: 'require' });
User,
} from "./definitions";
import { formatCurrency } from "./utils";
const sql = postgres(process.env.POSTGRES_URL!, { ssl: "require" });
export async function fetchRevenue() {
// Add noStore() here prevent the response from being cached.
// This is equivalent to in fetch(..., {cache: 'no-store'}).
@@ -18,17 +19,17 @@ export async function fetchRevenue() {
// Artificially delay a reponse for demo purposes.
// Don't do this in real life :)
console.log('Fetching revenue data...');
await new Promise((resolve) => setTimeout(resolve, 3000));
console.log("Fetching revenue data...");
await new Promise((resolve) => setTimeout(resolve, 3000));
const data = sql<Revenue[]>`SELECT * FROM revenue`;
const data = sql<Revenue[]>`SELECT * FROM revenue`;
console.log('Data fetch complete after 3 seconds.');
console.log("Data fetch complete after 3 seconds.");
return data;
} catch (error) {
console.error('Database Error:', error);
throw new Error('Failed to fetch revenue data.');
console.error("Database Error:", error);
throw new Error("Failed to fetch revenue data.");
}
}
@@ -47,8 +48,8 @@ export async function fetchLatestInvoices() {
}));
return latestInvoices;
} catch (error) {
console.error('Database Error:', error);
throw new Error('Failed to fetch the latest invoices.');
console.error("Database Error:", error);
throw new Error("Failed to fetch the latest invoices.");
}
}
@@ -70,10 +71,10 @@ export async function fetchCardData() {
invoiceStatusPromise,
]);
const numberOfInvoices = Number(data[0][0].count ?? '0');
const numberOfCustomers = Number(data[1][0].count ?? '0');
const totalPaidInvoices = formatCurrency(data[2][0].paid ?? '0');
const totalPendingInvoices = formatCurrency(data[2][0].pending ?? '0');
const numberOfInvoices = Number(data[0][0].count ?? "0");
const numberOfCustomers = Number(data[1][0].count ?? "0");
const totalPaidInvoices = formatCurrency(data[2][0].paid ?? "0");
const totalPendingInvoices = formatCurrency(data[2][0].pending ?? "0");
return {
numberOfCustomers,
@@ -82,8 +83,8 @@ export async function fetchCardData() {
totalPendingInvoices,
};
} catch (error) {
console.error('Database Error:', error);
throw new Error('Failed to card data.');
console.error("Database Error:", error);
throw new Error("Failed to card data.");
}
}
@@ -118,8 +119,8 @@ export async function fetchFilteredInvoices(
return invoices;
} catch (error) {
console.error('Database Error:', error);
throw new Error('Failed to fetch invoices.');
console.error("Database Error:", error);
throw new Error("Failed to fetch invoices.");
}
}
@@ -139,8 +140,8 @@ export async function fetchInvoicesPages(query: string) {
const totalPages = Math.ceil(Number(count[0].count) / ITEMS_PER_PAGE);
return totalPages;
} catch (error) {
console.error('Database Error:', error);
throw new Error('Failed to fetch total number of invoices.');
console.error("Database Error:", error);
throw new Error("Failed to fetch total number of invoices.");
}
}
@@ -165,7 +166,7 @@ export async function fetchInvoiceById(id: string) {
console.log(invoice); // Invoice is an empty array []
return invoice[0];
} catch (error) {
console.error('Database Error:', error);
console.error("Database Error:", error);
}
}
@@ -182,8 +183,8 @@ export async function fetchCustomers() {
const customers = data;
return customers;
} catch (err) {
console.error('Database Error:', err);
throw new Error('Failed to fetch all customers.');
console.error("Database Error:", err);
throw new Error("Failed to fetch all customers.");
}
}
@@ -215,8 +216,8 @@ export async function fetchFilteredCustomers(query: string) {
return customers;
} catch (err) {
console.error('Database Error:', err);
throw new Error('Failed to fetch customer table.');
console.error("Database Error:", err);
throw new Error("Failed to fetch customer table.");
}
}
@@ -225,7 +226,7 @@ export async function getUser(email: string) {
const user = await sql`SELECT * from USERS where email=${email}`;
return user[0] as User;
} catch (error) {
console.error('Failed to fetch user:', error);
throw new Error('Failed to fetch user.');
console.error("Failed to fetch user:", error);
throw new Error("Failed to fetch user.");
}
}
}