ปรับ paging

This commit is contained in:
Bright 2024-03-14 17:59:49 +07:00
parent 5682fb8424
commit 0d28379d68

View file

@ -19,7 +19,7 @@ import {
UpdateSalaryEmployee, UpdateSalaryEmployee,
} from "../entities/SalaryEmployee"; } from "../entities/SalaryEmployee";
import { AppDataSource } from "../database/data-source"; import { AppDataSource } from "../database/data-source";
import { Not } from "typeorm"; import { Not, Like } from "typeorm";
import HttpSuccess from "../interfaces/http-success"; import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status"; import HttpStatusCode from "../interfaces/http-status";
@ -216,38 +216,53 @@ export class SalaryEmployeeController extends Controller {
@Query("pageSize") pageSize: number = 10, @Query("pageSize") pageSize: number = 10,
@Query("keyword") keyword?: string, @Query("keyword") keyword?: string,
) { ) {
let whereClause : any = {};
if (keyword != undefined && keyword !== "") {
whereClause = {
where: [
{ name: Like(`%${keyword}%`) },
{ group: Like(`%${keyword}%`) },
{ isActive: Like(`%${keyword}%`) },
{ date: Like(`%${keyword}%`) },
{ startDate: Like(`%${keyword}%`) },
{ endDate: Like(`%${keyword}%`) },
{ details: Like(`%${keyword}%`) },
]
};
}
const [salary, total] = await this.salaryEmployeeRepository.findAndCount({ const [salary, total] = await this.salaryEmployeeRepository.findAndCount({
...whereClause,
order: { order: {
isActive: "DESC", isActive: "DESC",
group: "ASC", group: "ASC",
}, },
...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }), ...(keyword ? {} : { skip: (page - 1) * pageSize, take: pageSize }),
}); });
if (keyword != undefined && keyword !== "") { // if (keyword != undefined && keyword !== "") {
const filteredSalary = salary.filter( // const filteredSalary = salary.filter(
(x) => // (x) =>
x.name?.toString().includes(keyword) || // x.name?.toString().includes(keyword) ||
x.group?.toString().includes(keyword) || // x.group?.toString().includes(keyword) ||
x.isActive?.toString().includes(keyword) || // x.isActive?.toString().includes(keyword) ||
x.date?.toString().includes(keyword) || // x.date?.toString().includes(keyword) ||
x.startDate?.toString().includes(keyword) || // x.startDate?.toString().includes(keyword) ||
x.endDate?.toString().includes(keyword) || // x.endDate?.toString().includes(keyword) ||
x.details?.toString().includes(keyword), // x.details?.toString().includes(keyword),
); // );
const formattedData = filteredSalary.map((item) => ({ // const formattedData = filteredSalary.map((item) => ({
id: item.id, // id: item.id,
name: item.name, // name: item.name,
group: item.group, // group: item.group,
isActive: item.isActive, // isActive: item.isActive,
date: item.date, // date: item.date,
startDate: item.startDate, // startDate: item.startDate,
endDate: item.endDate, // endDate: item.endDate,
details: item.details, // details: item.details,
})); // }));
const slicedData = formattedData.slice((page - 1) * pageSize, page * pageSize); // const slicedData = formattedData.slice((page - 1) * pageSize, page * pageSize);
return new HttpSuccess({ data: slicedData, total: formattedData.length }); // return new HttpSuccess({ data: slicedData, total: formattedData.length });
} // }
const formattedData = salary.map((item) => ({ const formattedData = salary.map((item) => ({
id: item.id, id: item.id,