diff --git a/src/controllers/challenge.location.controller.ts b/src/controllers/challenge.location.controller.ts index dcb9f7a..c83cc09 100644 --- a/src/controllers/challenge.location.controller.ts +++ b/src/controllers/challenge.location.controller.ts @@ -12,7 +12,7 @@ import { ResponseFromLocationChallenge, } from '../models/challenge.entities.js'; import {bodyToLocationCreation} from '../dtos/challenge.dtos.js'; -import {BaseError, DataValidationError, ServerError} from '../errors.js'; +import {BaseError, DataValidationError, LocationChallengeCreationError, ServerError} from '../errors.js'; import { Controller, Post, @@ -30,6 +30,7 @@ import { ITsoaSuccessResponse, TsoaSuccessResponse, } from '../models/tsoaResponse.js'; +import { challengeExist } from 'src/repositories/challenge.repositories.js'; @Route('challenge') export class LocationController extends Controller { @@ -240,6 +241,16 @@ export class LocationController extends Controller { timestamp: Date; }[], ): Promise> { + if (!req.user) { + throw new DataValidationError({ + reason: '유저 정보가 없습니다. 다시 로그인 해주세요.', + }); + } + + if(await challengeExist(req.user.id)){ + throw new LocationChallengeCreationError({reason: '이미 챌린지가 존재합니다.'}); + } + if (!body) { throw new DataValidationError({reason: '사진 데이터가 없습니다.'}); } diff --git a/src/repositories/challenge.repositories.ts b/src/repositories/challenge.repositories.ts index 4a7caad..1a921a5 100644 --- a/src/repositories/challenge.repositories.ts +++ b/src/repositories/challenge.repositories.ts @@ -12,6 +12,43 @@ import { } from '../models/challenge.entities.js'; import {Challenge, LocationChallenge} from '@prisma/client'; +export const challengeExist = async (userId: bigint): Promise => { + const currentTime = new Date(); //현재 시간 + const minute: number = currentTime.getMinutes() > 30 ? 30 : 0; + currentTime.setMinutes(minute, 0, 0); //시간 초기화 + const nextTime = new Date(); + nextTime.setMinutes(currentTime.getMinutes() + 30, 0, 0); + + // if(minute === 30){ + // currentTime.setHours(currentTime.getHours() - 1); + // nextTime.setHours(nextTime.getHours() - 1); + // } + + // console.log(currentTime); + // console.log(nextTime); + // console.log(new Date(currentTime.toUTCString())); + // console.log(new Date(nextTime.toUTCString())); + + const isExistChallenge = await prisma.challenge.findFirst({ + where: { + userId: userId, + createdAt: { + lt: new Date(nextTime.toUTCString()), + gte: new Date(currentTime.toUTCString()) + } + } + }); + + console.log(isExistChallenge); + + if(isExistChallenge){ + return true; + } + else{ + return false; + } +}; + export const newLocationChallenge = async ( data: LocationChallengeCreation, ): Promise => { diff --git a/src/repositories/history.repositories.ts b/src/repositories/history.repositories.ts index 90a5df1..5d9998e 100644 --- a/src/repositories/history.repositories.ts +++ b/src/repositories/history.repositories.ts @@ -73,7 +73,7 @@ export const getMostTagged = async (userId: bigint, year: number, month: number) export const newUserAward = async (id: bigint): Promise => { const currentDate: Date = new Date(); - currentDate.setMonth(currentDate.getMonth() + 1); + currentDate.setMonth(currentDate.getMonth()); currentDate.setDate(1); currentDate.setHours(0, 0, 0, 0);