Skip to content

Commit

Permalink
Merge pull request #58 from boostcampwm-2024/back/main
Browse files Browse the repository at this point in the history
[BE] alpha 서버 배포 (11.11)
  • Loading branch information
sieunie authored Nov 11, 2024
2 parents 123b608 + 5920449 commit b508aee
Show file tree
Hide file tree
Showing 30 changed files with 183 additions and 198 deletions.
6 changes: 3 additions & 3 deletions BE/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthModule } from './auth/auth.module';
import { User } from './auth/user.entity';
import { StockIndexModule } from './stock/index/stock.index.module';
import { StockTopfiveModule } from './stock/topfive/stock.topfive.module';
import { KoreaInvestmentModule } from './koreaInvestment/korea.investment.module';
import { StockIndexModule } from './stock/index/stock-index.module';
import { StockTopfiveModule } from './stock/topfive/stock-topfive.module';
import { KoreaInvestmentModule } from './koreaInvestment/korea-investment.module';
import { SocketModule } from './websocket/socket.module';

@Module({
Expand Down
2 changes: 1 addition & 1 deletion BE/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { AuthGuard } from '@nestjs/passport';
import { ApiOperation } from '@nestjs/swagger';
import { AuthService } from './auth.service';
import { AuthCredentialsDto } from './dto/authCredentials.dto';
import { AuthCredentialsDto } from './dto/auth-credentials.dto';

@Controller('auth')
export class AuthController {
Expand Down
2 changes: 1 addition & 1 deletion BE/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { InjectRepository } from '@nestjs/typeorm';
import { JwtService } from '@nestjs/jwt';
import * as bcrypt from 'bcrypt';
import { UserRepository } from './user.repository';
import { AuthCredentialsDto } from './dto/authCredentials.dto';
import { AuthCredentialsDto } from './dto/auth-credentials.dto';

@Injectable()
export class AuthService {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion BE/src/auth/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { InjectDataSource } from '@nestjs/typeorm';
import { DataSource, Repository } from 'typeorm';
import * as bcrypt from 'bcrypt';
import { User } from './user.entity';
import { AuthCredentialsDto } from './dto/authCredentials.dto';
import { AuthCredentialsDto } from './dto/auth-credentials.dto';

@Injectable()
export class UserRepository extends Repository<User> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface AccessTokenInterface {
access_token: string;
access_token_token_expired: string;
token_type: string;
expires_in: number;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Module } from '@nestjs/common';
import { KoreaInvestmentService } from './korea.investment.service';
import { KoreaInvestmentService } from './korea-investment.service';

@Module({
imports: [],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import axios from 'axios';
import { UnauthorizedException } from '@nestjs/common';
import { getFullURL } from '../util/get-full-URL';
import { AccessTokenInterface } from './interface/korea-investment.interface';

export class KoreaInvestmentService {
private accessToken: string;
Expand All @@ -9,19 +12,20 @@ export class KoreaInvestmentService {
if (this.accessToken && this.tokenExpireTime > new Date()) {
return this.accessToken;
}
const response = await axios.post(
`${process.env.KOREA_INVESTMENT_BASE_URL}/oauth2/tokenP`,
{
const response = await axios
.post<AccessTokenInterface>(getFullURL('/oauth2/tokenP'), {
grant_type: 'client_credentials',
appkey: process.env.KOREA_INVESTMENT_APP_KEY,
appsecret: process.env.KOREA_INVESTMENT_APP_SECRET,
},
);
})
.catch(() => {
throw new UnauthorizedException('액세스 토큰을 조회하지 못했습니다.');
});

const { data } = response;

this.accessToken = data.access_token;
this.tokenExpireTime = new Date(Date.now() + +data.expires_in);
this.tokenExpireTime = new Date(data.access_token_token_expired);

return this.accessToken;
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { ApiProperty } from '@nestjs/swagger';

export class StockIndexListChartElementDto {
constructor(time: string, value: string) {
constructor(time: string, value: string, diff: string) {
this.time = time;
this.value = value;
this.diff = diff;
}

@ApiProperty({ description: 'HHMMSS', example: '130500' })
time: string;

@ApiProperty({ description: '주가 지수' })
value: string;

@ApiProperty({ description: '전일 대비 주가 지수' })
diff: string;
}
14 changes: 14 additions & 0 deletions BE/src/stock/index/dto/stock-index-response-element.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ApiProperty } from '@nestjs/swagger';
import { StockIndexValueElementDto } from './stock-index-value-element.dto';
import { StockIndexListChartElementDto } from './stock-index-list-chart.element.dto';

export class StockIndexResponseElementDto {
@ApiProperty({ description: '실시간 값', type: StockIndexValueElementDto })
value: StockIndexValueElementDto;

@ApiProperty({
description: '실시간 차트',
type: [StockIndexListChartElementDto],
})
chart: StockIndexListChartElementDto[];
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';
import { StockIndexResponseElementDto } from './stock.index.response.element.dto';
import { StockIndexResponseElementDto } from './stock-index-response-element.dto';

export class StockIndexResponseDto {
@ApiProperty({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
import { ApiProperty } from '@nestjs/swagger';

export class StockIndexValueElementDto {
constructor(
code: string,
value: string,
diff: string,
diffRate: string,
sign: string,
) {
this.code = code;
this.value = value;
constructor(value: string, diff: string, diffRate: string, sign: string) {
this.curr_value = value;
this.diff = diff;
this.diffRate = diffRate;
this.diff_rate = diffRate;
this.sign = sign;
}

@ApiProperty({
description: '코스피: 0001, 코스닥: 1001, 코스피200: 2001, KSQ150: 3003',
})
code: string;

@ApiProperty({ description: '주가 지수' })
value: string;
curr_value: string;

@ApiProperty({ description: '전일 대비 등락' })
diff: string;

@ApiProperty({ description: '전일 대비 등락률' })
diffRate: string;
diff_rate: string;

@ApiProperty({ description: '부호... 인데 추후에 알아봐야 함' })
sign: string;
Expand Down
17 changes: 0 additions & 17 deletions BE/src/stock/index/dto/stock.index.list.element.dto.ts

This file was deleted.

16 changes: 0 additions & 16 deletions BE/src/stock/index/dto/stock.index.response.element.dto.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Controller, Get } from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { Cron } from '@nestjs/schedule';
import { StockIndexService } from './stock.index.service';
import { StockIndexResponseDto } from './dto/stock.index.response.dto';
import { KoreaInvestmentService } from '../../koreaInvestment/korea.investment.service';
import { StockIndexService } from './stock-index.service';
import { StockIndexResponseDto } from './dto/stock-index-response.dto';
import { KoreaInvestmentService } from '../../koreaInvestment/korea-investment.service';
import { SocketGateway } from '../../websocket/socket.gateway';

@Controller('/api/stocks/index')
Expand Down Expand Up @@ -70,22 +70,18 @@ export class StockIndexController {

const stockIndexResponse = new StockIndexResponseDto();
stockIndexResponse.KOSPI = {
code: '0001',
value: kospiValue,
chart: kospiChart,
};
stockIndexResponse.KOSDAQ = {
code: '1001',
value: kosdaqValue,
chart: kosdaqChart,
};
stockIndexResponse.KOSPI200 = {
code: '2001',
value: kospi200Value,
chart: kospi200Chart,
};
stockIndexResponse.KSQ150 = {
code: '3003',
value: ksq150Value,
chart: ksq150Chart,
};
Expand Down Expand Up @@ -115,6 +111,11 @@ export class StockIndexController {
), // KSQ150
]);

this.socketGateway.sendStockIndexListToClient(stockLists);
this.socketGateway.sendStockIndexListToClient({
KOSPI: stockLists[0],
KOSDAQ: stockLists[1],
KOSPI200: stockLists[2],
KSQ150: stockLists[3],
});
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Module } from '@nestjs/common';
import { StockIndexController } from './stock.index.controller';
import { StockIndexService } from './stock.index.service';
import { KoreaInvestmentModule } from '../../koreaInvestment/korea.investment.module';
import { StockIndexController } from './stock-index.controller';
import { StockIndexService } from './stock-index.service';
import { KoreaInvestmentModule } from '../../koreaInvestment/korea-investment.module';
import { SocketModule } from '../../websocket/socket.module';

@Module({
Expand Down
95 changes: 95 additions & 0 deletions BE/src/stock/index/stock-index.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { Injectable, InternalServerErrorException } from '@nestjs/common';
import axios from 'axios';
import { StockIndexListChartElementDto } from './dto/stock-index-list-chart.element.dto';
import { StockIndexValueElementDto } from './dto/stock-index-value-element.dto';
import {
StockIndexChartInterface,
StockIndexValueInterface,
} from './interface/stock-index.interface';
import { getFullURL } from '../../util/get-full-URL';
import { getHeader } from '../../util/get-header';

@Injectable()
export class StockIndexService {
async getDomesticStockIndexListByCode(code: string, accessToken: string) {
const result = await this.requestDomesticStockIndexListApi(
code,
accessToken,
);

return result.output.map((element) => {
return new StockIndexListChartElementDto(
element.bsop_hour,
element.bstp_nmix_prpr,
element.bstp_nmix_prdy_vrss,
);
});
}

async getDomesticStockIndexValueByCode(code: string, accessToken: string) {
const result = await this.requestDomesticStockIndexValueApi(
code,
accessToken,
);

const data = result.output;

return new StockIndexValueElementDto(
data.bstp_nmix_prpr,
data.bstp_nmix_prdy_vrss,
data.bstp_nmix_prdy_ctrt,
data.prdy_vrss_sign,
);
}

private async requestDomesticStockIndexListApi(
code: string,
accessToken: string,
) {
const response = await axios
.get<StockIndexChartInterface>(
getFullURL(
'/uapi/domestic-stock/v1/quotations/inquire-index-timeprice',
),
{
headers: getHeader(accessToken, 'FHPUP02110200'),
params: {
fid_input_hour_1: 300,
fid_cond_mrkt_div_code: 'U',
fid_input_iscd: code,
},
},
)
.catch(() => {
throw new InternalServerErrorException(
'주가 지수 차트 정보를 조회하지 못했습니다.',
);
});

return response.data;
}

private async requestDomesticStockIndexValueApi(
code: string,
accessToken: string,
) {
const response = await axios
.get<StockIndexValueInterface>(
getFullURL('/uapi/domestic-stock/v1/quotations/inquire-index-price'),
{
headers: getHeader(accessToken, 'FHPUP02100000'),
params: {
fid_cond_mrkt_div_code: 'U',
fid_input_iscd: code,
},
},
)
.catch(() => {
throw new InternalServerErrorException(
'주가 지수 값 정보를 조회하지 못했습니다.',
);
});

return response.data;
}
}
Loading

0 comments on commit b508aee

Please sign in to comment.