-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConsultingHourDto.ts
120 lines (98 loc) · 3.69 KB
/
ConsultingHourDto.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
115
116
117
118
119
120
import { IsArray, IsBoolean, IsDate, IsInstance, IsString, validateSync, ValidationError } from 'class-validator';
import UidNameStructure from './UidNameStructure';
import ConsultingHourTimeSlotDto from './ConsultingHourTimeSlotDto';
export interface ConsultingHourFields {
Terem: UidNameStructure;
Idopontok: Array<ConsultingHourTimeSlotDto>;
JelentkezesHatarido: Date;
VegIdopont: Date;
IsJelentkezesFeatureEnabled: boolean;
KezdoIdopont: Date;
Uid: string;
}
export default class ConsultingHourDto implements Partial<ConsultingHourFields> {
@IsInstance(UidNameStructure)
private readonly classroomDescriptor?: UidNameStructure;
@IsArray()
@IsInstance(ConsultingHourTimeSlotDto, { each: true })
private readonly consultingHourTimeSlots?: Array<ConsultingHourTimeSlotDto>;
@IsDate()
private readonly deadline?: Date;
@IsDate()
private readonly endTime?: Date;
@IsBoolean()
private readonly isReservationEnabled?: boolean;
@IsDate()
private readonly startTime?: Date;
@IsString()
private readonly uid?: string;
constructor(input: any) {
if (typeof input === 'object' && input !== null) {
this.classroomDescriptor = typeof input['Terem'] === 'object' ? new UidNameStructure(input['Terem']) : undefined;
this.consultingHourTimeSlots = Array.isArray(input['Idopontok']) ?
input['Idopontok'].map((item: any) => new ConsultingHourTimeSlotDto(item)) : [];
this.deadline = typeof input['JelentkezesHatarido'] === 'string' ? new Date(input['JelentkezesHatarido']) : undefined;
this.endTime = typeof input['VegIdopont'] === 'string' ? new Date(input['VegIdopont']) : undefined;
this.isReservationEnabled = typeof input['IsJelentkezesFeatureEnabled'] === 'boolean' ? input['IsJelentkezesFeatureEnabled'] :
undefined;
this.startTime = typeof input['KezdoIdopont'] === 'string' ? new Date(input['KezdoIdopont']) : undefined;
this.uid = typeof input['Uid'] === 'string' ? input['Uid'].trim() : undefined;
}
const errors = validateSync(this, { skipMissingProperties: true });
if (errors.length > 0) {
throw this.validationErrorResponse(errors);
}
}
public get Terem(): UidNameStructure | undefined {
return this.classroomDescriptor;
}
public get Idopontok(): Array<ConsultingHourTimeSlotDto> | undefined {
return this.consultingHourTimeSlots;
}
public get JelentkezesHatarido(): Date | undefined {
return this.deadline;
}
public get VegIdopont(): Date | undefined {
return this.endTime;
}
public get IsJelentkezesFeatureEnabled(): boolean | undefined {
return this.isReservationEnabled;
}
public get KezdoIdopont(): Date | undefined {
return this.startTime;
}
public get Uid(): string | undefined {
return this.uid;
}
public get json(): ConsultingHourFields {
return {
Idopontok: this.consultingHourTimeSlots?.map((item) => item.json),
IsJelentkezesFeatureEnabled: this.isReservationEnabled,
JelentkezesHatarido: this.deadline,
KezdoIdopont: this.startTime,
Terem: this.classroomDescriptor?.json,
Uid: this.uid,
VegIdopont: this.endTime,
} as ConsultingHourFields;
}
private validationErrorResponse(errors: Array<ValidationError>): object {
const validFields: Partial<ConsultingHourFields> = {
Idopontok: this.consultingHourTimeSlots,
IsJelentkezesFeatureEnabled: this.isReservationEnabled,
JelentkezesHatarido: this.deadline,
KezdoIdopont: this.startTime,
Terem: this.classroomDescriptor,
Uid: this.uid,
VegIdopont: this.endTime,
};
const errorMessages: Array<string> = [];
for (const error of errors) {
validFields[error.property as keyof ConsultingHourFields] = undefined;
errorMessages.push(...Object.values(error.constraints || {}));
}
return {
valid: validFields,
errors: errorMessages,
};
}
}