Skip to content

Commit

Permalink
refactor: Update AddressService to return formatted address data
Browse files Browse the repository at this point in the history
  • Loading branch information
PleBea committed Jul 13, 2024
1 parent 9576f94 commit 0f6f212
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 23 deletions.
12 changes: 11 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/modules/address/address.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ import { AddressService } from './address.service';
imports: [HttpModule],
controllers: [AddressController],
providers: [AddressService],
exports: [AddressService],
})
export class AddressModule {}
10 changes: 8 additions & 2 deletions src/modules/address/address.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@ export class AddressService {
this.apiKey = this.configService.get<string>('KAKAO_REST_API_KEY');
}

public async getSimilarAddress(address: string): Promise<AddressResponseDTO> {
public async getAddressByKeyword(address: string) {
const url = 'https://dapi.kakao.com/v2/local/search/keyword.json';
const res = await this.httpService.axiosRef.get<KakaoMapModel>(url, {
params: { query: address },
headers: { Authorization: `KakaoAK ${this.apiKey}` },
});

return res.data;
}

public async getSimilarAddress(address: string): Promise<AddressResponseDTO> {
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,
Expand Down
24 changes: 14 additions & 10 deletions src/modules/location/location.service.ts
Original file line number Diff line number Diff line change
@@ -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<Record<string, LocationDTO>> {
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);
}
Expand Down
7 changes: 3 additions & 4 deletions src/modules/plan/dto/create-plan.dto.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
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: '개학전밤샘등교팟',
})
@IsString()
name: string;

@ApiProperty({
example: '선린인터넷고등학교',
example: '서울 용산구 청파동3가 131-2',
})
@IsString()
place: string;
address: string;

@ApiProperty({
example: '2017-03-16T17:40:00',
Expand Down
23 changes: 23 additions & 0 deletions src/modules/plan/dto/place.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
}
10 changes: 7 additions & 3 deletions src/modules/plan/dto/plan-response.dto.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
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;

@ApiProperty()
name: string;

@ApiProperty()
place: string;
@ApiProperty({
type: PlaceDTO,
})
place: Place;

@ApiProperty()
date: string;
Expand Down
3 changes: 2 additions & 1 deletion src/modules/plan/plan.module.ts
Original file line number Diff line number Diff line change
@@ -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],
Expand Down
20 changes: 18 additions & 2 deletions src/modules/plan/plan.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -14,6 +15,7 @@ export class PlanService {
constructor(
private readonly prisma: PrismaService,
private readonly firebase: FirebaseService,
private readonly addressService: AddressService,
) {}

async getPlan(user: User) {
Expand All @@ -27,6 +29,7 @@ export class PlanService {
},
include: {
users: true,
place: true,
},
});

Expand All @@ -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({
Expand All @@ -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,
},
});
}
Expand Down Expand Up @@ -101,6 +115,7 @@ export class PlanService {
},
include: {
users: true,
place: true,
},
})
: await this.prisma.plan.update({
Expand All @@ -116,6 +131,7 @@ export class PlanService {
},
include: {
users: true,
place: true,
},
});

Expand Down

0 comments on commit 0f6f212

Please sign in to comment.