Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/subdomains/core/buy-crypto/routes/buy/buy.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ export class BuyService {
select: ['id', 'user'],
});
const userVolume = await this.getUserVolume(user.id);
await this.userService.updateBuyVolume(user.id, userVolume.volume, userVolume.annualVolume);
const monthlyVolume = await this.getMonthlyVolume(user.id);

await this.userService.updateBuyVolume(user.id, userVolume.volume, userVolume.annualVolume, monthlyVolume.volume);
}

async getUserVolume(userId: number): Promise<{ volume: number; annualVolume: number }> {
Expand All @@ -85,6 +87,17 @@ export class BuyService {
.getRawOne<{ volume: number; annualVolume: number }>();
}

async getMonthlyVolume(userId: number): Promise<{ volume: number }> {
return this.buyRepo
.createQueryBuilder('buy')
.select('SUM(volume)', 'volume')
.where('userId = :id', { id: userId })
.andWhere('buy.created >= :startOfMonth', {
startOfMonth: new Date(new Date().getFullYear(), new Date().getMonth(), 1),
})
.getRawOne<{ volume: number }>();
}

async getTotalVolume(): Promise<number> {
return this.buyRepo
.createQueryBuilder('buy')
Expand Down
20 changes: 19 additions & 1 deletion src/subdomains/core/buy-crypto/routes/swap/swap.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,14 @@ export class SwapService {
select: ['id', 'user'],
});
const userVolume = await this.getUserVolume(user.id);
await this.userService.updateCryptoVolume(user.id, userVolume.volume, userVolume.annualVolume);
const monthlyVolume = await this.getMonthlyVolume(user.id);

await this.userService.updateCryptoVolume(
user.id,
userVolume.volume,
userVolume.annualVolume,
monthlyVolume.volume,
);
}

async getUserVolume(userId: number): Promise<{ volume: number; annualVolume: number }> {
Expand All @@ -107,6 +114,17 @@ export class SwapService {
.getRawOne<{ volume: number; annualVolume: number }>();
}

async getMonthlyVolume(userId: number): Promise<{ volume: number }> {
return this.swapRepo
.createQueryBuilder('crypto')
.select('SUM(volume)', 'volume')
.where('userId = :id', { id: userId })
.andWhere('crypto.created >= :startOfMonth', {
startOfMonth: new Date(new Date().getFullYear(), new Date().getMonth(), 1),
})
.getRawOne<{ volume: number }>();
}

async getTotalVolume(): Promise<number> {
return this.swapRepo
.createQueryBuilder('crypto')
Expand Down
15 changes: 14 additions & 1 deletion src/subdomains/core/sell-crypto/route/sell.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ export class SellService {
select: ['id', 'user'],
});
const userVolume = await this.getUserVolume(user.id);
await this.userService.updateSellVolume(user.id, userVolume.volume, userVolume.annualVolume);
const monthlyVolume = await this.getMonthlyVolume(user.id);

await this.userService.updateSellVolume(user.id, userVolume.volume, userVolume.annualVolume, monthlyVolume.volume);
}

async getUserVolume(userId: number): Promise<{ volume: number; annualVolume: number }> {
Expand All @@ -296,6 +298,17 @@ export class SellService {
.getRawOne<{ volume: number; annualVolume: number }>();
}

async getMonthlyVolume(userId: number): Promise<{ volume: number }> {
return this.sellRepo
.createQueryBuilder('sell')
.select('SUM(volume)', 'volume')
.where('userId = :id', { id: userId })
.andWhere('sell.created >= :startOfMonth', {
startOfMonth: new Date(new Date().getFullYear(), new Date().getMonth(), 1),
})
.getRawOne<{ volume: number }>();
}

async getTotalVolume(): Promise<number> {
return this.sellRepo
.createQueryBuilder('sell')
Expand Down
10 changes: 10 additions & 0 deletions src/subdomains/generic/user/models/user-data/user-data.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,18 +293,28 @@ export class UserData extends IEntity {
apiFilterCT?: string;

// Volumes

@Column({ type: 'float', default: 0 })
monthlyBuyVolume: number; // CHF

@Column({ type: 'float', default: 0 })
annualBuyVolume: number; // CHF

@Column({ type: 'float', default: 0 })
buyVolume: number; // CHF

@Column({ type: 'float', default: 0 })
monthlySellVolume: number; // CHF

@Column({ type: 'float', default: 0 })
annualSellVolume: number; // CHF

@Column({ type: 'float', default: 0 })
sellVolume: number; // CHF

@Column({ type: 'float', default: 0 })
monthlyCryptoVolume: number; // CHF

@Column({ type: 'float', default: 0 })
annualCryptoVolume: number; // CHF

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ export class UserDtoMapper {

private static mapVolumes(user: UserData | User): VolumesDto {
const dto: VolumesDto = {
buy: { total: user.buyVolume, annual: user.annualBuyVolume },
sell: { total: user.sellVolume, annual: user.annualSellVolume },
swap: { total: user.cryptoVolume, annual: user.annualCryptoVolume },
buy: { total: user.buyVolume, annual: user.annualBuyVolume, monthly: user.monthlyBuyVolume },
sell: { total: user.sellVolume, annual: user.annualSellVolume, monthly: user.monthlySellVolume },
swap: { total: user.cryptoVolume, annual: user.annualCryptoVolume, monthly: user.monthlyCryptoVolume },
};

return Object.assign(new VolumesDto(), dto);
Expand Down
3 changes: 3 additions & 0 deletions src/subdomains/generic/user/models/user/dto/user.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export class VolumeInformation {

@ApiProperty()
annual: number;

@ApiProperty()
monthly: number;
}

export class TradingLimit {
Expand Down
9 changes: 9 additions & 0 deletions src/subdomains/generic/user/models/user/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,27 @@ export class User extends IEntity {
@Column({ length: 256, nullable: true })
apiFilterCT?: string;

@Column({ type: 'float', default: 0 })
monthlyBuyVolume: number; // CHF

@Column({ type: 'float', default: 0 })
annualBuyVolume: number; // CHF

@Column({ type: 'float', default: 0 })
buyVolume: number; // CHF

@Column({ type: 'float', default: 0 })
monthlySellVolume: number; // CHF

@Column({ type: 'float', default: 0 })
annualSellVolume: number; // CHF

@Column({ type: 'float', default: 0 })
sellVolume: number; // CHF

@Column({ type: 'float', default: 0 })
monthlyCryptoVolume: number; // CHF

@Column({ type: 'float', default: 0 })
annualCryptoVolume: number; // CHF

Expand Down
22 changes: 16 additions & 6 deletions src/subdomains/generic/user/models/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,28 +379,38 @@ export class UserService {
await this.userRepo.update({ annualSellVolume: Not(0) }, { annualSellVolume: 0 });
}

async updateBuyVolume(userId: number, volume: number, annualVolume: number): Promise<void> {
@DfxCron(CronExpression.EVERY_1ST_DAY_OF_MONTH_AT_MIDNIGHT)
async resetMonthVolumes(): Promise<void> {
await this.userRepo.update({ monthlyBuyVolume: Not(0) }, { monthlyBuyVolume: 0 });
await this.userRepo.update({ monthlySellVolume: Not(0) }, { monthlySellVolume: 0 });
await this.userRepo.update({ monthlyCryptoVolume: Not(0) }, { monthlyCryptoVolume: 0 });
}

async updateBuyVolume(userId: number, volume: number, annualVolume: number, monthlyVolume: number): Promise<void> {
await this.userRepo.update(userId, {
buyVolume: Util.round(volume, Config.defaultVolumeDecimal),
annualBuyVolume: Util.round(annualVolume, Config.defaultVolumeDecimal),
monthlyBuyVolume: Util.round(monthlyVolume, Config.defaultVolumeDecimal),
});

await this.updateUserDataVolume(userId);
}

async updateCryptoVolume(userId: number, volume: number, annualVolume: number): Promise<void> {
async updateCryptoVolume(userId: number, volume: number, annualVolume: number, monthlyVolume: number): Promise<void> {
await this.userRepo.update(userId, {
cryptoVolume: Util.round(volume, Config.defaultVolumeDecimal),
annualCryptoVolume: Util.round(annualVolume, Config.defaultVolumeDecimal),
monthlyCryptoVolume: Util.round(monthlyVolume, Config.defaultVolumeDecimal),
});

await this.updateUserDataVolume(userId);
}

async updateSellVolume(userId: number, volume: number, annualVolume: number): Promise<void> {
async updateSellVolume(userId: number, volume: number, annualVolume: number, monthlyVolume: number): Promise<void> {
await this.userRepo.update(userId, {
sellVolume: Util.round(volume, Config.defaultVolumeDecimal),
annualSellVolume: Util.round(annualVolume, Config.defaultVolumeDecimal),
monthlySellVolume: Util.round(monthlyVolume, Config.defaultVolumeDecimal),
});

await this.updateUserDataVolume(userId);
Expand Down Expand Up @@ -615,9 +625,9 @@ export class UserService {
user.buyVolume + user.sellVolume + user.cryptoVolume >= Config.support.blackSquad.limit
? Config.support.blackSquad.link
: undefined,
buyVolume: { total: user.buyVolume, annual: user.annualBuyVolume },
sellVolume: { total: user.sellVolume, annual: user.annualSellVolume },
cryptoVolume: { total: user.cryptoVolume, annual: user.annualCryptoVolume },
buyVolume: { total: user.buyVolume, annual: user.annualBuyVolume, monthly: user.monthlyBuyVolume },
sellVolume: { total: user.sellVolume, annual: user.annualSellVolume, monthly: user.monthlySellVolume },
cryptoVolume: { total: user.cryptoVolume, annual: user.annualCryptoVolume, monthly: user.monthlyCryptoVolume },
stakingBalance: 0,
};
}
Expand Down