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

[Delegate] 대표자 변경 요청이 만료되지 않는 오류 #1414

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class ClubDelegateDRepository {
/**
* @param id 삭제할 변경 요청의 id
*/
async deleteDelegatChangeRequestById(param: {
async deleteDelegateChangeRequestById(param: {
id: number;
}): Promise<boolean> {
const [result] = await this.db
Expand Down Expand Up @@ -77,18 +77,13 @@ export class ClubDelegateDRepository {
* 3일 이내에 신청된 요청만을 조회합니다.
* 최근에 신청된 요청이 가장 위에 위치합니다.
*/
// TODO: 만료 enum 추가
async findDelegateChangeRequestByClubId(param: { clubId: number }) {
const threeDaysAgo = new Date();
threeDaysAgo.setDate(threeDaysAgo.getDate() - 3);

const result = await this.db
.select()
.from(ClubDelegateChangeRequest)
.where(
and(
eq(ClubDelegateChangeRequest.clubId, param.clubId),
gte(ClubDelegateChangeRequest.createdAt, threeDaysAgo),
isNull(ClubDelegateChangeRequest.deletedAt),
),
)
Expand Down
40 changes: 39 additions & 1 deletion packages/api/src/feature/club/delegate/delegate.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,40 @@ export default class ClubDelegateService {
private clubPublicService: ClubPublicService,
) {}

/**
* @param clubId 동아리 Id
* @description 해당 동아리의 대표자 변경 요청 중 3일이 지난 요청을 만료(soft delete)합니다.
* **_모든 변경 요청 조회 관련 로직에서 조회 이전에 호출되어야 합니다._**
*/
private async cleanExpiredChangeRequests(param: {
clubId: number;
}): Promise<void> {
const requests =
await this.clubDelegateDRepository.findDelegateChangeRequestByClubId({
clubId: param.clubId,
});

const threeDaysAgo = getKSTDate();
threeDaysAgo.setDate(threeDaysAgo.getDate() - 3);

await Promise.all(
requests.map(async request => {
if (
request.clubDelegateChangeRequestStatusEnumId ===
ClubDelegateChangeRequestStatusEnum.Applied &&
request.createdAt < threeDaysAgo
) {
logger.debug(
`Found expired change request created on ${request.createdAt}`,
);
await this.clubDelegateDRepository.deleteDelegateChangeRequestById({
id: request.id,
});
}
}),
);
}

async getStudentClubDelegates(
param: { studentId: number } & ApiClb006RequestParam,
): Promise<ApiClb006ResponseOK> {
Expand Down Expand Up @@ -225,6 +259,7 @@ export default class ClubDelegateService {
*
* @description getStudentClubDelegateRequests의 서비스 진입점입니다.
* 동아리 대표자 변경 요청을 조회합니다.
* 조회한 대표자 변경 요청이 3일이 지났다면 soft delete합니다.
*/
async getStudentClubDelegateRequests(param: {
param: ApiClb011RequestParam;
Expand All @@ -243,6 +278,9 @@ export default class ClubDelegateService {
HttpStatus.FORBIDDEN,
);

// 3일이 지난 요청은 soft delete합니다.
await this.cleanExpiredChangeRequests({ clubId: param.param.clubId });

const result =
await this.clubDelegateDRepository.findDelegateChangeRequestByClubId({
clubId: param.param.clubId,
Expand Down Expand Up @@ -360,7 +398,7 @@ export default class ClubDelegateService {
requests.map(request => {
if (request === undefined)
throw new HttpException("No request", HttpStatus.BAD_REQUEST);
return this.clubDelegateDRepository.deleteDelegatChangeRequestById({
return this.clubDelegateDRepository.deleteDelegateChangeRequestById({
id: request.id,
});
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,38 +54,51 @@ const ChangeRepresentativeCard: React.FC<{
item =>
item.value !==
delegatesNow?.delegates
.find(delegate => delegate.delegateEnumId === 1)
.find(
delegate =>
delegate.delegateEnumId === ClubDelegateEnum.Representative,
)
?.studentId?.toString(),
),
);

const representative =
delegatesNow?.delegates
.find(delegate => delegate.delegateEnumId === 1)
.find(
delegate => delegate.delegateEnumId === ClubDelegateEnum.Representative,
)
?.studentId?.toString() ?? "";

const representativeName =
delegatesNow?.delegates.find(delegate => delegate.delegateEnumId === 1)
?.name ?? "";
delegatesNow?.delegates.find(
delegate => delegate.delegateEnumId === ClubDelegateEnum.Representative,
)?.name ?? "";

const representativeStudentNumber =
delegatesNow?.delegates.find(delegate => delegate.delegateEnumId === 1)
?.studentNumber ?? "";
delegatesNow?.delegates.find(
delegate => delegate.delegateEnumId === ClubDelegateEnum.Representative,
)?.studentNumber ?? "";

const [delegate1, setDelegate1] = useState<string>(
delegatesNow?.delegates.find(delegate => delegate.delegateEnumId === 2)
?.studentId === 0
delegatesNow?.delegates.find(
delegate => delegate.delegateEnumId === ClubDelegateEnum.Delegate1,
)?.studentId === 0
? ""
: delegatesNow?.delegates
.find(delegate => delegate.delegateEnumId === 2)
.find(
delegate => delegate.delegateEnumId === ClubDelegateEnum.Delegate1,
)
?.studentId.toString() ?? "",
);
const [delegate2, setDelegate2] = useState<string>(
delegatesNow?.delegates.find(delegate => delegate.delegateEnumId === 3)
?.studentId === 0
delegatesNow?.delegates.find(
delegate => delegate.delegateEnumId === ClubDelegateEnum.Delegate2,
)?.studentId === 0
? ""
: delegatesNow?.delegates
.find(delegate => delegate.delegateEnumId === 3)
.find(
delegate => delegate.delegateEnumId === ClubDelegateEnum.Delegate2,
)
?.studentId.toString() ?? "",
);

Expand Down Expand Up @@ -138,7 +151,9 @@ const ChangeRepresentativeCard: React.FC<{
if (
delegate1 !==
delegatesNow?.delegates
.find(delegate => delegate.delegateEnumId === 2)
.find(
delegate => delegate.delegateEnumId === ClubDelegateEnum.Delegate1,
)
?.studentId?.toString() &&
type !== "Applied" &&
delegate1 !== ""
Expand All @@ -158,7 +173,9 @@ const ChangeRepresentativeCard: React.FC<{
if (
delegate2 !==
delegatesNow?.delegates
.find(delegate => delegate.delegateEnumId === 3)
.find(
delegate => delegate.delegateEnumId === ClubDelegateEnum.Delegate2,
)
?.studentId?.toString() &&
type !== "Applied" &&
delegate2 !== ""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React from "react";

import { ClubDelegateEnum } from "@sparcs-clubs/interface/common/enum/club.enum";

import AsyncBoundary from "@sparcs-clubs/web/common/components/AsyncBoundary";
import { useGetClubDetail } from "@sparcs-clubs/web/features/clubs/services/getClubDetail";
import ChangeRepresentativeCard from "@sparcs-clubs/web/features/manage-club/components/ChangeRepresentativeCard";
Expand All @@ -19,7 +21,10 @@ const RepresentativeLoadFrame: React.FC<{
data: clubMembers,
isLoading: clubMembersIsLoading,
isError: clubMembersIsError,
} = useGetDelegateCandidates({ clubId, delegateEnumId: 1 });
} = useGetDelegateCandidates({
clubId,
delegateEnumId: ClubDelegateEnum.Representative,
});

const {
data: clubInfo,
Expand Down