Skip to content

Commit

Permalink
feat(cb2-8776): move error logic to comonent from the html
Browse files Browse the repository at this point in the history
feat(cb2-8776): added access descriptor

feat(cb2-8776): changed date logic to be greater than -1

feat(cb2-8776): added ternary to convertToNumber logic and removed unused variable

feat(cb2-8776): prevent submission when time is not provided
  • Loading branch information
Daniel-Searle committed Jul 26, 2023
1 parent 055e3cc commit 8fe234d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 41 deletions.
19 changes: 1 addition & 18 deletions src/app/forms/components/date/date.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
<fieldset class="govuk-fieldset" role="group" attr.aria-describedby="{{ name }}-hint">
<legend class="govuk-fieldset__legend govuk-fieldset__legend--m">{{ label }}</legend>
<div *ngIf="hint" id="{{ name }}-hint" class="govuk-hint">{{ hint }}</div>
<app-field-error-message
*ngIf="dayEl.dirty && monthEl.dirty && yearEl.dirty && (yearEl.value ?? '').toString().length > 3 && !displayTime"
[error]="error"
[name]="name"
></app-field-error-message>
<app-field-error-message *ngIf="showError" [error]="error" [name]="name"></app-field-error-message>
<div class="govuk-date-input" id="{{ name }}">
<div class="govuk-date-input__item">
<div class="govuk-form-group">
Expand Down Expand Up @@ -86,19 +82,6 @@
</div>

<ng-container *ngIf="displayTime">
<app-field-error-message
*ngIf="
dayEl.dirty &&
monthEl.dirty &&
yearEl.dirty &&
displayTime &&
hourEl.dirty &&
minuteEl.dirty &&
(minuteEl.value ?? '').toString().length > 0
"
[error]="error"
[name]="name"
></app-field-error-message>
<div class="govuk-date-input__item">
<div class="govuk-form-group">
<label class="govuk-label govuk-date-input__label" for="{{ name }}-hour"> Hour </label>
Expand Down
18 changes: 15 additions & 3 deletions src/app/forms/components/date/date.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,20 +137,32 @@ export class DateComponent extends BaseControlComponent implements OnInit, OnDes
minute = this.displayTime ? minute : this.dateFieldOrDefault?.minutes;
const second = this.dateFieldOrDefault?.seconds;

this.onChange(this.processDate(year, month, day, hour, minute, second));
this.onChange(
this.processDate(
this.convertToNumber(year),
this.convertToNumber(month),
this.convertToNumber(day),
this.convertToNumber(hour),
this.convertToNumber(minute),
this.convertToNumber(second)
)
);
}
});
}

convertToNumber(input: string | number | undefined): number {
return typeof input === 'number' ? input : 0;
}
processDate(year: any, month: any, day: any, hour: any, minute: any, second: any) {
if (this.isoDate) {
if (year > 999 && month > 0 && day > 0 && hour > 0 && minute > 0) {
if (year > 999 && month > -1 && day > -1 && hour > -1 && minute > -1) {
this.showError = true;
}

return `${year || ''}-${this.padded(month)}-${this.padded(day)}T${this.padded(hour)}:${this.padded(minute)}:${this.padded(second)}.000`;
}
if (year > 999 && month > 0 && day > 0) {
if (year > 999 && month > -1 && day > -1) {
this.showError = true;
}
return `${year || ''}-${this.padded(month)}-${this.padded(day)}`;
Expand Down
4 changes: 2 additions & 2 deletions src/app/forms/validators/date/date.validators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ describe(DateValidators.validDate.name, () => {
'20-01-01'
],
[{ invalidDate: { error: true, reason: "'Date' must include time" } }, '2022-01-01T:00:00:000Z', true],
[{ invalidDate: { error: true, reason: "'Date' hours must be between 0 and 23" } }, '2022-01-01T24:00:00:000Z', true],
[{ invalidDate: { error: true, reason: "'Date' hours must be between 0 and 23" } }, '2022-01-01T24:1331:00:000Z', true],
[{ invalidDate: { error: true, reason: "'Date' must include time" } }, '2022-01-01T00::00:000Z', true],
[{ invalidDate: { error: true, reason: "'Date' minutes must be between 0 and 59" } }, '2022-01-01T00:60:00:000Z', true],
[{ invalidDate: { error: true, reason: "'Date' minutes must be between 0 and 59" } }, '2022-01-01T23:60:00:000Z', true],
[null, ''],
[null, '2022-01-01T00:00:00.000Z']
])('should validate date and return %s for %p', (expected, date: string, displayTime = false) => {
Expand Down
5 changes: 3 additions & 2 deletions src/app/forms/validators/date/date.validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ export class DateValidators {

private static validateTime(time: string, label: string | undefined) {
const [hours, minutes] = time.split(':');

if (!hours || !minutes) {
console.log(`hours ${hours}`);
console.log(`minutes ${minutes}`);
if (hours === '00' || minutes == '00') {
return { invalidDate: { error: true, reason: `'${label || 'Date'}' must include time` } };
}
if (23 < Number.parseInt(hours, 10)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import {
VehicleTechRecordModel
} from '@models/vehicle-tech-record.model';
import { Store } from '@ngrx/store';
import { fetchSearchResult } from '@store/tech-record-search/actions/tech-record-search.actions';
import { SearchResult } from '@store/tech-record-search/reducer/tech-record-search.reducer';
import { cloneDeep } from 'lodash';
import { Observable } from 'rxjs';
import { environment } from '../../../environments/environment';
import { SearchResult } from '@store/tech-record-search/reducer/tech-record-search.reducer';
import { fetchSearchResult } from '@store/tech-record-search/actions/tech-record-search.actions';

export enum SEARCH_TYPES {
VIN = 'vin',
Expand Down Expand Up @@ -131,17 +131,13 @@ export class TechnicalRecordHttpService {
return this.http.put<VehicleTechRecordModel>(url, body, { responseType: 'json' });
}

generatePlate(
vehicleRecord: VehicleTechRecordModel,
techRecord: TechRecordModel,
reason: string,
user: { id?: string; name?: string; email?: string }
) {
generatePlate(vehicleRecord: VehicleTechRecordModel, reason: string, user: { id?: string; name?: string; email?: string }) {
const url = `${environment.VTM_API_URI}/vehicles/documents/plate`;

const updatedVehicleRecord = cloneDeep(vehicleRecord);
const currentRecordIndex = updatedVehicleRecord.techRecord.findIndex(techRecord => techRecord.statusCode === StatusCodes.CURRENT);
updatedVehicleRecord.techRecord[currentRecordIndex].axles?.sort((a, b) => a.axleNumber! - b.axleNumber!);
const techRecord = updatedVehicleRecord.techRecord[currentRecordIndex];

const body = {
vin: vehicleRecord.vin,
Expand All @@ -160,13 +156,14 @@ export class TechnicalRecordHttpService {

generateLetter(
vehicleRecord: VehicleTechRecordModel,
techRecord: TechRecordModel,
letterType: string,
paragraphId: number,
user: { id?: string; name?: string; email?: string }
) {
const url = `${environment.VTM_API_URI}/vehicles/documents/letter`;

const techRecord = vehicleRecord.techRecord.find(techRecord => techRecord.statusCode === StatusCodes.CURRENT);

const body = {
vin: vehicleRecord.vin,
primaryVrm: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
Vrm
} from '@models/vehicle-tech-record.model';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { select, Store } from '@ngrx/store';
import { Store, select } from '@ngrx/store';
import { BatchTechnicalRecordService } from '@services/batch-technical-record/batch-technical-record.service';
import { TechnicalRecordHttpService } from '@services/technical-record-http/technical-record-http.service';
import { TechnicalRecordService } from '@services/technical-record/technical-record.service';
Expand Down Expand Up @@ -173,13 +173,12 @@ export class TechnicalRecordServiceEffects {
ofType(generatePlate),
withLatestFrom(
this.store.select(selectVehicleTechnicalRecordsBySystemNumber),
this.store.select(editableTechRecord),
this.userService.name$,
this.userService.id$,
this.userService.userEmail$
),
switchMap(([{ reason }, vehicle, techRecord, name, id, email]) =>
this.techRecordHttpService.generatePlate(vehicle!, techRecord!, reason, { name, id, email }).pipe(
switchMap(([{ reason }, vehicle, name, id, email]) =>
this.techRecordHttpService.generatePlate(vehicle!, reason, { name, id, email }).pipe(
map(() => generatePlateSuccess()),
catchError(error => of(generatePlateFailure({ error: this.getTechRecordErrorMessage(error, 'generatePlate') })))
)
Expand All @@ -192,13 +191,12 @@ export class TechnicalRecordServiceEffects {
ofType(generateLetter),
withLatestFrom(
this.store.select(selectVehicleTechnicalRecordsBySystemNumber),
this.store.select(editableTechRecord),
this.userService.name$,
this.userService.id$,
this.userService.userEmail$
),
switchMap(([{ letterType, paragraphId }, vehicle, techRecord, name, id, email]) =>
this.techRecordHttpService.generateLetter(vehicle!, techRecord!, letterType, paragraphId, { name, id, email }).pipe(
switchMap(([{ letterType, paragraphId }, vehicle, name, id, email]) =>
this.techRecordHttpService.generateLetter(vehicle!, letterType, paragraphId, { name, id, email }).pipe(
map(value => generateLetterSuccess()),
catchError(error => of(generateLetterFailure({ error: this.getTechRecordErrorMessage(error, 'generateLetter') })))
)
Expand Down

0 comments on commit 8fe234d

Please sign in to comment.