Skip to content

Commit

Permalink
Merge pull request #1711 from bcgov/feature/ALCS-1922
Browse files Browse the repository at this point in the history
Keep organization name when changing from indv -> organization
  • Loading branch information
dhaselhan authored May 27, 2024
2 parents 6d8f332 + 40ad829 commit 54c4645
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class TransfereeDialogComponent {
public data: {
submissionUuid: string;
existingTransferee?: NotificationTransfereeDto;
}
},
) {
if (data && data.existingTransferee) {
this.onChangeType({
Expand All @@ -66,7 +66,7 @@ export class TransfereeDialogComponent {
this.organizationName.setValidators([Validators.required]);
} else {
this.organizationName.setValidators([]);
this.organizationName.reset();
this.organizationName.updateValueAndValidity();
}
}

Expand All @@ -78,8 +78,10 @@ export class TransfereeDialogComponent {
}
this.isLoading = true;

const orgName = this.type.value === OWNER_TYPE.ORGANIZATION ? this.organizationName.getRawValue() : null;

const createDto: NotificationTransfereeCreateDto = {
organizationName: this.organizationName.getRawValue() || undefined,
organizationName: orgName,
firstName: this.firstName.getRawValue() || undefined,
lastName: this.lastName.getRawValue() || undefined,
email: this.email.getRawValue()!,
Expand All @@ -91,7 +93,7 @@ export class TransfereeDialogComponent {
await this.transfereeService.create(createDto);
this.dialogRef.close(true);
} else {
this.form.markAllAsTouched()
this.form.markAllAsTouched();
}
}

Expand All @@ -101,8 +103,9 @@ export class TransfereeDialogComponent {

async onSave() {
if (this.form.valid) {
const orgName = this.type.value === OWNER_TYPE.ORGANIZATION ? this.organizationName.getRawValue() : null;
const updateDto: NotificationTransfereeUpdateDto = {
organizationName: this.organizationName.getRawValue(),
organizationName: orgName,
firstName: this.firstName.getRawValue(),
lastName: this.lastName.getRawValue(),
email: this.email.getRawValue()!,
Expand All @@ -115,7 +118,7 @@ export class TransfereeDialogComponent {
this.dialogRef.close(true);
}
} else {
this.form.markAllAsTouched()
this.form.markAllAsTouched();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class OwnerDialogComponent {
existingOwner?: ApplicationOwnerDto;
documentService: ApplicationDocumentService | NoticeOfIntentDocumentService;
ownerService: ApplicationOwnerService | NoticeOfIntentOwnerService;
}
},
) {
if (data && data.existingOwner) {
this.onChangeType({
Expand Down Expand Up @@ -98,9 +98,10 @@ export class OwnerDialogComponent {
} else {
this.organizationName.setValidators([]);
this.corporateSummary.setValidators([]);
this.organizationName.reset();
this.corporateSummary.reset();
}
this.corporateSummary.updateValueAndValidity();
this.organizationName.updateValueAndValidity();
}

async onCreate() {
Expand All @@ -118,11 +119,13 @@ export class OwnerDialogComponent {
return;
}
}

const orgName = this.type.value === OWNER_TYPE.ORGANIZATION ? this.organizationName.getRawValue() : null;
const createDto: ApplicationOwnerCreateDto & NoticeOfIntentOwnerCreateDto = {
organizationName: this.organizationName.getRawValue() || undefined,
organizationName: orgName,
firstName: this.firstName.getRawValue() || undefined,
lastName: this.lastName.getRawValue() || undefined,
corporateSummaryUuid: documentUuid?.uuid,
corporateSummaryUuid: documentUuid?.uuid ?? null,
email: this.email.getRawValue()!,
phoneNumber: this.phoneNumber.getRawValue()!,
typeCode: this.type.getRawValue()!,
Expand Down Expand Up @@ -171,11 +174,12 @@ export class OwnerDialogComponent {
document = await this.uploadPendingFile(this.pendingFile);
}

const orgName = this.type.value === OWNER_TYPE.ORGANIZATION ? this.organizationName.getRawValue() : null;
const updateDto: ApplicationOwnerUpdateDto = {
organizationName: this.organizationName.getRawValue(),
organizationName: orgName,
firstName: this.firstName.getRawValue(),
lastName: this.lastName.getRawValue(),
corporateSummaryUuid: document?.uuid,
corporateSummaryUuid: document?.uuid ?? null,
email: this.email.getRawValue()!,
phoneNumber: this.phoneNumber.getRawValue()!,
typeCode: this.type.getRawValue()!,
Expand All @@ -190,6 +194,7 @@ export class OwnerDialogComponent {
this.isLoading = false;
} else {
this.form.markAllAsTouched();
this.showFileErrors = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,11 @@ describe('ApplicationOwnerService', () => {
corporateSummaryUuid: 'oldUuid',
corporateSummary: new ApplicationDocument(),
});
const mockDocument = new ApplicationDocument();
mockRepo.findOneOrFail.mockResolvedValue(owner);
mockRepo.save.mockResolvedValue(new ApplicationOwner());
mockAppDocumentService.delete.mockResolvedValue({} as any);
mockAppDocumentService.get.mockResolvedValue(mockDocument);

await service.update('', {
organizationName: '',
Expand All @@ -182,10 +184,10 @@ describe('ApplicationOwnerService', () => {
corporateSummaryUuid: 'newUuid',
});

expect(owner.corporateSummaryUuid).toEqual('newUuid');
expect(owner.corporateSummary).toBe(mockDocument);
expect(mockAppDocumentService.delete).toHaveBeenCalledTimes(1);
expect(mockRepo.findOneOrFail).toHaveBeenCalledTimes(1);
expect(mockRepo.save).toHaveBeenCalledTimes(2);
expect(mockRepo.save).toHaveBeenCalledTimes(1);
});

it('should call through for delete', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,30 @@ export class ApplicationOwnerService {
});
}

//If attaching new document and old one was defined, delete it
//If null or new document, delete old one
if (
updateDto.corporateSummaryUuid &&
updateDto.corporateSummaryUuid !== undefined &&
existingOwner.corporateSummaryUuid !== updateDto.corporateSummaryUuid &&
existingOwner.corporateSummary
) {
const oldSummary = existingOwner.corporateSummary;
await this.applicationDocumentService.delete(
existingOwner.corporateSummary,
);
existingOwner.corporateSummary = null;
await this.repository.save(existingOwner);
await this.applicationDocumentService.delete(oldSummary);
existingOwner.corporateSummaryUuid = null;
}

existingOwner.corporateSummaryUuid =
updateDto.corporateSummaryUuid !== undefined
? updateDto.corporateSummaryUuid
: existingOwner.corporateSummaryUuid;
//Attach new one
if (
updateDto.corporateSummaryUuid &&
updateDto.corporateSummaryUuid !== existingOwner.corporateSummaryUuid
) {
const newSummary = await this.applicationDocumentService.get(
updateDto.corporateSummaryUuid,
);
existingOwner.corporateSummary = newSummary;
existingOwner.corporateSummaryUuid = newSummary.uuid;
}

existingOwner.organizationName =
updateDto.organizationName !== undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
} from 'class-validator';
import { NoticeOfIntentDocumentDto } from '../../../alcs/notice-of-intent/notice-of-intent-document/notice-of-intent-document.dto';
import {
OwnerTypeDto,
OWNER_TYPE,
OwnerTypeDto,
} from '../../../common/owner-type/owner-type.entity';
import { emailRegex } from '../../../utils/email.helper';
import { NoticeOfIntentParcelDto } from '../notice-of-intent-parcel/notice-of-intent-parcel.dto';
Expand Down Expand Up @@ -66,7 +66,7 @@ export class NoticeOfIntentOwnerUpdateDto {

@IsString()
@IsOptional()
organizationName?: string;
organizationName?: string | null;

@IsString()
@IsOptional()
Expand All @@ -75,7 +75,7 @@ export class NoticeOfIntentOwnerUpdateDto {
@Matches(emailRegex)
@IsOptional()
email?: string;

@IsString()
@IsOptional()
crownLandOwnerType?: string;
Expand All @@ -86,7 +86,7 @@ export class NoticeOfIntentOwnerUpdateDto {

@IsUUID()
@IsOptional()
corporateSummaryUuid?: string;
corporateSummaryUuid?: string | null;
}

export class NoticeOfIntentOwnerCreateDto extends NoticeOfIntentOwnerUpdateDto {
Expand Down Expand Up @@ -125,7 +125,7 @@ export class SetPrimaryContactDto {

@IsString()
noticeOfIntentSubmissionUuid: string;

@IsString()
@IsOptional()
crownLandOwnerType?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ describe('NoticeOfIntentOwnerService', () => {
mockRepo.findOneOrFail.mockResolvedValue(owner);
mockRepo.save.mockResolvedValue(new NoticeOfIntentOwner());
mockAppDocumentService.delete.mockResolvedValue({} as any);
const mockDocument = new NoticeOfIntentDocument();
mockAppDocumentService.get.mockResolvedValue(mockDocument);

await service.update(
'',
Expand All @@ -194,10 +196,11 @@ describe('NoticeOfIntentOwnerService', () => {
new User(),
);

expect(owner.corporateSummaryUuid).toEqual('newUuid');
expect(owner.corporateSummary).toBe(mockDocument);
expect(mockAppDocumentService.delete).toHaveBeenCalledTimes(1);
expect(mockRepo.findOneOrFail).toHaveBeenCalledTimes(1);
expect(mockRepo.save).toHaveBeenCalledTimes(2);
expect(mockRepo.save).toHaveBeenCalledTimes(1);
expect(mockAppDocumentService.get).toHaveBeenCalledTimes(1);
});

it('should call through for delete', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,30 @@ export class NoticeOfIntentOwnerService {
});
}

//If attaching new document and old one was defined, delete it
//If null or new document, delete old one
if (
updateDto.corporateSummaryUuid &&
updateDto.corporateSummaryUuid !== undefined &&
existingOwner.corporateSummaryUuid !== updateDto.corporateSummaryUuid &&
existingOwner.corporateSummary
) {
const oldSummary = existingOwner.corporateSummary;
await this.noticeOfIntentDocumentService.delete(
existingOwner.corporateSummary,
);
existingOwner.corporateSummary = null;
await this.repository.save(existingOwner);
await this.noticeOfIntentDocumentService.delete(oldSummary);
existingOwner.corporateSummaryUuid = null;
}

existingOwner.corporateSummaryUuid =
updateDto.corporateSummaryUuid !== undefined
? updateDto.corporateSummaryUuid
: existingOwner.corporateSummaryUuid;
//Attach new one
if (
updateDto.corporateSummaryUuid &&
updateDto.corporateSummaryUuid !== existingOwner.corporateSummaryUuid
) {
const newSummary = await this.noticeOfIntentDocumentService.get(
updateDto.corporateSummaryUuid,
);
existingOwner.corporateSummary = newSummary;
existingOwner.corporateSummaryUuid = newSummary.uuid;
}

existingOwner.organizationName =
updateDto.organizationName !== undefined
Expand Down

0 comments on commit 54c4645

Please sign in to comment.