Skip to content

Commit 9a6ef06

Browse files
committed
fix(deployment): searches leases with the correct params
refs #714
1 parent 54c501c commit 9a6ef06

File tree

4 files changed

+88
-22
lines changed

4 files changed

+88
-22
lines changed

apps/api/src/deployment/services/deployment-setting/deployment-setting.service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class DeploymentSettingService {
6565
return { ...params, estimatedTopUpAmount: 0, topUpFrequencyMs: this.topUpFrequencyMs };
6666
}
6767

68-
const estimatedTopUpAmount = await this.drainingDeploymentService.calculateTopUpAmountForDseqAndOwner(params.dseq, params.userId);
68+
const estimatedTopUpAmount = await this.drainingDeploymentService.calculateTopUpAmountForDseqAndUserId(params.dseq, params.userId);
6969

7070
return { ...params, estimatedTopUpAmount, topUpFrequencyMs: this.topUpFrequencyMs };
7171
}

apps/api/src/deployment/services/draining-deployment/draining-deployment.service.spec.ts

+69-11
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
import "@test/mocks/logger-service.mock";
22

3+
import { faker } from "@faker-js/faker";
4+
5+
import { UserWalletRepository } from "@src/billing/repositories";
36
import { BlockHttpService } from "@src/chain/services/block-http/block-http.service";
47
import { AutoTopUpDeployment, DeploymentSettingRepository } from "@src/deployment/repositories/deployment-setting/deployment-setting.repository";
58
import { LeaseRepository } from "@src/deployment/repositories/lease/lease.repository";
69
import { averageBlockCountInAnHour } from "@src/utils/constants";
10+
import { DeploymentConfigService } from "../deployment-config/deployment-config.service";
711
import { DrainingDeploymentService } from "./draining-deployment.service";
812

9-
import { MockConfigService } from "@test/mocks/config-service.mock";
13+
import { AkashAddressSeeder } from "@test/seeders/akash-address.seeder";
1014
import { AutoTopUpDeploymentSeeder } from "@test/seeders/auto-top-up-deployment.seeder";
1115
import { DrainingDeploymentSeeder } from "@test/seeders/draining-deployment.seeder";
16+
import { UserWalletSeeder } from "@test/seeders/user-wallet.seeder";
1217

1318
jest.mock("@akashnetwork/logging");
1419

15-
type DeploymentConfigValues = {
16-
AUTO_TOP_UP_JOB_INTERVAL_IN_H: number;
17-
AUTO_TOP_UP_DEPLOYMENT_INTERVAL_IN_H: number;
18-
};
19-
2020
describe(DrainingDeploymentService.name, () => {
2121
let blockHttpService: jest.Mocked<BlockHttpService>;
2222
let leaseRepository: jest.Mocked<LeaseRepository>;
23+
let userWalletRepository: jest.Mocked<UserWalletRepository>;
2324
let deploymentSettingRepository: jest.Mocked<DeploymentSettingRepository>;
2425
let service: DrainingDeploymentService;
25-
let config: MockConfigService<DeploymentConfigValues>;
26+
let config: jest.Mocked<DeploymentConfigService>;
2627
const CURRENT_BLOCK_HEIGHT = 7481457;
2728

2829
beforeEach(() => {
@@ -32,19 +33,29 @@ describe(DrainingDeploymentService.name, () => {
3233
} as Partial<jest.Mocked<BlockHttpService>> as jest.Mocked<BlockHttpService>;
3334

3435
leaseRepository = {
35-
findManyByDseqAndOwner: jest.fn()
36+
findManyByDseqAndOwner: jest.fn(),
37+
findOneByDseqAndOwner: jest.fn()
3638
} as Partial<jest.Mocked<LeaseRepository>> as jest.Mocked<LeaseRepository>;
3739

40+
userWalletRepository = {
41+
findOneByUserId: jest.fn()
42+
} as Partial<jest.Mocked<UserWalletRepository>> as jest.Mocked<UserWalletRepository>;
43+
3844
deploymentSettingRepository = {
3945
paginateAutoTopUpDeployments: jest.fn()
4046
} as Partial<jest.Mocked<DeploymentSettingRepository>> as jest.Mocked<DeploymentSettingRepository>;
4147

42-
config = new MockConfigService<DeploymentConfigValues>({
48+
const configValues = {
4349
AUTO_TOP_UP_JOB_INTERVAL_IN_H: 1,
4450
AUTO_TOP_UP_DEPLOYMENT_INTERVAL_IN_H: 3
45-
});
51+
};
52+
53+
config = {
54+
get: jest.fn().mockImplementation((key: keyof typeof configValues) => configValues[key]),
55+
config: configValues
56+
} as unknown as jest.Mocked<DeploymentConfigService>;
4657

47-
service = new DrainingDeploymentService(blockHttpService, leaseRepository, deploymentSettingRepository, config);
58+
service = new DrainingDeploymentService(blockHttpService, leaseRepository, userWalletRepository, deploymentSettingRepository, config);
4859
});
4960

5061
describe("paginate", () => {
@@ -156,4 +167,51 @@ describe(DrainingDeploymentService.name, () => {
156167
});
157168
});
158169
});
170+
171+
describe("calculateTopUpAmountForDseqAndUserId", () => {
172+
const userId = faker.string.uuid();
173+
const dseq = faker.string.numeric(6);
174+
const address = AkashAddressSeeder.create();
175+
const userWallet = UserWalletSeeder.create({ address });
176+
const expectedTopUpAmount = 100000;
177+
178+
beforeEach(() => {
179+
userWalletRepository.findOneByUserId.mockResolvedValue(userWallet);
180+
jest.spyOn(service, "calculateTopUpAmount").mockResolvedValue(expectedTopUpAmount);
181+
});
182+
183+
it("should calculate top up amount for valid deployment", async () => {
184+
const deployment = DrainingDeploymentSeeder.create();
185+
leaseRepository.findOneByDseqAndOwner.mockResolvedValue(deployment);
186+
187+
const amount = await service.calculateTopUpAmountForDseqAndUserId(dseq, userId);
188+
189+
expect(userWalletRepository.findOneByUserId).toHaveBeenCalledWith(userId);
190+
expect(leaseRepository.findOneByDseqAndOwner).toHaveBeenCalledWith(dseq, address);
191+
expect(service.calculateTopUpAmount).toHaveBeenCalledWith(deployment);
192+
expect(amount).toBe(expectedTopUpAmount);
193+
});
194+
195+
it("should return 0 if user wallet not found", async () => {
196+
userWalletRepository.findOneByUserId.mockResolvedValue(null);
197+
198+
const amount = await service.calculateTopUpAmountForDseqAndUserId(dseq, userId);
199+
200+
expect(userWalletRepository.findOneByUserId).toHaveBeenCalledWith(userId);
201+
expect(leaseRepository.findOneByDseqAndOwner).not.toHaveBeenCalled();
202+
expect(service.calculateTopUpAmount).not.toHaveBeenCalled();
203+
expect(amount).toBe(0);
204+
});
205+
206+
it("should return 0 if lease not found", async () => {
207+
leaseRepository.findOneByDseqAndOwner.mockResolvedValue(null);
208+
209+
const amount = await service.calculateTopUpAmountForDseqAndUserId(dseq, userId);
210+
211+
expect(userWalletRepository.findOneByUserId).toHaveBeenCalledWith(userId);
212+
expect(leaseRepository.findOneByDseqAndOwner).toHaveBeenCalledWith(dseq, address);
213+
expect(service.calculateTopUpAmount).not.toHaveBeenCalled();
214+
expect(amount).toBe(0);
215+
});
216+
});
159217
});

apps/api/src/deployment/services/draining-deployment/draining-deployment.service.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import keyBy from "lodash/keyBy";
22
import { singleton } from "tsyringe";
33

4+
import { UserWalletRepository } from "@src/billing/repositories";
45
import { BlockHttpService } from "@src/chain/services/block-http/block-http.service";
56
import { AutoTopUpDeployment, DeploymentSettingRepository } from "@src/deployment/repositories/deployment-setting/deployment-setting.repository";
67
import { DrainingDeploymentOutput, LeaseRepository } from "@src/deployment/repositories/lease/lease.repository";
@@ -16,6 +17,7 @@ export class DrainingDeploymentService {
1617
constructor(
1718
private readonly blockHttpService: BlockHttpService,
1819
private readonly leaseRepository: LeaseRepository,
20+
private readonly userWalletRepository: UserWalletRepository,
1921
private readonly deploymentSettingRepository: DeploymentSettingRepository,
2022
private readonly config: DeploymentConfigService
2123
) {}
@@ -47,8 +49,14 @@ export class DrainingDeploymentService {
4749
});
4850
}
4951

50-
async calculateTopUpAmountForDseqAndOwner(dseq: string, owner: string): Promise<number> {
51-
const deploymentSetting = await this.leaseRepository.findOneByDseqAndOwner(dseq, owner);
52+
async calculateTopUpAmountForDseqAndUserId(dseq: string, userId: string): Promise<number> {
53+
const userWallet = await this.userWalletRepository.findOneByUserId(userId);
54+
55+
if (!userWallet) {
56+
return 0;
57+
}
58+
59+
const deploymentSetting = await this.leaseRepository.findOneByDseqAndOwner(dseq, userWallet.address);
5260

5361
if (!deploymentSetting) {
5462
return 0;

apps/api/test/functional/deployment-setting.spec.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ describe("Deployment Settings", () => {
7474
});
7575

7676
it("should return deployment settings if found", async () => {
77-
const { token, user } = await walletService.createUserAndWallet();
77+
const { token, user, wallet } = await walletService.createUserAndWallet();
7878
const dseq = faker.string.numeric();
7979

8080
const settings = await deploymentSettingRepository.create({
@@ -102,7 +102,7 @@ describe("Deployment Settings", () => {
102102
topUpFrequencyMs: expect.any(Number)
103103
}
104104
});
105-
expect(leaseRepository.findOneByDseqAndOwner).toHaveBeenCalledWith(dseq, user.id);
105+
expect(leaseRepository.findOneByDseqAndOwner).toHaveBeenCalledWith(dseq, wallet.address);
106106
});
107107
});
108108

@@ -153,7 +153,7 @@ describe("Deployment Settings", () => {
153153
});
154154

155155
it("should create deployment settings", async () => {
156-
const { token, user } = await walletService.createUserAndWallet();
156+
const { token, user, wallet } = await walletService.createUserAndWallet();
157157
const dseq = faker.string.numeric();
158158

159159
const response = await app.request("/v1/deployment-settings", {
@@ -186,7 +186,7 @@ describe("Deployment Settings", () => {
186186
dseq,
187187
autoTopUpEnabled: true
188188
});
189-
expect(leaseRepository.findOneByDseqAndOwner).toHaveBeenCalledWith(dseq, user.id);
189+
expect(leaseRepository.findOneByDseqAndOwner).toHaveBeenCalledWith(dseq, wallet.address);
190190
});
191191
});
192192

@@ -208,7 +208,7 @@ describe("Deployment Settings", () => {
208208
});
209209

210210
it("should create and return new setting if not found", async () => {
211-
const { token, user } = await walletService.createUserAndWallet();
211+
const { token, user, wallet } = await walletService.createUserAndWallet();
212212
const dseq = faker.string.numeric();
213213

214214
const response = await app.request(`/v1/deployment-settings/${user.id}/${dseq}`, {
@@ -239,7 +239,7 @@ describe("Deployment Settings", () => {
239239
dseq,
240240
autoTopUpEnabled: true
241241
});
242-
expect(leaseRepository.findOneByDseqAndOwner).toHaveBeenCalledWith(dseq, user.id);
242+
expect(leaseRepository.findOneByDseqAndOwner).toHaveBeenCalledWith(dseq, wallet.address);
243243
});
244244

245245
it("should return 404 when updating other user's deployment settings", async () => {
@@ -273,7 +273,7 @@ describe("Deployment Settings", () => {
273273
});
274274

275275
it("should update deployment settings", async () => {
276-
const { token, user } = await walletService.createUserAndWallet();
276+
const { token, user, wallet } = await walletService.createUserAndWallet();
277277
const dseq = faker.string.numeric();
278278

279279
const settings = await deploymentSettingRepository.create({
@@ -312,7 +312,7 @@ describe("Deployment Settings", () => {
312312
dseq,
313313
autoTopUpEnabled: true
314314
});
315-
expect(leaseRepository.findOneByDseqAndOwner).toHaveBeenCalledWith(dseq, user.id);
315+
expect(leaseRepository.findOneByDseqAndOwner).toHaveBeenCalledWith(dseq, wallet.address);
316316
});
317317
});
318318
});

0 commit comments

Comments
 (0)