Skip to content

Commit

Permalink
feat/backend: Created query to display only the requiered fields and …
Browse files Browse the repository at this point in the history
…how FOM requested them
  • Loading branch information
mamartinezmejia committed Jul 13, 2022
1 parent ef2b364 commit ea384c6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ import { ClientPublicService } from '../services/clientPublic.service';
export class ClientPublicController {
constructor(private readonly clientPublicService: ClientPublicService) {}

@Get('/clientNumber')
findByClientNumber(@Query('clientNumber') clientNumber: string) {
return this.clientPublicService.findByClientNumber(clientNumber);
}

@Get('/findBy')
@ApiQuery({
name: 'clientNumber',
Expand Down
1 change: 1 addition & 0 deletions backend/src/clientpublic/entities/clientPublic.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ export class ClientPublicEntity extends BaseEntity {
})
@ViewColumn({ name: 'CLIENT_TYPE_CODE' })
clientTypeCode: string;

}
63 changes: 34 additions & 29 deletions backend/src/clientpublic/services/clientPublic.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ClientPublicEntity } from '../entities/clientPublic.entity';
import { ClientPublic } from '../entities/clientPublic.interface';
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { ClientPublicEntity } from "../entities/clientPublic.entity";
import { ClientPublic } from "../entities/clientPublic.interface";

@Injectable()
export class ClientPublicService {
Expand All @@ -11,51 +11,56 @@ export class ClientPublicService {
private clientPublicRepository: Repository<ClientPublicEntity>,
) {}

findByClientNumber(clientNumber: string): Promise<ClientPublic[]> {
return this.clientPublicRepository.find({
where: { clientNumber: clientNumber },
});
}

// the full non individual client list is too long, currently only set to return 10
findAllNonIndividualClients(): Promise<ClientPublic[]> {
return this.clientPublicRepository
.createQueryBuilder('V_CLIENT_PUBLIC')
.where('V_CLIENT_PUBLIC.CLIENT_TYPE_CODE != :clientTypeCode', {
clientTypeCode: 'I',
.createQueryBuilder("V_CLIENT_PUBLIC")
.where("V_CLIENT_PUBLIC.CLIENT_TYPE_CODE != :clientTypeCode", {
clientTypeCode: "I",
})
.take(10)
.getMany();
}

findBy(clientNumber?: string, clientName?: string): Promise<ClientPublic[]> {
let sqlWhereStr = '1 = 1';
findBy(clientNumber: string, clientName: string): Promise<ClientPublic[]> {
let sqlWhereStr = "1 = 1";

if (clientNumber) {
sqlWhereStr =
sqlWhereStr + ' AND ' + 'C.CLIENT_NUMBER LIKE :clientNumber';
sqlWhereStr = sqlWhereStr + " AND C.CLIENT_NUMBER LIKE :clientNumber";
}

if (clientName) {
sqlWhereStr =
sqlWhereStr +
' AND ' +
'(LOWER(C.CLIENT_NAME) LIKE LOWER(:clientName) OR ' +
' LOWER(C.LEGAL_FIRST_NAME) LIKE LOWER(:clientName) OR ' +
' LOWER(C.LEGAL_MIDDLE_NAME) LIKE LOWER(:clientName)' +
')';
" AND " +
"(LOWER(C.CLIENT_NAME) LIKE LOWER(:clientName) OR " +
" LOWER(C.LEGAL_FIRST_NAME) LIKE LOWER(:clientName) OR " +
" LOWER(C.LEGAL_MIDDLE_NAME) LIKE LOWER(:clientName)" +
")";
}

//TODO: Put harcoded values in a different place
return this.clientPublicRepository
.createQueryBuilder()
.select('C')
.from(ClientPublicEntity, 'C')
.select("C.CLIENT_NUMBER", "Client Number")
.addSelect(
"CASE WHEN C.CLIENT_TYPE_CODE = 'I' " +
"THEN C.LEGAL_FIRST_NAME || ' ' || C.LEGAL_MIDDLE_NAME " +
"ELSE C.CLIENT_NAME END",
"Client Name",
)
.addSelect("C.CLIENT_TYPE_CODE", "Client Type Code")
.addSelect(
"CASE WHEN C.CLIENT_STATUS_CODE = 'ACT' " +
"THEN 'true' " +
"ELSE 'false' END", "Client Active")
.from(ClientPublicEntity, "C")
.where(sqlWhereStr, {
clientNumber: '%' + clientNumber,
clientName: clientName + '%',
clientNumber: "%" + clientNumber,
clientName: clientName + "%",
})
.take(10)
.orderBy('C.LEGAL_FIRST_NAME')
.getMany();
.orderBy("C.LEGAL_FIRST_NAME")
.getRawOne();
}
}

0 comments on commit ea384c6

Please sign in to comment.