/
StudentPresenceDto.ts
94 lines (77 loc) · 2.61 KB
/
StudentPresenceDto.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
import { IsBoolean, IsNumber, IsOptional, IsString, validateSync, ValidationError } from 'class-validator';
export interface StudentPresenceFields {
Keses?: number;
Szoveg?: string;
HasMulasztasGomb: boolean;
MulasztasTipus: number;
TanuloId: number;
}
export default class StudentPresenceDto implements Partial<StudentPresenceFields> {
@IsOptional()
@IsNumber()
private readonly delayTimeInMinutes?: number;
@IsOptional()
@IsString()
private readonly exemptionText?: string;
@IsBoolean()
private readonly omissionAllowed?: boolean;
@IsNumber()
private readonly presenceType?: number;
@IsNumber()
private readonly studentId?: number;
constructor(input: any) {
if (typeof input === 'object' && input !== null) {
this.delayTimeInMinutes = typeof input['Keses'] === 'number' ? input['Keses'] : undefined;
this.exemptionText = typeof input['Szoveg'] === 'string' ? input['Szoveg'].trim() : undefined;
this.omissionAllowed = typeof input['HasMulasztasGomb'] === 'boolean' ? input['HasMulasztasGomb'] : undefined;
this.presenceType = typeof input['MulasztasTipus'] === 'number' ? input['MulasztasTipus'] : undefined;
this.studentId = typeof input['TanuloId'] === 'number' ? input['TanuloId'] : undefined;
}
const errors = validateSync(this, { skipMissingProperties: true });
if (errors.length > 0) {
throw this.validationErrorResponse(errors);
}
}
public get Keses(): number | undefined {
return this.delayTimeInMinutes;
}
public get Szoveg(): string | undefined {
return this.exemptionText;
}
public get HasMulasztasGomb(): boolean | undefined {
return this.omissionAllowed;
}
public get MulasztasTipus(): number | undefined {
return this.presenceType;
}
public get TanuloId(): number | undefined {
return this.studentId;
}
public get json(): StudentPresenceFields {
return {
HasMulasztasGomb: this.omissionAllowed,
Keses: this.delayTimeInMinutes,
MulasztasTipus: this.presenceType,
Szoveg: this.exemptionText,
TanuloId: this.studentId,
} as StudentPresenceFields;
}
private validationErrorResponse(errors: Array<ValidationError>): object {
const validFields: Partial<StudentPresenceFields> = {
Keses: this.delayTimeInMinutes,
Szoveg: this.exemptionText,
HasMulasztasGomb: this.omissionAllowed,
MulasztasTipus: this.presenceType,
TanuloId: this.studentId,
};
const errorMessages: Array<string> = [];
for (const error of errors) {
validFields[error.property as keyof StudentPresenceFields] = undefined;
errorMessages.push(...Object.values(error.constraints || {}));
}
return {
valid: validFields,
errors: errorMessages,
};
}
}