Skip to content

Commit

Permalink
Feat: Fixing Companies Query (#1117)
Browse files Browse the repository at this point in the history
Co-authored-by: praju-aot <[email protected]>
  • Loading branch information
erikataot and praju-aot authored Jan 29, 2024
1 parent 1a85be5 commit 008b125
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 19 deletions.
24 changes: 23 additions & 1 deletion frontend/src/features/idir/search/api/idirSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
PaginatedResponse,
PaginationOptions,
} from "../../../../common/types/common";
import { CompanyProfile } from "../../../manageProfile/types/manageProfile";
import { Permit } from "../../../permits/types/permit";
import { SEARCH_ENTITIES, SearchEntity, SearchFields } from "../types/types";

Expand All @@ -26,7 +27,7 @@ const getSearchURLbyEntity = (searchEntity: SearchEntity): string | URL => {
* @param SearchFields The search parameters.
* @returns The response from the API.
*/
export const getDataBySearch = (
export const getPermitDataBySearch = (
{ searchEntity, searchByFilter, searchString }: SearchFields,
{ page = 0, take = 10 }: PaginationOptions,
): Promise<PaginatedResponse<Permit>> => {
Expand All @@ -39,3 +40,24 @@ export const getDataBySearch = (
searchURL.searchParams.set("take", take.toString());
return httpGETRequest(searchURL.toString()).then((response) => response.data);
};

/**
* Searches the data with options and value entered by the user.
* @param SearchFields The search parameters.
* @returns The response from the API.
*/
export const getCompanyDataBySearch = (
{ searchEntity, searchByFilter, searchString }: SearchFields,
{ page = 0, take = 10 }: PaginationOptions,
): Promise<PaginatedResponse<CompanyProfile>> => {
const searchURL = new URL(getSearchURLbyEntity(searchEntity));
if (searchByFilter === 'companyName')
searchURL.searchParams.set("legalName", searchString);
else
searchURL.searchParams.set("clientNumber", searchString);

// API pagination index starts at 1. Hence page + 1.
searchURL.searchParams.set("page", (page + 1).toString());
searchURL.searchParams.set("take", take.toString());
return httpGETRequest(searchURL.toString()).then((response) => response.data);
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import OnRouteBCContext from "../../../../common/authentication/OnRouteBCContext";
import { Optional } from "../../../../common/types/common";
import { USER_AUTH_GROUP } from "../../../../common/authentication/types";
import { getDataBySearch } from "../api/idirSearch";
import { getCompanyDataBySearch } from "../api/idirSearch";
import { SearchFields } from "../types/types";
import {
defaultTableInitialStateOptions,
Expand Down Expand Up @@ -68,6 +68,7 @@ export const IDIRCompanySearchResults = memo(
pageIndex: 0,
pageSize: 10,
});

// TODO: if data is [] AND current_user is PPC_ADMIN then (eventually)
// display the UX to allow the creation of a new Company Profile
const canCreateCompany = false;
Expand All @@ -81,7 +82,7 @@ export const IDIRCompanySearchResults = memo(
pagination.pageSize,
],
() =>
getDataBySearch(
getCompanyDataBySearch(
{
searchByFilter,
searchEntity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { USER_AUTH_GROUP } from "../../../../common/authentication/types";
import { hasPermitExpired } from "../../../permits/helpers/permitState";
import { isPermitInactive } from "../../../permits/types/PermitStatus";
import { Permit } from "../../../permits/types/permit";
import { getDataBySearch } from "../api/idirSearch";
import { getPermitDataBySearch } from "../api/idirSearch";
import { PermitSearchResultColumnDef } from "../table/PermitSearchResultColumnDef";
import { SearchFields } from "../types/types";
import { IDIRPermitSearchRowActions } from "./IDIRPermitSearchRowActions";
Expand Down Expand Up @@ -82,7 +82,7 @@ export const IDIRPermitSearchResults = memo(
pagination.pageSize,
],
() =>
getDataBySearch(
getPermitDataBySearch(
{
searchByFilter,
searchEntity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,44 @@ export const CompanySearchResultColumnDef: MRT_ColumnDef<CompanyProfile>[] = [
},
{
accessorKey: "clientNumber",
header: "onRouteBC Client Number",
header: "onRouteBC Client No.",
enableSorting: true,
sortingFn: "alphanumeric",
},
{
accessorKey: "mailingAddress",
accessorKey: "mailingAddress.addressLine1",
header: "Company Address",
enableSorting: true,
sortingFn: "alphanumeric",
Cell: (props: { row: any }) => {
const mailingAddress = props.row?.original?.mailingAddress
const country = mailingAddress?.countryCode === 'CA' ? 'Canada' : mailingAddress?.countryCode

return (
<>
{mailingAddress?.addressLine1}<br />
{mailingAddress?.city}<br />
{mailingAddress?.provinceCode}<br />
{country} {mailingAddress?.postalCode}
</>
);
},
},
{
accessorKey: "primaryContact",
accessorKey: "primaryContact.firstName",
header: "Primary Contact",
enableSorting: true,
sortingFn: "alphanumeric",
Cell: (props: { row: any }) => {
const contact = props.row?.original?.primaryContact

return (
<>
{contact?.firstName} {contact?.lastName}<br />
{contact?.email}<br />
{contact?.phone}
</>
);
},
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ export class CompanyController {
const companies: PaginationDto<ReadCompanyDto> =
await this.companyService.findCompanyPaginated(
pageOptionsDto,
legalName,
clientNumber,
legalName?.trim(),
clientNumber?.trim(),
);

return companies;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,19 +302,35 @@ export class CompanyService {
@Query() legalName?: string,
@Query() clientNumber?: string,
): Promise<PaginationDto<ReadCompanyDto>> {
const companies = await this.companyRepository
let companiesQuery = this.companyRepository
.createQueryBuilder('company')
.innerJoinAndSelect('company.mailingAddress', 'mailingAddress')
.innerJoinAndSelect('company.primaryContact', 'primaryContact')
.innerJoinAndSelect('primaryContact.province', 'province')
.innerJoinAndSelect('mailingAddress.province', 'provinceType')
.where('company.legalName LIKE :legalName', {
legalName: `%${legalName}%`,
})
.orWhere('company.clientNumber LIKE :clientNumber', {
clientNumber: `%${clientNumber}%`,
})
.getMany();
.innerJoinAndSelect('mailingAddress.province', 'provinceType');

// Apply conditions based on parameters
companiesQuery = companiesQuery.where('company.companyId IS NOT NULL');

if (legalName) {
companiesQuery = companiesQuery.andWhere(
'company.legalName LIKE :legalName',
{
legalName: `%${legalName}%`,
},
);
}

if (clientNumber) {
companiesQuery = companiesQuery.andWhere(
'company.clientNumber LIKE :clientNumber',
{
clientNumber: `%${clientNumber}%`,
},
);
}

const companies = await companiesQuery.getMany();

const companyData = await this.classMapper.mapArrayAsync(
companies,
Expand Down

0 comments on commit 008b125

Please sign in to comment.