-
Notifications
You must be signed in to change notification settings - Fork 4
/
test-records.service.ts
241 lines (203 loc) · 7.7 KB
/
test-records.service.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { Injectable } from '@angular/core';
import {
CompleteTestResults, DefaultService as CreateTestResultsService, GetTestResultsService, UpdateTestResultsService,
} from '@api/test-results';
import { TEST_TYPES, TEST_TYPES_GROUP1_SPEC_TEST, TEST_TYPES_GROUP5_SPEC_TEST } from '@forms/models/testTypeId.enum';
import { FormNode } from '@forms/services/dynamic-form.types';
import { contingencyTestTemplates } from '@forms/templates/test-records/create-master.template';
import { masterTpl } from '@forms/templates/test-records/master.template';
import { TestResultStatus } from '@models/test-results/test-result-status.enum';
import { TestResultModel } from '@models/test-results/test-result.model';
import { VehicleTypes } from '@models/vehicle-tech-record.model';
import { Store, select } from '@ngrx/store';
import {
TestResultsState,
cancelEditingTestResult,
cleanTestResult,
contingencyTestTypeSelected,
createTestResult,
editingTestResult,
fetchTestResults,
fetchTestResultsBySystemNumber,
isTestTypeKeySame,
sectionTemplates,
selectAllTestResults,
selectAmendedDefectData,
selectDefectData,
selectedAmendedTestResultState,
selectedTestResultState,
testResultInEdit,
testTypeIdChanged,
toEditOrNotToEdit,
updateEditingTestResult,
updateTestResult,
updateTestResultFailed,
} from '@store/test-records';
import cloneDeep from 'lodash.clonedeep';
import { Observable, take, throwError } from 'rxjs';
@Injectable({
providedIn: 'root',
})
export class TestRecordsService {
constructor(
private store: Store<TestResultsState>,
private updateTestResultsService: UpdateTestResultsService,
private getTestResultService: GetTestResultsService,
private createTestResultsService: CreateTestResultsService,
) {}
fetchTestResultbySystemNumber(
systemNumber: string,
queryparams: {
status?: string;
fromDateTime?: Date;
toDateTime?: Date;
testResultId?: string;
version?: string;
} = {},
): Observable<Array<TestResultModel>> {
if (!systemNumber) {
return throwError(() => new Error('systemNumber is required'));
}
const {
status, fromDateTime, toDateTime, testResultId, version,
} = queryparams;
return this.getTestResultService.testResultsSystemNumberGet(systemNumber, status, fromDateTime, toDateTime, testResultId, version) as Observable<
Array<TestResultModel>
>;
}
loadTestResults(): void {
this.store.dispatch(fetchTestResults());
}
loadTestResultBySystemNumber(systemNumber: string): void {
this.store.dispatch(fetchTestResultsBySystemNumber({ systemNumber }));
}
get testResult$() {
return this.store.pipe(select(selectedTestResultState));
}
get editingTestResult$() {
return this.store.pipe(select(testResultInEdit));
}
get testRecords$() {
return this.store.pipe(select(selectAllTestResults));
}
get defectData$() {
return this.store.pipe(select(selectDefectData));
}
get amendedTestResult$() {
return this.store.pipe(select(selectedAmendedTestResultState));
}
get amendedDefectData$() {
return this.store.pipe(select(selectAmendedDefectData));
}
get sectionTemplates$() {
return this.store.pipe(select(sectionTemplates));
}
saveTestResult(
systemNumber: string,
user: { name: string; id?: string; userEmail?: string },
body: TestResultModel,
observe?: 'body',
reportProgress?: boolean,
): Observable<TestResultModel> {
const { name, id, userEmail } = user;
const tr = cloneDeep(body);
delete tr.testHistory;
return this.updateTestResultsService.testResultsSystemNumberPut(
{ msUserDetails: { msOid: id, msUser: name, msEmailAddress: userEmail }, testResult: tr } as CompleteTestResults,
systemNumber,
observe,
reportProgress,
) as Observable<TestResultModel>;
}
updateTestResult(value: TestResultModel): void {
this.store.dispatch(updateTestResult({ value }));
}
postTestResult(body: TestResultModel) {
return this.createTestResultsService.testResultsPost(body as CompleteTestResults, 'response', false);
}
createTestResult(value: TestResultModel): void {
this.store.dispatch(createTestResult({ value }));
}
cleanTestResult() {
return this.store.dispatch(cleanTestResult());
}
prepareTestResultForAmendment(testResults: TestResultModel[], testResult: TestResultModel): TestResultModel {
const lastIvaOrMsvaTest = testResults.find((test) => {
const testType = test?.testTypes[0];
const testTypeId = testType?.testTypeId ?? '';
const isIVAorMSVATest = TEST_TYPES_GROUP1_SPEC_TEST.includes(testTypeId) || TEST_TYPES_GROUP5_SPEC_TEST.includes(testTypeId);
return isIVAorMSVATest;
});
if (!lastIvaOrMsvaTest) {
return testResult;
}
// If certificateNumber is falsy, then use the last IVA or MSVA test certificate number
testResult.testTypes[0].certificateNumber ??= lastIvaOrMsvaTest.testTypes[0].certificateNumber;
return testResult;
}
static getTestTypeGroup(testTypeId: string): string | undefined {
// eslint-disable-next-line no-restricted-syntax
for (const groupName in TEST_TYPES) {
if (TEST_TYPES[groupName as keyof typeof TEST_TYPES].includes(testTypeId)) {
return groupName;
}
}
return undefined;
}
editingTestResult(testResult: TestResultModel): void {
this.store.dispatch(editingTestResult({ testTypeId: testResult.testTypes[0].testTypeId }));
}
cancelEditingTestResult(): void {
this.store.dispatch(cancelEditingTestResult());
}
updateEditingTestResult(testResult: TestResultModel): void {
this.store.dispatch(updateEditingTestResult({ testResult }));
}
get isSameTestTypeId$(): Observable<boolean> {
return this.store.pipe(select(isTestTypeKeySame('testTypeId')));
}
testTypeChange(testTypeId: string) {
this.store.dispatch(testTypeIdChanged({ testTypeId }));
}
get isTestTypeGroupEditable$() {
return this.store.pipe(select(toEditOrNotToEdit), this.canHandleTestType(masterTpl));
}
get canCreate$() {
return this.store.pipe(select(toEditOrNotToEdit), this.canHandleTestType(contingencyTestTemplates));
}
private canHandleTestType(templateMap: Record<VehicleTypes, Record<string, Record<string, FormNode>>>) {
return function handleTestType <T>(source: Observable<T>): Observable<boolean> {
const handle = (testResult: TestResultModel | undefined): boolean => {
if (!testResult) {
return false;
}
const { vehicleType } = testResult;
const testTypeId = testResult.testTypes && testResult.testTypes[0].testTypeId;
const testTypeGroup = TestRecordsService.getTestTypeGroup(testTypeId);
const vehicleTpl = vehicleType && templateMap[`${vehicleType}`];
return !!testTypeGroup && !!vehicleTpl && Object.prototype.hasOwnProperty.call(vehicleTpl, testTypeGroup);
};
return new Observable((subscriber) => {
source.subscribe({
next: (val) => {
subscriber.next(handle(val as unknown as TestResultModel));
},
error: (e) => subscriber.error(e),
complete: () => subscriber.complete(),
});
});
};
}
contingencyTestTypeSelected(testType: string) {
this.store.dispatch(contingencyTestTypeSelected({ testType }));
}
cancelTest(reason: string): void {
this.store.pipe(select(testResultInEdit), take(1)).subscribe((testResult) => {
if (!testResult) {
return this.store.dispatch(updateTestResultFailed({ errors: [{ error: 'No selected test result.' }] }));
}
const cancelledTest = { ...testResult, testStatus: TestResultStatus.CANCELLED, reasonForCancellation: reason };
this.store.dispatch(updateTestResult({ value: cancelledTest }));
});
}
}