refactor: structure of calling usecases to just be from controller

This commit is contained in:
Behnamrhp74 2025-03-08 22:16:29 +03:00
parent 7d9a6e77bd
commit 4306ea1bdc
26 changed files with 74 additions and 36 deletions

View File

@ -1,4 +1,4 @@
import cardController from "@/app/[lang]/dashboard/components/server/card/card.controller"; import cardController from "@/app/[lang]/dashboard/controller/card.controller";
export function Card({ export function Card({
title, title,

View File

@ -1,5 +1,5 @@
import { Card } from "@/app/[lang]/dashboard/components/server/card/card"; import { Card } from "@/app/[lang]/dashboard/components/server/card";
import cardsController from "@/app/[lang]/dashboard/components/server/cards/cards.controller"; import cardsController from "@/app/[lang]/dashboard/controller/cards.controller";
export default async function CardWrapper() { export default async function CardWrapper() {
const { customersNumber, invoicesNumber, invoicesSummary } = const { customersNumber, invoicesNumber, invoicesSummary } =

View File

@ -1,5 +1,5 @@
import CreateRandomInvoiceContainer from "@/app/[lang]/dashboard/components/client/create-random-invoice/create-random-invoice"; import CreateRandomInvoiceContainer from "@/app/[lang]/dashboard/components/client/create-random-invoice/create-random-invoice";
import latestInvoicesController from "@/app/[lang]/dashboard/components/server/latest-invoices/latest-invoices.controller"; import latestInvoicesController from "@/app/[lang]/dashboard/controller/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 { isLeft } from "fp-ts/lib/Either";

View File

@ -1,4 +1,4 @@
import revenueChartController from "@/app/[lang]/dashboard/components/server/revenue-chart/revenue-chart.controller"; import revenueChartController from "@/app/[lang]/dashboard/controller/revenue-chart.controller";
import { CalendarIcon } from "@heroicons/react/24/outline"; import { CalendarIcon } from "@heroicons/react/24/outline";
export default async function RevenueChart() { export default async function RevenueChart() {

View File

@ -7,6 +7,9 @@ import {
/** /**
* Controllers are bridge between feature layer and application layer. * Controllers are bridge between feature layer and application layer.
* They decide, feature layer will be cached or not, where to run in client or server
* Or connect multiple usecases and run them, handle their failure, hydrate and store data in
* client state managements.
*/ */
export default function cardController(props: { export default function cardController(props: {
type: "invoices" | "customers" | "pending" | "collected"; type: "invoices" | "customers" | "pending" | "collected";

View File

@ -3,6 +3,9 @@ import { connection } from "next/server";
/** /**
* Controllers are bridge between feature layer and application layer. * Controllers are bridge between feature layer and application layer.
* They decide, feature layer will be cached or not, where to run in client or server
* Or connect multiple usecases and run them, handle their failure, hydrate and store data in
* client state managements.
*/ */
export default function cardsController() { export default function cardsController() {
connection(); connection();

View File

@ -0,0 +1,19 @@
"use server";
import { ApiEither } from "@/feature/common/data/api-task";
import { InvoiceParam } from "@/feature/core/invoice/domain/param/invoice.param";
import createInvoiceUsecase from "@/feature/core/invoice/domain/usecase/create-invoice.usecase";
import { connection } from "next/server";
/**
* Controllers are bridge between feature layer and application layer.
* They decide, feature layer will be cached or not, where to run in client or server
* Or connect multiple usecases and run them, handle their failure, hydrate and store data in
* client state managements.
*/
export default async function createInvoiceController(
params: InvoiceParam,
): Promise<ApiEither<string>> {
connection();
return createInvoiceUsecase(params);
}

View File

@ -3,6 +3,9 @@ import { connection } from "next/server";
/** /**
* Controllers are bridge between feature layer and application layer. * Controllers are bridge between feature layer and application layer.
* They decide, feature layer will be cached or not, where to run in client or server
* Or connect multiple usecases and run them, handle their failure, hydrate and store data in
* client state managements.
*/ */
export default function latestInvoicesController() { export default function latestInvoicesController() {
connection(); connection();

View File

@ -3,6 +3,9 @@ import fetchRevenuesUsecase from "@/feature/core/revenue/domain/usecase/fetch-re
/** /**
* Controllers are bridge between feature layer and application layer. * Controllers are bridge between feature layer and application layer.
* They decide, feature layer will be cached or not, where to run in client or server
* Or connect multiple usecases and run them, handle their failure, hydrate and store data in
* client state managements.
*/ */
export default async function revenueChartController() { export default async function revenueChartController() {
const revenue = await fetchRevenuesUsecase(); const revenue = await fetchRevenuesUsecase();

View File

@ -1,4 +1,4 @@
import DashboardSkeleton from "@/app/[lang]/dashboard/components/server/skeletons/skeletons"; import DashboardSkeleton from "@/app/[lang]/dashboard/components/server/skeletons";
export default function Loading() { export default function Loading() {
return <DashboardSkeleton />; return <DashboardSkeleton />;

View File

@ -1,6 +1,6 @@
import CreateRandomInvoiceButtonVM from "@/app/[lang]/dashboard/vm/create-random-invoice-button-vm"; import CreateRandomInvoiceButtonVM from "@/app/[lang]/dashboard/vm/create-random-invoice-button-vm";
import di from "@/bootstrap/di/init-di"; import di from "@/bootstrap/di/init-di";
import createInvoiceUsecase from "@/feature/core/invoice/domain/usecase/create-invoice.usecase"; // import createInvoiceUsecase from "@/feature/core/invoice/domain/usecase/create-invoice.usecase";
/** /**
* Each page can have its own di to connect all vms, usecases or controllers * Each page can have its own di to connect all vms, usecases or controllers
@ -8,9 +8,9 @@ import createInvoiceUsecase from "@/feature/core/invoice/domain/usecase/create-i
export default function dashboardAppModule() { export default function dashboardAppModule() {
const dashboardDi = di.createChildContainer(); const dashboardDi = di.createChildContainer();
dashboardDi.register(createInvoiceUsecase.name, { // dashboardDi.register(createInvoiceUsecase.name, {
useValue: createInvoiceUsecase, // useValue: createInvoiceUsecase,
}); // });
dashboardDi.register( dashboardDi.register(
CreateRandomInvoiceButtonVM, CreateRandomInvoiceButtonVM,
CreateRandomInvoiceButtonVM, CreateRandomInvoiceButtonVM,

View File

@ -1,13 +1,13 @@
import { import {
LatestInvoicesSkeleton, LatestInvoicesSkeleton,
RevenueChartSkeleton, RevenueChartSkeleton,
} from "@/app/[lang]/dashboard/components/server/skeletons/skeletons"; } from "@/app/[lang]/dashboard/components/server/skeletons";
import CardWrapper from "@/app/[lang]/dashboard/components/server/cards/cards"; import CardWrapper from "@/app/[lang]/dashboard/components/server/cards";
import LatestInvoices from "@/app/[lang]/dashboard/components/server/latest-invoices/latest-invoices"; import RevenueChart from "@/app/[lang]/dashboard/components/server/revenue-chart";
import RevenueChart from "@/app/[lang]/dashboard/components/server/revenue-chart/revenue-chart";
import { Suspense } from "react"; import { Suspense } from "react";
import { getServerTranslation, LANGS } from "@/bootstrap/i18n/i18n"; import { getServerTranslation, LANGS } from "@/bootstrap/i18n/i18n";
import langKey from "@/bootstrap/i18n/dictionaries/lang-key"; import langKey from "@/bootstrap/i18n/dictionaries/lang-key";
import LatestInvoices from "@/app/[lang]/dashboard/components/server/latest-invoices";
export default async function Dashboard(props: { export default async function Dashboard(props: {
params: Promise<{ lang: LANGS }>; params: Promise<{ lang: LANGS }>;

View File

@ -1,3 +1,4 @@
import createInvoiceController from "@/app/[lang]/dashboard/controller/create-invoice.controller";
import ButtonVm from "@/app/components/button/button.i-vm"; import ButtonVm from "@/app/components/button/button.i-vm";
import { useServerAction } from "@/bootstrap/helpers/hooks/use-server-action"; import { useServerAction } from "@/bootstrap/helpers/hooks/use-server-action";
import useThrottle from "@/bootstrap/helpers/hooks/use-throttle"; import useThrottle from "@/bootstrap/helpers/hooks/use-throttle";
@ -19,7 +20,7 @@ export default class CreateRandomInvoiceButtonVM extends BaseVM<ButtonVm> {
constructor() { constructor() {
super(); super();
this.createInvoice = this.di.resolve(createInvoiceUsecase.name); this.createInvoice = createInvoiceController;
} }
useVM(): ButtonVm { useVM(): ButtonVm {

View File

@ -20,7 +20,7 @@ export default async function layout(
) { ) {
const { params, children } = props; const { params, children } = props;
const { lang } = await params; const { lang } = await params;
const { resources } = await getI18n({ lng: lang }); await getI18n({ lng: lang });
return ( return (
<html lang={lang} suppressHydrationWarning> <html lang={lang} suppressHydrationWarning>
<body <body
@ -32,9 +32,7 @@ export default async function layout(
enableSystem enableSystem
disableTransitionOnChange disableTransitionOnChange
> >
<TranslationsProvider lng={lang} resources={resources}> <TranslationsProvider lng={lang}>{children}</TranslationsProvider>
{children}
</TranslationsProvider>
</ThemeProvider> </ThemeProvider>
</body> </body>
</html> </html>

View File

@ -1,17 +1,24 @@
"use client"; "use client";
import { I18nextProvider } from "react-i18next"; import { I18nextProvider } from "react-i18next";
import { i18nInstance, getI18n, LANGS } from "@/bootstrap/i18n/i18n"; import { getI18n, LANGS } from "@/bootstrap/i18n/i18n";
import { Resource } from "i18next"; import { PropsWithChildren, useEffect, useState } from "react";
import { PropsWithChildren } from "react"; import { i18n } from "i18next";
import storeLang from "@/bootstrap/i18n/store-lang-action";
export default function TranslationsProvider({ export default function TranslationsProvider({
children, children,
lng, lng,
resources, }: PropsWithChildren & { lng: LANGS }) {
}: PropsWithChildren & { lng: LANGS; resources: Resource }) { const [i18n, setI18n] = useState<i18n>();
if (!resources) return children;
getI18n({ lng, resources });
return <I18nextProvider i18n={i18nInstance}>{children}</I18nextProvider>; useEffect(() => {
(async () => {
storeLang(lng);
setI18n((await getI18n({ lng })).i18n);
})();
}, [lng]);
if (!i18n) return null;
return <I18nextProvider i18n={i18n}>{children}</I18nextProvider>;
} }

View File

@ -0,0 +1,9 @@
"use server";
import { LANGS } from "@/bootstrap/i18n/i18n";
import { cookieName } from "@/bootstrap/i18n/settings";
import { cookies } from "next/headers";
export default async function storeLang(lng: LANGS) {
(await cookies()).set(cookieName, lng);
}

View File

@ -1,4 +1,3 @@
import "server-only";
import { ApiEither } from "@/feature/common/data/api-task"; 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.entity"; import CustomerInvoice from "@/feature/core/customer-invoice/domain/entity/customer-invoice.entity";

View File

@ -1,4 +1,3 @@
import "server-only";
import serverDi from "@/feature/common/server.di"; import serverDi from "@/feature/common/server.di";
import { customerKey } from "@/feature/core/customer/customer-key"; import { customerKey } from "@/feature/core/customer/customer-key";
import CustomerRepo, { import CustomerRepo, {

View File

@ -1,4 +1,3 @@
import "server-only";
import { ApiEither } from "@/feature/common/data/api-task"; import { ApiEither } from "@/feature/common/data/api-task";
import serverDi from "@/feature/common/server.di"; import serverDi from "@/feature/common/server.di";
import { customerKey } from "@/feature/core/customer/customer-key"; import { customerKey } from "@/feature/core/customer/customer-key";

View File

@ -1,3 +1,4 @@
import "server-only";
import { sql } from "@/bootstrap/boundaries/db/db"; import { sql } from "@/bootstrap/boundaries/db/db";
import ApiTask from "@/feature/common/data/api-task"; import ApiTask from "@/feature/common/data/api-task";
import { failureOr } from "@/feature/common/failures/failure-helpers"; import { failureOr } from "@/feature/common/failures/failure-helpers";

View File

@ -1,5 +1,3 @@
"use server";
import { ApiEither } from "@/feature/common/data/api-task"; import { ApiEither } from "@/feature/common/data/api-task";
import ParamsFailure from "@/feature/common/failures/params.failure"; import ParamsFailure from "@/feature/common/failures/params.failure";
import serverDi from "@/feature/common/server.di"; import serverDi from "@/feature/common/server.di";

View File

@ -1,4 +1,3 @@
import "server-only";
import serverDi from "@/feature/common/server.di"; import serverDi from "@/feature/common/server.di";
import InvoiceRepo, { import InvoiceRepo, {
invoiceRepoKey, invoiceRepoKey,

View File

@ -1,4 +1,3 @@
import "server-only";
import serverDi from "@/feature/common/server.di"; import serverDi from "@/feature/common/server.di";
import InvoiceRepo, { import InvoiceRepo, {
invoiceRepoKey, invoiceRepoKey,

View File

@ -1,4 +1,3 @@
import "server-only";
import serverDi from "@/feature/common/server.di"; import serverDi from "@/feature/common/server.di";
import Revenue from "@/feature/core/revenue/domain/entity/revenue.entity"; import Revenue from "@/feature/core/revenue/domain/entity/revenue.entity";
import RevenueRepo, { import RevenueRepo, {

View File

@ -1,4 +1,3 @@
import "server-only";
import serverDi from "@/feature/common/server.di"; import serverDi from "@/feature/common/server.di";
import fetchCustomersAmountUsecase from "@/feature/core/customer/domain/usecase/fetch-customers-amount-usecase"; import fetchCustomersAmountUsecase from "@/feature/core/customer/domain/usecase/fetch-customers-amount-usecase";
import fetchAllInvoicesAmountUsecase from "@/feature/core/invoice/domain/usecase/fetch-all-invoices-amount.usecase"; import fetchAllInvoicesAmountUsecase from "@/feature/core/invoice/domain/usecase/fetch-all-invoices-amount.usecase";