Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/hexagonal pattern poc v2 #235

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
c165af7
feat: add ApiClient class with dependency injection
timothyrusso Aug 20, 2024
4a261bb
feat: add new Ideation Service
timothyrusso Aug 21, 2024
480996a
feat: tsyring configuration
timothyrusso Aug 21, 2024
ee52014
feat: tsyring configuration
timothyrusso Aug 21, 2024
c1e39ff
feat: tsyring configuration
timothyrusso Aug 21, 2024
673017d
feat: tsyring configuration
timothyrusso Aug 21, 2024
b882198
feat: add ideation use cases
timothyrusso Aug 22, 2024
fe1455f
feat: add injection utils
timothyrusso Aug 22, 2024
e148ba1
Merge remote-tracking branch 'origin/dev' into feature/exagonal-patte…
timothyrusso Aug 22, 2024
775d568
Merge branch 'feature/exagonal-pattern-poc' of github.com:chingu-x/ch…
Dan-Y-Ko Sep 2, 2024
77393e3
rest api module implementation
Dan-Y-Ko Sep 2, 2024
e8800cf
add dtos
Dan-Y-Ko Sep 3, 2024
75aab33
add ideation api repository implementation
Dan-Y-Ko Sep 3, 2024
de50783
refactor usecases
Dan-Y-Ko Sep 3, 2024
7065ad1
refactor add ideation in service
Dan-Y-Ko Sep 3, 2024
16865ee
test
Dan-Y-Ko Sep 3, 2024
83dc75e
move dtos
Dan-Y-Ko Sep 3, 2024
b5dcce9
refactor folder structure
Dan-Y-Ko Sep 4, 2024
effa6d9
usecase implement port
Dan-Y-Ko Sep 4, 2024
2a72351
test
Dan-Y-Ko Sep 4, 2024
6b789c3
test
Dan-Y-Ko Sep 4, 2024
98f341b
why is this so annoying
Dan-Y-Ko Sep 4, 2024
5972f54
update filename casing
Dan-Y-Ko Sep 4, 2024
99ae94b
not so optimal solution to enforcing cache and token
Dan-Y-Ko Sep 4, 2024
1c6d399
refactor server action as a functon to act as a wrapper calling a met…
Dan-Y-Ko Oct 1, 2024
5109f3d
rename ideation service file
Dan-Y-Ko Oct 1, 2024
f9a2a4c
move types
Dan-Y-Ko Oct 1, 2024
a857f86
fix build errors
Dan-Y-Ko Oct 1, 2024
5641cfb
move class instantiation out of the function
Dan-Y-Ko Oct 1, 2024
2ab7364
refactor server action and adapter
Dan-Y-Ko Oct 1, 2024
8aa077f
refactor types
Dan-Y-Ko Oct 1, 2024
23f220a
remove unnecessary interface
Dan-Y-Ko Oct 1, 2024
eaa93a9
fix build error
Dan-Y-Ko Oct 1, 2024
2f622a6
Merge branch 'dev' of github.com:chingu-x/chingu-dashboard into refac…
Dan-Y-Ko Oct 1, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
"react-hook-form": "^7.46.2",
"react-redux": "^8.1.2",
"redux-persist": "^6.0.0",
"reflect-metadata": "^0.2.2",
"tailwind-merge": "^2.0.0",
"tsyringe": "^4.8.0",
"zod": "^3.22.2"
},
"devDependencies": {
Expand Down
70 changes: 61 additions & 9 deletions src/app/(main)/my-voyage/[teamId]/ideation/ideationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import { getAccessToken } from "@/utils/getCookie";
import { DELETE, PATCH, POST } from "@/utils/requests";
import { type AsyncActionResponse, handleAsync } from "@/utils/handleAsync";
import { CacheTag } from "@/utils/cacheTag";
import { type AddIdeationResponseDto } from "@/modules/ideation/application/dtos/response.dto";
import { NextJsRestApiAdapter } from "@/modules/restApi/adapters/secondary/nextJsRestApiAdapter";
import { IdeationApiAdapter } from "@/modules/ideation/adapters/secondary/ideationApiAdapter";
import { type AddIdeationUsecaseDto } from "@/modules/ideation/application/dtos/addIdeationUsecaseDto";
import { AddIdeationUseCase } from "@/modules/ideation/application/usecases/addIdeationUseCase";
import { type AddIdeationRequestDto } from "@/modules/ideation/application/dtos/request.dto";

interface IdeationProps {
teamId: number;
Expand Down Expand Up @@ -53,23 +59,43 @@ export interface IdeationVoteResponse extends IdeationResponse {
projectIdeaId: number;
}

const nextJsRestApiAdapter = new NextJsRestApiAdapter(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, do we prefer to keep all the types/interfaces in the same file with the implementation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really sure what you mean by this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like I mentioned before, I prefer keeping types/interfaces separate from the actual implementation/logic

process.env.NEXT_PUBLIC_API_URL!,
);
const ideationApiPort = new IdeationApiAdapter(nextJsRestApiAdapter);

interface NextJsAddIdeationAdapter extends AddIdeationUseCase {
execute(
props: Required<AddIdeationRequestDto>,
): Promise<AddIdeationResponseDto>;
}

export async function addIdeation({
teamId,
title,
description,
vision,
}: AddIdeationProps): Promise<AsyncActionResponse<AddIdeationResponse>> {
}: AddIdeationUsecaseDto): Promise<
AsyncActionResponse<AddIdeationResponseDto>
> {
const token = getAccessToken();

const addIdeationAsync = () =>
POST<AddIdeationBody, AddIdeationResponse>(
`api/v1/voyages/teams/${teamId}/ideations`,
const addIdeationUseCase: NextJsAddIdeationAdapter = new AddIdeationUseCase(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use the "const { Class } = resolveInjection()" (server side) method to import the class from the tsyringe container and use it here or in a component.
Or we can use "const { walletUseCases } = useInjection()" (client side).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a stupid example from another project:

// CLIENT SIDE

const { classUseCase } = useInjection();

useEffect(() => {
	getData();
}, []);

const getData = async () => {
	const response = await classUseCase().getUserData();
	if (response.success) {
                      // do something
	} else {
		// do something
	}
};

// SERVER SIDE

const { classUseCase } = resolveInjection();
const getData = classUseCase.getUserData();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we resolve the dependency of that usecase? For example in the add ideation usecase, it needs to inject the ideationApi (type ideationApiPort)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as you can see I instantiate it like const addIdeationUseCase = new AddIdeationUseCase(ideationApiPort);

ideationApiPort,
);

// refactor this later to not expect a function
const addIdeationAsync = async () =>
await addIdeationUseCase.execute({
teamId,
title,
description,
vision,
cache: "default",
token,
"default",
{ title, description, vision },
);
});

const [res, error] = await handleAsync(addIdeationAsync);
const [res, error] =
await handleAsync<AddIdeationResponseDto>(addIdeationAsync);

if (res) {
revalidateTag(CacheTag.ideation);
Expand All @@ -78,6 +104,32 @@ export async function addIdeation({
return [res, error];
}

// test
// export async function addIdeation({
// teamId,
// title,
// description,
// vision,
// }: AddIdeationProps): Promise<AsyncActionResponse<AddIdeationResponse>> {
// const token = getAccessToken();

// const addIdeationAsync = () =>
// POST<AddIdeationBody, AddIdeationResponse>(
// `api/v1/voyages/teams/${teamId}/ideations`,
// token,
// "default",
// { title, description, vision },
// );

// const [res, error] = await handleAsync(addIdeationAsync);

// if (res) {
// revalidateTag(CacheTag.ideation);
// }

// return [res, error];
// }

export async function editIdeation({
ideationId,
title,
Expand Down
2 changes: 2 additions & 0 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Inter } from "next/font/google";
import StoreProvider from "@/components/providers/StoreProvider";
import ThemeProvider from "@/components/providers/ThemeProvider";
import ModalProvider from "@/components/providers/ModalProvider";
import "reflect-metadata";
import "@/di/config";

export const metadata: Metadata = {
title: "Chingu Dashboard",
Expand Down
17 changes: 17 additions & 0 deletions src/di/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { container } from "tsyringe";

import { TYPES } from "./types";
import { type IdeationApiPort } from "@/modules/ideation/ports/secondary/ideationApiPort";
import { IdeationApiAdapter } from "@/modules/ideation/adapters/secondary/ideationApiAdapter";
import { type RestApiPort } from "@/modules/restApi/ports/secondary/restApiPort";
import { NextJsRestApiAdapter } from "@/modules/restApi/adapters/secondary/nextJsRestApiAdapter";

container.register<IdeationApiPort>(TYPES.IdeationApiPort, {
useClass: IdeationApiAdapter,
});

container.register<RestApiPort>(TYPES.RestApiPort, {
useValue: new NextJsRestApiAdapter(process.env.NEXT_PUBLIC_API_URL!),
});

export default container;
30 changes: 30 additions & 0 deletions src/di/injectables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { container } from "tsyringe";
import { TYPES } from "./types";
import type { AddIdeationUseCase } from "@/modules/ideation/application/usecases/addIdeationUseCase";
import type { AddIdeationVoteUseCase } from "@/modules/ideation/application/usecases/addIdeationVoteUseCase";
import type { DeleteIdeationUseCase } from "@/modules/ideation/application/usecases/deleteIdeationUseCase";
import type { EditIdeationUseCase } from "@/modules/ideation/application/usecases/editIdeationUseCase";
import type { FinalizeIdeationUseCase } from "@/modules/ideation/application/usecases/finalizeIdeationUseCase";
import type { RemoveIdeationVoteUseCase } from "@/modules/ideation/application/usecases/removeIdeationVoteUseCase";

export const injectables = {
/* Ideation */
addIdeationUseCase: container.resolve<AddIdeationUseCase>(
TYPES.AddIdeationUseCase,
),
addIdeationVoteUseCase: container.resolve<AddIdeationVoteUseCase>(
TYPES.AddIdeationVoteUseCase,
),
deleteIdeationUseCase: container.resolve<DeleteIdeationUseCase>(
TYPES.DeleteIdeationUseCase,
),
editIdeationUseCase: container.resolve<EditIdeationUseCase>(
TYPES.EditIdeationUseCase,
),
finalizeIdeationUseCase: container.resolve<FinalizeIdeationUseCase>(
TYPES.FinalizeIdeationUseCase,
),
removeIdeationVoteUseCase: container.resolve<RemoveIdeationVoteUseCase>(
TYPES.RemoveIdeationVoteUseCase,
),
} as const;
9 changes: 9 additions & 0 deletions src/di/resolveInjection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import "reflect-metadata";

import { injectables } from "./injectables";

const resolveInjection = () => ({
...injectables,
});

export default resolveInjection;
10 changes: 10 additions & 0 deletions src/di/resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { InjectionToken } from "tsyringe";

import container from "./config";

export const resolve =
<T>(token: InjectionToken<T>) =>
// eslint-disable-next-line indent
() =>
// eslint-disable-next-line indent
container.resolve(token);
17 changes: 17 additions & 0 deletions src/di/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const TYPES = {
/* Ports */
RestApiPort: Symbol.for("RestApiPort"),
IdeationApiPort: Symbol.for("IdeationApiPort"),

/* Adapters */
// NextJsRestApiAdapter: Symbol.for("NextJsRestApiAdapter"),
IdeationApiAdapter: Symbol.for("IdeationApiAdapter"),

/* UseCases */
AddIdeationUseCase: Symbol.for("AddIdeationUseCase"),
AddIdeationVoteUseCase: Symbol.for("AddIdeationVoteUseCase"),
DeleteIdeationUseCase: Symbol.for("DeleteIdeationUseCase"),
EditIdeationUseCase: Symbol.for("EditIdeationUseCase"),
FinalizeIdeationUseCase: Symbol.for("FinalizeIdeationUseCase"),
RemoveIdeationVoteUseCase: Symbol.for("RemoveIdeationVoteUseCase"),
};
18 changes: 18 additions & 0 deletions src/hooks/useInjection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import "reflect-metadata";

import { useMemo } from "react";

import { injectables } from "@/di/injectables";

const useInjection = () => {
const injection = useMemo(
() => ({
...injectables,
}),
[],
);

return injection;
};

export default useInjection;
128 changes: 128 additions & 0 deletions src/modules/ideation/adapters/secondary/ideationApiAdapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { inject, injectable } from "tsyringe";
import { TYPES } from "@/di/types";
import { type IdeationApiPort } from "@/modules/ideation/ports/secondary/ideationApiPort";
import { type RestApiPort } from "@/modules/restApi/ports/secondary/restApiPort";
import type {
AddIdeationBodyDto,
AddIdeationRequestDto,
DeleteIdeationRequestDto,
EditIdeationBodyDto,
EditIdeationRequestDto,
FinalizeIdeationRequestDto,
IdeationVoteRequestDto,
} from "@/modules/ideation/application/dtos/request.dto";
import type {
EditIdeationResponseDto,
AddIdeationResponseDto,
DeleteIdeationResponseDto,
IdeationVoteResponseDto,
FinalizeIdeationResponseDto,
} from "@/modules/ideation/application/dtos/response.dto";
import { IdeationUrls } from "@/modules/ideation/application/constants/ideationUrls";

@injectable()
export class IdeationApiAdapter implements IdeationApiPort {
constructor(
@inject(TYPES.RestApiPort)
private readonly apiClient: RestApiPort,
) {}

async addIdeation({
teamId,
title,
description,
vision,
cache,
token,
}: AddIdeationRequestDto): Promise<AddIdeationResponseDto> {
return await this.apiClient.post<
AddIdeationBodyDto,
AddIdeationResponseDto
>({
url: `${IdeationUrls.BASE_URL_TEAMS}/${teamId}/${IdeationUrls.SUB_URLS.IDEATIONS}`,
options: {
cache,
token,
},
payload: { title, description, vision },
});
}

async editIdeation({
ideationId,
title,
description,
vision,
cache,
token,
}: EditIdeationRequestDto): Promise<EditIdeationResponseDto> {
return await this.apiClient.patch<
EditIdeationBodyDto,
EditIdeationResponseDto
>({
url: `${IdeationUrls.BASE_URL_IDEATIONS}/${ideationId}`,
options: {
cache,
token,
},
payload: { title, description, vision },
});
}

async deleteIdeation({
ideationId,
cache,
token,
}: DeleteIdeationRequestDto): Promise<DeleteIdeationResponseDto> {
return await this.apiClient.delete<DeleteIdeationResponseDto>({
url: `${IdeationUrls.BASE_URL_IDEATIONS}/${ideationId}`,
options: {
cache,
token,
},
});
}

async addIdeationVote({
ideationId,
cache,
token,
}: IdeationVoteRequestDto): Promise<IdeationVoteResponseDto> {
return await this.apiClient.post<undefined, IdeationVoteResponseDto>({
url: `${IdeationUrls.BASE_URL_IDEATIONS}/${ideationId}/${IdeationUrls.SUB_URLS.IDEATION_VOTES}`,
options: {
cache,
token,
},
});
}

async removeIdeationVote({
ideationId,
cache,
token,
}: IdeationVoteRequestDto): Promise<IdeationVoteResponseDto> {
return await this.apiClient.delete<IdeationVoteResponseDto>({
url: `${IdeationUrls.BASE_URL_IDEATIONS}/${ideationId}/${IdeationUrls.SUB_URLS.IDEATION_VOTES}`,
options: {
cache,
token,
},
});
}

async finalizeIdeation({
teamId,
ideationId,
cache,
token,
}: FinalizeIdeationRequestDto): Promise<FinalizeIdeationResponseDto> {
return await this.apiClient.post<undefined, FinalizeIdeationResponseDto>({
url: `${IdeationUrls.BASE_URL_TEAMS}/${teamId}/${IdeationUrls.SUB_URLS.IDEATIONS}/${ideationId}/${IdeationUrls.SUB_URLS.SELECT}`,
options: {
cache,
token,
},
});
}
}
9 changes: 9 additions & 0 deletions src/modules/ideation/application/constants/ideationUrls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const IdeationUrls = {
BASE_URL_IDEATIONS: "api/v1/voyages/ideations",
BASE_URL_TEAMS: "api/v1/voyages/teams",
SUB_URLS: {
IDEATIONS: "ideations",
IDEATION_VOTES: "ideation-votes",
SELECT: "select",
},
} as const;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type {
IdeationBodyDto,
IdeationRequestDto,
} from "@/modules/ideation/application/dtos/common.dto";

export interface AddIdeationUsecaseDto
extends Pick<IdeationRequestDto, "teamId">,
IdeationBodyDto {}
11 changes: 11 additions & 0 deletions src/modules/ideation/application/dtos/common.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { type Ideation } from "@/modules/ideation/application/entities/ideation";

export type IdeationBodyDto = Pick<
Ideation,
"title" | "description" | "vision"
>;

export interface IdeationRequestDto {
teamId: number;
ideationId: number;
}
Loading
Loading