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] Added job id relation to the payment #835

Merged
merged 3 commits into from
Aug 28, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export class InitialMigration1691485394906 implements MigrationInterface {
"source" "hmt"."payments_source_enum" NOT NULL,
"status" "hmt"."payments_status_enum" NOT NULL,
"user_id" integer NOT NULL,
"job_id" integer,
CONSTRAINT "REL_f83af8ea8055b85bde0e095e40" UNIQUE ("job_id"),
CONSTRAINT "PK_197ab7af18c93fbb0c9b28b4a59" PRIMARY KEY ("id")
)
`);
Expand Down Expand Up @@ -132,6 +134,10 @@ export class InitialMigration1691485394906 implements MigrationInterface {
ALTER TABLE "hmt"."jobs"
ADD CONSTRAINT "FK_9027c8f0ba75fbc1ac46647d043" FOREIGN KEY ("user_id") REFERENCES "hmt"."users"("id") ON DELETE NO ACTION ON UPDATE NO ACTION
`);
await queryRunner.query(`
ALTER TABLE "hmt"."payments"
ADD CONSTRAINT "FK_f83af8ea8055b85bde0e095e400" FOREIGN KEY ("job_id") REFERENCES "hmt"."jobs"("id") ON DELETE NO ACTION ON UPDATE NO ACTION
`);
await queryRunner.query(`
ALTER TABLE "hmt"."tokens"
ADD CONSTRAINT "FK_8769073e38c365f315426554ca5" FOREIGN KEY ("user_id") REFERENCES "hmt"."users"("id") ON DELETE NO ACTION ON UPDATE NO ACTION
Expand All @@ -152,6 +158,9 @@ export class InitialMigration1691485394906 implements MigrationInterface {
await queryRunner.query(`
ALTER TABLE "hmt"."jobs" DROP CONSTRAINT "FK_9027c8f0ba75fbc1ac46647d043"
`);
await queryRunner.query(`
ALTER TABLE "hmt"."payments" DROP CONSTRAINT "FK_f83af8ea8055b85bde0e095e400"
`);
await queryRunner.query(`
ALTER TABLE "hmt"."payments" DROP CONSTRAINT "FK_427785468fb7d2733f59e7d7d39"
`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
Column,
Entity,
Generated,
JoinColumn,
OneToOne,
PrimaryGeneratedColumn,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Column, Entity, Index, ManyToOne } from 'typeorm';
import { Column, Entity, Index, ManyToOne, OneToOne } from 'typeorm';

import { NS } from '../../common/constants';
import { IJob } from '../../common/interfaces';
import { JobStatus } from '../../common/enums/job';
import { BaseEntity } from '../../database/base.entity';
import { UserEntity } from '../user/user.entity';
import { PaymentEntity } from '../payment/payment.entity';

@Entity({ schema: NS, name: 'jobs' })
@Index(['chainId', 'escrowAddress'], { unique: true })
Expand Down Expand Up @@ -39,6 +40,9 @@ export class JobEntity extends BaseEntity implements IJob {
@Column({ type: 'int' })
public userId: number;

@OneToOne(() => PaymentEntity, (payment) => payment.job)
public payment: PaymentEntity;

@Column({ type: 'int', default: 0 })
public retriesCount: number;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ describe('JobService', () => {

describe('createJob', () => {
const userId = 1;
const jobId = 123;
const fortuneJobDto: JobFortuneDto = {
chainId: MOCK_CHAIN_ID,
submissionsRequired: MOCK_SUBMISSION_REQUIRED,
Expand All @@ -187,11 +188,27 @@ describe('JobService', () => {
const userBalance = 25;
getUserBalanceMock.mockResolvedValue(userBalance);

const mockJobEntity: Partial<JobEntity> = {
id: jobId,
userId: userId,
chainId: ChainId.LOCALHOST,
manifestUrl: MOCK_FILE_URL,
manifestHash: MOCK_FILE_HASH,
escrowAddress: MOCK_ADDRESS,
fee,
fundAmount,
status: JobStatus.PENDING,
save: jest.fn().mockResolvedValue(true),
};

jobRepository.create = jest.fn().mockResolvedValue(mockJobEntity);

await jobService.createJob(userId, JobRequestType.FORTUNE, fortuneJobDto);

expect(paymentService.getUserBalance).toHaveBeenCalledWith(userId);
expect(paymentRepository.create).toHaveBeenCalledWith({
userId,
jobId,
source: PaymentSource.BALANCE,
type: PaymentType.WITHDRAWAL,
currency: TokenId.HMT,
Expand Down Expand Up @@ -278,6 +295,7 @@ describe('JobService', () => {

describe('createJob with image label binary type', () => {
const userId = 1;
const jobId = 123;

const imageLabelBinaryJobDto: JobCvatDto = {
chainId: MOCK_CHAIN_ID,
Expand Down Expand Up @@ -309,6 +327,21 @@ describe('JobService', () => {
const userBalance = 25;
getUserBalanceMock.mockResolvedValue(userBalance);

const mockJobEntity: Partial<JobEntity> = {
id: jobId,
userId: userId,
chainId: ChainId.LOCALHOST,
manifestUrl: MOCK_FILE_URL,
manifestHash: MOCK_FILE_HASH,
escrowAddress: MOCK_ADDRESS,
fee,
fundAmount,
status: JobStatus.PENDING,
save: jest.fn().mockResolvedValue(true),
};

jobRepository.create = jest.fn().mockResolvedValue(mockJobEntity);

await jobService.createJob(
userId,
JobRequestType.IMAGE_LABEL_BINARY,
Expand All @@ -318,6 +351,7 @@ describe('JobService', () => {
expect(paymentService.getUserBalance).toHaveBeenCalledWith(userId);
expect(paymentRepository.create).toHaveBeenCalledWith({
userId,
jobId,
source: PaymentSource.BALANCE,
type: PaymentType.WITHDRAWAL,
currency: TokenId.HMT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ export class JobService {
try {
await this.paymentRepository.create({
userId,
jobId: jobEntity.id,
source: PaymentSource.BALANCE,
type: PaymentType.WITHDRAWAL,
amount: -tokenTotalAmount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class PaymentCreateDto {
public type?: PaymentType;
public chainId?: number;
public status?: PaymentStatus;
public jobId?: number;
}

export class GetRateDto {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Column, Entity, Index, JoinColumn, ManyToOne } from 'typeorm';
import { Column, Entity, Index, JoinColumn, ManyToOne, OneToOne } from 'typeorm';
import { NS } from '../../common/constants';
import { BaseEntity } from '../../database/base.entity';
import { PaymentSource, PaymentStatus, PaymentType } from '../../common/enums/payment';
import { UserEntity } from '../user/user.entity';
import { JobEntity } from '../job/job.entity';

@Entity({ schema: NS, name: 'payments' })
@Index(['chainId', 'transaction'], {
Expand Down Expand Up @@ -53,4 +54,11 @@ export class PaymentEntity extends BaseEntity {

@Column({ type: 'int' })
public userId: number;

@JoinColumn()
@OneToOne(() => JobEntity, (job) => job.payment)
public job: JobEntity;

@Column({ type: 'int', nullable: true })
public jobId: number;
}