-
Notifications
You must be signed in to change notification settings - Fork 1
/
SchoolYearCalendarEntryDto.ts
110 lines (90 loc) · 3.32 KB
/
SchoolYearCalendarEntryDto.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
import { IsDate, IsInstance, IsOptional, IsString, validateSync, ValidationError } from 'class-validator';
import ValueDescriptor from './ValueDescriptor';
import UidStructure from './UidStructure';
export interface SchoolYearCalendarEntryFields {
Datum: Date;
Naptipus: ValueDescriptor;
OsztalyCsoport?: UidStructure;
ElteroOrarendSzerintiTanitasiNap?: ValueDescriptor;
Uid: string;
OrarendiNapHetirendje: ValueDescriptor;
}
export default class SchoolYearCalendarEntryDto implements Partial<SchoolYearCalendarEntryFields> {
@IsDate()
private readonly date?: Date;
@IsInstance(ValueDescriptor)
private readonly dayType?: ValueDescriptor;
@IsOptional()
@IsInstance(UidStructure)
private readonly group?: UidStructure;
@IsOptional()
@IsInstance(ValueDescriptor)
private readonly irregularDay?: ValueDescriptor;
@IsString()
private readonly uid?: string;
@IsInstance(ValueDescriptor)
private readonly weekTypeSchedule?: ValueDescriptor;
constructor(input: any) {
if (typeof input === 'object' && input !== null) {
this.date = typeof input['Datum'] === 'string' ? new Date(input['Datum']) : undefined;
this.dayType = typeof input['Naptipus'] === 'object' ? new ValueDescriptor(input['Naptipus']) : undefined;
this.group = typeof input['OsztalyCsoport'] === 'object' ? new UidStructure(input['OsztalyCsoport']) : undefined;
this.irregularDay = typeof input['ElteroOrarendSzerintiTanitasiNap'] === 'object' ?
new ValueDescriptor(input['ElteroOrarendSzerintiTanitasiNap']) : undefined;
this.uid = typeof input['Uid'] === 'string' ? input['Uid'].trim() : undefined;
this.weekTypeSchedule = typeof input['OrarendiNapHetirendje'] === 'object' ? new ValueDescriptor(input['OrarendiNapHetirendje']) :
undefined;
}
const errors = validateSync(this, { skipMissingProperties: true });
if (errors.length > 0) {
throw this.validationErrorResponse(errors);
}
}
public get Datum(): Date | undefined {
return this.date;
}
public get Naptipus(): ValueDescriptor | undefined {
return this.dayType;
}
public get OsztalyCsoport(): UidStructure | undefined {
return this.group;
}
public get ElteroOrarendSzerintiTanitasiNap(): ValueDescriptor | undefined {
return this.irregularDay;
}
public get Uid(): string | undefined {
return this.uid;
}
public get OrarendiNapHetirendje(): ValueDescriptor | undefined {
return this.weekTypeSchedule;
}
public get json(): SchoolYearCalendarEntryFields {
return {
Datum: this.date,
ElteroOrarendSzerintiTanitasiNap: this.irregularDay?.json,
Naptipus: this.dayType?.json,
OrarendiNapHetirendje: this.weekTypeSchedule?.json,
OsztalyCsoport: this.group?.json,
Uid: this.uid,
} as SchoolYearCalendarEntryFields;
}
private validationErrorResponse(errors: Array<ValidationError>): object {
const validFields: Partial<SchoolYearCalendarEntryFields> = {
Datum: this.date,
Naptipus: this.dayType,
OsztalyCsoport: this.group,
ElteroOrarendSzerintiTanitasiNap: this.irregularDay,
Uid: this.uid,
OrarendiNapHetirendje: this.weekTypeSchedule,
};
const errorMessages: Array<string> = [];
for (const error of errors) {
validFields[error.property as keyof SchoolYearCalendarEntryFields] = undefined;
errorMessages.push(...Object.values(error.constraints || {}));
}
return {
valid: validFields,
errors: errorMessages,
};
}
}