Skip to content

Commit 4721eb0

Browse files
committed
[FEAT]#9 #52 유저 알림 신청 API 및 알림 로직 완성
1 parent e7325bc commit 4721eb0

File tree

5 files changed

+69
-6
lines changed

5 files changed

+69
-6
lines changed

prisma/schema.prisma

+7
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,13 @@ model TyphoonPredictionCircleWithAroundWeatherPrediction {
202202
@@map("typhoon_predictions_circle_with_around_weather_prediction")
203203
}
204204

205+
model User {
206+
phone_number String @id @db.VarChar(100)
207+
city String
208+
@@map("users")
209+
}
210+
211+
205212
enum GRADE_TYPE {
206213
TD
207214
TS

src/app/sms/provider/sms.service.ts

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
import { Injectable } from '@nestjs/common';
1+
import { WeatherService } from '@app/weather/provider/weather.service';
2+
import { BadRequestException, Injectable } from '@nestjs/common';
23

4+
import { PrismaService } from '@src/prisma/prisma.service';
35
import axios from 'axios';
46
import * as crypto from 'crypto';
7+
import { SMSApplyDto } from '../sms-apply.dto';
58

69
@Injectable()
710
export class SmsService {
811
private ACCESS_KEY_ID = process.env.SENS_ACCESS_KEY;
912
private SECRET_KEY = process.env.SENS_SECRET_KEY;
1013
private SERVICE_ID = process.env.SENS_SERVICE_ID;
14+
15+
constructor(
16+
private readonly weatherService: WeatherService,
17+
private readonly prisma: PrismaService,
18+
) {}
1119
// private headers = {
1220
// 'Content-Type': 'application/json;charset=UTF-8',
1321
// 'x-ncp-apigw-timestamp': Date.now().toString(),
@@ -74,4 +82,32 @@ export class SmsService {
7482
//this.logger.error(`Error occurred: ${error.message}`, error.stack);
7583
}
7684
}
85+
86+
async applyNotice(smsApplyDto: SMSApplyDto) {
87+
const { phone_number, longitude, latitude } = smsApplyDto;
88+
const { city } = await this.weatherService.getCity(latitude, longitude);
89+
if (city === 'non-city')
90+
throw new BadRequestException('존재하지 않는 지역입니다.');
91+
92+
return await this.prisma.user.upsert({
93+
where: {
94+
phone_number,
95+
},
96+
update: {
97+
city,
98+
},
99+
create: {
100+
phone_number,
101+
city,
102+
},
103+
});
104+
}
105+
106+
async getUserListByCity(city: string) {
107+
return await this.prisma.user.findMany({
108+
where: {
109+
city,
110+
},
111+
});
112+
}
77113
}

src/app/sms/sms-apply.dto.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
import { IsNumber, IsString } from 'class-validator';
3+
4+
export class SMSApplyDto {
5+
@ApiProperty({ description: '전화번호' })
6+
@IsString()
7+
phone_number: string;
8+
9+
@ApiProperty({ description: 'longitude' })
10+
@IsNumber()
11+
longitude: number;
12+
13+
@ApiProperty({ description: 'latitude' })
14+
@IsNumber()
15+
latitude: number;
16+
}

src/app/sms/sms.controller.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import { Controller, Param, Post } from '@nestjs/common';
1+
import { Body, Controller, Param, Post } from '@nestjs/common';
22
import { SmsService } from './provider/sms.service';
3+
import { SMSApplyDto } from './sms-apply.dto';
34

45
@Controller('sms')
56
export class SmsController {
67
constructor(private readonly SmsService: SmsService) {}
7-
8+
@Post('/notice')
9+
async appllyNotice(@Body() smsApplyDto: SMSApplyDto) {
10+
return await this.SmsService.applyNotice(smsApplyDto);
11+
}
812
@Post('/:phone_number')
913
async sendSms(@Param('phone_number') phone_number: string) {
1014
return await this.SmsService.sendSms(phone_number, 'test');

src/app/sms/sms.event-listener.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ export class SmsListener {
4444
overpassCoords,
4545
);
4646
cities.forEach(async (city) => {
47-
const users = [{ phone: '01073616616', city }];
48-
// const users = await this.smsService.getUsersByCity(city);
47+
const users = await this.smsService.getUserListByCity(city);
4948
users.forEach(async (user) => {
5049
// await this.smsService.sendSms(
5150
// user.phone,
@@ -61,7 +60,8 @@ export class SmsListener {
6160
// );
6261

6362
//테스트용
64-
console.log(`
63+
if (user)
64+
console.log(`
6565
[SRT] 태풍 경보 알림
6666
${typhoonPrediction1.prediction_date}~${typhoonPrediction2.prediction_date}
6767
${user.city}에 태풍(${name})위험이 예측되었습니다.(기준 일자 : ${typhoonPrediction1.observation_date})

0 commit comments

Comments
 (0)