develop #3
@ -1,5 +1,5 @@
|
|||||||
import fetchCustomerInvoicesUsecase from "@/feature/core/customer-invoice/domain/usecase/fetch-customer-invoices-usecase";
|
import fetchCustomerInvoicesUsecase from "@/feature/core/customer-invoice/domain/usecase/fetch-customer-invoices-usecase";
|
||||||
|
|
||||||
export default function latestInvoicesController() {
|
export default async function latestInvoicesController() {
|
||||||
return fetchCustomerInvoicesUsecase()
|
return await fetchCustomerInvoicesUsecase()
|
||||||
}
|
}
|
@ -2,12 +2,15 @@ import CreateRandomInvoiceContainer from '@/app/dashboard/components/client/crea
|
|||||||
import latestInvoicesController from '@/app/dashboard/components/server/latest-invoices/latest-invoices-controller';
|
import latestInvoicesController from '@/app/dashboard/components/server/latest-invoices/latest-invoices-controller';
|
||||||
import { ArrowPathIcon } from '@heroicons/react/24/outline';
|
import { ArrowPathIcon } from '@heroicons/react/24/outline';
|
||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
|
import { isLeft } from 'fp-ts/lib/Either';
|
||||||
import Image from 'next/image';
|
import Image from 'next/image';
|
||||||
|
|
||||||
export default async function LatestInvoices() {
|
export default async function LatestInvoices() {
|
||||||
const latestInvoices = await latestInvoicesController();
|
const latestInvoices = await latestInvoicesController();
|
||||||
|
|
||||||
const invoices = latestInvoices.map((invoice, i) => {
|
if (isLeft(latestInvoices)) return <div>Error</div>
|
||||||
|
|
||||||
|
const invoices = latestInvoices.right.map((invoice, i) => {
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
key={invoice.id}
|
key={invoice.id}
|
||||||
|
12
src/feature/common/failures/params-failure.ts
Normal file
12
src/feature/common/failures/params-failure.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import BaseFailure from "./base-failure";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Failure for params failure
|
||||||
|
*/
|
||||||
|
export default class ParamsFailure extends BaseFailure {
|
||||||
|
/* ------------------------------- Constructor ------------------------------ */
|
||||||
|
constructor() {
|
||||||
|
super("params");
|
||||||
|
}
|
||||||
|
/* -------------------------------------------------------------------------- */
|
||||||
|
}
|
@ -1,7 +1,12 @@
|
|||||||
import { sql } from "@/bootstrap/boundaries/db/db";
|
import { sql } from "@/bootstrap/boundaries/db/db";
|
||||||
|
import ApiTask from "@/feature/common/data/api-task";
|
||||||
|
import { failureOr } from "@/feature/common/failures/failure-helpers";
|
||||||
|
import NetworkFailure from "@/feature/common/failures/network-failure";
|
||||||
import { formatCurrency } from "@/feature/common/feature-helpers";
|
import { formatCurrency } from "@/feature/common/feature-helpers";
|
||||||
import CustomerInvoice from "@/feature/core/customer-invoice/domain/entity/customer-invoice";
|
import CustomerInvoice from "@/feature/core/customer-invoice/domain/entity/customer-invoice";
|
||||||
import CustomerInvoiceRepo from "@/feature/core/customer-invoice/domain/i-repo/customer-invoice-repo";
|
import CustomerInvoiceRepo from "@/feature/core/customer-invoice/domain/i-repo/customer-invoice-repo";
|
||||||
|
import { pipe } from "fp-ts/lib/function";
|
||||||
|
import { tryCatch } from "fp-ts/lib/TaskEither";
|
||||||
import postgres from "postgres";
|
import postgres from "postgres";
|
||||||
|
|
||||||
type customerInvoiceDbResponse = {
|
type customerInvoiceDbResponse = {
|
||||||
@ -13,22 +18,24 @@ type customerInvoiceDbResponse = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default class CustomerInvoiceDbRepo implements CustomerInvoiceRepo {
|
export default class CustomerInvoiceDbRepo implements CustomerInvoiceRepo {
|
||||||
async fetchList(): Promise<CustomerInvoice[]> {
|
fetchList(): ApiTask<CustomerInvoice[]> {
|
||||||
try {
|
|
||||||
const data = await sql`
|
return pipe(
|
||||||
|
tryCatch(
|
||||||
|
async () => {
|
||||||
|
const response = await sql`
|
||||||
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 20 ` as postgres.RowList<customerInvoiceDbResponse[]>;
|
LIMIT 20 ` as postgres.RowList<customerInvoiceDbResponse[]>;
|
||||||
|
|
||||||
return this.customerInvoicesDto(data)
|
return this.customerInvoicesDto(response)
|
||||||
} catch (error) {
|
},
|
||||||
console.error('Database Error:', error);
|
(l) => failureOr(l, new NetworkFailure())
|
||||||
throw new Error('Failed to fetch the latest invoices.');
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private customerInvoicesDto(dbCustomers: customerInvoiceDbResponse[]): CustomerInvoice[] {
|
private customerInvoicesDto(dbCustomers: customerInvoiceDbResponse[]): CustomerInvoice[] {
|
||||||
return dbCustomers.map((customer) => this.customerInvoiceDto(customer));
|
return dbCustomers.map((customer) => this.customerInvoiceDto(customer));
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
|
import ApiTask from "@/feature/common/data/api-task"
|
||||||
import CustomerInvoice from "@/feature/core/customer-invoice/domain/entity/customer-invoice"
|
import CustomerInvoice from "@/feature/core/customer-invoice/domain/entity/customer-invoice"
|
||||||
|
|
||||||
export default interface CustomerInvoiceRepo {
|
export default interface CustomerInvoiceRepo {
|
||||||
fetchList(): Promise<CustomerInvoice[]>
|
fetchList(): ApiTask<CustomerInvoice[]>
|
||||||
}
|
}
|
||||||
|
|
||||||
export const customerInvoiceRepoKey = "customerInvoiceRepoKey"
|
export const customerInvoiceRepoKey = "customerInvoiceRepoKey"
|
@ -1,12 +1,12 @@
|
|||||||
"use server"
|
import { ApiEither } from "@/feature/common/data/api-task";
|
||||||
import serverDi from "@/feature/common/server-di";
|
import serverDi from "@/feature/common/server-di";
|
||||||
import CustomerInvoice from "@/feature/core/customer-invoice/domain/entity/customer-invoice";
|
import CustomerInvoice from "@/feature/core/customer-invoice/domain/entity/customer-invoice";
|
||||||
import CustomerInvoiceRepo, { customerInvoiceRepoKey } from "@/feature/core/customer-invoice/domain/i-repo/customer-invoice-repo";
|
import CustomerInvoiceRepo, { customerInvoiceRepoKey } from "@/feature/core/customer-invoice/domain/i-repo/customer-invoice-repo";
|
||||||
import { customerInvoiceModuleKey } from "@/feature/core/customer-invoice/invoice-module-key";
|
import { customerInvoiceModuleKey } from "@/feature/core/customer-invoice/invoice-module-key";
|
||||||
import { connection } from "next/server";
|
import { connection } from "next/server";
|
||||||
|
|
||||||
export default async function fetchCustomerInvoicesUsecase(): Promise<CustomerInvoice[]> {
|
export default async function fetchCustomerInvoicesUsecase(): Promise<ApiEither<CustomerInvoice[]>> {
|
||||||
connection()
|
connection()
|
||||||
const repo = serverDi(customerInvoiceModuleKey).resolve<CustomerInvoiceRepo>(customerInvoiceRepoKey)
|
const repo = serverDi(customerInvoiceModuleKey).resolve<CustomerInvoiceRepo>(customerInvoiceRepoKey)
|
||||||
return repo.fetchList()
|
return repo.fetchList()()
|
||||||
}
|
}
|
@ -1,7 +1,8 @@
|
|||||||
|
import ApiTask from "@/feature/common/data/api-task"
|
||||||
import Customer from "@/feature/core/customer/domain/entity/customer"
|
import Customer from "@/feature/core/customer/domain/entity/customer"
|
||||||
|
|
||||||
export default interface CustomerRepo {
|
export default interface CustomerRepo {
|
||||||
fetchList(query: string): Promise<Customer[]>
|
fetchList(query: string): ApiTask<Customer[]>
|
||||||
fetchCustomersAmount(): Promise<number>
|
fetchCustomersAmount(): Promise<number>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
|
import ApiTask from "@/feature/common/data/api-task"
|
||||||
import { InvoiceParam } from "@/feature/core/invoice/domain/param/invoice-param"
|
import { InvoiceParam } from "@/feature/core/invoice/domain/param/invoice-param"
|
||||||
import InvoiceStatusSummary from "@/feature/core/invoice/domain/value-object/invoice-status"
|
import InvoiceStatusSummary from "@/feature/core/invoice/domain/value-object/invoice-status"
|
||||||
|
|
||||||
export default interface InvoiceRepo {
|
export default interface InvoiceRepo {
|
||||||
fetchAllInvoicesAmount(): Promise<number>
|
fetchAllInvoicesAmount(): Promise<number>
|
||||||
fetchInvoicesStatusSummary(): Promise<InvoiceStatusSummary>
|
fetchInvoicesStatusSummary(): Promise<InvoiceStatusSummary>
|
||||||
createInvoice(params: InvoiceParam): Promise<string>
|
createInvoice(params: InvoiceParam): ApiTask<string>
|
||||||
}
|
}
|
||||||
|
|
||||||
export const invoiceRepoKey = "invoiceRepoKey"
|
export const invoiceRepoKey = "invoiceRepoKey"
|
@ -1,19 +1,24 @@
|
|||||||
"use server"
|
"use server"
|
||||||
|
import { ApiEither } from "@/feature/common/data/api-task";
|
||||||
|
import ParamsFailure from "@/feature/common/failures/params-failure";
|
||||||
import serverDi from "@/feature/common/server-di";
|
import serverDi from "@/feature/common/server-di";
|
||||||
import InvoiceRepo, { invoiceRepoKey } from "@/feature/core/invoice/domain/i-repo/invoice-repo";
|
import InvoiceRepo, { invoiceRepoKey } from "@/feature/core/invoice/domain/i-repo/invoice-repo";
|
||||||
import { InvoiceParam, invoiceSchema } from "@/feature/core/invoice/domain/param/invoice-param";
|
import { InvoiceParam, invoiceSchema } from "@/feature/core/invoice/domain/param/invoice-param";
|
||||||
import { invoiceModuleKey } from "@/feature/core/invoice/invoice-module-key";
|
import { invoiceModuleKey } from "@/feature/core/invoice/invoice-module-key";
|
||||||
|
import { pipe } from "fp-ts/lib/function";
|
||||||
|
import { chain, fromNullable, left, map, right } from "fp-ts/lib/TaskEither";
|
||||||
|
|
||||||
export default async function createInvoiceUsecase(params: InvoiceParam): Promise<string | {errorMessage: string}> {
|
export default async function createInvoiceUsecase(params: InvoiceParam): Promise<ApiEither<string>> {
|
||||||
const isParamsValid = invoiceSchema.safeParse(params)
|
|
||||||
|
|
||||||
if (!isParamsValid) {
|
|
||||||
return {
|
|
||||||
errorMessage: "Please pass correct params"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const repo = serverDi(invoiceModuleKey).resolve<InvoiceRepo>(invoiceRepoKey)
|
const repo = serverDi(invoiceModuleKey).resolve<InvoiceRepo>(invoiceRepoKey)
|
||||||
|
|
||||||
return repo.createInvoice(params)
|
return pipe(
|
||||||
|
fromNullable(new ParamsFailure())(params),
|
||||||
|
map((params) => invoiceSchema.safeParse(params)),
|
||||||
|
chain((params) => {
|
||||||
|
const isParamsValid = invoiceSchema.safeParse(params)
|
||||||
|
if (!isParamsValid.success) left(new ParamsFailure())
|
||||||
|
return right(params.data as InvoiceParam)
|
||||||
|
}),
|
||||||
|
chain((params) => repo.createInvoice(params))
|
||||||
|
)()
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user