-
Notifications
You must be signed in to change notification settings - Fork 1
/
JustificationDto.ts
114 lines (93 loc) · 2.8 KB
/
JustificationDto.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
108
109
110
111
112
113
114
import { IsDate, IsNumber, IsString, validateSync, ValidationError } from 'class-validator';
export interface JustificationFields {
Id: number;
Kezdete: Date;
Vege: Date;
Tipusa: string;
Megjegyzes: string;
Rogzito: string;
Torolheto: boolean;
}
export default class JustificationDto implements Partial<JustificationFields> {
@IsNumber()
private readonly id?: number;
@IsDate()
private readonly start?: Date;
@IsDate()
private readonly end?: Date;
@IsString()
private readonly type?: string;
@IsString()
private readonly comment?: string;
@IsString()
private readonly recorder?: string;
@IsString()
private readonly deletable?: boolean;
constructor(input: any) {
if (typeof input === 'object' && input !== null) {
this.id = typeof input['Id'] === 'number' ? input['Id'] : undefined;
this.start = typeof input['Kezdete'] === 'string' ? new Date(input['Kezdete']) : input['Kezdete'];
this.end = typeof input['Vege'] === 'string' ? new Date(input['Vege']) : input['Vege'];
this.type = typeof input['Tipusa'] === 'string' ? input['Tipusa'.trim()] : undefined;
this.comment = typeof input['Megjegyzes'] === 'string' ? input['Megjegyzes'].trim() : undefined;
this.recorder = typeof input['Rogzito'] === 'string' ? input['Rogzito'].trim() : undefined;
this.deletable = typeof input['Torolheto'] === 'boolean' ? input['Torolheto'] : undefined;
}
const errors = validateSync(this, { skipMissingProperties: true });
if (errors.length > 0) {
throw this.validationErrorResponse(errors);
}
}
public get Id(): number | undefined {
return this.id;
}
public get Kezdete(): Date | undefined {
return this.start;
}
public get Vege(): Date | undefined {
return this.end;
}
public get Tipusa(): string | undefined {
return this.type;
}
public get Megjegyzes(): string | undefined {
return this.comment;
}
public get Rogzito(): string | undefined {
return this.recorder;
}
public get Torolheto(): boolean | undefined {
return this.deletable;
}
public get json(): JustificationFields {
return {
Id: this.id,
Kezdete: this.start,
Megjegyzes: this.comment,
Rogzito: this.recorder,
Tipusa: this.type,
Torolheto: this.deletable,
Vege: this.end,
} as JustificationFields;
}
private validationErrorResponse(errors: Array<ValidationError>): object {
const validFields: Partial<JustificationFields> = {
Id: this.id,
Kezdete: this.start,
Vege: this.end,
Tipusa: this.type,
Megjegyzes: this.comment,
Rogzito: this.recorder,
Torolheto: this.deletable,
};
const errorMessages: Array<string> = [];
for (const error of errors) {
validFields[error.property as keyof JustificationFields] = undefined;
errorMessages.push(...Object.values(error.constraints || {}));
}
return {
valid: validFields,
errors: errorMessages,
};
}
}