From c930295157f8ed13423c4196dcefa3b98a59a00a Mon Sep 17 00:00:00 2001 From: jspark2000 Date: Tue, 19 Mar 2024 07:53:48 +0000 Subject: [PATCH] fix: block ended survey --- backend/app/src/survey/survey.controller.ts | 12 +++++++ backend/app/src/survey/survey.service.ts | 27 ++++++++++++++++ .../[id]/_components/SurveyGroupCard.tsx | 9 ------ .../src/app/(public)/survey/[id]/page.tsx | 32 +++++++++++++++---- frontend/src/lib/actions.ts | 9 ++++++ 5 files changed, 73 insertions(+), 16 deletions(-) diff --git a/backend/app/src/survey/survey.controller.ts b/backend/app/src/survey/survey.controller.ts index cc51b7e..95cae63 100644 --- a/backend/app/src/survey/survey.controller.ts +++ b/backend/app/src/survey/survey.controller.ts @@ -36,6 +36,18 @@ export class SurveyController { } } + @Public() + @Get('/groups/:surveyGroupId/is-ended') + async checkIsEndedSurvey( + @Param('surveyGroupId', ParseIntPipe) surveyGroupId: number + ): Promise<{ ended: boolean }> { + try { + return await this.surveyService.checkIsEndedSurvey(surveyGroupId) + } catch (error) { + BusinessExceptionHandler(error) + } + } + @Public() @Get('/groups/:surveyGroupId/schedules') async getSurveyGroupWithSchedule( diff --git a/backend/app/src/survey/survey.service.ts b/backend/app/src/survey/survey.service.ts index 90b3e45..602507e 100644 --- a/backend/app/src/survey/survey.service.ts +++ b/backend/app/src/survey/survey.service.ts @@ -80,6 +80,33 @@ export class SurveyService { } } + async checkIsEndedSurvey(surveyGroupId: number): Promise<{ + ended: boolean + }> { + try { + const now = new Date() + + const { endedAt } = await this.prisma.surveyGroup.findUniqueOrThrow({ + where: { + id: surveyGroupId + }, + select: { + endedAt: true + } + }) + + return { ended: new Date(endedAt) >= now } + } catch (error) { + if ( + error instanceof Prisma.PrismaClientKnownRequestError && + error.code === 'P2025' + ) { + throw new EntityNotExistException('출석조사 그룹이 존재하지 않습니다') + } + throw new UnexpectedException(error) + } + } + async getSurveyGroups( page: number, limit = 10 diff --git a/frontend/src/app/(public)/survey/[id]/_components/SurveyGroupCard.tsx b/frontend/src/app/(public)/survey/[id]/_components/SurveyGroupCard.tsx index b979778..2c70172 100644 --- a/frontend/src/app/(public)/survey/[id]/_components/SurveyGroupCard.tsx +++ b/frontend/src/app/(public)/survey/[id]/_components/SurveyGroupCard.tsx @@ -3,21 +3,12 @@ import LocalTime from '@/components/Localtime' import { Card, CardContent, CardHeader } from '@/components/ui/card' import type { SurveyGroupListItem } from '@/lib/types/survey' import { PencilSquareIcon } from '@heroicons/react/24/outline' -import { useRouter } from 'next/navigation' -import { toast } from 'sonner' export default async function SurveyGroupCard({ surveyGroup }: { surveyGroup: SurveyGroupListItem }) { - const router = useRouter() - - if (new Date(surveyGroup.endedAt) >= new Date()) { - toast.error('마감된 출석조사입니다') - router.push('/survey') - } - const renderSurveyStatus = (surveyGroup: SurveyGroupListItem) => { const now = new Date() if (now > new Date(surveyGroup.endedAt)) { diff --git a/frontend/src/app/(public)/survey/[id]/page.tsx b/frontend/src/app/(public)/survey/[id]/page.tsx index 16015c2..f0ed646 100644 --- a/frontend/src/app/(public)/survey/[id]/page.tsx +++ b/frontend/src/app/(public)/survey/[id]/page.tsx @@ -1,5 +1,7 @@ -import { getSurveyGroup } from '@/lib/actions' +import { Button } from '@/components/ui/button' +import { getIsEndedSurvey, getSurveyGroup } from '@/lib/actions' import type { Metadata } from 'next' +import Link from 'next/link' import StudentIdForm from './_components/StudentIdForm' import SubmitSurveySection from './_components/SubmitSurveySection' import SurveyGroupCardSection from './_components/SurveyGroupCardSection' @@ -29,16 +31,32 @@ export default async function SubmitSurvey({ studentId?: string } }) { + const { ended: isEnded } = await getIsEndedSurvey(params.id) + return (
- - {searchParams?.studentId ? ( -
- -
+ {isEnded ? ( + <> +

마감된 출석조사 입니다

+ + + + ) : ( - + <> + + {searchParams?.studentId ? ( +
+ +
+ ) : ( + + )} + )}
diff --git a/frontend/src/lib/actions.ts b/frontend/src/lib/actions.ts index 4168e57..932734b 100644 --- a/frontend/src/lib/actions.ts +++ b/frontend/src/lib/actions.ts @@ -44,6 +44,15 @@ export const getSurveyGroups = async ( ) } +export const getIsEndedSurvey = async ( + surveyGroupId: number +): Promise<{ ended: boolean }> => { + return await fetcher.get<{ ended: boolean }>( + `/surveys/groups/${surveyGroupId}/is-ended`, + false + ) +} + export const getSurveyGroupWithSchedules = async ( surveyGroupId: number ): Promise => {