Skip to content

Commit 243838a

Browse files
authored
Merge pull request #2098 from bcgov/feature/ALCS-2468-delete-decisions-relations
ALCS-2468 Delete dependencies
2 parents e35e1fb + 54a9748 commit 243838a

File tree

10 files changed

+97
-53
lines changed

10 files changed

+97
-53
lines changed

services/apps/alcs/src/alcs/application-decision/application-decision-condition/application-decision-condition-date/application-decision-condition-date.service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export class ApplicationDecisionConditionDateService {
9797
return await this.repository.save(newDate);
9898
}
9999

100-
async delete(dateUuid: string) {
100+
async delete(dateUuid: string, forceSingleDateDeletion: boolean = false) {
101101
const conditionDate = await this.repository.findOne({
102102
where: { uuid: dateUuid },
103103
relations: ['condition', 'condition.type'],
@@ -107,7 +107,7 @@ export class ApplicationDecisionConditionDateService {
107107
throw new ServiceNotFoundException(`Condition Date ${dateUuid} was not found`);
108108
}
109109

110-
if (conditionDate.condition.type.dateType !== DateType.MULTIPLE) {
110+
if (!forceSingleDateDeletion && conditionDate.condition.type.dateType !== DateType.MULTIPLE) {
111111
throw new ServiceValidationException(`Deleting the date ${dateUuid} is not permitted on single date type`);
112112
}
113113

services/apps/alcs/src/alcs/application-decision/application-decision-condition/application-decision-condition.service.spec.ts

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { ApplicationDecisionConditionType } from './application-decision-conditi
88
import { UpdateApplicationDecisionConditionDto } from './application-decision-condition.dto';
99
import { ApplicationDecisionCondition } from './application-decision-condition.entity';
1010
import { ApplicationDecisionConditionService } from './application-decision-condition.service';
11+
import { ApplicationDecisionConditionDate } from './application-decision-condition-date/application-decision-condition-date.entity';
1112

1213
describe('ApplicationDecisionConditionService', () => {
1314
let service: ApplicationDecisionConditionService;
@@ -19,12 +20,14 @@ describe('ApplicationDecisionConditionService', () => {
1920
let mockApplicationDecisionConditionToComponentLot: DeepMocked<
2021
Repository<ApplicationDecisionConditionToComponentLot>
2122
>;
23+
let mockApplicationDecisionConditionDate: DeepMocked<Repository<ApplicationDecisionConditionDate>>;
2224

2325
beforeEach(async () => {
2426
mockApplicationDecisionConditionRepository = createMock();
2527
mockAppDecCondTypeRepository = createMock();
2628
mockApplicationDecisionConditionComponentPlanNumber = createMock();
2729
mockApplicationDecisionConditionToComponentLot = createMock();
30+
mockApplicationDecisionConditionDate = createMock();
2831

2932
const module: TestingModule = await Test.createTestingModule({
3033
providers: [
@@ -45,6 +48,10 @@ describe('ApplicationDecisionConditionService', () => {
4548
provide: getRepositoryToken(ApplicationDecisionConditionToComponentLot),
4649
useValue: mockApplicationDecisionConditionToComponentLot,
4750
},
51+
{
52+
provide: getRepositoryToken(ApplicationDecisionConditionDate),
53+
useValue: mockApplicationDecisionConditionDate,
54+
},
4855
],
4956
}).compile();
5057

services/apps/alcs/src/alcs/application-decision/application-decision-v2/application-decision/application-decision-v2.service.spec.ts

+7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { ApplicationDecisionComponent } from './component/application-decision-c
2727
import { ApplicationDecisionComponentService } from './component/application-decision-component.service';
2828
import { ApplicationDecisionConditionCardService } from '../../application-decision-condition/application-decision-condition-card/application-decision-condition-card.service';
2929
import { User } from '../../../../user/user.entity';
30+
import { ApplicationDecisionConditionDateService } from '../../application-decision-condition/application-decision-condition-date/application-decision-condition-date.service';
3031

3132
describe('ApplicationDecisionV2Service', () => {
3233
let service: ApplicationDecisionV2Service;
@@ -44,6 +45,7 @@ describe('ApplicationDecisionV2Service', () => {
4445
let mockUserRepository: DeepMocked<Repository<User>>;
4546
let mockApplicationSubmissionStatusService: DeepMocked<ApplicationSubmissionStatusService>;
4647
let mockApplicationDecisionConditionCardService: DeepMocked<ApplicationDecisionConditionCardService>;
48+
let mockApplicationDecisionConditionDateService: DeepMocked<ApplicationDecisionConditionDateService>;
4749
let mockdataSource: DeepMocked<DataSource>;
4850

4951
let mockApplication;
@@ -64,6 +66,7 @@ describe('ApplicationDecisionV2Service', () => {
6466
mockNaruSubtypeRepository = createMock();
6567
mockApplicationSubmissionStatusService = createMock();
6668
mockApplicationDecisionConditionCardService = createMock();
69+
mockApplicationDecisionConditionDateService = createMock();
6770
mockdataSource = createMock();
6871

6972
const module: TestingModule = await Test.createTestingModule({
@@ -134,6 +137,10 @@ describe('ApplicationDecisionV2Service', () => {
134137
provide: ApplicationDecisionConditionCardService,
135138
useValue: mockApplicationDecisionConditionCardService,
136139
},
140+
{
141+
provide: ApplicationDecisionConditionDateService,
142+
useValue: mockApplicationDecisionConditionDateService,
143+
},
137144
{
138145
provide: DataSource,
139146
useValue: mockdataSource,

services/apps/alcs/src/alcs/application-decision/application-decision-v2/application-decision/application-decision-v2.service.ts

+24-4
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import { CreateApplicationDecisionDto, UpdateApplicationDecisionDto } from './ap
3131
import { ApplicationDecisionComponentType } from './component/application-decision-component-type.entity';
3232
import { ApplicationDecisionComponent } from './component/application-decision-component.entity';
3333
import { ApplicationDecisionComponentService } from './component/application-decision-component.service';
34-
import { ApplicationDecisionConditionCardService } from '../../application-decision-condition/application-decision-condition-card/application-decision-condition-card.service';
3534
import { ApplicationDecisionConditionDate } from '../../application-decision-condition/application-decision-condition-date/application-decision-condition-date.entity';
35+
import { ApplicationDecisionConditionDateService } from '../../application-decision-condition/application-decision-condition-date/application-decision-condition-date.service';
3636

3737
@Injectable()
3838
export class ApplicationDecisionV2Service {
@@ -51,6 +51,8 @@ export class ApplicationDecisionV2Service {
5151
private decisionComponentTypeRepository: Repository<ApplicationDecisionComponentType>,
5252
@InjectRepository(ApplicationDecisionConditionType)
5353
private decisionConditionTypeRepository: Repository<ApplicationDecisionConditionType>,
54+
@InjectRepository(ApplicationDecisionDocument)
55+
private applicationDecisionDocumentRepository: Repository<ApplicationDecisionDocument>,
5456
@InjectRepository(NaruSubtype)
5557
private naruNaruSubtypeRepository: Repository<NaruSubtype>,
5658
@InjectRepository(User)
@@ -59,9 +61,8 @@ export class ApplicationDecisionV2Service {
5961
private documentService: DocumentService,
6062
private decisionComponentService: ApplicationDecisionComponentService,
6163
private decisionConditionService: ApplicationDecisionConditionService,
64+
private dateService: ApplicationDecisionConditionDateService,
6265
private applicationSubmissionStatusService: ApplicationSubmissionStatusService,
63-
@Inject(forwardRef(() => ApplicationDecisionConditionCardService))
64-
private applicationDecisionConditionCardService: ApplicationDecisionConditionCardService,
6566
private dataSource: DataSource,
6667
) {}
6768

@@ -476,6 +477,9 @@ export class ApplicationDecisionV2Service {
476477
const applicationDecision = await this.appDecisionRepository.findOne({
477478
where: { uuid },
478479
relations: {
480+
conditions: {
481+
dates: true,
482+
},
479483
outcome: true,
480484
documents: {
481485
document: true,
@@ -490,15 +494,28 @@ export class ApplicationDecisionV2Service {
490494
throw new ServiceNotFoundException(`Failed to find decision with uuid ${uuid}`);
491495
}
492496

497+
const dateIds: string[] = [];
498+
applicationDecision.conditions.forEach((c) => {
499+
c.dates.forEach((d) => {
500+
dateIds.push(d.uuid);
501+
});
502+
});
503+
504+
await this.decisionConditionService.remove(applicationDecision.conditions);
505+
493506
for (const document of applicationDecision.documents) {
507+
await this.applicationDecisionDocumentRepository.softRemove(document);
494508
await this.documentService.softRemove(document.document);
495509
}
496510

497511
await this.decisionComponentService.softRemove(applicationDecision.components);
498512

499513
if (applicationDecision.conditionCards && applicationDecision.conditionCards.length > 0) {
500514
for (const conditionCard of applicationDecision.conditionCards) {
501-
await this.applicationDecisionConditionCardService.softRemove(conditionCard);
515+
conditionCard.conditions.forEach(async (c) => {
516+
c.conditionCard = null;
517+
c.save();
518+
});
502519
}
503520
}
504521

@@ -509,6 +526,9 @@ export class ApplicationDecisionV2Service {
509526

510527
await this.appDecisionRepository.softRemove([applicationDecision]);
511528
await this.updateApplicationDecisionDates(applicationDecision);
529+
dateIds.forEach(async (id) => {
530+
await this.dateService.delete(id, true);
531+
});
512532
}
513533

514534
async attachDocument(decisionUuid: string, file: MultipartFile, user: User) {

services/apps/alcs/src/alcs/notice-of-intent-decision/notice-of-intent-decision-condition/notice-of-intent-decision-condition-date/notice-of-intent-decision-condition-date.service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export class NoticeOfIntentDecisionConditionDateService {
100100
return await this.repository.save(newDate);
101101
}
102102

103-
async delete(dateUuid: string) {
103+
async delete(dateUuid: string, forceSingleDateDeletion: boolean = false) {
104104
const conditionDate = await this.repository.findOne({
105105
where: { uuid: dateUuid },
106106
relations: ['condition', 'condition.type'],
@@ -110,7 +110,7 @@ export class NoticeOfIntentDecisionConditionDateService {
110110
throw new ServiceNotFoundException(`Condition Date ${dateUuid} was not found`);
111111
}
112112

113-
if (conditionDate.condition.type.dateType !== DateType.MULTIPLE) {
113+
if (!forceSingleDateDeletion && conditionDate.condition.type.dateType !== DateType.MULTIPLE) {
114114
throw new ServiceValidationException(`Deleting the date ${dateUuid} is not permitted on single date type`);
115115
}
116116

services/apps/alcs/src/alcs/notice-of-intent-decision/notice-of-intent-decision-condition/notice-of-intent-decision-condition.entity.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,5 @@ export class NoticeOfIntentDecisionCondition extends Base {
8484
@ManyToOne(() => NoticeOfIntentDecisionConditionCard, (conditionCard) => conditionCard.conditions, {
8585
nullable: true,
8686
})
87-
conditionCard: NoticeOfIntentDecisionConditionCard;
87+
conditionCard: NoticeOfIntentDecisionConditionCard | null;
8888
}

services/apps/alcs/src/alcs/notice-of-intent-decision/notice-of-intent-decision-condition/notice-of-intent-decision-condition.service.spec.ts

+20-42
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@ import { NoticeOfIntentDecisionConditionType } from './notice-of-intent-decision
66
import { UpdateNoticeOfIntentDecisionConditionDto } from './notice-of-intent-decision-condition.dto';
77
import { NoticeOfIntentDecisionCondition } from './notice-of-intent-decision-condition.entity';
88
import { NoticeOfIntentDecisionConditionService } from './notice-of-intent-decision-condition.service';
9+
import { NoticeOfIntentDecisionConditionDate } from './notice-of-intent-decision-condition-date/notice-of-intent-decision-condition-date.entity';
910

1011
describe('NoticeOfIntentDecisionConditionService', () => {
1112
let service: NoticeOfIntentDecisionConditionService;
12-
let mockNOIDecisionConditionRepository: DeepMocked<
13-
Repository<NoticeOfIntentDecisionCondition>
14-
>;
15-
let mockNOIDecisionConditionTypeRepository: DeepMocked<
16-
Repository<NoticeOfIntentDecisionConditionType>
17-
>;
13+
let mockNOIDecisionConditionRepository: DeepMocked<Repository<NoticeOfIntentDecisionCondition>>;
14+
let mockNOIDecisionConditionTypeRepository: DeepMocked<Repository<NoticeOfIntentDecisionConditionType>>;
15+
let mockNoticeOfIntentDecisionConditionDateRepository: DeepMocked<Repository<NoticeOfIntentDecisionConditionDate>>;
1816

1917
beforeEach(async () => {
2018
mockNOIDecisionConditionRepository = createMock();
2119
mockNOIDecisionConditionTypeRepository = createMock();
20+
mockNoticeOfIntentDecisionConditionDateRepository = createMock();
2221

2322
const module: TestingModule = await Test.createTestingModule({
2423
providers: [
@@ -31,22 +30,22 @@ describe('NoticeOfIntentDecisionConditionService', () => {
3130
provide: getRepositoryToken(NoticeOfIntentDecisionConditionType),
3231
useValue: mockNOIDecisionConditionTypeRepository,
3332
},
33+
{
34+
provide: getRepositoryToken(NoticeOfIntentDecisionConditionDate),
35+
useValue: mockNoticeOfIntentDecisionConditionDateRepository,
36+
},
3437
],
3538
}).compile();
3639

37-
service = module.get<NoticeOfIntentDecisionConditionService>(
38-
NoticeOfIntentDecisionConditionService,
39-
);
40+
service = module.get<NoticeOfIntentDecisionConditionService>(NoticeOfIntentDecisionConditionService);
4041
});
4142

4243
it('should be defined', () => {
4344
expect(service).toBeDefined();
4445
});
4546

4647
it('should call repo to get one or fails with correct parameters', async () => {
47-
mockNOIDecisionConditionRepository.findOneOrFail.mockResolvedValue(
48-
new NoticeOfIntentDecisionCondition(),
49-
);
48+
mockNOIDecisionConditionRepository.findOneOrFail.mockResolvedValue(new NoticeOfIntentDecisionCondition());
5049

5150
const result = await service.getOneOrFail('fake');
5251

@@ -59,24 +58,17 @@ describe('NoticeOfIntentDecisionConditionService', () => {
5958
});
6059

6160
it('calls remove method for deleted conditions', async () => {
62-
const conditions = [
63-
new NoticeOfIntentDecisionCondition(),
64-
new NoticeOfIntentDecisionCondition(),
65-
];
61+
const conditions = [new NoticeOfIntentDecisionCondition(), new NoticeOfIntentDecisionCondition()];
6662

67-
mockNOIDecisionConditionRepository.remove.mockResolvedValue(
68-
{} as NoticeOfIntentDecisionCondition,
69-
);
63+
mockNOIDecisionConditionRepository.remove.mockResolvedValue({} as NoticeOfIntentDecisionCondition);
7064

7165
await service.remove(conditions);
7266

7367
expect(mockNOIDecisionConditionRepository.remove).toBeCalledTimes(1);
7468
});
7569

7670
it('should create new components when given a DTO without a UUID', async () => {
77-
mockNOIDecisionConditionRepository.findOneOrFail.mockResolvedValue(
78-
new NoticeOfIntentDecisionCondition(),
79-
);
71+
mockNOIDecisionConditionRepository.findOneOrFail.mockResolvedValue(new NoticeOfIntentDecisionCondition());
8072

8173
const updateDtos: UpdateNoticeOfIntentDecisionConditionDto[] = [{}, {}];
8274

@@ -111,12 +103,8 @@ describe('NoticeOfIntentDecisionConditionService', () => {
111103
});
112104

113105
it('should persist entity if persist flag is true', async () => {
114-
mockNOIDecisionConditionRepository.findOneOrFail.mockResolvedValue(
115-
new NoticeOfIntentDecisionCondition(),
116-
);
117-
mockNOIDecisionConditionRepository.save.mockResolvedValue(
118-
new NoticeOfIntentDecisionCondition(),
119-
);
106+
mockNOIDecisionConditionRepository.findOneOrFail.mockResolvedValue(new NoticeOfIntentDecisionCondition());
107+
mockNOIDecisionConditionRepository.save.mockResolvedValue(new NoticeOfIntentDecisionCondition());
120108

121109
const updateDtos: UpdateNoticeOfIntentDecisionConditionDto[] = [{}];
122110

@@ -128,12 +116,8 @@ describe('NoticeOfIntentDecisionConditionService', () => {
128116
});
129117

130118
it('should not persist entity if persist flag is false', async () => {
131-
mockNOIDecisionConditionRepository.findOneOrFail.mockResolvedValue(
132-
new NoticeOfIntentDecisionCondition(),
133-
);
134-
mockNOIDecisionConditionRepository.save.mockResolvedValue(
135-
new NoticeOfIntentDecisionCondition(),
136-
);
119+
mockNOIDecisionConditionRepository.findOneOrFail.mockResolvedValue(new NoticeOfIntentDecisionCondition());
120+
mockNOIDecisionConditionRepository.save.mockResolvedValue(new NoticeOfIntentDecisionCondition());
137121

138122
const updateDtos: UpdateNoticeOfIntentDecisionConditionDto[] = [{}];
139123

@@ -147,21 +131,15 @@ describe('NoticeOfIntentDecisionConditionService', () => {
147131
it('should update on the repo for update', async () => {
148132
const existingCondition = new NoticeOfIntentDecisionCondition();
149133
mockNOIDecisionConditionRepository.update.mockResolvedValue({} as any);
150-
mockNOIDecisionConditionRepository.findOneOrFail.mockResolvedValue(
151-
existingCondition,
152-
);
134+
mockNOIDecisionConditionRepository.findOneOrFail.mockResolvedValue(existingCondition);
153135

154136
const result = await service.update(existingCondition, {
155137
administrativeFee: 50,
156138
});
157139

158140
expect(result).toBeDefined();
159141
expect(mockNOIDecisionConditionRepository.update).toBeCalledTimes(1);
160-
expect(
161-
mockNOIDecisionConditionRepository.update.mock.calls[0][1][
162-
'administrativeFee'
163-
],
164-
).toEqual(50);
142+
expect(mockNOIDecisionConditionRepository.update.mock.calls[0][1]['administrativeFee']).toEqual(50);
165143
expect(mockNOIDecisionConditionRepository.findOneOrFail).toBeCalledTimes(1);
166144
});
167145
});

services/apps/alcs/src/alcs/notice-of-intent-decision/notice-of-intent-decision-condition/notice-of-intent-decision-condition.service.ts

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export class NoticeOfIntentDecisionConditionService {
1818
private repository: Repository<NoticeOfIntentDecisionCondition>,
1919
@InjectRepository(NoticeOfIntentDecisionConditionType)
2020
private typeRepository: Repository<NoticeOfIntentDecisionConditionType>,
21+
@InjectRepository(NoticeOfIntentDecisionConditionDate)
22+
private dateRepository: Repository<NoticeOfIntentDecisionConditionDate>,
2123
) {}
2224

2325
async getByTypeCode(typeCode: string): Promise<NoticeOfIntentDecisionCondition[]> {

services/apps/alcs/src/alcs/notice-of-intent-decision/notice-of-intent-decision-v2/notice-of-intent-decision-v2.service.spec.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { NoticeOfIntentDecision } from '../notice-of-intent-decision.entity';
2424
import { NoticeOfIntentDecisionV2Service } from './notice-of-intent-decision-v2.service';
2525
import { User } from '../../../user/user.entity';
2626
import { NoticeOfIntentDecisionConditionCardService } from '../notice-of-intent-decision-condition/notice-of-intent-decision-condition-card/notice-of-intent-decision-condition-card.service';
27-
import { mock } from 'node:test';
27+
import { NoticeOfIntentDecisionConditionDateService } from '../notice-of-intent-decision-condition/notice-of-intent-decision-condition-date/notice-of-intent-decision-condition-date.service';
2828

2929
describe('NoticeOfIntentDecisionV2Service', () => {
3030
let service: NoticeOfIntentDecisionV2Service;
@@ -39,6 +39,7 @@ describe('NoticeOfIntentDecisionV2Service', () => {
3939
let mockDecisionConditionService: DeepMocked<NoticeOfIntentDecisionConditionService>;
4040
let mockNoticeOfIntentSubmissionStatusService: DeepMocked<NoticeOfIntentSubmissionStatusService>;
4141
let mockNoticeOfIntentDecisionConditionCardService: DeepMocked<NoticeOfIntentDecisionConditionCardService>;
42+
let mockNoticeOfIntentDecisionConditionDateService: DeepMocked<NoticeOfIntentDecisionConditionDateService>;
4243
let mockdataSource: DeepMocked<DataSource>;
4344

4445
let mockNoticeOfIntent;
@@ -56,6 +57,7 @@ describe('NoticeOfIntentDecisionV2Service', () => {
5657
mockDecisionConditionService = createMock();
5758
mockNoticeOfIntentSubmissionStatusService = createMock();
5859
mockNoticeOfIntentDecisionConditionCardService = createMock();
60+
mockNoticeOfIntentDecisionConditionDateService = createMock();
5961
mockdataSource = createMock();
6062

6163
const module: TestingModule = await Test.createTestingModule({
@@ -114,6 +116,10 @@ describe('NoticeOfIntentDecisionV2Service', () => {
114116
provide: NoticeOfIntentDecisionConditionCardService,
115117
useValue: mockNoticeOfIntentDecisionConditionCardService,
116118
},
119+
{
120+
provide: NoticeOfIntentDecisionConditionDateService,
121+
useValue: mockNoticeOfIntentDecisionConditionDateService,
122+
},
117123
{
118124
provide: DataSource,
119125
useValue: mockdataSource,
@@ -129,6 +135,7 @@ describe('NoticeOfIntentDecisionV2Service', () => {
129135
mockDecision = new NoticeOfIntentDecision({
130136
noticeOfIntent: mockNoticeOfIntent,
131137
documents: [],
138+
conditions: [],
132139
});
133140

134141
mockDecisionRepository.find.mockResolvedValue([mockDecision]);

0 commit comments

Comments
 (0)