Skip to content

Commit

Permalink
Merge pull request #185 from boostcampwm-2024/back/main
Browse files Browse the repository at this point in the history
[BE] 5주차 alpha 서버 배포
  • Loading branch information
uuuo3o authored Nov 28, 2024
2 parents 1806e3c + 1b65323 commit cf27595
Show file tree
Hide file tree
Showing 115 changed files with 2,517 additions and 1,457 deletions.
58 changes: 45 additions & 13 deletions .github/workflows/deploy-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,34 @@ jobs:
matrix:
app:
[
{ name: 'be', dir: 'BE', port: 3000, container: 'juga-docker-be' },
{ name: 'fe', dir: 'FE', port: 5173, container: 'juga-docker-fe' },
{
name: 'be-1',
dir: 'BE',
port: 3000,
container: 'juga-docker-be-1',
env: 'ENV_BE_1',
},
{
name: 'be-2',
dir: 'BE',
port: 3001,
container: 'juga-docker-be-2',
env: 'ENV_BE_2',
},
{
name: 'be-3',
dir: 'BE',
port: 3002,
container: 'juga-docker-be-3',
env: 'ENV_BE_3',
},
{
name: 'fe',
dir: 'FE',
port: 5173,
container: 'juga-docker-fe',
env: 'ENV_FE',
},
]

steps:
Expand All @@ -35,15 +61,15 @@ jobs:
- name: Create .env file
run: |
touch ./${{ matrix.app.dir }}/.env
echo "${{ secrets.ENV }}" > ./${{matrix.app.dir}}/.env
echo "${{ secrets[matrix.app.env] }}" > ./${{matrix.app.dir}}/.env
- name: Install dependencies
working-directory: ./${{matrix.app.dir}}
continue-on-error: true
run: npm ci

- name: Run tests
if: ${{ matrix.app.name == 'be' }}
if: ${{ contains(matrix.app.name, 'be') }}
working-directory: ./${{matrix.app.dir}}
run: npm test
env:
Expand Down Expand Up @@ -116,26 +142,32 @@ jobs:
script: |
docker system prune -af
echo "${{ secrets.ENV }}" > .env
echo "${{ secrets[matrix.app.env] }}" > ${{matrix.app.name}}.env
docker network create juga-network || true
docker run -d \
--name redis \
--network juga-network \
-p 6379:6379 \
-v redis_data:/data \
redis:latest redis-server --appendonly yes
if [ "${{ matrix.app.name }}" = "be-1" ]; then
docker run -d \
--name redis \
--network juga-network \
-p 6379:6379 \
-v redis_data:/data \
redis:latest redis-server --appendonly yes
fi
docker pull ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:${{ env.DOCKER_TAG }}
docker stop ${{ matrix.app.container }} || true
docker rm ${{ matrix.app.container }} || true
docker run -d \
--name ${{ matrix.app.container }} \
--network juga-network \
-p ${{ matrix.app.port }}:${{ matrix.app.port }} \
--env-file .env \
-p ${{ matrix.app.port }}:3000 \
--env-file ${{matrix.app.name}}.env \
${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:${{ env.DOCKER_TAG }}
docker ps | grep ${{ matrix.app.container }}
docker logs ${{ matrix.app.container }}
rm ${{matrix.app.name}}.env
- name: Remove Github Action Ip to Security group
run: |
chmod -R 777 ~/cli_linux
Expand Down
4 changes: 4 additions & 0 deletions BE/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { StockTradeHistoryModule } from './stock/trade/history/stock-trade-histo
import { RedisModule } from './common/redis/redis.module';
import { HTTPExceptionFilter } from './common/filters/http-exception.filter';
import { RankingModule } from './ranking/ranking.module';
import { NewsModule } from './news/news.module';
import { StockBookmarkModule } from './stock/bookmark/stock-bookmark.module';

@Module({
imports: [
Expand All @@ -35,6 +37,8 @@ import { RankingModule } from './ranking/ranking.module';
StockTradeHistoryModule,
RedisModule,
RankingModule,
NewsModule,
StockBookmarkModule,
],
controllers: [AppController],
providers: [
Expand Down
27 changes: 23 additions & 4 deletions BE/src/asset/asset.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ export class AssetController {
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ApiOperation({
summary: '매도 가능 주식 개수 조회 API',
description: '특정 주식 매도 시에 필요한 매도 가능한 주식 개수를 조회한다.',
summary: '매도 가능 주식 개수, 매수 평균가 조회 API',
description:
'특정 주식 매도 시에 필요한 매도 가능한 주식 개수와 매수 평균가를 조회한다.',
})
@ApiResponse({
status: 200,
description: '매도 가능 주식 개수 조회 성공',
example: { quantity: 0 },
description: '매도 가능 주식 개수 및 매수 평균가 조회 성공',
example: { quantity: 0, avg_price: 0 },
})
async getUserStockByCode(
@Req() request: Request,
Expand Down Expand Up @@ -71,8 +72,26 @@ export class AssetController {
return this.assetService.getMyPage(parseInt(request.user.userId, 10));
}

@Get('/unsubscribe')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ApiOperation({
summary: '마이페이지 내 주식 소켓 연결 취소 API',
description: '마이페이지에서 나갈 때 소켓 연결 취소를 위한 API',
})
async unsubscribeMyStocks(@Req() request: Request) {
await this.assetService.unsubscribeMyStocks(
parseInt(request.user.userId, 10),
);
}

@Cron('*/10 9-16 * * 1-5')
async updateAllAssets() {
await this.assetService.updateAllAssets();
}

@Cron('0 8 * * 1-5')
async updatePrevTotalAsset() {
await this.assetService.updatePrevTotalAsset();
}
}
2 changes: 1 addition & 1 deletion BE/src/asset/asset.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Asset {
@Column({ nullable: false, default: 0 })
total_profit: number;

@Column('decimal', { nullable: false, default: 0, precision: 10, scale: 5 })
@Column('decimal', { nullable: false, default: 0, precision: 10, scale: 2 })
total_profit_rate: number;

@Column({ nullable: true })
Expand Down
7 changes: 6 additions & 1 deletion BE/src/asset/asset.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ import { Asset } from './asset.entity';
import { UserStock } from './user-stock.entity';
import { UserStockRepository } from './user-stock.repository';
import { StockDetailModule } from '../stock/detail/stock-detail.module';
import { StockSocketModule } from '../stockSocket/stock-socket.module';

@Module({
imports: [TypeOrmModule.forFeature([Asset, UserStock]), StockDetailModule],
imports: [
TypeOrmModule.forFeature([Asset, UserStock]),
StockDetailModule,
StockSocketModule,
],
controllers: [AssetController],
providers: [AssetService, AssetRepository, UserStockRepository],
exports: [AssetRepository, UserStockRepository],
Expand Down
33 changes: 32 additions & 1 deletion BE/src/asset/asset.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import { DataSource, Repository } from 'typeorm';
import { InjectDataSource } from '@nestjs/typeorm';
import { Injectable } from '@nestjs/common';
import { Asset } from './asset.entity';
import { Order } from '../stock/order/stock-order.entity';
import { StatusType } from '../stock/order/enum/status-type';
import { TradeType } from '../stock/order/enum/trade-type';

@Injectable()
export class AssetRepository extends Repository<Asset> {
constructor(@InjectDataSource() dataSource: DataSource) {
constructor(@InjectDataSource() private readonly dataSource: DataSource) {
super(Asset, dataSource.createEntityManager());
}

Expand All @@ -15,4 +18,32 @@ export class AssetRepository extends Repository<Asset> {
.select(['asset.* ', 'user.nickname as nickname'])
.getRawMany();
}

async findAllPendingOrders(
userId: number,
tradeType: TradeType,
stockCode?: string,
) {
const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.startTransaction();

try {
const orders = await queryRunner.manager.find<Order>(Order, {
where: {
user_id: userId,
status: StatusType.PENDING,
trade_type: tradeType,
...(stockCode ? { stock_code: stockCode } : {}),
},
});

await queryRunner.commitTransaction();
return orders;
} catch (error) {
await queryRunner.rollbackTransaction();
throw error;
} finally {
await queryRunner.release();
}
}
}
Loading

0 comments on commit cf27595

Please sign in to comment.