Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Job Launcher Server] Move withdrawal creation logic to payments module #3147

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ import { createMock } from '@golevelup/ts-jest';
import { Encryption } from '@human-protocol/sdk';
import { ConfigService } from '@nestjs/config';
import { Test } from '@nestjs/testing';
import {
PaymentCurrency,
PaymentSource,
PaymentStatus,
PaymentType,
} from '../../common/enums/payment';
import { PaymentCurrency } from '../../common/enums/payment';
import {
JobRequestType,
JobStatus,
Expand Down Expand Up @@ -39,7 +34,6 @@ import { mul } from '../../common/utils/decimal';
describe('JobService', () => {
let jobService: JobService,
paymentService: PaymentService,
paymentRepository: PaymentRepository,
jobRepository: JobRepository,
rateService: RateService;

Expand Down Expand Up @@ -110,7 +104,6 @@ describe('JobService', () => {

jobService = moduleRef.get<JobService>(JobService);
paymentService = moduleRef.get<PaymentService>(PaymentService);
paymentRepository = moduleRef.get<PaymentRepository>(PaymentRepository);
rateService = moduleRef.get<RateService>(RateService);
jobRepository = moduleRef.get<JobRepository>(JobRepository);
});
Expand Down Expand Up @@ -163,19 +156,16 @@ describe('JobService', () => {
fortuneJobDto,
);

expect(paymentRepository.createUnique).toHaveBeenCalledWith({
userId: userMock.id,
jobId: jobEntityMock.id,
source: PaymentSource.BALANCE,
type: PaymentType.WITHDRAWAL,
currency: fortuneJobDto.paymentCurrency,
amount: expect.any(Number),
rate: await rateService.getRate(
expect(paymentService.createWithdrawalPayment).toHaveBeenCalledWith(
userMock.id,
jobEntityMock.id,
expect.any(Number),
fortuneJobDto.paymentCurrency,
await rateService.getRate(
fortuneJobDto.paymentCurrency,
PaymentCurrency.USD,
),
status: PaymentStatus.SUCCEEDED,
});
);
expect(jobRepository.createUnique).toHaveBeenCalledWith({
chainId: fortuneJobDto.chainId,
userId: userMock.id,
Expand Down Expand Up @@ -234,16 +224,13 @@ describe('JobService', () => {
fortuneJobDto,
);

expect(paymentRepository.createUnique).toHaveBeenCalledWith({
userId: userMock.id,
jobId: jobEntityMock.id,
source: PaymentSource.BALANCE,
type: PaymentType.WITHDRAWAL,
currency: fortuneJobDto.paymentCurrency,
amount: expect.any(Number),
rate: paymentToUsdRate,
status: PaymentStatus.SUCCEEDED,
});
expect(paymentService.createWithdrawalPayment).toHaveBeenCalledWith(
userMock.id,
jobEntityMock.id,
expect.any(Number),
fortuneJobDto.paymentCurrency,
paymentToUsdRate,
);
expect(jobRepository.createUnique).toHaveBeenCalledWith({
chainId: fortuneJobDto.chainId,
userId: userMock.id,
Expand Down
28 changes: 8 additions & 20 deletions packages/apps/job-launcher/server/src/modules/job/job.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,9 @@ import {
JobCaptchaRequestType,
JobCaptchaShapeType,
} from '../../common/enums/job';
import {
FiatCurrency,
PaymentSource,
PaymentStatus,
PaymentType,
} from '../../common/enums/payment';
import { FiatCurrency } from '../../common/enums/payment';
import { parseUrl } from '../../common/utils';
import { add, div, lt, mul, max } from '../../common/utils/decimal';
import { PaymentRepository } from '../payment/payment.repository';
import { PaymentService } from '../payment/payment.service';
import { Web3Service } from '../web3/web3.service';
import {
Expand Down Expand Up @@ -99,7 +93,6 @@ import {
listObjectsInBucket,
} from '../../common/utils/storage';
import { WebhookDataDto } from '../webhook/webhook.dto';
import { PaymentEntity } from '../payment/payment.entity';
import {
ManifestAction,
EscrowAction,
Expand Down Expand Up @@ -135,7 +128,6 @@ export class JobService {
private readonly jobRepository: JobRepository,
private readonly webhookRepository: WebhookRepository,
private readonly paymentService: PaymentService,
private readonly paymentRepository: PaymentRepository,
private readonly serverConfigService: ServerConfigService,
private readonly authConfigService: AuthConfigService,
private readonly web3ConfigService: Web3ConfigService,
Expand Down Expand Up @@ -923,17 +915,13 @@ export class JobService {

jobEntity = await this.jobRepository.createUnique(jobEntity);

const paymentEntity = new PaymentEntity();
paymentEntity.userId = user.id;
paymentEntity.jobId = jobEntity.id;
paymentEntity.source = PaymentSource.BALANCE;
paymentEntity.type = PaymentType.WITHDRAWAL;
paymentEntity.amount = -totalPaymentAmount; // In the currency used for the payment.
paymentEntity.currency = dto.paymentCurrency;
paymentEntity.rate = paymentCurrencyRate;
paymentEntity.status = PaymentStatus.SUCCEEDED;

await this.paymentRepository.createUnique(paymentEntity);
await this.paymentService.createWithdrawalPayment(
user.id,
jobEntity.id,
totalPaymentAmount,
dto.paymentCurrency,
paymentCurrencyRate,
);

jobEntity.status = JobStatus.PAID;
await this.jobRepository.updateOne(jobEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1529,4 +1529,61 @@ describe('PaymentService', () => {
);
});
});

describe('createWithdrawalPayment', () => {
it('should create a withdrawal payment successfully', async () => {
const userId = 1;
const jobId = 2;
const amount = 100;
const currency = PaymentCurrency.USD;
const rate = 1;

jest
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should not be mocked, we should mock repository response

.spyOn(paymentRepository, 'createUnique')
.mockResolvedValueOnce(undefined as any);

await expect(
paymentService.createWithdrawalPayment(
userId,
jobId,
amount,
currency,
rate,
),
).resolves.not.toThrow();

expect(paymentRepository.createUnique).toHaveBeenCalledWith({
userId,
jobId,
source: PaymentSource.BALANCE,
type: PaymentType.WITHDRAWAL,
amount: -amount,
currency,
rate,
status: PaymentStatus.SUCCEEDED,
});
});

it('should throw an error if the payment creation fails', async () => {
const userId = 1;
const jobId = 2;
const amount = 100;
const currency = PaymentCurrency.USD;
const rate = 1;

jest
.spyOn(paymentRepository, 'createUnique')
.mockRejectedValueOnce(new Error('Database error'));

await expect(
paymentService.createWithdrawalPayment(
userId,
jobId,
amount,
currency,
rate,
),
).rejects.toThrow('Database error');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,26 @@ export class PaymentService {
// return;
// }

public async createWithdrawalPayment(
userId: number,
jobId: number,
amount: number,
currency: string,
rate: number,
): Promise<void> {
const paymentEntity = new PaymentEntity();
paymentEntity.userId = userId;
paymentEntity.jobId = jobId;
paymentEntity.source = PaymentSource.BALANCE;
paymentEntity.type = PaymentType.WITHDRAWAL;
paymentEntity.amount = -amount; // In the currency used for the payment.
paymentEntity.currency = currency;
paymentEntity.rate = rate;
paymentEntity.status = PaymentStatus.SUCCEEDED;

await this.paymentRepository.createUnique(paymentEntity);
}

async listUserPaymentMethods(user: UserEntity): Promise<CardDto[]> {
const cards: CardDto[] = [];
if (!user.stripeCustomerId) {
Expand Down