Le1
This commit is contained in:
+20
-20
@@ -1,4 +1,4 @@
|
|||||||
import { sql } from '@neondatabase/serverless';
|
import postgres from 'postgres';
|
||||||
import {
|
import {
|
||||||
CustomerField,
|
CustomerField,
|
||||||
CustomersTable,
|
CustomersTable,
|
||||||
@@ -9,7 +9,7 @@ import {
|
|||||||
Revenue,
|
Revenue,
|
||||||
} from './definitions';
|
} from './definitions';
|
||||||
import { formatCurrency } from './utils';
|
import { formatCurrency } from './utils';
|
||||||
|
const sql = postgres(process.env.POSTGRES_URL!, { ssl: 'require' });
|
||||||
export async function fetchRevenue() {
|
export async function fetchRevenue() {
|
||||||
// Add noStore() here prevent the response from being cached.
|
// Add noStore() here prevent the response from being cached.
|
||||||
// This is equivalent to in fetch(..., {cache: 'no-store'}).
|
// This is equivalent to in fetch(..., {cache: 'no-store'}).
|
||||||
@@ -21,11 +21,11 @@ export async function fetchRevenue() {
|
|||||||
// console.log('Fetching revenue data...');
|
// console.log('Fetching revenue data...');
|
||||||
// await new Promise((resolve) => setTimeout(resolve, 3000));
|
// await new Promise((resolve) => setTimeout(resolve, 3000));
|
||||||
|
|
||||||
const data = await 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.rows;
|
return data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Database Error:', error);
|
console.error('Database Error:', error);
|
||||||
throw new Error('Failed to fetch revenue data.');
|
throw new Error('Failed to fetch revenue data.');
|
||||||
@@ -34,14 +34,14 @@ export async function fetchRevenue() {
|
|||||||
|
|
||||||
export async function fetchLatestInvoices() {
|
export async function fetchLatestInvoices() {
|
||||||
try {
|
try {
|
||||||
const data = await sql<LatestInvoiceRaw>`
|
const data = await sql<LatestInvoiceRaw[]>`
|
||||||
SELECT invoices.amount, customers.name, customers.image_url, customers.email, invoices.id
|
SELECT invoices.amount, customers.name, customers.image_url, customers.email, invoices.id
|
||||||
FROM invoices
|
FROM invoices
|
||||||
JOIN customers ON invoices.customer_id = customers.id
|
JOIN customers ON invoices.customer_id = customers.id
|
||||||
ORDER BY invoices.date DESC
|
ORDER BY invoices.date DESC
|
||||||
LIMIT 5`;
|
LIMIT 5`;
|
||||||
|
|
||||||
const latestInvoices = data.rows.map((invoice) => ({
|
const latestInvoices = data.map((invoice) => ({
|
||||||
...invoice,
|
...invoice,
|
||||||
amount: formatCurrency(invoice.amount),
|
amount: formatCurrency(invoice.amount),
|
||||||
}));
|
}));
|
||||||
@@ -70,10 +70,10 @@ export async function fetchCardData() {
|
|||||||
invoiceStatusPromise,
|
invoiceStatusPromise,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const numberOfInvoices = Number(data[0].rows[0].count ?? '0');
|
const numberOfInvoices = Number(data[0][0].count ?? '0');
|
||||||
const numberOfCustomers = Number(data[1].rows[0].count ?? '0');
|
const numberOfCustomers = Number(data[1][0].count ?? '0');
|
||||||
const totalPaidInvoices = formatCurrency(data[2].rows[0].paid ?? '0');
|
const totalPaidInvoices = formatCurrency(data[2][0].paid ?? '0');
|
||||||
const totalPendingInvoices = formatCurrency(data[2].rows[0].pending ?? '0');
|
const totalPendingInvoices = formatCurrency(data[2][0].pending ?? '0');
|
||||||
|
|
||||||
return {
|
return {
|
||||||
numberOfCustomers,
|
numberOfCustomers,
|
||||||
@@ -95,7 +95,7 @@ export async function fetchFilteredInvoices(
|
|||||||
const offset = (currentPage - 1) * ITEMS_PER_PAGE;
|
const offset = (currentPage - 1) * ITEMS_PER_PAGE;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const invoices = await sql<InvoicesTable>`
|
const invoices = await sql<InvoicesTable[]>`
|
||||||
SELECT
|
SELECT
|
||||||
invoices.id,
|
invoices.id,
|
||||||
invoices.amount,
|
invoices.amount,
|
||||||
@@ -116,7 +116,7 @@ export async function fetchFilteredInvoices(
|
|||||||
LIMIT ${ITEMS_PER_PAGE} OFFSET ${offset}
|
LIMIT ${ITEMS_PER_PAGE} OFFSET ${offset}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
return invoices.rows;
|
return invoices;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Database Error:', error);
|
console.error('Database Error:', error);
|
||||||
throw new Error('Failed to fetch invoices.');
|
throw new Error('Failed to fetch invoices.');
|
||||||
@@ -136,7 +136,7 @@ export async function fetchInvoicesPages(query: string) {
|
|||||||
invoices.status ILIKE ${`%${query}%`}
|
invoices.status ILIKE ${`%${query}%`}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const totalPages = Math.ceil(Number(count.rows[0].count) / ITEMS_PER_PAGE);
|
const totalPages = Math.ceil(Number(count[0].count) / ITEMS_PER_PAGE);
|
||||||
return totalPages;
|
return totalPages;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Database Error:', error);
|
console.error('Database Error:', error);
|
||||||
@@ -146,7 +146,7 @@ export async function fetchInvoicesPages(query: string) {
|
|||||||
|
|
||||||
export async function fetchInvoiceById(id: string) {
|
export async function fetchInvoiceById(id: string) {
|
||||||
try {
|
try {
|
||||||
const data = await sql<InvoiceForm>`
|
const data = await sql<InvoiceForm[]>`
|
||||||
SELECT
|
SELECT
|
||||||
invoices.id,
|
invoices.id,
|
||||||
invoices.customer_id,
|
invoices.customer_id,
|
||||||
@@ -156,7 +156,7 @@ export async function fetchInvoiceById(id: string) {
|
|||||||
WHERE invoices.id = ${id};
|
WHERE invoices.id = ${id};
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const invoice = data.rows.map((invoice) => ({
|
const invoice = data.map((invoice) => ({
|
||||||
...invoice,
|
...invoice,
|
||||||
// Convert amount from cents to dollars
|
// Convert amount from cents to dollars
|
||||||
amount: invoice.amount / 100,
|
amount: invoice.amount / 100,
|
||||||
@@ -170,7 +170,7 @@ export async function fetchInvoiceById(id: string) {
|
|||||||
|
|
||||||
export async function fetchCustomers() {
|
export async function fetchCustomers() {
|
||||||
try {
|
try {
|
||||||
const data = await sql<CustomerField>`
|
const data = await sql<CustomerField[]>`
|
||||||
SELECT
|
SELECT
|
||||||
id,
|
id,
|
||||||
name
|
name
|
||||||
@@ -178,7 +178,7 @@ export async function fetchCustomers() {
|
|||||||
ORDER BY name ASC
|
ORDER BY name ASC
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const customers = data.rows;
|
const customers = data;
|
||||||
return customers;
|
return customers;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Database Error:', err);
|
console.error('Database Error:', err);
|
||||||
@@ -188,7 +188,7 @@ export async function fetchCustomers() {
|
|||||||
|
|
||||||
export async function fetchFilteredCustomers(query: string) {
|
export async function fetchFilteredCustomers(query: string) {
|
||||||
try {
|
try {
|
||||||
const data = await sql<CustomersTable>`
|
const data = await sql<CustomersTable[]>`
|
||||||
SELECT
|
SELECT
|
||||||
customers.id,
|
customers.id,
|
||||||
customers.name,
|
customers.name,
|
||||||
@@ -206,7 +206,7 @@ export async function fetchFilteredCustomers(query: string) {
|
|||||||
ORDER BY customers.name ASC
|
ORDER BY customers.name ASC
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const customers = data.rows.map((customer) => ({
|
const customers = data.map((customer) => ({
|
||||||
...customer,
|
...customer,
|
||||||
total_pending: formatCurrency(customer.total_pending),
|
total_pending: formatCurrency(customer.total_pending),
|
||||||
total_paid: formatCurrency(customer.total_paid),
|
total_paid: formatCurrency(customer.total_paid),
|
||||||
@@ -222,7 +222,7 @@ export async function fetchFilteredCustomers(query: string) {
|
|||||||
export async function getUser(email: string) {
|
export async function getUser(email: string) {
|
||||||
try {
|
try {
|
||||||
const user = await sql`SELECT * from USERS where email=${email}`;
|
const user = await sql`SELECT * from USERS where email=${email}`;
|
||||||
return user.rows[0] as User;
|
return user[0] as User;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to fetch user:', error);
|
console.error('Failed to fetch user:', error);
|
||||||
throw new Error('Failed to fetch user.');
|
throw new Error('Failed to fetch user.');
|
||||||
|
|||||||
Reference in New Issue
Block a user