Skip to content

Commit

Permalink
feat(cb2-9882): Add ability to produce a rejection letter for TRLs on…
Browse files Browse the repository at this point in the history
… provisional tech records (#1350)

* feat(cb2-9882): provisional letter work

* feat(cb2-9882): fix the unexpected display buy

* feat(cb2-9882): refactor code to more readable

* feat(cb2-9882): add unit tests

* feat(cb2-9882): remove unnecessary variable

* feat(cb2-9882): amend variable name

* feat(cb2-9882): amend the logic of ineligibility

* feat(cb2-9882): fix memory leak

* feat(cb2-9882): update tests

---------

Co-authored-by: pbardy2000 <[email protected]>
  • Loading branch information
JunYanBJSS and pbardy2000 authored Jan 10, 2024
1 parent 19def4f commit 9203be1
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
CustomFormControl, FormNodeOption, FormNodeTypes, FormNodeWidth,
} from '@forms/services/dynamic-form.types';
import { LETTER_TYPES } from '@forms/templates/general/letter-types';
import { V3TechRecordModel } from '@models/vehicle-tech-record.model';
import { StatusCodes, V3TechRecordModel } from '@models/vehicle-tech-record.model';
import { Actions, ofType } from '@ngrx/effects';
import { Store } from '@ngrx/store';
import { TechnicalRecordService } from '@services/technical-record/technical-record.service';
Expand Down Expand Up @@ -64,6 +64,11 @@ export class GenerateLetterComponent implements OnInit {
}

get reasons(): Array<FormNodeOption<string>> {
if (this.techRecord?.techRecord_statusCode === StatusCodes.PROVISIONAL) {
return [
{ label: 'Trailer rejected', value: LETTER_TYPES[1].value },
];
}
return [
{ label: 'Trailer accepted', value: LETTER_TYPES[0].value },
{ label: 'Trailer rejected', value: LETTER_TYPES[1].value },
Expand Down
37 changes: 37 additions & 0 deletions src/app/forms/custom-sections/letters/letters.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,31 @@ import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ActivatedRoute, RouterModule } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { ApprovalType } from '@dvsa/cvs-type-definitions/types/v3/tech-record/enums/approvalType.enum.js';
import { TechRecordSearchSchema } from '@dvsa/cvs-type-definitions/types/v3/tech-record/get/search';
import { TechRecordType } from '@dvsa/cvs-type-definitions/types/v3/tech-record/tech-record-vehicle-type';
import { DynamicFormsModule } from '@forms/dynamic-forms.module';
import { Roles } from '@models/roles.enum';
import { StatusCodes } from '@models/vehicle-tech-record.model';
import { StoreModule } from '@ngrx/store';
import { provideMockStore } from '@ngrx/store/testing';
import { TechnicalRecordService } from '@services/technical-record/technical-record.service';
import { UserService } from '@services/user-service/user-service';
import { SharedModule } from '@shared/shared.module';
import { State, initialAppState } from '@store/index';
import { of } from 'rxjs';
import { LettersComponent } from './letters.component';

const mockTechRecordService = {
techRecordHistory$: of([{
vin: 'test',
techRecord_statusCode: 'current',
techRecord_vehicleType: 'trl',
createdTimestamp: '12345',
systemNumber: '123',
techRecord_manufactureYear: 2021,
}] as TechRecordSearchSchema[]),
};

describe('LettersComponent', () => {
let component: LettersComponent;
let fixture: ComponentFixture<LettersComponent>;
Expand All @@ -41,8 +55,13 @@ describe('LettersComponent', () => {
provide: APP_BASE_HREF,
useValue: '/',
},
{
provide: TechnicalRecordService,
useValue: mockTechRecordService,
},
],
}).compileComponents();

});

beforeEach(() => {
Expand Down Expand Up @@ -70,6 +89,24 @@ describe('LettersComponent', () => {
(component.techRecord as TechRecordType<'trl'>).techRecord_approvalType = ApprovalType.NTA;
expect(component.eligibleForLetter).toBeFalsy();
});

it('should return false if the statuscode is archived', () => {
(component.techRecord as TechRecordType<'trl'>).techRecord_approvalType = ApprovalType.GB_WVTA;
(component.techRecord as TechRecordType<'trl'>).techRecord_statusCode = StatusCodes.ARCHIVED;
expect(component.eligibleForLetter).toBeFalsy();
});
});

describe('checkRecordHistoryHasCurrent', () => {
it('should return false if the current technical record history has current status', () => {
expect(component.hasCurrent).toBeFalsy();
});

it('should return true if the provisional technical record history has current status', () => {
(component.techRecord as TechRecordType<'trl'>).techRecord_statusCode = 'provisional';
component.ngOnInit();
expect(component.hasCurrent).toBeTruthy();
});
});

describe('letter', () => {
Expand Down
38 changes: 32 additions & 6 deletions src/app/forms/custom-sections/letters/letters.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Component, EventEmitter, Input, OnChanges, OnDestroy, OnInit, Output,
} from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { TechRecordSearchSchema } from '@dvsa/cvs-type-definitions/types/v3/tech-record/get/search';
import { ParagraphIds } from '@dvsa/cvs-type-definitions/types/v3/tech-record/get/trl/complete';
import { TechRecordType } from '@dvsa/cvs-type-definitions/types/v3/tech-record/tech-record-vehicle-type';
import { TechRecordType as TechRecordTypeVehicleVerb } from '@dvsa/cvs-type-definitions/types/v3/tech-record/tech-record-verb-vehicle-type';
Expand All @@ -13,8 +14,11 @@ import { LettersTemplate } from '@forms/templates/general/letters.template';
import { Roles } from '@models/roles.enum';
import { LettersIntoAuthApprovalType, LettersOfAuth, StatusCodes } from '@models/vehicle-tech-record.model';
import { Store } from '@ngrx/store';
import { TechnicalRecordService } from '@services/technical-record/technical-record.service';
import { updateScrollPosition } from '@store/technical-records';
import { Subscription, debounceTime } from 'rxjs';
import {
ReplaySubject, Subscription, debounceTime, takeUntil,
} from 'rxjs';

@Component({
selector: 'app-letters[techRecord]',
Expand All @@ -29,10 +33,14 @@ export class LettersComponent implements OnInit, OnDestroy, OnChanges {

form!: CustomFormGroup;

hasCurrent = false;

private formSubscription = new Subscription();
private destroy$ = new ReplaySubject<boolean>(1);

constructor(
private dynamicFormService: DynamicFormService,
private techRecordService: TechnicalRecordService,
private viewportScroller: ViewportScroller,
private router: Router,
private route: ActivatedRoute,
Expand All @@ -42,6 +50,7 @@ export class LettersComponent implements OnInit, OnDestroy, OnChanges {
ngOnInit(): void {
this.form = this.dynamicFormService.createForm(LettersTemplate, this.techRecord) as CustomFormGroup;
this.formSubscription = this.form.cleanValueChanges.pipe(debounceTime(400)).subscribe((event) => this.formChange.emit(event));
this.checkForCurrentRecordInHistory();
}

ngOnChanges(): void {
Expand All @@ -52,6 +61,19 @@ export class LettersComponent implements OnInit, OnDestroy, OnChanges {

ngOnDestroy(): void {
this.formSubscription.unsubscribe();
this.destroy$.next(true);
this.destroy$.unsubscribe();
}

checkForCurrentRecordInHistory() {
this.techRecordService.techRecordHistory$.pipe(takeUntil(this.destroy$)).subscribe((historyArray: TechRecordSearchSchema[] | undefined) => {
historyArray?.forEach((history: TechRecordSearchSchema) => {
if (history.techRecord_statusCode === StatusCodes.CURRENT
&& this.techRecord?.techRecord_statusCode === StatusCodes.PROVISIONAL) {
this.hasCurrent = true;
}
});
});
}

get roles(): typeof Roles {
Expand All @@ -75,18 +97,22 @@ export class LettersComponent implements OnInit, OnDestroy, OnChanges {
}

get eligibleForLetter(): boolean {
const currentTechRecord = this.techRecord?.techRecord_statusCode === StatusCodes.CURRENT;

return this.correctApprovalType && currentTechRecord && !this.isEditing;
const isArchivedTechRecord = this.techRecord?.techRecord_statusCode === StatusCodes.ARCHIVED;
return this.correctApprovalType && !isArchivedTechRecord && !this.isEditing && !this.hasCurrent;
}

get reasonForIneligibility(): string {
if (this.isEditing) {
return 'This section is not available when amending or creating a technical record.';
}

if (this.techRecord?.techRecord_statusCode !== StatusCodes.CURRENT) {
return 'Generating letters is only applicable to current technical records.';
if (this.techRecord?.techRecord_statusCode === StatusCodes.PROVISIONAL) {
if (this.hasCurrent) {
// eslint-disable-next-line max-len
return 'Generating letters is not applicable to provisional records, where a current record also exists for a vehicle. Open the current record to generate letters.';
}
} else if (this.techRecord?.techRecord_statusCode === StatusCodes.ARCHIVED) {
return 'Generating letters is not applicable to archived technical records.';
}

if (!this.correctApprovalType) {
Expand Down

0 comments on commit 9203be1

Please sign in to comment.