Compare commits
3 Commits
0c70c66cf2
...
7046c5adc9
Author | SHA1 | Date | |
---|---|---|---|
7046c5adc9 | |||
4c4b00c51f | |||
26833e1818 |
@ -8,7 +8,7 @@
|
||||
"start": "next start --port 4000",
|
||||
"lint": "next lint",
|
||||
"test": "vitest",
|
||||
"seed": "node -r dotenv/config ./src/bootstrap/db/seed.js"
|
||||
"seed": "node -r dotenv/config ./src/bootstrap/boundaries/db/seed.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@heroicons/react": "^2.1.5",
|
||||
@ -16,6 +16,7 @@
|
||||
"@radix-ui/react-slot": "^1.1.0",
|
||||
"class-variance-authority": "^0.7.0",
|
||||
"clsx": "^2.1.1",
|
||||
"fp-ts": "^2.16.9",
|
||||
"lucide-react": "^0.454.0",
|
||||
"next": "15.0.2",
|
||||
"postgres": "^3.4.5",
|
||||
|
@ -1,5 +1,5 @@
|
||||
import fetchCustomerInvoicesUsecase from "@/feature/core/customer-invoice/domain/usecase/fetch-customer-invoices-usecase";
|
||||
|
||||
export default function latestInvoicesController() {
|
||||
return fetchCustomerInvoicesUsecase()
|
||||
export default async function latestInvoicesController() {
|
||||
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 { ArrowPathIcon } from '@heroicons/react/24/outline';
|
||||
import clsx from 'clsx';
|
||||
import { isLeft } from 'fp-ts/lib/Either';
|
||||
import Image from 'next/image';
|
||||
|
||||
export default async function LatestInvoices() {
|
||||
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 (
|
||||
<div
|
||||
key={invoice.id}
|
||||
|
@ -10,4 +10,4 @@ const dbConfigs = {
|
||||
database: envs.POSTGRES_DB,
|
||||
}
|
||||
|
||||
export const sql = postgres(dbConfigs);
|
||||
export const sql = postgres(dbConfigs);
|
@ -3,7 +3,7 @@
|
||||
import { z } from 'zod';
|
||||
import { revalidatePath } from 'next/cache';
|
||||
import { redirect } from 'next/navigation';
|
||||
import { sql } from '@/bootstrap/db/db';
|
||||
import { sql } from '@/bootstrap/boundaries/db/db';
|
||||
|
||||
const FormSchema = z.object({
|
||||
id: z.string(),
|
||||
|
8
src/feature/common/data/api-task.ts
Normal file
8
src/feature/common/data/api-task.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import { Either } from "fp-ts/lib/Either";
|
||||
import { TaskEither } from "fp-ts/lib/TaskEither";
|
||||
import BaseFailure from "@/feature/common/failures/base-failure";
|
||||
|
||||
type ApiTask<ResponseType> = TaskEither<BaseFailure, ResponseType>;
|
||||
export type ApiEither<ResponseType> = Either<BaseFailure, ResponseType>;
|
||||
|
||||
export default ApiTask;
|
22
src/feature/common/failures/base-failure.ts
Normal file
22
src/feature/common/failures/base-failure.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { makeFailureMessage } from "@/feature/common/failures/failure-helpers";
|
||||
|
||||
/**
|
||||
* This is a class called BaseFailure that extends the Error class. It is
|
||||
* used as a base class for creating custom failure classes.
|
||||
*/
|
||||
export default abstract class BaseFailure {
|
||||
/* ------------------------------- Attributes ------------------------------- */
|
||||
private readonly BASE_FAILURE_MESSAGE = "failure";
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/**
|
||||
* Use this message as key lang for failure messages
|
||||
*/
|
||||
message = this.BASE_FAILURE_MESSAGE;
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
constructor(key: string) {
|
||||
this.message = makeFailureMessage(this.message, key);
|
||||
}
|
||||
/* -------------------------------------------------------------------------- */
|
||||
}
|
12
src/feature/common/failures/dev/arguments-failure.ts
Normal file
12
src/feature/common/failures/dev/arguments-failure.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import BaseDevFailure from "@/feature/common/failures/dev/base-dev-failure";
|
||||
|
||||
/**
|
||||
* Failure for needed arguments in a method but sent wrong one
|
||||
*/
|
||||
export default class ArgumentsFailure extends BaseDevFailure {
|
||||
/* ------------------------------- Constructor ------------------------------ */
|
||||
constructor() {
|
||||
super("arguments");
|
||||
}
|
||||
/* -------------------------------------------------------------------------- */
|
||||
}
|
3
src/feature/common/failures/dev/base-dev-failure.ts
Normal file
3
src/feature/common/failures/dev/base-dev-failure.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import BaseFailure from "@/feature/common/failures/base-failure";
|
||||
|
||||
export default abstract class BaseDevFailure extends BaseFailure {}
|
10
src/feature/common/failures/dev/dependency-failure.ts
Normal file
10
src/feature/common/failures/dev/dependency-failure.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import BaseDevFailure from "@/feature/common/failures/dev/base-dev-failure";
|
||||
|
||||
/**
|
||||
* This is a failure of not having specific dependency
|
||||
*/
|
||||
export default class DependencyFailure extends BaseDevFailure {
|
||||
constructor() {
|
||||
super("DependencyFailure");
|
||||
}
|
||||
}
|
95
src/feature/common/failures/failure-helpers.ts
Normal file
95
src/feature/common/failures/failure-helpers.ts
Normal file
@ -0,0 +1,95 @@
|
||||
import BaseFailure from "@/feature/common/failures/base-failure";
|
||||
|
||||
/**
|
||||
* This method is supposed to save previous failure of TaskEither
|
||||
* to prevent it from loosing and overriding by the new one.
|
||||
*
|
||||
* Usage example:
|
||||
* ```ts
|
||||
* tryCatch(
|
||||
* async () => {
|
||||
* ...
|
||||
* throw ValidationFailure();
|
||||
* ...
|
||||
* },
|
||||
* (reason) => failureOr(reason, new UserCreationFailure()),
|
||||
* )
|
||||
* ```
|
||||
* In this example `failureOr` will return already throwed
|
||||
* instance of `BaseFailure` which is `ValidationFailure`.
|
||||
*
|
||||
*
|
||||
* @param reason is throwed object.
|
||||
* Basically it can be default `Error` or instance of `BaseFailure`.
|
||||
* @param failure instance of `BaseFailure` that will be returned
|
||||
* if reason is not instance of `BaseFailure`.
|
||||
* @returns `BaseFailure`
|
||||
*/
|
||||
export function failureOr(reason: unknown, failure: BaseFailure): BaseFailure {
|
||||
if (reason instanceof BaseFailure) {
|
||||
return reason;
|
||||
}
|
||||
return failure;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a function that maps a BaseFailure instance to a new BaseFailure instance of type IfType using the provided mapping function.
|
||||
* @param f A function that maps an instance of IfType to a new instance of BaseFailure.
|
||||
* @param ctor A constructor function for IfType.
|
||||
* @returns A function that maps a BaseFailure instance to a new BaseFailure instance of type IfType.
|
||||
*/
|
||||
export function mapToFailureFrom<IfType extends BaseFailure>(
|
||||
f: (t: IfType) => BaseFailure,
|
||||
ctor: new (...args: never[]) => IfType,
|
||||
): (t: BaseFailure) => BaseFailure {
|
||||
return mapIfInstance<IfType, BaseFailure>(f, ctor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps an instance of a class to a response using a provided function.
|
||||
*
|
||||
* @template IfType - The type of the instance to map.
|
||||
* @template Response - The type of the response to map to.
|
||||
* @param {function} f - The function to use to map the instance to a response.
|
||||
* @param {new (...args: never[]) => IfType} ctor - The constructor function of the instance to map.
|
||||
* @returns {(t: IfType | Response) => IfType | Response} - A function that maps the instance to a response using the provided function.
|
||||
*/
|
||||
export function mapIfInstance<IfType, Response>(
|
||||
f: (t: IfType) => Response,
|
||||
ctor: new (...args: never[]) => IfType,
|
||||
) {
|
||||
return (t: IfType | Response) => {
|
||||
if (t instanceof ctor) {
|
||||
return f(t);
|
||||
}
|
||||
return t;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps a function to a value if it is not an instance of a given class.
|
||||
* @template IfType The type of the value to be mapped.
|
||||
* @template Response The type of the mapped value.
|
||||
* @param {function} f The function to map the value with.
|
||||
* @param {new (...args: never[]) => IfType} ctor The class to check the value against.
|
||||
* @returns {function} A function that maps the value if it is not an instance of the given class.
|
||||
*/
|
||||
export function mapIfNotInstance<IfType, Response>(
|
||||
f: (t: IfType) => Response,
|
||||
ctor: new (...args: never[]) => IfType,
|
||||
) {
|
||||
return (t: IfType | Response) => {
|
||||
if (t! instanceof ctor) {
|
||||
return f(t);
|
||||
}
|
||||
return t;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets Message key and it'll add it to the failure message key hierarchy
|
||||
*/
|
||||
export function makeFailureMessage(message: string, key: string) {
|
||||
if (!key) return message;
|
||||
return `${message}.${key}`;
|
||||
}
|
12
src/feature/common/failures/network-failure.ts
Normal file
12
src/feature/common/failures/network-failure.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import BaseFailure from "./base-failure";
|
||||
|
||||
/**
|
||||
* Failure for HTTP response when response dosn't have base structure
|
||||
*/
|
||||
export default class NetworkFailure extends BaseFailure {
|
||||
/* ------------------------------- Constructor ------------------------------ */
|
||||
constructor() {
|
||||
super("network");
|
||||
}
|
||||
/* -------------------------------------------------------------------------- */
|
||||
}
|
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/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 CustomerInvoice from "@/feature/core/customer-invoice/domain/entity/customer-invoice";
|
||||
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";
|
||||
|
||||
type customerInvoiceDbResponse = {
|
||||
@ -13,23 +18,25 @@ type customerInvoiceDbResponse = {
|
||||
}
|
||||
|
||||
export default class CustomerInvoiceDbRepo implements CustomerInvoiceRepo {
|
||||
async fetchList(): Promise<CustomerInvoice[]> {
|
||||
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 20 ` as postgres.RowList<customerInvoiceDbResponse[]>;
|
||||
|
||||
return this.customerInvoicesDto(data)
|
||||
} catch (error) {
|
||||
console.error('Database Error:', error);
|
||||
throw new Error('Failed to fetch the latest invoices.');
|
||||
}
|
||||
fetchList(): ApiTask<CustomerInvoice[]> {
|
||||
|
||||
return pipe(
|
||||
tryCatch(
|
||||
async () => {
|
||||
const response = 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 20 ` as postgres.RowList<customerInvoiceDbResponse[]>;
|
||||
|
||||
return this.customerInvoicesDto(response)
|
||||
},
|
||||
(l) => failureOr(l, new NetworkFailure())
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
private customerInvoicesDto(dbCustomers: customerInvoiceDbResponse[]): CustomerInvoice[] {
|
||||
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"
|
||||
|
||||
export default interface CustomerInvoiceRepo {
|
||||
fetchList(): Promise<CustomerInvoice[]>
|
||||
fetchList(): ApiTask<CustomerInvoice[]>
|
||||
}
|
||||
|
||||
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 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 { customerInvoiceModuleKey } from "@/feature/core/customer-invoice/invoice-module-key";
|
||||
import { connection } from "next/server";
|
||||
|
||||
export default async function fetchCustomerInvoicesUsecase(): Promise<CustomerInvoice[]> {
|
||||
export default async function fetchCustomerInvoicesUsecase(): Promise<ApiEither<CustomerInvoice[]>> {
|
||||
connection()
|
||||
const repo = serverDi(customerInvoiceModuleKey).resolve<CustomerInvoiceRepo>(customerInvoiceRepoKey)
|
||||
return repo.fetchList()
|
||||
return repo.fetchList()()
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
import { sql } from "@/bootstrap/db/db";
|
||||
import { sql } from "@/bootstrap/boundaries/db/db";
|
||||
import { formatCurrency } from "@/feature/common/feature-helpers";
|
||||
import Customer from "@/feature/core/customer/domain/entity/customer";
|
||||
import CustomerRepo from "@/feature/core/customer/domain/i-repo/customer-repo";
|
||||
|
@ -1,7 +1,8 @@
|
||||
import ApiTask from "@/feature/common/data/api-task"
|
||||
import Customer from "@/feature/core/customer/domain/entity/customer"
|
||||
|
||||
export default interface CustomerRepo {
|
||||
fetchList(query: string): Promise<Customer[]>
|
||||
fetchList(query: string): ApiTask<Customer[]>
|
||||
fetchCustomersAmount(): Promise<number>
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { sql } from "@/bootstrap/db/db";
|
||||
import { sql } from "@/bootstrap/boundaries/db/db";
|
||||
import { formatCurrency } from "@/feature/common/feature-helpers";
|
||||
import InvoiceRepo from "@/feature/core/invoice/domain/i-repo/invoice-repo";
|
||||
import { InvoiceParam } from "@/feature/core/invoice/domain/param/invoice-param";
|
||||
|
@ -1,10 +1,11 @@
|
||||
import ApiTask from "@/feature/common/data/api-task"
|
||||
import { InvoiceParam } from "@/feature/core/invoice/domain/param/invoice-param"
|
||||
import InvoiceStatusSummary from "@/feature/core/invoice/domain/value-object/invoice-status"
|
||||
|
||||
export default interface InvoiceRepo {
|
||||
fetchAllInvoicesAmount(): Promise<number>
|
||||
fetchInvoicesStatusSummary(): Promise<InvoiceStatusSummary>
|
||||
createInvoice(params: InvoiceParam): Promise<string>
|
||||
createInvoice(params: InvoiceParam): ApiTask<string>
|
||||
}
|
||||
|
||||
export const invoiceRepoKey = "invoiceRepoKey"
|
@ -1,19 +1,24 @@
|
||||
"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 InvoiceRepo, { invoiceRepoKey } from "@/feature/core/invoice/domain/i-repo/invoice-repo";
|
||||
import { InvoiceParam, invoiceSchema } from "@/feature/core/invoice/domain/param/invoice-param";
|
||||
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}> {
|
||||
const isParamsValid = invoiceSchema.safeParse(params)
|
||||
|
||||
if (!isParamsValid) {
|
||||
return {
|
||||
errorMessage: "Please pass correct params"
|
||||
}
|
||||
}
|
||||
export default async function createInvoiceUsecase(params: InvoiceParam): Promise<ApiEither<string>> {
|
||||
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))
|
||||
)()
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
import { sql } from "@/bootstrap/db/db";
|
||||
import { sql } from "@/bootstrap/boundaries/db/db";
|
||||
import Revenue from "@/feature/core/revenue/domain/entity/revenue";
|
||||
import RevenueRepo from "@/feature/core/revenue/domain/i-repo/revenue-repo";
|
||||
import { connection } from "next/server";
|
||||
|
@ -25,6 +25,6 @@
|
||||
"@/*": ["./src/*"]
|
||||
}
|
||||
},
|
||||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts", "src/bootstrap/db/seed.js", "src/bootstrap/db/placeholder-data.js"],
|
||||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts", "src/bootstrap/boundaries/db/seed.js", "src/bootstrap/boundaries/db/placeholder-data.js"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
|
@ -2125,6 +2125,11 @@ form-data@^4.0.0:
|
||||
combined-stream "^1.0.8"
|
||||
mime-types "^2.1.12"
|
||||
|
||||
fp-ts@^2.16.9:
|
||||
version "2.16.9"
|
||||
resolved "https://registry.yarnpkg.com/fp-ts/-/fp-ts-2.16.9.tgz#99628fc5e0bb3b432c4a16d8f4455247380bae8a"
|
||||
integrity sha512-+I2+FnVB+tVaxcYyQkHUq7ZdKScaBlX53A41mxQtpIccsfyv8PzdzP7fzp2AY832T4aoK6UZ5WRX/ebGd8uZuQ==
|
||||
|
||||
fs-minipass@^2.0.0:
|
||||
version "2.1.0"
|
||||
resolved "https://registry.yarnpkg.com/fs-minipass/-/fs-minipass-2.1.0.tgz#7f5036fdbf12c63c169190cbe4199c852271f9fb"
|
||||
|
Loading…
x
Reference in New Issue
Block a user