Skip to content

Commit

Permalink
chore: changes after code review
Browse files Browse the repository at this point in the history
  • Loading branch information
MWBlocky committed Mar 5, 2024
1 parent ed7d548 commit 4ba85d0
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 101 deletions.
22 changes: 22 additions & 0 deletions packages/apps/human-app/server/src/common/enums/job-assignment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export enum StatusEnum {
ACTIVE = 'ACTIVE',
VALIDATION = 'VALIDATION',
COMPLETED = 'COMPLETED',
EXPIRED = 'EXPIRED',
CANCELED = 'CANCELED',
REJECTED = 'REJECTED',
}

export enum SortOrder {
ASC = 'ASC',
DESC = 'DESC',
}

export enum SortField {
CHAIN_ID = 'chain_id',
JOB_TYPE = 'job_type',
STATUS = 'status',
REWARD_AMOUNT = 'reward_amount',
CREATED_AT = 'created_at',
EXPIRES_AT = 'expires_at',
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { ApiProperty } from '@nestjs/swagger';
import { AutoMap } from '@automapper/classes';
import {
SortField,
SortOrder,
StatusEnum,
} from '../../../common/enums/job-assignment';

export class JobAssignmentDto {
@AutoMap()
@ApiProperty({ example: 'string' })
exchange_oracle_url: string;

@AutoMap()
@ApiProperty({ example: 'string' })
escrow_address: string;
Expand All @@ -12,53 +21,42 @@ export class JobAssignmentDto {
}

export class JobAssignmentCommand {
@AutoMap()
exchange_oracle_url: string;
@AutoMap()
escrow_address: string;
@AutoMap()
chain_id: number;
}

export class JobAssignmentData {
@AutoMap()
exchange_oracle_url: string;
@AutoMap()
escrow_address: string;
@AutoMap()
chain_id: number;
}

export class JobAssignmentResponse {
@AutoMap()
assignment_id: string;
@AutoMap()
escrow_address: string;
@AutoMap()
chain_id: number;
@AutoMap()
job_type: string;
@AutoMap()
url: string;
@AutoMap()
url?: string; //Only for ACTIVE status
status: string;
@AutoMap()
reward_amount: string;
@AutoMap()
reward_token: string;
@AutoMap()
created_at: string;
@AutoMap()
updated_at: string;
@AutoMap()
updated_at?: string; //Only for COMPLETED, EXPIRED, CANCELED and REJECTED status
expires_at: string;
}

export enum StatusEnum {
ACTIVE = 'ACTIVE',
VALIDATION = 'VALIDATION',
COMPLETED = 'COMPLETED',
EXPIRED = 'EXPIRED',
CANCELED = 'CANCELED',
REJECTED = 'REJECTED',
}
export class JobsAssignmentParamsDto {
@AutoMap()
@ApiProperty({ example: 'string' })
exchange_oracle_url: string;

@AutoMap()
@ApiProperty({ example: 'string' })
assignment_id: string;
Expand Down Expand Up @@ -89,14 +87,16 @@ export class JobsAssignmentParamsDto {

@AutoMap()
@ApiProperty({ example: 'ASC', default: 'ASC' })
sort: 'ASC' | 'DESC';
sort: SortOrder;

@AutoMap()
@ApiProperty({ example: 'created_at', default: 'created_at' })
sort_field: 'chain_id' | 'job_type' | 'reward_amount' | 'created_at';
sort_field: SortField;
}

export class JobsAssignmentParamsCommand {
@AutoMap()
exchange_oracle_url: string;
@AutoMap()
assignment_id: string;
@AutoMap()
Expand All @@ -112,12 +112,14 @@ export class JobsAssignmentParamsCommand {
@AutoMap()
page: number;
@AutoMap()
sort: 'ASC' | 'DESC';
sort: SortOrder;
@AutoMap()
sort_field: 'chain_id' | 'job_type' | 'reward_amount' | 'created_at';
sort_field: SortField;
}

export class JobsAssignmentParamsData {
@AutoMap()
exchange_oracle_url: string;
@AutoMap()
assignment_id: string;
@AutoMap()
Expand All @@ -133,33 +135,22 @@ export class JobsAssignmentParamsData {
@AutoMap()
page: number;
@AutoMap()
sort: 'ASC' | 'DESC';
sort: SortOrder;
@AutoMap()
sort_field: 'chain_id' | 'job_type' | 'reward_amount' | 'created_at';
sort_field: SortField;
}

export class JobsAssignmentResponseItem {
@AutoMap()
assignment_id: string;
@AutoMap()
escrow_address: string;
@AutoMap()
chain_id: number;
@AutoMap()
job_type: string;
@AutoMap()
url: string;
@AutoMap()
url?: string; //Only for ACTIVE status
status: string;
@AutoMap()
reward_amount: string;
@AutoMap()
reward_token: string;
@AutoMap()
created_at: string;
@AutoMap()
updated_at: string;
@AutoMap()
updated_at: string; //Only for VALIDATION, COMPLETED, EXPIRED, CANCELED and REJECTED status
expires_at: string;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
Body,
Controller,
Get,
Param,
Post,
Query,
UsePipes,
Expand All @@ -13,10 +12,10 @@ import { InjectMapper } from '@automapper/nestjs';
import { Mapper } from '@automapper/core';
import { JobAssignmentService } from './job-assignment.service';
import {
JobAssignmentCommand,
JobAssignmentDto,
JobsAssignmentParamsDto,
JobAssignmentCommand,
JobAssignmentResponse,
JobsAssignmentParamsDto,
JobsAssignmentParamsCommand,
JobsAssignmentResponse,
} from './interfaces/job-assignment.interface';
Expand All @@ -29,42 +28,36 @@ export class JobAssignmentController {
) {}

@ApiTags('Job-Assignment')
@Post('/:url/assignment')
@Post('/assignment/job')
@ApiOperation({
summary: 'Request to assign a job to a logged user',
})
@UsePipes(new ValidationPipe())
public async assignJob(
@Param('url') url: string,
@Body() jobAssignmentDto: JobAssignmentDto,
): Promise<JobAssignmentResponse> {
const jobAssignmentCommand = this.mapper.map(
jobAssignmentDto,
JobAssignmentDto,
JobAssignmentCommand,
);
return this.jobAssignmentService.processJobAssignment(
url,
jobAssignmentCommand,
);
return this.jobAssignmentService.processJobAssignment(jobAssignmentCommand);
}

@ApiTags('Job-Assignment')
@Get('/:url/assignment')
@Get('/assignment/job')
@ApiOperation({
summary: 'Request to get a jobs assigned to a specific user',
summary: 'Request to get a jobs assigned to a logged user',
})
public async getAssignedJobs(
@Param('url') url: string,
@Query() jobsAssignmentParamsDto: JobsAssignmentParamsDto,
): Promise<JobsAssignmentResponse> {
const jobsAssignmentParamsCommand = this.mapper.map(
jobsAssignmentParamsDto,
JobsAssignmentParamsDto,
JobsAssignmentParamsCommand,
);
return this.jobAssignmentService.processGettingAssignedJobs(
url,
return this.jobAssignmentService.processGetAssignedJobs(
jobsAssignmentParamsCommand,
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { lastValueFrom } from 'rxjs';
import {
JobsAssignmentParamsCommand,
Expand All @@ -20,7 +20,6 @@ export class JobAssignmentService {
) {}

async processJobAssignment(
url: string,
jobAssignmentCommand: JobAssignmentCommand,
): Promise<JobAssignmentResponse> {
const jobAssignmentData = this.mapper.map(
Expand All @@ -29,6 +28,7 @@ export class JobAssignmentService {
JobAssignmentData,
);
try {
const url = jobAssignmentData.exchange_oracle_url;
const options = {
method: 'POST',
url: `${url}/assignment`,
Expand All @@ -37,19 +37,11 @@ export class JobAssignmentService {
const response = await lastValueFrom(this.httpService.request(options));
return response.data;
} catch (error) {
if (error.response) {
throw new HttpException(error.response.data, error.response.status);
} else {
throw new HttpException(
'Internal Server Error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
throw error;
}
}

async processGettingAssignedJobs(
url: string,
async processGetAssignedJobs(
jobsAssignmentParamsCommand: JobsAssignmentParamsCommand,
): Promise<JobsAssignmentResponse> {
const jobsAssignmentParamsData = this.mapper.map(
Expand All @@ -58,6 +50,7 @@ export class JobAssignmentService {
JobsAssignmentParamsData,
);
try {
const url = jobsAssignmentParamsData.exchange_oracle_url;
const options = {
method: 'GET',
url: `${url}/assignment`,
Expand All @@ -66,14 +59,7 @@ export class JobAssignmentService {
const response = await lastValueFrom(this.httpService.request(options));
return response.data;
} catch (error) {
if (error.response) {
throw new HttpException(error.response.data, error.response.status);
} else {
throw new HttpException(
'Internal Server Error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
throw error;
}
}
}
Loading

0 comments on commit 4ba85d0

Please sign in to comment.