develop #3
@ -3,7 +3,6 @@ import {
|
|||||||
Revenue,
|
Revenue,
|
||||||
Invoice,
|
Invoice,
|
||||||
Customer,
|
Customer,
|
||||||
LatestInvoice,
|
|
||||||
} from './definitions';
|
} from './definitions';
|
||||||
import { formatCurrency } from './utils';
|
import { formatCurrency } from './utils';
|
||||||
import postgres from 'postgres';
|
import postgres from 'postgres';
|
||||||
@ -31,29 +30,6 @@ export async function fetchRevenue() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchLatestInvoices() {
|
|
||||||
// This is equivalent to in fetch(..., {cache: 'no-store'}).
|
|
||||||
connection()
|
|
||||||
|
|
||||||
try {
|
|
||||||
const data = await sql`
|
|
||||||
SELECT invoices.amount, customers.name, customers.image_url, customers.email, invoices.id
|
|
||||||
FROM invoices
|
|
||||||
JOIN customers ON invoices.customer_id = customers.id
|
|
||||||
ORDER BY invoices.date DESC
|
|
||||||
LIMIT 5` as postgres.RowList<LatestInvoice[]>;
|
|
||||||
|
|
||||||
const latestInvoices = data.map((invoice) => ({
|
|
||||||
...invoice,
|
|
||||||
amount: formatCurrency(+invoice.amount),
|
|
||||||
}));
|
|
||||||
return latestInvoices;
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Database Error:', error);
|
|
||||||
throw new Error('Failed to fetch the latest invoices.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function fetchCardData() {
|
export async function fetchCardData() {
|
||||||
// This is equivalent to in fetch(..., {cache: 'no-store'}).
|
// This is equivalent to in fetch(..., {cache: 'no-store'}).
|
||||||
connection()
|
connection()
|
||||||
|
@ -18,14 +18,6 @@ export type Revenue = {
|
|||||||
revenue: number;
|
revenue: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type LatestInvoice = {
|
|
||||||
id: string;
|
|
||||||
name: string;
|
|
||||||
image_url: string;
|
|
||||||
email: string;
|
|
||||||
amount: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type InvoiceForm = {
|
export type InvoiceForm = {
|
||||||
id: string;
|
id: string;
|
||||||
customer_id: string;
|
customer_id: string;
|
||||||
|
@ -1,13 +1,19 @@
|
|||||||
|
import getCustomerInvoiceDi from "@/feature/customer-invoice/data/module/customer-invoice-di";
|
||||||
|
import { customerInvoiceModuleKey } from "@/feature/customer-invoice/invoice-module-key";
|
||||||
import { customerKey } from "@/feature/customer/customer-key";
|
import { customerKey } from "@/feature/customer/customer-key";
|
||||||
import getCustomerDi from "@/feature/customer/data/module/customer-di";
|
import getCustomerDi from "@/feature/customer/data/module/customer-di";
|
||||||
import { testModuleKey } from "@/feature/domain/test/test-module-key";
|
import { testModuleKey } from "@/feature/domain/test/test-module-key";
|
||||||
import getTestModule from "@/feature/infra/test/module/test-module";
|
import getTestModule from "@/feature/infra/test/module/test-module";
|
||||||
|
import getInvoiceDi from "@/feature/invoice/data/module/invoice-di";
|
||||||
|
import { invoiceModuleKey } from "@/feature/invoice/invoice-module-key";
|
||||||
import { DependencyContainer } from "tsyringe";
|
import { DependencyContainer } from "tsyringe";
|
||||||
|
|
||||||
export default function serverDi(module: string): DependencyContainer {
|
export default function serverDi(module: string): DependencyContainer {
|
||||||
const getDi = {
|
const getDi = {
|
||||||
[testModuleKey]: getTestModule,
|
[testModuleKey]: getTestModule,
|
||||||
[customerKey]: getCustomerDi
|
[customerKey]: getCustomerDi,
|
||||||
|
[customerInvoiceModuleKey]: getCustomerInvoiceDi,
|
||||||
|
[invoiceModuleKey]: getInvoiceDi,
|
||||||
}[module]
|
}[module]
|
||||||
|
|
||||||
if (!getDi) throw new Error("Server Di didn't found for module: " + module)
|
if (!getDi) throw new Error("Server Di didn't found for module: " + module)
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
import di from "@/bootstrap/di/init-di";
|
|
||||||
import CustomerDbRepo from "@/feature/customer/data/repo/customer-db-repo";
|
|
||||||
import { customerRepoKey } from "@/feature/customer/domain/i-repo/customer-repo";
|
|
||||||
import fetchCustomersUsecase from "@/feature/customer/domain/usecase/fetch-customers-usecase";
|
|
||||||
import { DependencyContainer } from "tsyringe";
|
|
||||||
|
|
||||||
export default function getCustomerDi(): DependencyContainer {
|
|
||||||
const customerDi = di.createChildContainer()
|
|
||||||
|
|
||||||
customerDi.register(fetchCustomersUsecase.name, {
|
|
||||||
useValue: fetchCustomersUsecase
|
|
||||||
})
|
|
||||||
|
|
||||||
customerDi.register(customerRepoKey, CustomerDbRepo)
|
|
||||||
return customerDi
|
|
||||||
}
|
|
@ -0,0 +1,16 @@
|
|||||||
|
import di from "@/bootstrap/di/init-di";
|
||||||
|
import { customerInvoiceRepoKey } from "@/feature/customer-invoice/domain/i-repo/customer-invoice-repo";
|
||||||
|
import fetchCustomerInvoicesUsecase from "@/feature/customer-invoice/domain/usecase/fetch-customer-invoices-usecase";
|
||||||
|
import CustomerDbRepo from "@/feature/customer/data/repo/customer-db-repo";
|
||||||
|
import { DependencyContainer } from "tsyringe";
|
||||||
|
|
||||||
|
export default function getCustomerInvoiceInvoiceDi(): DependencyContainer {
|
||||||
|
const customerInvoiceDi = di.createChildContainer()
|
||||||
|
|
||||||
|
customerInvoiceDi.register(fetchCustomerInvoicesUsecase.name, {
|
||||||
|
useValue: fetchCustomerInvoicesUsecase
|
||||||
|
})
|
||||||
|
|
||||||
|
customerInvoiceDi.register(customerInvoiceRepoKey, CustomerDbRepo)
|
||||||
|
return customerInvoiceDi
|
||||||
|
}
|
16
src/feature/invoice/data/module/invoice-di.ts
Normal file
16
src/feature/invoice/data/module/invoice-di.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import di from "@/bootstrap/di/init-di";
|
||||||
|
import invoiceDbRepo from "@/feature/invoice/data/repo/invoice-db-repo";
|
||||||
|
import { invoiceRepoKey } from "@/feature/invoice/domain/i-repo/invoice-repo";
|
||||||
|
import fetchAllInvoicesAmountUsecase from "@/feature/invoice/domain/usecase/fetch-all-invoices-amount-usecase";
|
||||||
|
import { DependencyContainer } from "tsyringe";
|
||||||
|
|
||||||
|
export default function getInvoiceDi(): DependencyContainer {
|
||||||
|
const invoiceDi = di.createChildContainer()
|
||||||
|
|
||||||
|
invoiceDi.register(fetchAllInvoicesAmountUsecase.name, {
|
||||||
|
useValue: fetchAllInvoicesAmountUsecase
|
||||||
|
})
|
||||||
|
|
||||||
|
invoiceDi.register(invoiceRepoKey, invoiceDbRepo)
|
||||||
|
return invoiceDi
|
||||||
|
}
|
11
src/feature/invoice/data/repo/invoice-db-repo.ts
Normal file
11
src/feature/invoice/data/repo/invoice-db-repo.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { sql } from "@/bootstrap/db/db";
|
||||||
|
import InvoiceRepo from "@/feature/invoice/domain/i-repo/invoice-repo";
|
||||||
|
import postgres from "postgres";
|
||||||
|
|
||||||
|
export default class InvoiceDbRepo implements InvoiceRepo {
|
||||||
|
async fetchAllInvoicesAmount(): Promise<number> {
|
||||||
|
const data = await sql`SELECT COUNT(*) FROM invoices` as postgres.RowList<unknown[]>;
|
||||||
|
|
||||||
|
return data.count ?? 0
|
||||||
|
}
|
||||||
|
}
|
6
src/feature/invoice/domain/i-repo/invoice-repo.ts
Normal file
6
src/feature/invoice/domain/i-repo/invoice-repo.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
|
||||||
|
export default interface InvoiceRepo {
|
||||||
|
fetchAllInvoicesAmount(): Promise<number>
|
||||||
|
}
|
||||||
|
|
||||||
|
export const invoiceRepoKey = "invoiceRepoKey"
|
@ -0,0 +1,10 @@
|
|||||||
|
"use server"
|
||||||
|
import serverDi from "@/feature/common/server-di";
|
||||||
|
import InvoiceRepo, { invoiceRepoKey } from "@/feature/invoice/domain/i-repo/invoice-repo";
|
||||||
|
import { invoiceModuleKey } from "@/feature/invoice/invoice-module-key";
|
||||||
|
|
||||||
|
export default function fetchAllInvoicesAmountUsecase(): Promise<number> {
|
||||||
|
const repo = serverDi(invoiceModuleKey).resolve<InvoiceRepo>(invoiceRepoKey)
|
||||||
|
|
||||||
|
return repo.fetchAllInvoicesAmount()
|
||||||
|
}
|
1
src/feature/invoice/invoice-module-key.ts
Normal file
1
src/feature/invoice/invoice-module-key.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export const invoiceModuleKey = "invoiceModuleKey"
|
Loading…
x
Reference in New Issue
Block a user