-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassGroupStudentDto.ts
109 lines (90 loc) · 3.51 KB
/
ClassGroupStudentDto.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
import { IsArray, IsDate, IsInstance, IsNumber, IsString, validateSync, ValidationError } from 'class-validator';
import ClassGroupGuardianDto from './ClassGroupStudent/ClassGroupGuardianDto';
import ClassGroupDto from './ClassGroupStudent/ClassGroupDto';
export interface ClassGroupStudentFields {
TanuloId: number;
TanuloNev: string;
TanuloSzuletesiDatum: Date;
TanuloAnyjaNeve: string;
OsztalyCsoportok: Array<ClassGroupDto>;
Gondviselok: Array<ClassGroupGuardianDto>;
}
export default class ClassGroupStudentDto implements Partial<ClassGroupStudentFields> {
@IsNumber()
private readonly studentId?: number;
@IsString()
private readonly studentName?: string;
@IsDate()
private readonly studentBirthDate?: Date;
@IsString()
private readonly studentMotherName?: string;
@IsArray()
@IsInstance(ClassGroupDto, { each: true })
private readonly studentClassGroups?: Array<ClassGroupDto>;
@IsArray()
@IsInstance(ClassGroupGuardianDto, { each: true })
private readonly studentGuardians?: Array<ClassGroupGuardianDto>;
constructor(input: any) {
if (typeof input === 'object' && input !== null) {
this.studentId = typeof input['TanuloId'] === 'number' ? input['TanuloId'] : undefined;
this.studentName = typeof input['TanuloNev'] === 'string' ? input['TanuloNev'].trim() : undefined;
this.studentBirthDate = input['TanuloSzuletesiDatum'] ? new Date(input['TanuloSzuletesiDatum']) : undefined;
this.studentMotherName = typeof input['TanuloAnyjaNeve'] === 'string' ? input['TanuloAnyjaNeve'].trim() : undefined;
this.studentClassGroups = Array.isArray(input['OsztalyCsoportok']) ?
input['OsztalyCsoportok'].map((item: any) => new ClassGroupDto(item)) : [];
this.studentGuardians = Array.isArray(input['Gondviselok']) ?
input['Gondviselok'].map((item: any) => new ClassGroupGuardianDto(item)) : [];
}
const errors = validateSync(this, { skipMissingProperties: true });
if (errors.length > 0) {
throw this.validationErrorResponse(errors);
}
}
public get TanuloId(): number | undefined {
return this.studentId;
}
public get TanuloNev(): string | undefined {
return this.studentName;
}
public get TanuloSzuletesiDatum(): Date | undefined {
return this.studentBirthDate;
}
public get TanuloAnyjaNeve(): string | undefined {
return this.studentMotherName;
}
public get OsztalyCsoportok(): Array<ClassGroupDto> | undefined {
return this.studentClassGroups;
}
public get Gondviselok(): Array<ClassGroupGuardianDto> | undefined {
return this.studentGuardians;
}
public get json(): ClassGroupStudentFields {
return {
Gondviselok: this.studentGuardians?.map((item) => item.json),
OsztalyCsoportok: this.studentClassGroups?.map((item) => item.json),
TanuloAnyjaNeve: this.studentMotherName,
TanuloId: this.studentId,
TanuloNev: this.studentName,
TanuloSzuletesiDatum: this.studentBirthDate,
} as ClassGroupStudentFields;
}
private validationErrorResponse(errors: Array<ValidationError>): object {
const validFields: Partial<ClassGroupStudentFields> = {
TanuloId: this.studentId,
TanuloNev: this.studentName,
TanuloSzuletesiDatum: this.studentBirthDate,
TanuloAnyjaNeve: this.studentMotherName,
OsztalyCsoportok: this.studentClassGroups,
Gondviselok: this.studentGuardians,
};
const errorMessages: Array<string> = [];
for (const error of errors) {
validFields[error.property as keyof ClassGroupStudentFields] = undefined;
errorMessages.push(...Object.values(error.constraints || {}));
}
return {
valid: validFields,
errors: errorMessages,
};
}
}