Skip to content
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
13 changes: 12 additions & 1 deletion src/controllers/challenge.location.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -240,6 +241,16 @@ export class LocationController extends Controller {
timestamp: Date;
}[],
): Promise<ITsoaSuccessResponse<PhotoInfo[]>> {
if (!req.user) {
throw new DataValidationError({
reason: '유저 정보가 없습니다. 다시 로그인 해주세요.',
});
}

if(await challengeExist(req.user.id)){
throw new LocationChallengeCreationError({reason: '이미 챌린지가 존재합니다.'});
}

if (!body) {
throw new DataValidationError({reason: '사진 데이터가 없습니다.'});
}
Expand Down
37 changes: 37 additions & 0 deletions src/repositories/challenge.repositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,43 @@ import {
} from '../models/challenge.entities.js';
import {Challenge, LocationChallenge} from '@prisma/client';

export const challengeExist = async (userId: bigint): Promise<Boolean> => {
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<Challenge | null> => {
Expand Down
2 changes: 1 addition & 1 deletion src/repositories/history.repositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const getMostTagged = async (userId: bigint, year: number, month: number)

export const newUserAward = async (id: bigint): Promise<Award | null> => {
const currentDate: Date = new Date();
currentDate.setMonth(currentDate.getMonth() + 1);
currentDate.setMonth(currentDate.getMonth());
currentDate.setDate(1);
currentDate.setHours(0, 0, 0, 0);

Expand Down