-
Notifications
You must be signed in to change notification settings - Fork 1
/
JustificationPostDto.ts
103 lines (84 loc) · 2.86 KB
/
JustificationPostDto.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
import { IsDate, IsNumber, IsString, validateSync, ValidationError } from 'class-validator';
export interface JustificationPostFields {
TanuloId: number;
OsztalyCsoportId: number;
IgazolasKezdete: Date;
IgazolasVege: Date;
IgazolasTipus: string;
IgazolasMegjegyzes: string;
}
export default class JustificationPostDto implements Partial<JustificationPostFields> {
@IsNumber()
private readonly studentId?: number;
@IsNumber()
private readonly classGroupId?: number;
@IsDate()
private readonly start?: Date;
@IsDate()
private readonly end?: Date;
@IsString()
private readonly type?: string;
@IsString()
private readonly comment?: string;
constructor(input: any) {
if (typeof input === 'object' && input !== null) {
this.studentId = typeof input['TanuloId'] === 'number' ? input['TanuloId'] : undefined;
this.classGroupId = typeof input['OsztalyCsoportId'] === 'number' ? input['OsztalyCsoportId'] : undefined;
this.start = typeof input['IgazolasKezdete'] === 'string' ? new Date(input['IgazolasKezdete']) : input['IgazolasKezdete'];
this.end = typeof input['IgazolasVege'] === 'string' ? new Date(input['IgazolasVege']) : input['IgazolasVege'];
this.type = typeof input['IgazolasTipus'] === 'string' ? input['IgazolasTipus'].trim() : undefined;
this.comment = typeof input['IgazolasMegjegyzes'] === 'string' ? input['IgazolasMegjegyzes'].trim() : undefined;
}
const errors = validateSync(this, { skipMissingProperties: true });
if (errors.length > 0) {
throw this.validationErrorResponse(errors);
}
}
public get TanuloId(): number | undefined {
return this.studentId;
}
public get OsztalyCsoportId(): number | undefined {
return this.classGroupId;
}
public get IgazolasKezdete(): Date | undefined {
return this.start;
}
public get IgazolasVege(): Date | undefined {
return this.end;
}
public get IgazolasTipus(): string | undefined {
return this.type;
}
public get IgazolasMegjegyzes(): string | undefined {
return this.comment;
}
public get json(): JustificationPostFields {
return {
IgazolasKezdete: this.start,
IgazolasMegjegyzes: this.comment,
IgazolasTipus: this.type,
IgazolasVege: this.end,
OsztalyCsoportId: this.classGroupId,
TanuloId: this.studentId,
} as JustificationPostFields;
}
private validationErrorResponse(errors: Array<ValidationError>): object {
const validFields: Partial<JustificationPostFields> = {
TanuloId: this.studentId,
OsztalyCsoportId: this.classGroupId,
IgazolasKezdete: this.start,
IgazolasVege: this.end,
IgazolasTipus: this.type,
IgazolasMegjegyzes: this.comment,
};
const errorMessages: Array<string> = [];
for (const error of errors) {
validFields[error.property as keyof JustificationPostFields] = undefined;
errorMessages.push(...Object.values(error.constraints || {}));
}
return {
valid: validFields,
errors: errorMessages,
};
}
}