From 0f6f2129c994e56dc492bf85c50f5c203cdd3ee1 Mon Sep 17 00:00:00 2001 From: PleBea Date: Sun, 14 Jul 2024 00:25:13 +0900 Subject: [PATCH] refactor: Update AddressService to return formatted address data --- prisma/schema.prisma | 12 +++++++++++- src/modules/address/address.module.ts | 1 + src/modules/address/address.service.ts | 10 ++++++++-- src/modules/location/location.service.ts | 24 +++++++++++++---------- src/modules/plan/dto/create-plan.dto.ts | 7 +++---- src/modules/plan/dto/place.dto.ts | 23 ++++++++++++++++++++++ src/modules/plan/dto/plan-response.dto.ts | 10 +++++++--- src/modules/plan/plan.module.ts | 3 ++- src/modules/plan/plan.service.ts | 20 +++++++++++++++++-- 9 files changed, 87 insertions(+), 23 deletions(-) create mode 100644 src/modules/plan/dto/place.dto.ts diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 88c4b86..9b51758 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -38,13 +38,23 @@ model UserProfileImage { model Plan { id String @id @default(uuid()) name String - place String date String + place Place? users User[] Invite Invite[] createdAt DateTime @default(now()) } +model Place { + id String @id @default(uuid()) + name String + address String + x String + y String + planId String @unique + Plan Plan @relation(fields: [planId], references: [id]) +} + enum InviteStatus { PENDING ACCEPTED diff --git a/src/modules/address/address.module.ts b/src/modules/address/address.module.ts index 340470f..ad3bfaa 100644 --- a/src/modules/address/address.module.ts +++ b/src/modules/address/address.module.ts @@ -8,5 +8,6 @@ import { AddressService } from './address.service'; imports: [HttpModule], controllers: [AddressController], providers: [AddressService], + exports: [AddressService], }) export class AddressModule {} diff --git a/src/modules/address/address.service.ts b/src/modules/address/address.service.ts index ff80a99..192d99c 100644 --- a/src/modules/address/address.service.ts +++ b/src/modules/address/address.service.ts @@ -17,15 +17,21 @@ export class AddressService { this.apiKey = this.configService.get('KAKAO_REST_API_KEY'); } - public async getSimilarAddress(address: string): Promise { + public async getAddressByKeyword(address: string) { const url = 'https://dapi.kakao.com/v2/local/search/keyword.json'; const res = await this.httpService.axiosRef.get(url, { params: { query: address }, headers: { Authorization: `KakaoAK ${this.apiKey}` }, }); + return res.data; + } + + public async getSimilarAddress(address: string): Promise { + const place = await this.getAddressByKeyword(address); + return { - documents: res.data.documents + documents: place.documents .map((document) => ({ addressName: document.address_name, roadAddressName: document.road_address_name, diff --git a/src/modules/location/location.service.ts b/src/modules/location/location.service.ts index 5a25f0a..73f0ef1 100644 --- a/src/modules/location/location.service.ts +++ b/src/modules/location/location.service.ts @@ -1,29 +1,33 @@ +import { User } from '@prisma/client'; + import { CACHE_MANAGER, Cache } from '@nestjs/cache-manager'; import { Inject, Injectable } from '@nestjs/common'; -import { LocationDTO } from './dto/update.dto'; import { PlanService } from '../plan/plan.service'; -import { User } from '@prisma/client'; +import { LocationDTO } from './dto/update.dto'; @Injectable() export class LocationService { - constructor(@Inject(CACHE_MANAGER) private readonly cacheManager: Cache, private readonly planService: PlanService) {} + constructor( + @Inject(CACHE_MANAGER) private readonly cacheManager: Cache, + private readonly planService: PlanService, + ) {} async updateLocationById(id: string, data: LocationDTO) { return this.cacheManager.set(id, data); } /* - * Get participants location - * @param User - * @returns participants location { - * id: UpdateLocationDTO - * } - */ + * Get participants location + * @param User + * @returns participants location { + * id: UpdateLocationDTO + * } + */ async getParticipantsLocation(user: User): Promise> { const plan = await this.planService.getPlan(user); - let locations = {}; + const locations = {}; for (const user of plan.users) { locations[user.id] = await this.getLocationById(user.id); } diff --git a/src/modules/plan/dto/create-plan.dto.ts b/src/modules/plan/dto/create-plan.dto.ts index a3724cb..47267cd 100644 --- a/src/modules/plan/dto/create-plan.dto.ts +++ b/src/modules/plan/dto/create-plan.dto.ts @@ -1,9 +1,8 @@ -import { Prisma } from '@prisma/client'; import { IsString } from 'class-validator'; import { ApiProperty } from '@nestjs/swagger'; -export class CreatePlanDTO implements Prisma.PlanCreateInput { +export class CreatePlanDTO { @ApiProperty({ example: '개학전밤샘등교팟', }) @@ -11,10 +10,10 @@ export class CreatePlanDTO implements Prisma.PlanCreateInput { name: string; @ApiProperty({ - example: '선린인터넷고등학교', + example: '서울 용산구 청파동3가 131-2', }) @IsString() - place: string; + address: string; @ApiProperty({ example: '2017-03-16T17:40:00', diff --git a/src/modules/plan/dto/place.dto.ts b/src/modules/plan/dto/place.dto.ts new file mode 100644 index 0000000..1815941 --- /dev/null +++ b/src/modules/plan/dto/place.dto.ts @@ -0,0 +1,23 @@ +import { Place } from '@prisma/client'; + +import { ApiProperty } from '@nestjs/swagger'; + +export class PlaceDTO implements Place { + @ApiProperty() + id: string; + + @ApiProperty() + name: string; + + @ApiProperty() + address: string; + + @ApiProperty() + x: string; + + @ApiProperty() + y: string; + + @ApiProperty() + planId: string; +} diff --git a/src/modules/plan/dto/plan-response.dto.ts b/src/modules/plan/dto/plan-response.dto.ts index 1e733a0..3b6b767 100644 --- a/src/modules/plan/dto/plan-response.dto.ts +++ b/src/modules/plan/dto/plan-response.dto.ts @@ -1,9 +1,11 @@ -import { Plan, User } from '@prisma/client'; +import { Place, Plan, User } from '@prisma/client'; import { ApiProperty } from '@nestjs/swagger'; import { UserResponseDTO } from 'src/modules/user/dto/user.dto'; +import { PlaceDTO } from './place.dto'; + export class PlanResponseDTO implements Plan { @ApiProperty() id: string; @@ -11,8 +13,10 @@ export class PlanResponseDTO implements Plan { @ApiProperty() name: string; - @ApiProperty() - place: string; + @ApiProperty({ + type: PlaceDTO, + }) + place: Place; @ApiProperty() date: string; diff --git a/src/modules/plan/plan.module.ts b/src/modules/plan/plan.module.ts index 40be4bb..ab69558 100644 --- a/src/modules/plan/plan.module.ts +++ b/src/modules/plan/plan.module.ts @@ -1,12 +1,13 @@ import { Module } from '@nestjs/common'; import { FirebaseModule, PrismaModule } from 'src/common'; +import { AddressModule } from 'src/modules/address'; import { PlanController } from './plan.controller'; import { PlanService } from './plan.service'; @Module({ - imports: [PrismaModule, FirebaseModule], + imports: [PrismaModule, FirebaseModule, AddressModule], controllers: [PlanController], providers: [PlanService], exports: [PlanService], diff --git a/src/modules/plan/plan.service.ts b/src/modules/plan/plan.service.ts index d088685..6b10cb3 100644 --- a/src/modules/plan/plan.service.ts +++ b/src/modules/plan/plan.service.ts @@ -6,6 +6,7 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; import { FirebaseService } from 'src/common/modules/firebase/firebase.service'; import { PrismaService } from 'src/common/modules/prisma/prisma.service'; +import { AddressService } from '../address/address.service'; import { CreatePlanDTO } from './dto/create-plan.dto'; import { PlanResponseDTO } from './dto/plan-response.dto'; @@ -14,6 +15,7 @@ export class PlanService { constructor( private readonly prisma: PrismaService, private readonly firebase: FirebaseService, + private readonly addressService: AddressService, ) {} async getPlan(user: User) { @@ -27,6 +29,7 @@ export class PlanService { }, include: { users: true, + place: true, }, }); @@ -46,7 +49,6 @@ export class PlanService { }); if (!user) return; - if (user.Plan) throw new HttpException('User already has a plan', HttpStatus.BAD_GATEWAY); await this.prisma.invite.deleteMany({ @@ -55,17 +57,29 @@ export class PlanService { }, }); + const place = await this.addressService.getAddressByKeyword(dto.address); + return await this.prisma.plan.create({ data: { - ...dto, + date: dto.date, + name: dto.name, users: { connect: { id, }, }, + place: { + create: { + address: dto.address, + name: place.documents[0].place_name, + x: place.documents[0].x, + y: place.documents[0].y, + }, + }, }, include: { users: true, + place: true, }, }); } @@ -101,6 +115,7 @@ export class PlanService { }, include: { users: true, + place: true, }, }) : await this.prisma.plan.update({ @@ -116,6 +131,7 @@ export class PlanService { }, include: { users: true, + place: true, }, });