-
Notifications
You must be signed in to change notification settings - Fork 1
/
StudentDataForLoggingRequestDto.ts
73 lines (60 loc) · 2.12 KB
/
StudentDataForLoggingRequestDto.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
import { IsArray, IsInstance, IsNumber, IsOptional, validateSync, ValidationError } from 'class-validator';
import PresenceDto from './PresenceDto';
export interface StudentDataForLoggingRequestFields {
FeljegyzesTipusLista?: Array<number>;
Mulasztas: PresenceDto;
Id: number;
}
export default class StudentDataForLoggingRequestDto implements Partial<StudentDataForLoggingRequestFields> {
@IsOptional()
@IsArray()
@IsNumber({}, { each: true })
private readonly noteList?: Array<number>;
@IsInstance(PresenceDto)
private readonly presence?: PresenceDto;
@IsNumber()
private readonly studentId?: number;
constructor(input: any) {
if (typeof input === 'object' && input !== null) {
this.noteList = Array.isArray(input['FeljegyzesTipusLista']) ? input['FeljegyzesTipusLista'].map((e: any) => e) : undefined;
this.presence = typeof input['Mulasztas'] === 'object' ? new PresenceDto(input['Mulasztas']) : undefined;
this.studentId = typeof input['Id'] === 'number' ? input['Id'] : undefined;
}
const errors = validateSync(this, { skipMissingProperties: true });
if (errors.length > 0) {
throw this.validationErrorResponse(errors);
}
}
public get FeljegyzesTipusLista(): Array<number> | undefined {
return this.noteList;
}
public get Mulasztas(): PresenceDto | undefined {
return this.presence;
}
public get Id(): number | undefined {
return this.studentId;
}
public get json(): StudentDataForLoggingRequestFields {
return {
FeljegyzesTipusLista: this.noteList,
Id: this.studentId,
Mulasztas: this.presence?.json,
} as StudentDataForLoggingRequestFields;
}
private validationErrorResponse(errors: Array<ValidationError>): object {
const validFields: Partial<StudentDataForLoggingRequestFields> = {
FeljegyzesTipusLista: this.noteList,
Mulasztas: this.presence,
Id: this.studentId,
};
const errorMessages: Array<string> = [];
for (const error of errors) {
validFields[error.property as keyof StudentDataForLoggingRequestFields] = undefined;
errorMessages.push(...Object.values(error.constraints || {}));
}
return {
valid: validFields,
errors: errorMessages,
};
}
}