Skip to content

Commit

Permalink
feat: account 스키마 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
SeongHyeon0409 committed Nov 28, 2024
1 parent f19f360 commit ccc7ed6
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 23 deletions.
3 changes: 3 additions & 0 deletions packages/server/src/account/account.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export class Account {
@Column('double')
KRW: number;

@Column('double')
availableKRW: number;

@Column('double')
USDT: number;

Expand Down
2 changes: 2 additions & 0 deletions packages/server/src/account/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ export class AccountService {
totalPrice = this.calculateTotalPrice(coins);

accountData.KRW = account.KRW;
accountData.availableKRW = account.availableKRW;
accountData.total_bid = totalPrice;
accountData.coins = coins;

this.logger.log(`계정 데이터 조회 완료: ${user.userId}`);

return {
KRW: accountData.KRW,
availableKRW: accountData.availableKRW,
total_bid: accountData.total_bid,
coins: accountData.coins
};
Expand Down
3 changes: 3 additions & 0 deletions packages/server/src/account/dtos/my-account.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export class MyAccountDto {
@IsNumber()
KRW: number;

@IsNumber()
availableKRW: number;

@IsNumber()
total_bid: number;

Expand Down
6 changes: 6 additions & 0 deletions packages/server/src/account/dtos/my-account.response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ export class AccountResponseDto {
description: '계좌 잔액',
})
KRW: number;

@ApiProperty({
example: 2000000,
description: '매수가능한 계좌 잔액',
})
availableKRW: number;

@ApiProperty({
type: MyAccountDto,
Expand Down
19 changes: 2 additions & 17 deletions packages/server/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ export class AuthService {
private accountRepository: AccountRepository,
private jwtService: JwtService,
private readonly redisRepository: RedisRepository,
) {
this.createAdminUser();
}
) {}

async signIn(
username: string,
Expand Down Expand Up @@ -120,20 +118,6 @@ export class AuthService {
return { message: 'User logged out successfully' };
}

async createAdminUser() {
const user = await this.userRepository.findOneBy({ username: 'admin' });

if (!user) {
const adminUser = new User();
adminUser.username = 'admin';
await this.userRepository.save(adminUser);
await this.accountRepository.createAccountForAdmin(adminUser);
this.logger.log('Admin user created successfully.');
} else {
this.logger.log('Admin user already exists.');
}
}

private async generateTokens(
userId: number,
username: string,
Expand Down Expand Up @@ -223,6 +207,7 @@ export class AuthService {
await this.accountRepository.save({
user,
KRW: DEFAULT_KRW,
availableKRW: DEFAULT_KRW,
USDT: DEFAULT_USDT,
BTC: DEFAULT_BTC,
});
Expand Down
11 changes: 11 additions & 0 deletions packages/server/src/trade/trade-ask.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,23 @@ export class AskService extends TradeAskBidService implements OnModuleInit {
account[askDto.typeReceived] + buyData.price * buyData.quantity,
);

const availableChange = formatQuantity(
account.availableKRW + buyData.price * buyData.quantity,
);

await this.accountRepository.updateAccountCurrency(
askDto.typeReceived,
change,
account.id,
queryRunner,
);

await this.accountRepository.updateAccountCurrency(
'availableKRW',
availableChange,
account.id,
queryRunner,
);
}

private async waitForTransaction(
Expand Down
16 changes: 12 additions & 4 deletions packages/server/src/trade/trade-bid.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class BidService extends TradeAskBidService implements OnModuleInit {
);

await this.accountRepository.updateAccountCurrency(
bidDto.typeGiven,
'availableKRW',
accountBalance,
userAccount.id,
queryRunner,
Expand All @@ -101,8 +101,8 @@ export class BidService extends TradeAskBidService implements OnModuleInit {
bidDto: TradeData,
account: any,
): Promise<number> {
const { typeGiven, receivedPrice, receivedAmount } = bidDto;
const balance = account[typeGiven];
const { receivedPrice, receivedAmount } = bidDto;
const balance = account.availableKRW;

const givenAmount = formatQuantity(receivedPrice * receivedAmount);
const remaining = formatQuantity(balance - givenAmount);
Expand Down Expand Up @@ -253,14 +253,22 @@ export class BidService extends TradeAskBidService implements OnModuleInit {
const change = formatQuantity(
(bidDto.receivedPrice - buyData.price) * buyData.quantity,
);
const returnChange = formatQuantity(change + account[typeGiven]);
const returnChange = formatQuantity(account[typeGiven] - (buyData.price * buyData.quantity));
const returnAvailableChange = formatQuantity(change + account.availableKRW);

await this.accountRepository.updateAccountCurrency(
typeGiven,
returnChange,
account.id,
queryRunner,
);

await this.accountRepository.updateAccountCurrency(
'availableKRW',
returnAvailableChange,
account.id,
queryRunner,
);
}

private async waitForTransaction(
Expand Down
4 changes: 2 additions & 2 deletions packages/server/src/trade/trade.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export class TradeService {
const accountBalance = this.calculateAccountBalance(trade, userAccount);

await this.accountRepository.updateAccountCurrency(
trade.tradeCurrency,
'availableKRW',
accountBalance,
userAccount.id,
queryRunner,
Expand Down Expand Up @@ -238,7 +238,7 @@ export class TradeService {

private calculateAccountBalance(trade: any, userAccount: any): number {
return parseFloat(
(trade.price * trade.quantity + userAccount[trade.tradeCurrency]).toFixed(
(trade.price * trade.quantity + userAccount.availableKRW).toFixed(
8,
),
);
Expand Down

0 comments on commit ccc7ed6

Please sign in to comment.