diff --git a/src/controllers/SalaryController.ts b/src/controllers/SalaryController.ts index bd2d8b7..d89e40c 100644 --- a/src/controllers/SalaryController.ts +++ b/src/controllers/SalaryController.ts @@ -17,7 +17,7 @@ import { Salarys, CreateSalary, UpdateSalary } from "../entities/Salarys"; import { PosType } from "../entities/PosType"; import { PosLevel } from "../entities/PosLevel"; import { AppDataSource } from "../database/data-source"; -import { Like, Not } from "typeorm"; +import { Brackets, Like, Not } from "typeorm"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; @@ -73,25 +73,25 @@ export class SalaryController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ระดับของตำแหน่ง ไม่ถูกต้อง"); } - let chk_fields: any - if(salarys.isSpecial){ + let chk_fields: any; + if (salarys.isSpecial) { chk_fields = await this.salaryRepository.findOne({ where: { posTypeId: salarys.posTypeId, posLevelId: salarys.posLevelId, - isSpecial: true + isSpecial: true, }, }); - }else{ + } else { chk_fields = await this.salaryRepository.findOne({ where: { posTypeId: salarys.posTypeId, posLevelId: salarys.posLevelId, - isSpecial: false + isSpecial: false, }, }); } - + if (chk_fields && salarys.isActive) { salarys.isActive = false; } @@ -142,8 +142,8 @@ export class SalaryController extends Controller { ); } - let chk_fields: any - if(chk_Salary.isSpecial){ + let chk_fields: any; + if (chk_Salary.isSpecial) { chk_fields = await this.salaryRepository.find({ where: { posTypeId: requestBody.posTypeId, @@ -153,7 +153,7 @@ export class SalaryController extends Controller { id: Not(id), }, }); - }else{ + } else { chk_fields = await this.salaryRepository.find({ where: { posTypeId: requestBody.posTypeId, @@ -164,9 +164,9 @@ export class SalaryController extends Controller { }, }); } - + if (chk_fields.length > 0 && requestBody.isActive) { - chk_fields.forEach(async (item:any) => { + chk_fields.forEach(async (item: any) => { item.isActive = false; item.lastUpdateUserId = request.user.sub; item.lastUpdateFullName = request.user.name; @@ -278,61 +278,25 @@ export class SalaryController extends Controller { @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword?: string, ) { - const [salary, total] = await this.salaryRepository.findAndCount({ - relations: ["posLevel_", "posType_"], - where: { - name: Like(`%${keyword}%`), - posType_: { - posTypeName: Like(`%${keyword}%`) - }, - posLevel_: { - posLevelName: Like(`%${keyword}%`) - } - }, - order: { - isActive: "DESC", - posType_: { - posTypeRank: "DESC", - }, - posLevel_: { - posLevelRank: "DESC", - }, - }, - ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), - }); - // if (keyword != undefined && keyword !== "") { - // const filteredSalary = salary.filter( - // (x) => - // x.name?.toString().includes(keyword) || - // x.isSpecial?.toString().includes(keyword) || //new 20.02.67 - // x.posLevel_?.posLevelName?.toString().includes(keyword) || - // x.posType_?.posTypeName?.toString().includes(keyword) || - // x.isActive?.toString().includes(keyword) || - // x.date?.toString().includes(keyword) || - // x.startDate?.toString().includes(keyword) || - // x.endDate?.toString().includes(keyword) || - // x.details?.toString().includes(keyword), - // ); + const [salary, total] = await AppDataSource.getRepository(Salarys) + .createQueryBuilder("salary") + .leftJoinAndSelect("salary.posType", "posType") + .leftJoinAndSelect("salary.posLevel", "posLevel") + .andWhere( + new Brackets((qb) => { + qb.orWhere("salary.name LIKE :keyword", { keyword: `%${keyword}%` }) + .orWhere("posType.posTypeName LIKE :keyword", { keyword: `%${keyword}%` }) + .orWhere("posLevel.posLevelName LIKE :keyword", { keyword: `%${keyword}%` }); + }), + ) + .orderBy("salary.isActive", "DESC") + .addOrderBy("posType.posTypeRank", "DESC") + .addOrderBy("posLevel.posLevelRank", "DESC") + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); - // const formattedData = filteredSalary.map((item) => ({ - // id: item.id, - // name: item.name, - // isSpecial: item.isSpecial, - // posTypeId: item.posType_?.id, - // posType: item.posType_?.posTypeName, - // posLevelId: item.posLevel_?.id, - // posLevel: item.posLevel_?.posLevelName, - // isActive: item.isActive, - // date: item.date, - // startDate: item.startDate, - // endDate: item.endDate, - // details: item.details, - // })); - // const slicedData = formattedData.slice((page - 1) * pageSize, page * pageSize); - // return new HttpSuccess({ data: slicedData, total: formattedData.length }); - // } - - const formattedData = salary.map((item) => ({ + const _salary = salary.map((item) => ({ id: item.id, name: item.name, isSpecial: item.isSpecial, @@ -346,7 +310,8 @@ export class SalaryController extends Controller { endDate: item.endDate, details: item.details, })); - return new HttpSuccess({ data: formattedData, total }); + + return new HttpSuccess({ data: _salary, total }); } /** diff --git a/src/controllers/SalaryEmployeeController.ts b/src/controllers/SalaryEmployeeController.ts index cdd89fe..5213ed3 100644 --- a/src/controllers/SalaryEmployeeController.ts +++ b/src/controllers/SalaryEmployeeController.ts @@ -19,12 +19,13 @@ import { UpdateSalaryEmployee, } from "../entities/SalaryEmployee"; import { AppDataSource } from "../database/data-source"; -import { Not, Like } from "typeorm"; +import { Not, Like, Brackets } from "typeorm"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; import { SalaryRankEmployee } from "../entities/SalaryRankEmployee"; import { randomUUID } from "crypto"; +import { Salarys } from "../entities/Salarys"; @Route("api/v1/salary/employee") @Tags("SalaryEmployee") @@ -216,22 +217,25 @@ export class SalaryEmployeeController extends Controller { @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword?: string, ) { - let whereClause: any = {}; - if (keyword != undefined && keyword !== "") { - whereClause = { - where: [{ name: Like(`%${keyword}%`) }, { group: Like(`%${keyword}%`) }], - }; - } - const [salary, total] = await this.salaryEmployeeRepository.findAndCount({ - ...whereClause, - order: { - isActive: "DESC", - group: "ASC", - }, - ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), - }); + const [salaryEmployee, total] = await AppDataSource.getRepository(SalaryEmployee) + .createQueryBuilder("salaryEmployee") + .leftJoinAndSelect("salaryEmployee.posType", "posType") + .leftJoinAndSelect("salaryEmployee.posLevel", "posLevel") + .andWhere( + new Brackets((qb) => { + qb.orWhere("salaryEmployee.name LIKE :keyword", { keyword: `%${keyword}%` }).orWhere( + "salaryEmployee.group LIKE :keyword", + { keyword: `%${keyword}%` }, + ); + }), + ) + .orderBy("salaryEmployee.isActive", "DESC") + .addOrderBy("salaryEmployee.group", "ASC") + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); - const formattedData = salary.map((item) => ({ + const _salaryEmployee = salaryEmployee.map((item) => ({ id: item.id, name: item.name, group: item.group, @@ -241,7 +245,7 @@ export class SalaryEmployeeController extends Controller { endDate: item.endDate, details: item.details, })); - return new HttpSuccess({ data: formattedData, total }); + return new HttpSuccess({ data: _salaryEmployee, total }); } /** diff --git a/src/controllers/SalaryRankController.ts b/src/controllers/SalaryRankController.ts index 283c411..5ae8769 100644 --- a/src/controllers/SalaryRankController.ts +++ b/src/controllers/SalaryRankController.ts @@ -14,7 +14,7 @@ import { Get, Query, } from "tsoa"; -import { Like } from "typeorm"; +import { Brackets, Like } from "typeorm"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; import HttpStatusCode from "../interfaces/http-status"; @@ -122,56 +122,30 @@ export class SalaryRanksController extends Controller { @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword?: string, ) { - let whereClause: any = {}; - if (keyword != undefined && keyword !== "") { - whereClause = { - where: [ - { salary: Like(`%${keyword}%`) }, - { salaryHalf: Like(`%${keyword}%`) }, - { salaryHalfSpecial: Like(`%${keyword}%`) }, - { salaryFull: Like(`%${keyword}%`) }, - { salaryFullSpecial: Like(`%${keyword}%`) }, - { salaryFullHalf: Like(`%${keyword}%`) }, - { salaryFullHalfSpecial: Like(`%${keyword}%`) }, - ], - }; - } - const [salaryRank, total] = await this.salaryRankRepository.findAndCount({ - ...whereClause, - where: { - salaryId: id, - }, - select: [ - "id", - "salary", - "salaryHalf", - "salaryHalfSpecial", - "salaryFull", - "salaryFullSpecial", - "salaryFullHalf", - "salaryFullHalfSpecial", - "isNext", - ], - order: { - salary: "DESC", - salaryHalf: "DESC", - }, - ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), - }); - // if (keyword != undefined && keyword !== "") { - // const filteredSalaryRank = salaryRank.filter( - // (x) => - // x.salary?.toString().includes(keyword) || - // x.salaryHalf?.toString().includes(keyword) || - // x.salaryHalfSpecial?.toString().includes(keyword) || - // x.salaryFull?.toString().includes(keyword) || - // x.salaryFullSpecial?.toString().includes(keyword) || - // x.salaryFullHalf?.toString().includes(keyword) || - // x.salaryFullHalfSpecial?.toString().includes(keyword), - // ); - // const slicedData = filteredSalaryRank.slice((page - 1) * pageSize, page * pageSize); - // return new HttpSuccess({ data: slicedData, total: filteredSalaryRank.length }); - // } + const [salaryRank, total] = await AppDataSource.getRepository(SalaryRanks) + .createQueryBuilder("salaryRank") + .andWhere( + new Brackets((qb) => { + qb.andWhere(`salaryRank.salaryId LIKE :id`, { id: id }).andWhere( + new Brackets((qb) => { + qb.orWhere("salaryRank.salary LIKE :keyword", { keyword: `%${keyword}%` }) + .orWhere("salaryRank.salaryHalf LIKE :keyword", { keyword: `%${keyword}%` }) + .orWhere("salaryRank.salaryHalfSpecial LIKE :keyword", { keyword: `%${keyword}%` }) + .orWhere("salaryRank.salaryFull LIKE :keyword", { keyword: `%${keyword}%` }) + .orWhere("salaryRank.salaryFullSpecial LIKE :keyword", { keyword: `%${keyword}%` }) + .orWhere("salaryRank.salaryFullHalf LIKE :keyword", { keyword: `%${keyword}%` }) + .orWhere("salaryRank.salaryFullHalfSpecial LIKE :keyword", { + keyword: `%${keyword}%`, + }); + }), + ); + }), + ) + .orderBy("salaryRank.salary", "DESC") + .addOrderBy("salaryRank.salaryHalf", "DESC") + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); return new HttpSuccess({ data: salaryRank, total }); } diff --git a/src/controllers/SalaryRankEmployeeController.ts b/src/controllers/SalaryRankEmployeeController.ts index 55db17c..ca51580 100644 --- a/src/controllers/SalaryRankEmployeeController.ts +++ b/src/controllers/SalaryRankEmployeeController.ts @@ -14,7 +14,7 @@ import { Get, Query, } from "tsoa"; -import { Not, Like } from "typeorm"; +import { Not, Like, Brackets } from "typeorm"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; import HttpStatusCode from "../interfaces/http-status"; @@ -143,37 +143,26 @@ export class SalaryRankEmployeeController extends Controller { @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword?: string, ) { - let whereClause: any = {}; - if (keyword != undefined && keyword !== "") { - whereClause = { - where: [ - { step: Like(`%${keyword}%`) }, - { salaryMonth: Like(`%${keyword}%`) }, - { salaryDay: Like(`%${keyword}%`) }, - ], - }; - } - const [salaryRankEmployee, total] = await this.salaryRankEmployeeRepository.findAndCount({ - ...whereClause, - where: { - salaryEmployeeId: id, - }, - select: ["id", "step", "salaryMonth", "salaryDay"], - order: { - step: "ASC", - }, - ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), - }); - // if (keyword != undefined && keyword !== "") { - // const filteredSalaryRankEmployee = salaryRankEmployee.filter( - // (x) => - // x.step?.toString().includes(keyword) || - // x.salaryMonth?.toString().includes(keyword) || - // x.salaryDay?.toString().includes(keyword), - // ); - // const slicedData = filteredSalaryRankEmployee.slice((page - 1) * pageSize, page * pageSize); - // return new HttpSuccess({ data: slicedData, total: filteredSalaryRankEmployee.length }); - // } + const [salaryRankEmployee, total] = await AppDataSource.getRepository(SalaryRankEmployee) + .createQueryBuilder("salaryRankEmployee") + .andWhere( + new Brackets((qb) => { + qb.andWhere(`salaryRankEmployee.salaryEmployeeId LIKE :id`, { id: id }).andWhere( + new Brackets((qb) => { + qb.orWhere("salaryRankEmployee.step LIKE :keyword", { keyword: `%${keyword}%` }) + .orWhere("salaryRankEmployee.salaryMonth LIKE :keyword", { + keyword: `%${keyword}%`, + }) + .orWhere("salaryRankEmployee.salaryDay LIKE :keyword", { keyword: `%${keyword}%` }); + }), + ); + }), + ) + .orderBy("salaryRankEmployee.salary", "DESC") + .addOrderBy("salaryRankEmployee.salaryHalf", "DESC") + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); return new HttpSuccess({ data: salaryRankEmployee, total }); }