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

feat base repository and fix funding api for executive feedbacks #1419

Merged
merged 8 commits into from
Feb 5, 2025
10 changes: 5 additions & 5 deletions packages/api/src/common/repository/base.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ interface TableWithId {
id: MySqlColumn<ColumnBaseConfig<ColumnDataType, string>>;
}

interface ModelStatic<T extends MEntity, D> {
fromDbResult(result: D): T;
interface ModelWithFrom<T extends MEntity, D> {
from(result: D): T;
}

@Injectable()
Expand All @@ -30,7 +30,7 @@ export abstract class BaseRepository<

constructor(
protected table: T,
protected modelClass: ModelStatic<M, D>,
protected modelClass: ModelWithFrom<M, D>,
) {}

async withTransaction<Result>(
Expand All @@ -44,7 +44,7 @@ export abstract class BaseRepository<
.select()
.from(this.table)
.where(eq(this.table.id, id))
.then(rows => rows.map(row => this.modelClass.fromDbResult(row as D)));
.then(rows => rows.map(row => this.modelClass.from(row as D)));

return (result[0] as M) ?? null;
}
Expand Down Expand Up @@ -75,7 +75,7 @@ export abstract class BaseRepository<
.from(this.table)
.where(inArray(this.table.id, ids));

return result.map(row => this.modelClass.fromDbResult(row as D));
return result.map(row => this.modelClass.from(row as D));
}

async fetchAll(ids: number[]): Promise<M[]> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class MFundingComment extends MEntity implements IFundingComment {
);
}

static fromDbResult(result: FundingCommentDbResult): MFundingComment {
static from(result: FundingCommentDbResult): MFundingComment {
return new MFundingComment({
id: result.id,
funding: { id: result.fundingId },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default class FundingCommentRepository extends BaseRepository<
.from(FundingFeedback)
.where(eq(FundingFeedback.fundingId, arg1));

return result.map(row => MFundingComment.fromDbResult(row));
return result.map(row => MFundingComment.from(row));
}

async fetchAll(fundingId: number): Promise<MFundingComment[]>;
Expand Down
21 changes: 3 additions & 18 deletions packages/api/src/feature/funding/service/funding.service.ts
Copy link
Contributor

Choose a reason for hiding this comment

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

엔티티로 묶는 게 아니라 API Interface에서 정의해서 보내주는 건가?

Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,14 @@ export default class FundingService {
executive => executive.id === comment.chargedExecutive.id,
);
});
funding.comments = comments.map(comment => ({
const commentResponses = comments.map(comment => ({
...comment,
chargedExecutive: chargedExecutive.find(
executive => executive.id === comment.chargedExecutive.id,
),
}));

return funding;
return { funding, comments: commentResponses };
}

private async buildFundingResponse(
Expand Down Expand Up @@ -286,26 +286,11 @@ export default class FundingService {
};
}

const comments = await this.fundingCommentRepository.fetchAll(funding.id);

const chargedExecutive =
await this.userPublicService.fetchExecutiveSummaries(
comments.map(comment => comment.chargedExecutive.id),
);

const commentResponses = comments.map(comment => ({
...comment,
chargedExecutive: chargedExecutive.find(
executive => executive.id === comment.chargedExecutive.id,
),
}));

return {
...funding,
purposeActivity,
...resolvedFiles,
transportation,
comments: commentResponses,
};
}

Expand Down Expand Up @@ -487,7 +472,7 @@ export default class FundingService {
const funding = await this.fundingRepository.fetch(id); // TODO: 이거 이래도 되나? comments 필드가 없는데. 에러 안나나?

const fundingResponse = await this.buildFundingResponse(funding);
return fundingResponse;
return { funding: fundingResponse, comments: [] };
}

/**
Expand Down
6 changes: 5 additions & 1 deletion packages/interface/src/api/funding/endpoint/apiFnd002.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HttpStatusCode } from "axios";
import { z } from "zod";

import { zFundingCommentResponse } from "../type/funding.comment.type";
import { zFundingResponse } from "../type/funding.type";

/**
Expand All @@ -20,7 +21,10 @@ const requestQuery = z.object({});
const requestBody = z.object({});

const responseBodyMap = {
[HttpStatusCode.Ok]: zFundingResponse,
[HttpStatusCode.Ok]: z.object({
funding: zFundingResponse,
comments: z.array(zFundingCommentResponse),
}),
};

const responseErrorMap = {};
Expand Down
6 changes: 5 additions & 1 deletion packages/interface/src/api/funding/endpoint/apiFnd012.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HttpStatusCode } from "axios";
import { z } from "zod";

import { zFundingCommentResponse } from "../type/funding.comment.type";
import { zFundingResponse } from "../type/funding.type";

/**
Expand All @@ -23,7 +24,10 @@ const requestQuery = z.object({});
const requestBody = z.object({});

const responseBodyMap = {
[HttpStatusCode.Ok]: zFundingResponse,
[HttpStatusCode.Ok]: z.object({
funding: zFundingResponse,
comments: z.array(zFundingCommentResponse),
}),
};

const responseErrorMap = {};
Expand Down
2 changes: 1 addition & 1 deletion packages/interface/src/api/funding/endpoint/apiFnd013.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { HttpStatusCode } from "axios";
import { z } from "zod";

import { zFundingCommentRequest } from "../type/funding.comment.type";
import { zFunding } from "../type/funding.common.type";
import { zFunding } from "../type/funding.type";

/**
* @version v0.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { zExecutiveSummary } from "@sparcs-clubs/interface/api/user/type/user.ty
import { FundingStatusEnum } from "@sparcs-clubs/interface/common/enum/funding.enum";
import zId from "@sparcs-clubs/interface/common/type/id.type";

import { zFunding } from "./funding.common.type";
import { zFunding } from "./funding.type";

export const zFundingComment = z.object({
id: zId,
Expand Down
113 changes: 0 additions & 113 deletions packages/interface/src/api/funding/type/funding.common.type.ts
Copy link
Contributor Author

@pbc1017 pbc1017 Feb 2, 2025

Choose a reason for hiding this comment

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

funding.comment.type.ts와 funding.type.ts 간의 순환참조 문제를 해결하기 위해 별도 파일로 분리

Copy link
Contributor

Choose a reason for hiding this comment

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

common은 너무 가독성 떨어지는 거 같은데
순환 참조 문제는 나도 알고 있는데 다른 해결법 없나?

This file was deleted.

Loading