Skip to content

Commit a0f90de

Browse files
authored
[HUMAN App] Added enums parsing (#2633)
* Added enums parsing * Improved interceptor * Removed comments
1 parent ece8ef8 commit a0f90de

File tree

13 files changed

+452
-64
lines changed

13 files changed

+452
-64
lines changed

packages/apps/human-app/server/src/common/config/spec/gateway-config-service.mock.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,78 +2,78 @@ export const gatewayConfigServiceMock = {
22
getConfig: jest.fn().mockReturnValue({
33
url: 'https://example.com',
44
endpoints: {
5-
WORKER_SIGNUP: {
5+
worker_signup: {
66
endpoint: '/auth/signup',
77
method: 'POST',
88
headers: { 'Content-Type': 'application/json' },
99
},
10-
OPERATOR_SIGNUP: {
10+
operator_signup: {
1111
endpoint: '/auth/web3/signup',
1212
method: 'POST',
1313
headers: { 'Content-Type': 'application/json' },
1414
},
15-
WORKER_SIGNIN: {
15+
worker_signin: {
1616
endpoint: '/auth/signin',
1717
method: 'POST',
1818
headers: { 'Content-Type': 'application/json' },
1919
},
20-
WORKER_REGISTRATION: {
20+
worker_registration: {
2121
endpoint: '/user/registration',
2222
method: 'POST',
2323
headers: { 'Content-Type': 'application/json' },
2424
},
25-
EMAIL_VERIFICATION: {
25+
email_verification: {
2626
endpoint: '/auth/email-verification',
2727
method: 'POST',
2828
headers: { 'Content-Type': 'application/json' },
2929
},
30-
RESEND_EMAIL_VERIFICATION: {
30+
resend_email_verification: {
3131
endpoint: '/auth/resend-email-verification',
3232
method: 'POST',
3333
headers: { 'Content-Type': 'application/json' },
3434
},
35-
FORGOT_PASSWORD: {
35+
forgot_password: {
3636
endpoint: '/auth/forgot-password',
3737
method: 'POST',
3838
headers: { 'Content-Type': 'application/json' },
3939
},
40-
RESTORE_PASSWORD: {
40+
restore_password: {
4141
endpoint: '/auth/restore-password',
4242
method: 'POST',
4343
headers: { 'Content-Type': 'application/json' },
4444
},
45-
PREPARE_SIGNATURE: {
45+
prepare_signature: {
4646
endpoint: '/user/prepare-signature',
4747
method: 'POST',
4848
headers: { 'Content-Type': 'application/json' },
4949
},
50-
DISABLE_OPERATOR: {
50+
disable_operator: {
5151
endpoint: '/user/disable-operator',
5252
method: 'POST',
5353
headers: { 'Content-Type': 'application/json' },
5454
},
55-
KYC_PROCEDURE_START: {
55+
kyc_procedure_start: {
5656
endpoint: '/kyc/start',
5757
method: 'POST',
5858
headers: { 'Content-Type': 'application/json' },
5959
},
60-
ENABLE_LABELING: {
60+
enable_labeling: {
6161
endpoint: '/labeler/register',
6262
method: 'POST',
6363
params: { api_key: 'mock-api-key' },
6464
},
65-
OPERATOR_SIGNIN: {
65+
operator_signin: {
6666
endpoint: '/auth/web3/signin',
6767
method: 'POST',
6868
headers: { 'Content-Type': 'application/json' },
6969
params: {},
7070
},
71-
REGISTER_ADDRESS: {
71+
register_address: {
7272
endpoint: '/user/register-address',
7373
method: 'POST',
7474
headers: { 'Content-Type': 'application/json' },
7575
},
76-
TOKEN_REFRESH: {
76+
token_refresh: {
7777
endpoint: '/auth/refresh',
7878
method: 'POST',
7979
headers: { 'Content-Type': 'application/json' },
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import {
2+
registerDecorator,
3+
ValidationOptions,
4+
ValidationArguments,
5+
} from 'class-validator';
6+
import 'reflect-metadata';
7+
8+
export function IsEnumCaseInsensitive(
9+
enumType: any,
10+
validationOptions?: ValidationOptions,
11+
) {
12+
// eslint-disable-next-line @typescript-eslint/ban-types
13+
return function (object: Object, propertyName: string) {
14+
// Attach enum metadata to the property
15+
Reflect.defineMetadata('custom:enum', enumType, object, propertyName);
16+
17+
// Register the validation logic using class-validator
18+
registerDecorator({
19+
name: 'isEnumWithMetadata',
20+
target: object.constructor,
21+
propertyName: propertyName,
22+
options: validationOptions,
23+
validator: {
24+
validate(value: any, args: ValidationArguments) {
25+
// Retrieve enum type from metadata
26+
const enumType = Reflect.getMetadata(
27+
'custom:enum',
28+
args.object,
29+
args.property,
30+
);
31+
if (!enumType) {
32+
return false; // If no enum metadata is found, validation fails
33+
}
34+
35+
// Validate value is part of the enum
36+
const enumValues = Object.values(enumType);
37+
return enumValues.includes(value);
38+
},
39+
defaultMessage(args: ValidationArguments) {
40+
// Default message if validation fails
41+
const enumType = Reflect.getMetadata(
42+
'custom:enum',
43+
args.object,
44+
args.property,
45+
);
46+
const enumValues = Object.values(enumType).join(', ');
47+
return `${args.property} must be a valid enum value. Valid values: [${enumValues}]`;
48+
},
49+
},
50+
});
51+
};
52+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './enums';
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export enum ExternalApiName {
2-
REPUTATION_ORACLE = 'REPUTATION_ORACLE',
3-
HCAPTCHA_LABELING_STATS = 'HCAPTCHA_LABELING_STATS',
4-
HCAPTCHA_LABELING_VERIFY = 'HCAPTCHA_LABELING_VERIFY',
2+
REPUTATION_ORACLE = 'reputation_oracle',
3+
HCAPTCHA_LABELING_STATS = 'hcaptcha_labeling_stats',
4+
HCAPTCHA_LABELING_VERIFY = 'hcaptcha_labeling_verify',
55
}

packages/apps/human-app/server/src/common/enums/global-common.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,34 @@ export enum JobDiscoveryFieldName {
55
CreatedAt = 'created_at',
66
Qualifications = 'qualifications',
77
}
8+
89
export enum JobStatus {
9-
ACTIVE = 'ACTIVE',
10-
COMPLETED = 'COMPLETED',
11-
CANCELED = 'CANCELED',
10+
ACTIVE = 'active',
11+
COMPLETED = 'completed',
12+
CANCELED = 'canceled',
1213
}
14+
1315
export enum AssignmentStatus {
14-
ACTIVE = 'ACTIVE',
15-
VALIDATION = 'VALIDATION',
16-
COMPLETED = 'COMPLETED',
17-
EXPIRED = 'EXPIRED',
18-
CANCELED = 'CANCELED',
19-
REJECTED = 'REJECTED',
16+
ACTIVE = 'active',
17+
VALIDATION = 'validation',
18+
COMPLETED = 'completed',
19+
EXPIRED = 'expired',
20+
CANCELED = 'canceled',
21+
REJECTED = 'rejected',
2022
}
23+
2124
export enum JobDiscoverySortField {
2225
CHAIN_ID = 'chain_id',
2326
JOB_TYPE = 'job_type',
2427
REWARD_AMOUNT = 'reward_amount',
2528
CREATED_AT = 'created_at',
2629
}
30+
2731
export enum SortOrder {
28-
ASC = 'ASC',
29-
DESC = 'DESC',
32+
ASC = 'asc',
33+
DESC = 'desc',
3034
}
35+
3136
export enum AssignmentSortField {
3237
CHAIN_ID = 'chain_id',
3338
JOB_TYPE = 'job_type',
@@ -36,10 +41,11 @@ export enum AssignmentSortField {
3641
CREATED_AT = 'created_at',
3742
EXPIRES_AT = 'expires_at',
3843
}
44+
3945
export enum SignatureType {
40-
SIGNUP = 'SIGNUP',
41-
SIGNIN = 'SIGNIN',
42-
DISABLE_OPERATOR = 'DISABLE_OPERATOR',
43-
CERTIFICATE_AUTHENTICATION = 'CERTIFICATE_AUTHENTICATION',
44-
REGISTER_ADDRESS = 'REGISTER_ADDRESS',
46+
SIGNUP = 'signup',
47+
SIGNIN = 'signin',
48+
DISABLE_OPERATOR = 'disable_operator',
49+
CERTIFICATE_AUTHENTICATION = 'certificate_authentication',
50+
REGISTER_ADDRESS = 'register_address',
4551
}
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
export enum ReputationOracleEndpoints {
2-
WORKER_SIGNUP = 'WORKER_SIGNUP',
3-
WORKER_SIGNIN = 'WORKER_SIGNIN',
4-
WORKER_REGISTRATION = 'WORKER_REGISTRATION',
5-
OPERATOR_SIGNUP = 'OPERATOR_SIGNUP',
6-
OPERATOR_SIGNIN = 'OPERATOR_SIGNIN',
7-
EMAIL_VERIFICATION = 'EMAIL_VERIFICATION',
8-
RESEND_EMAIL_VERIFICATION = 'RESEND_EMAIL_VERIFICATION',
9-
FORGOT_PASSWORD = 'FORGOT_PASSWORD',
10-
RESTORE_PASSWORD = 'RESTORE_PASSWORD',
11-
PREPARE_SIGNATURE = 'PREPARE_SIGNATURE',
12-
DISABLE_OPERATOR = 'DISABLE_OPERATOR',
13-
KYC_PROCEDURE_START = 'KYC_PROCEDURE_START',
14-
ENABLE_LABELING = 'ENABLE_LABELING',
15-
REGISTER_ADDRESS = 'REGISTER_ADDRESS',
16-
TOKEN_REFRESH = 'TOKEN_REFRESH',
17-
KYC_ON_CHAIN = 'KYC_ON_CHAIN',
2+
WORKER_SIGNUP = 'worker_signup',
3+
WORKER_SIGNIN = 'worker_signin',
4+
WORKER_REGISTRATION = 'worker_registration',
5+
OPERATOR_SIGNUP = 'operator_signup',
6+
OPERATOR_SIGNIN = 'operator_signin',
7+
EMAIL_VERIFICATION = 'email_verification',
8+
RESEND_EMAIL_VERIFICATION = 'resend_email_verification',
9+
FORGOT_PASSWORD = 'forgot_password',
10+
RESTORE_PASSWORD = 'restore_password',
11+
PREPARE_SIGNATURE = 'prepare_signature',
12+
DISABLE_OPERATOR = 'disable_operator',
13+
KYC_PROCEDURE_START = 'kyc_procedure_start',
14+
ENABLE_LABELING = 'enable_labeling',
15+
REGISTER_ADDRESS = 'register_address',
16+
TOKEN_REFRESH = 'token_refresh',
17+
KYC_ON_CHAIN = 'kyc_on_chain',
1818
}
1919
export enum HCaptchaLabelingStatsEndpoints {
20-
USER_STATS = 'USER_STATS',
21-
DAILY_HMT_SPENT = 'DAILY_HMT_SPENT',
20+
USER_STATS = 'user_stats',
21+
DAILY_HMT_SPENT = 'daily_hmt_spent',
2222
}
2323
export enum HCaptchaLabelingVerifyEndpoints {
24-
TOKEN_VERIFY = 'TOKEN_VERIFY',
24+
TOKEN_VERIFY = 'token_verify',
2525
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export enum UserType {
2-
OPERATOR = 'OPERATOR',
3-
WORKER = 'WORKER',
2+
OPERATOR = 'operator',
3+
WORKER = 'worker',
44
}

packages/apps/human-app/server/src/common/interceptors/interceptor.module.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Module, Global } from '@nestjs/common';
22
import { AxiosRequestInterceptor } from './axios-request.interceptor';
33
import { EnvironmentConfigService } from '../config/environment-config.service';
4+
import { TransformEnumInterceptor } from './transform-enum.interceptor';
5+
import { APP_INTERCEPTOR } from '@nestjs/core';
46

57
@Global()
68
@Module({
@@ -15,6 +17,10 @@ import { EnvironmentConfigService } from '../config/environment-config.service';
1517
},
1618
inject: [EnvironmentConfigService],
1719
},
20+
{
21+
provide: APP_INTERCEPTOR,
22+
useClass: TransformEnumInterceptor,
23+
},
1824
],
1925
exports: [AxiosRequestInterceptor],
2026
})

0 commit comments

Comments
 (0)