เพิ่ม paginate และ keyword org_065

This commit is contained in:
AdisakKanthawilang 2024-02-22 16:32:13 +07:00
parent 6c843f649f
commit 4d8fb8e5a3

View file

@ -12,6 +12,7 @@ import {
SuccessResponse, SuccessResponse,
Response, Response,
Get, Get,
Query,
} from "tsoa"; } from "tsoa";
import { AppDataSource } from "../database/data-source"; import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success"; import HttpSuccess from "../interfaces/http-success";
@ -254,8 +255,12 @@ export class ProfileController extends Controller {
* *
*/ */
@Get() @Get()
async listProfile() { async listProfile(
const profile = await this.profileRepository.find({ @Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
@Query("keyword") keyword?: string,
) {
const [profile, total] = await this.profileRepository.findAndCount({
select: [ select: [
"id", "id",
"prefix", "prefix",
@ -267,16 +272,54 @@ export class ProfileController extends Controller {
"posTypeId", "posTypeId",
], ],
order: { createdAt: "ASC" }, order: { createdAt: "ASC" },
skip: (page - 1) * pageSize,
take: pageSize,
}); });
if (keyword != undefined && keyword !== "") {
const formattedKeyword = keyword.toLowerCase().replace(/\s+/g, "");
const filteredProfile = profile.filter(
(x) =>
(
x.prefix +
x.firstName +
x.lastName
)
.replace(/\s+/g, "")
.includes(formattedKeyword) ||
x.citizenId?.toString().includes(keyword) ||
x.position?.toString().includes(keyword)
);
const formattedData = filteredProfile.map((item) => ({
id: item.id,
prefix: item.prefix,
firstName: item.firstName,
lastName: item.lastName,
citizenId: item.citizenId,
position: item.position,
posLevelId: item.posLevelId,
posTypeId: item.posTypeId
}));
return new HttpSuccess({ data: formattedData, total: formattedData.length });
}
if (!profile) { if (!profile) {
return new HttpSuccess([]); return new HttpSuccess([]);
} }
try {
return new HttpSuccess(profile); const formattedData = profile.map((item) => ({
} catch (error) { id: item.id,
return error; prefix: item.prefix,
} firstName: item.firstName,
lastName: item.lastName,
citizenId: item.citizenId,
position: item.position,
posLevelId: item.posLevelId,
posTypeId: item.posTypeId
}));
return new HttpSuccess({ data: formattedData, total });
} }
/** /**