/
GuardianDto.ts
107 lines (88 loc) · 2.91 KB
/
GuardianDto.ts
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
import { IsBoolean, IsEmail, IsOptional, IsPhoneNumber, IsString, validateSync, ValidationError } from 'class-validator';
export interface GuardianFields {
EmailCim?: string;
IsNincsFelugyeletiJoga?: boolean;
IsTorvenyesKepviselo?: boolean;
Nev: string;
Telefonszam?: string;
Uid: string;
}
export default class GuardianDto implements Partial<GuardianFields> {
@IsOptional()
@IsEmail()
private readonly email?: string;
@IsOptional()
@IsBoolean()
private readonly hasParentalRights?: boolean;
@IsOptional()
@IsBoolean()
private readonly isLegalRepresentative?: boolean;
@IsString()
private readonly name?: string;
@IsOptional()
@IsPhoneNumber()
private readonly phoneNumber?: string;
@IsString()
private readonly uid?: string;
constructor(input: any) {
if (typeof input === 'object' && input !== null) {
this.email = typeof input['EmailCim'] === 'string' ? input['EmailCim'].trim() : undefined;
this.hasParentalRights = typeof input['IsNincsFelugyeletiJoga'] === 'boolean' ? input['IsNincsFelugyeletiJoga'] : undefined;
this.isLegalRepresentative = typeof input['IsTorvenyesKepviselo'] === 'boolean' ? input['IsTorvenyesKepviselo'] : undefined;
this.name = typeof input['Nev'] === 'string' ? input['Nev'].trim() : undefined;
this.phoneNumber = typeof input['Telefonszam'] === 'string' ? input['Telefonszam'].trim() : undefined;
this.uid = typeof input['Uid'] === 'string' ? input['Uid'].trim() : undefined;
}
const errors = validateSync(this, { skipMissingProperties: true });
if (errors.length > 0) {
throw this.validationErrorResponse(errors);
}
}
public get EmailCim(): string | undefined {
return this.email;
}
public get IsNincsFelugyeletiJoga(): boolean | undefined {
return this.hasParentalRights;
}
public get IsTorvenyesKepviselo(): boolean | undefined {
return this.isLegalRepresentative;
}
public get Nev(): string | undefined {
return this.name;
}
public get Telefonszam(): string | undefined {
return this.phoneNumber;
}
public get Uid(): string | undefined {
return this.uid;
}
public get json(): GuardianFields {
return {
EmailCim: this.email,
IsNincsFelugyeletiJoga: this.hasParentalRights,
IsTorvenyesKepviselo: this.isLegalRepresentative,
Nev: this.name,
Telefonszam: this.phoneNumber,
Uid: this.uid,
} as GuardianFields;
}
private validationErrorResponse(errors: Array<ValidationError>): object {
const validFields: Partial<GuardianFields> = {
EmailCim: this.email,
IsNincsFelugyeletiJoga: this.hasParentalRights,
IsTorvenyesKepviselo: this.isLegalRepresentative,
Nev: this.name,
Telefonszam: this.phoneNumber,
Uid: this.uid,
};
const errorMessages: Array<string> = [];
for (const error of errors) {
validFields[error.property as keyof GuardianFields] = undefined;
errorMessages.push(...Object.values(error.constraints || {}));
}
return {
valid: validFields,
errors: errorMessages,
};
}
}