-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathjob-assignment.controller.ts
More file actions
188 lines (180 loc) · 5.47 KB
/
job-assignment.controller.ts
File metadata and controls
188 lines (180 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { Mapper } from '@automapper/core';
import { InjectMapper } from '@automapper/nestjs';
import {
BadRequestException,
Body,
Controller,
Get,
HttpCode,
Post,
Query,
Request,
} from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { RequestWithUser } from '../../common/interfaces/jwt';
import { JobAssignmentService } from './job-assignment.service';
import {
JobAssignmentCommand,
JobAssignmentDto,
JobAssignmentResponse,
JobsFetchParamsCommand,
JobsFetchParamsDto,
JobsFetchResponse,
RefreshJobDto,
ResignJobCommand,
ResignJobDto,
} from './model/job-assignment.model';
import { ChainId } from '@human-protocol/sdk';
import axios from 'axios';
import { JobStatus } from '../../common/enums/global-common';
@ApiTags('Job-Assignment')
@ApiBearerAuth()
@Controller('/assignment')
export class JobAssignmentController {
constructor(
private readonly service: JobAssignmentService,
@InjectMapper() private readonly mapper: Mapper,
) {}
@ApiOperation({
summary: 'Request to assign a job to a logged user',
})
@HttpCode(200)
@Post('/job')
async assignJob(
@Body() jobAssignmentDto: JobAssignmentDto,
@Request() req: RequestWithUser,
): Promise<JobAssignmentResponse> {
// TODO: temporal - THIRSTYFI
if (jobAssignmentDto.escrow_address === 'thirstyfi-task') {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (new Date(process.env.THIRSTYFI_TASK_EXPIRATION_DATE!) < new Date()) {
throw new BadRequestException('Expired task');
}
try {
const { data } = await axios.post<any>(
`${process.env.THIRSTIFY_EXO}/join`,
{
walletAddress: jobAssignmentDto.wallet_address,
email: req.user.email,
apiKey: jobAssignmentDto.api_key,
apiSecret: jobAssignmentDto.api_secret,
},
{
headers: {
Authorization: `Bearer ${process.env.THIRSTIFY_TOKEN}`,
'Content-Type': 'application/json',
},
},
);
return {
assignment_id: data.id,
escrow_address: 'thirstyfi-task',
chain_id: ChainId.POLYGON,
job_type: 'thirstyfi',
status: 'ACTIVE',
reward_amount: '5 - 50',
reward_token: 'USDT',
created_at: new Date().toISOString(),
expires_at: process.env.THIRSTYFI_TASK_EXPIRATION_DATE ?? '',
};
} catch (error) {
console.log(error);
throw new BadRequestException(error.response.data.error);
}
}
const jobAssignmentCommand = this.mapper.map(
jobAssignmentDto,
JobAssignmentDto,
JobAssignmentCommand,
);
jobAssignmentCommand.token = req.token;
return this.service.processJobAssignment(jobAssignmentCommand);
}
@ApiOperation({
summary: 'Request to get jobs assigned to a logged user',
})
@Get('/job')
async getAssignedJobs(
@Query() jobsAssignmentParamsDto: JobsFetchParamsDto,
@Request() req: RequestWithUser,
): Promise<JobsFetchResponse> {
// TODO: temporal - THIRSTYFI
if (
jobsAssignmentParamsDto.oracle_address ===
process.env.THIRSTYFI_ORACLE_ADDRESS
) {
const { data } = await axios.get<any>(
`${process.env.THIRSTIFY_EXO}/participant`,
{
params: { email: req.user.email },
headers: { Authorization: `Bearer ${process.env.THIRSTIFY_TOKEN}` },
},
);
return Number(data.id) <= 0
? {
page: 0,
page_size: 1,
total_pages: 1,
total_results: 0,
results: [],
}
: {
page: 0,
page_size: 1,
total_pages: 1,
total_results: 1,
results: [
{
chain_id: ChainId.POLYGON,
assignment_id: data.id,
escrow_address: 'thirstyfi-task',
job_type: 'thirstyfi',
reward_amount: '5 - 50',
reward_token: 'USDT',
status:
data.status === 'pending'
? JobStatus.ACTIVE
: JobStatus.COMPLETED,
created_at: new Date().toISOString(),
expires_at: process.env.THIRSTYFI_TASK_EXPIRATION_DATE,
url: 'https://thirsty.fi/blog/campaign-human-protocol',
} as any,
],
};
}
const jobsAssignmentParamsCommand = this.mapper.map(
jobsAssignmentParamsDto,
JobsFetchParamsDto,
JobsFetchParamsCommand,
);
jobsAssignmentParamsCommand.token = req.token;
return this.service.processGetAssignedJobs(jobsAssignmentParamsCommand);
}
@ApiOperation({
summary: 'Request to resign from assigment',
})
@HttpCode(200)
@Post('/resign-job')
async resignAssigment(
@Body() dto: ResignJobDto,
@Request() req: RequestWithUser,
) {
const command = this.mapper.map(dto, ResignJobDto, ResignJobCommand);
command.token = req.token;
return this.service.resignJob(command);
}
@ApiOperation({
summary: 'Request to refresh assigments data',
})
@HttpCode(200)
@Post('/refresh')
async refreshAssigments(
@Body() dto: RefreshJobDto,
@Request() req: RequestWithUser,
) {
const command = new JobsFetchParamsCommand();
command.oracleAddress = dto.oracle_address;
command.token = req.token;
return this.service.updateAssignmentsCache(command);
}
}