Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base Views for Public Search #1008

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,6 @@ export class ApplicationAdvancedSearchService {
searchDto: SearchRequestDto,
query,
) {
query = query;

if (searchDto.fileTypes.length > 0) {
// if decision is not joined yet -> join it. The join of decision happens in compileApplicationDecisionSearchQuery
if (
Expand Down
2 changes: 2 additions & 0 deletions services/apps/alcs/src/portal/portal.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { PortalNotificationDocumentModule } from './notification-document/notifi
import { ParcelModule } from './parcel/parcel.module';
import { PdfGenerationModule } from './pdf-generation/pdf-generation.module';
import { NotificationSubmissionModule } from './notification-submission/notification-submission.module';
import { PublicModule } from './public/public.module';

@Module({
imports: [
Expand All @@ -42,6 +43,7 @@ import { NotificationSubmissionModule } from './notification-submission/notifica
PortalNoticeOfIntentDecisionModule,
NotificationSubmissionModule,
PortalNotificationDocumentModule,
PublicModule,
RouterModule.register([
{ path: 'portal', module: ApplicationSubmissionModule },
{ path: 'portal', module: NoticeOfIntentSubmissionModule },
Expand Down
14 changes: 14 additions & 0 deletions services/apps/alcs/src/portal/public/public.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';
import { RouterModule } from '@nestjs/core';
import { PublicSearchModule } from './search/public-search.module';

@Module({
imports: [
PublicSearchModule,
RouterModule.register([{ path: 'public', module: PublicSearchModule }]),
],
controllers: [],
providers: [],
exports: [],
})
export class PublicModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import {
DataSource,
JoinColumn,
ManyToOne,
PrimaryColumn,
ViewColumn,
ViewEntity,
} from 'typeorm';
import { ApplicationDecision } from '../../../../alcs/application-decision/application-decision.entity';
import { ApplicationSubmissionToSubmissionStatus } from '../../../../alcs/application/application-submission-status/submission-status.entity';
import { Application } from '../../../../alcs/application/application.entity';
import { ApplicationType } from '../../../../alcs/code/application-code/application-type/application-type.entity';
import { LocalGovernment } from '../../../../alcs/local-government/local-government.entity';
import { ApplicationSubmission } from '../../../application-submission/application-submission.entity';
import { LinkedStatusType } from '../public-search.dto';

@ViewEntity({
expression: (datasource: DataSource) =>
datasource
.createQueryBuilder()
.select('app_sub.uuid', 'uuid')
.addSelect('app_sub.file_number', 'file_number')
.addSelect('app_sub.applicant', 'applicant')
.addSelect('app_sub.local_government_uuid', 'local_government_uuid')
.addSelect('localGovernment.name', 'local_government_name')
.addSelect('app_sub.type_code', 'application_type_code')
.addSelect('app.date_submitted_to_alc', 'date_submitted_to_alc')
.addSelect('app.decision_date', 'decision_date')
.addSelect('app.uuid', 'application_uuid')
.addSelect('app.region_code', 'application_region_code')
.addSelect(
'GREATEST(status_link.effective_date, decision_date.date)',
'last_update',
)
.addSelect(
'alcs.get_current_status_for_application_submission_by_uuid(app_sub.uuid)',
'status',
)
.from(ApplicationSubmission, 'app_sub')
.innerJoin(Application, 'app', 'app.file_number = app_sub.file_number')
.innerJoinAndSelect(
ApplicationType,
'applicationType',
'app_sub.type_code = applicationType.code',
)
.leftJoin(
LocalGovernment,
'localGovernment',
'app_sub.local_government_uuid = localGovernment.uuid',
)
.leftJoin(
(qb) =>
qb
.from(ApplicationSubmissionToSubmissionStatus, 'status_link')
.select('MAX("effective_date")', 'effective_date')
.addSelect('submission_uuid', 'submission_uuid')
.groupBy('submission_uuid'),
'status_link',
'status_link."submission_uuid" = app_sub.uuid',
)
.leftJoin(
(qb) =>
qb
.from(ApplicationDecision, 'decision_date')
.select('MAX("date")', 'date')
.addSelect('application_uuid', 'application_uuid')
.groupBy('application_uuid'),
'decision_date',
'decision_date."application_uuid" = app.uuid',
)
.where('app_sub.is_draft = FALSE')
.andWhere('app.date_received_all_items IS NOT NULL')
.andWhere(
"alcs.get_current_status_for_application_submission_by_uuid(app_sub.uuid)->>'status_type_code' != 'CNCL'",
),
})
export class PublicApplicationSubmissionSearchView {
@ViewColumn()
@PrimaryColumn()
uuid: string;

@ViewColumn()
applicationUuid: string;

@ViewColumn()
lastUpdate: Date;

@ViewColumn()
applicationRegionCode?: string;

@ViewColumn()
fileNumber: string;

@ViewColumn()
applicant?: string;

@ViewColumn()
localGovernmentUuid?: string;

@ViewColumn()
localGovernmentName?: string;

@ViewColumn()
applicationTypeCode: string;

@ViewColumn()
status: LinkedStatusType;

@ViewColumn()
dateSubmittedToAlc: Date | null;

@ViewColumn()
decisionDate: Date | null;

@ManyToOne(() => ApplicationType, {
nullable: false,
})
@JoinColumn({ name: 'application_type_code' })
applicationType: ApplicationType;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { createMock, DeepMocked } from '@golevelup/nestjs-testing';
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { LocalGovernment } from '../../../../alcs/local-government/local-government.entity';
import { SearchRequestDto } from '../public-search.dto';
import { PublicApplicationSearchService } from './public-application-search.service';
import { PublicApplicationSubmissionSearchView } from './public-application-search-view.entity';

describe('PublicApplicationSearchService', () => {
let service: PublicApplicationSearchService;
let mockApplicationSubmissionSearchViewRepository: DeepMocked<
Repository<PublicApplicationSubmissionSearchView>
>;
let mockLocalGovernmentRepository: DeepMocked<Repository<LocalGovernment>>;

const mockSearchRequestDto: SearchRequestDto = {
fileNumber: '123',
portalStatusCode: 'A',
governmentName: 'B',
regionCode: 'C',
name: 'D',
pid: 'E',
civicAddress: 'F',
dateDecidedFrom: new Date('2020-11-10').getTime(),
dateDecidedTo: new Date('2021-11-10').getTime(),
fileTypes: ['type1', 'type2'],
page: 1,
pageSize: 10,
sortField: 'ownerName',
sortDirection: 'ASC',
};

let mockQuery: any = {};

beforeEach(async () => {
mockApplicationSubmissionSearchViewRepository = createMock();
mockLocalGovernmentRepository = createMock();

mockQuery = {
getManyAndCount: jest.fn().mockResolvedValue([[], 0]),
orderBy: jest.fn().mockReturnThis(),
offset: jest.fn().mockReturnThis(),
limit: jest.fn().mockReturnThis(),
innerJoinAndMapOne: jest.fn().mockReturnThis(),
groupBy: jest.fn().mockReturnThis(),
where: jest.fn().mockReturnThis(),
andWhere: jest.fn().mockReturnThis(),
setParameters: jest.fn().mockReturnThis(),
leftJoin: jest.fn().mockReturnThis(),
};

const module: TestingModule = await Test.createTestingModule({
providers: [
PublicApplicationSearchService,
{
provide: getRepositoryToken(PublicApplicationSubmissionSearchView),
useValue: mockApplicationSubmissionSearchViewRepository,
},
{
provide: getRepositoryToken(LocalGovernment),
useValue: mockLocalGovernmentRepository,
},
],
}).compile();

service = module.get<PublicApplicationSearchService>(
PublicApplicationSearchService,
);

mockLocalGovernmentRepository.findOneByOrFail.mockResolvedValue(
new LocalGovernment(),
);
});

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

it('should successfully build a query using all search parameters defined', async () => {
mockApplicationSubmissionSearchViewRepository.createQueryBuilder.mockReturnValue(
mockQuery as any,
);

const result = await service.searchApplications(mockSearchRequestDto);

expect(result).toEqual({ data: [], total: 0 });
expect(
mockApplicationSubmissionSearchViewRepository.createQueryBuilder,
).toBeCalledTimes(1);
expect(mockQuery.andWhere).toBeCalledTimes(8);
expect(mockQuery.where).toBeCalledTimes(1);
});

it('should call compileApplicationSearchQuery method correctly', async () => {
const compileApplicationSearchQuerySpy = jest
.spyOn(service as any, 'compileApplicationSearchQuery')
.mockResolvedValue(mockQuery);

const result = await service.searchApplications(mockSearchRequestDto);

expect(result).toEqual({ data: [], total: 0 });
expect(compileApplicationSearchQuerySpy).toBeCalledWith(
mockSearchRequestDto,
);
expect(mockQuery.orderBy).toHaveBeenCalledTimes(1);
expect(mockQuery.offset).toHaveBeenCalledTimes(1);
expect(mockQuery.limit).toHaveBeenCalledTimes(1);
});
});
Loading