Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/EveripediaNetwork/ep-api in…
Browse files Browse the repository at this point in the history
…to new-email-template
  • Loading branch information
Aliiiu committed Feb 20, 2025
2 parents e909682 + fe75a4b commit cf489ab
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
28 changes: 17 additions & 11 deletions src/App/Treasury/treasury.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ConfigService } from '@nestjs/config'
import { HttpService } from '@nestjs/axios'
import { of, throwError } from 'rxjs'
import { AxiosResponse } from 'axios'
import { CACHE_MANAGER } from '@nestjs/cache-manager'
import TreasuryRepository from './treasury.repository'
import * as treasuryDto from './treasury.dto'
import TreasuryService from './treasury.service'
Expand All @@ -15,11 +16,20 @@ describe('TreasuryService', () => {
let configService: jest.Mocked<ConfigService>
let httpService: jest.Mocked<HttpService>
let treasuryRepository: jest.Mocked<TreasuryRepository>
let cacheManager: any
const treasuryAddress = 'test-treasury-address'
const url = `https://pro-openapi.debank.com/v1/user/total_balance?id=${treasuryAddress}`

beforeEach(async () => {
cacheManager = {
get: jest.fn().mockResolvedValue(1000.5),
set: jest.fn(),
}

const module: TestingModule = await Test.createTestingModule({
providers: [
TreasuryService,
{ provide: CACHE_MANAGER, useValue: cacheManager },
{
provide: ConfigService,
useValue: {
Expand Down Expand Up @@ -78,7 +88,6 @@ describe('TreasuryService', () => {

describe('requestTotalbalance', () => {
it('should return total USD value when API call is successful', async () => {
const treasuryAddress = 'test-treasury-address'
const debankKey = 'test-debank-key'

configService.get
Expand All @@ -90,19 +99,16 @@ describe('TreasuryService', () => {
}

httpService.get.mockReturnValue(of(response) as any)
httpService.get().toPromise = jest.fn().mockResolvedValue(response)
httpService.get(url).toPromise = jest.fn().mockResolvedValue(response)

const result = await treasuryService.requestTotalbalance()

expect(result).toBe(1000.5)
expect(httpService.get).toHaveBeenCalledWith(
`https://pro-openapi.debank.com/v1/user/total_balance?id=${treasuryAddress}`,
{
headers: {
Accesskey: debankKey,
},
expect(httpService.get).toHaveBeenCalledWith(url, {
headers: {
Accesskey: debankKey,
},
)
})
})

it('should return null when API call fails', async () => {
Expand All @@ -112,7 +118,7 @@ describe('TreasuryService', () => {

const error = new Error('API Error')
httpService.get.mockReturnValue(throwError(() => error))
httpService.get().toPromise = jest.fn().mockRejectedValue(error)
httpService.get(url).toPromise = jest.fn().mockRejectedValue(error)

const consoleSpy = jest.spyOn(console, 'error').mockImplementation()

Expand All @@ -136,7 +142,7 @@ describe('TreasuryService', () => {
.mockResolvedValue(totalValue)

await treasuryService.storeTotalValue()

expect(cacheManager.get).toHaveBeenCalledWith('treasuryBalance')
expect(treasuryRepository.saveData).toHaveBeenCalledWith(
totalValue.toFixed(1),
)
Expand Down
13 changes: 11 additions & 2 deletions src/App/Treasury/treasury.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Injectable } from '@nestjs/common'
import { Injectable, Inject } from '@nestjs/common'
import { Cache } from 'cache-manager'
import { Cron, CronExpression } from '@nestjs/schedule'
import { ConfigService } from '@nestjs/config'
import { HttpService } from '@nestjs/axios'
import { CACHE_MANAGER } from '@nestjs/cache-manager'
import TreasuryRepository from './treasury.repository'
import { firstLevelNodeProcess } from './treasury.dto'

Expand All @@ -11,6 +13,7 @@ class TreasuryService {
private repo: TreasuryRepository,
private configService: ConfigService,
private httpService: HttpService,
@Inject(CACHE_MANAGER) private cacheManager: Cache,
) {}

private getTreasuryENVs() {
Expand All @@ -23,7 +26,13 @@ class TreasuryService {
@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT)
async storeTotalValue() {
if (firstLevelNodeProcess()) {
const value = await this.requestTotalbalance()
let value: number | null | undefined = await this.cacheManager.get(
'treasuryBalance',
)
if (!value) {
value = await this.requestTotalbalance()
await this.cacheManager.set('treasuryBalance', value, 7200 * 1000)
}
await this.repo.saveData(`${value}`)
}
}
Expand Down

0 comments on commit cf489ab

Please sign in to comment.