From df0aa633db66919f47dc863a71043ae531eb0135 Mon Sep 17 00:00:00 2001 From: Bright Date: Fri, 19 Jul 2024 17:01:17 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B8=9B=E0=B8=A3=E0=B8=B1=E0=B8=9A=E0=B9=80?= =?UTF-8?q?=E0=B8=9E=E0=B8=B4=E0=B9=88=E0=B8=A1=20api=20=E0=B8=84=E0=B8=B3?= =?UTF-8?q?=E0=B8=A3=E0=B9=89=E0=B8=AD=E0=B8=87=E0=B8=82=E0=B8=AD=E0=B9=81?= =?UTF-8?q?=E0=B8=81=E0=B9=89=E0=B9=84=E0=B8=82=E0=B8=82=E0=B9=89=E0=B8=AD?= =?UTF-8?q?=E0=B8=A1=E0=B8=B9=E0=B8=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/ProfileEditController.ts | 74 ++++++++++++++++-- .../ProfileEditEmployeeController.ts | 76 +++++++++++++++++-- src/entities/ProfileEdit.ts | 2 + 3 files changed, 139 insertions(+), 13 deletions(-) diff --git a/src/controllers/ProfileEditController.ts b/src/controllers/ProfileEditController.ts index 1e9d8753..450d8bd5 100644 --- a/src/controllers/ProfileEditController.ts +++ b/src/controllers/ProfileEditController.ts @@ -19,7 +19,7 @@ import { AppDataSource } from "../database/data-source"; import { Profile } from "../entities/Profile"; import { CreateProfileEdit, EditProfileEdit, ProfileEdit } from "../entities/ProfileEdit"; import { RequestWithUser } from "../middlewares/user"; -import { IsNull, Not } from "typeorm"; +import { IsNull, Not, Brackets } from "typeorm"; @Route("api/v1/org/profile/edit") @Tags("ProfileEdit") @@ -34,6 +34,7 @@ export class ProfileEditController extends Controller { @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword: string = "", + @Query("status") status: string = "", ) { const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub }); if (!profile) { @@ -43,9 +44,32 @@ export class ProfileEditController extends Controller { let [getProfileEdit, total] = await AppDataSource.getRepository(ProfileEdit) .createQueryBuilder("ProfileEdit") .leftJoinAndSelect("ProfileEdit.profile", "profile") - .where({ - profileId: profile.id, + .where((qb) => { + if (status != "" && status != null) { + qb.andWhere("ProfileEdit.status = :status", { status: status }); + } + qb.andWhere("ProfileEdit.profileId = :profileId", { profileId: profile.id }); }) + .andWhere( + new Brackets((qb) => { + qb.where( + keyword != "" && keyword != null + ? "ProfileEdit.topic LIKE :keyword" + : "1=1", + { + keyword: `%${keyword}%`, + }, + ) + .orWhere( + keyword != "" && keyword != null + ? "ProfileEdit.detail LIKE :keyword" + : "1=1", + { + keyword: `%${keyword}%`, + }, + ) + }), + ) .orderBy("ProfileEdit.createdAt", "ASC") .skip((page - 1) * pageSize) .take(pageSize) @@ -77,13 +101,37 @@ export class ProfileEditController extends Controller { @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword: string = "", + @Query("status") status: string = "", ) { let [getProfileEdit, total] = await AppDataSource.getRepository(ProfileEdit) .createQueryBuilder("ProfileEdit") .leftJoinAndSelect("ProfileEdit.profile", "profile") - .where({ - profileId: Not(IsNull()), + .where((qb) => { + if (status != "" && status != null) { + qb.andWhere("ProfileEdit.status = :status", { status: status }); + } + qb.andWhere("ProfileEdit.profileId IS NOT NULL"); }) + .andWhere( + new Brackets((qb) => { + qb.where( + keyword != "" && keyword != null + ? "ProfileEdit.topic LIKE :keyword" + : "1=1", + { + keyword: `%${keyword}%`, + }, + ) + .orWhere( + keyword != "" && keyword != null + ? "ProfileEdit.detail LIKE :keyword" + : "1=1", + { + keyword: `%${keyword}%`, + }, + ) + }), + ) .orderBy("ProfileEdit.createdAt", "ASC") .skip((page - 1) * pageSize) .take(pageSize) @@ -108,6 +156,17 @@ export class ProfileEditController extends Controller { return new HttpSuccess({ data: _data, total: total }); } + @Get("{Id}") + public async detailProfileByIdEdit(@Path() Id: string) { + const getProfileEdit = await this.profileEditRepo.findOne({ + where: { id: Id }, + }); + if (!getProfileEdit) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + return new HttpSuccess(getProfileEdit); + } + @Get("{profileId}") public async detailProfileEdit(@Path() profileId: string) { const getProfileEdit = await this.profileEditRepo.findOne({ @@ -165,7 +224,10 @@ export class ProfileEditController extends Controller { @Request() req: RequestWithUser, @Path() editId: string, ) { - const record = await this.profileEditRepo.findOneBy({ id: editId }); + // const record = await this.profileEditRepo.findOneBy({ id: editId }); + const record = await this.profileEditRepo.findOne({ + where: { id: editId } + }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); Object.assign(record, requestBody); diff --git a/src/controllers/ProfileEditEmployeeController.ts b/src/controllers/ProfileEditEmployeeController.ts index 8a1e010e..36668ed5 100644 --- a/src/controllers/ProfileEditEmployeeController.ts +++ b/src/controllers/ProfileEditEmployeeController.ts @@ -23,7 +23,7 @@ import { } from "../entities/ProfileEdit"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import { RequestWithUser } from "../middlewares/user"; -import { IsNull, Not } from "typeorm"; +import { IsNull, Not, Brackets } from "typeorm"; @Route("api/v1/org/profile-employee/edit") @Tags("ProfileEmployeeEdit") @@ -38,6 +38,7 @@ export class ProfileEditEmployeeController extends Controller { @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword: string = "", + @Query("status") status: string = "", ) { const profile = await this.profileEmployeeRepo.findOneBy({ keycloak: request.user.sub }); if (!profile) { @@ -46,10 +47,33 @@ export class ProfileEditEmployeeController extends Controller { let [getProfileEdit, total] = await AppDataSource.getRepository(ProfileEdit) .createQueryBuilder("ProfileEdit") - .leftJoinAndSelect("ProfileEdit.profileEmployeeId", "profileEmployeeId") - .where({ - profileEmployeeId: profile.id, + .leftJoinAndSelect("ProfileEdit.profileEmployee", "profileEmployee") + .where((qb) => { + if (status != "" && status != null) { + qb.andWhere("ProfileEdit.status = :status", { status: status }); + } + qb.andWhere("ProfileEdit.profileEmployeeId = :profileEmployeeId", { profileEmployeeId: profile.id }); }) + .andWhere( + new Brackets((qb) => { + qb.where( + keyword != "" && keyword != null + ? "ProfileEdit.topic LIKE :keyword" + : "1=1", + { + keyword: `%${keyword}%`, + }, + ) + .orWhere( + keyword != "" && keyword != null + ? "ProfileEdit.detail LIKE :keyword" + : "1=1", + { + keyword: `%${keyword}%`, + }, + ) + }), + ) .orderBy("ProfileEdit.createdAt", "ASC") .skip((page - 1) * pageSize) .take(pageSize) @@ -81,13 +105,37 @@ export class ProfileEditEmployeeController extends Controller { @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword: string = "", + @Query("status") status: string = "", ) { let [getProfileEdit, total] = await AppDataSource.getRepository(ProfileEdit) .createQueryBuilder("ProfileEdit") .leftJoinAndSelect("ProfileEdit.profileEmployee", "profileEmployee") - .where({ - profileEmployeeId: Not(IsNull()), + .where((qb) => { + if (status != "" && status != null) { + qb.andWhere("ProfileEdit.status = :status", { status: status }); + } + qb.andWhere("ProfileEdit.profileEmployeeId IS NOT NULL"); }) + .andWhere( + new Brackets((qb) => { + qb.where( + keyword != "" && keyword != null + ? "ProfileEdit.topic LIKE :keyword" + : "1=1", + { + keyword: `%${keyword}%`, + }, + ) + .orWhere( + keyword != "" && keyword != null + ? "ProfileEdit.detail LIKE :keyword" + : "1=1", + { + keyword: `%${keyword}%`, + }, + ) + }), + ) .orderBy("ProfileEdit.createdAt", "ASC") .skip((page - 1) * pageSize) .take(pageSize) @@ -113,6 +161,17 @@ export class ProfileEditEmployeeController extends Controller { return new HttpSuccess({ data: _data, total: total }); } + @Get("{Id}") + public async detailProfileByIdEdit(@Path() Id: string) { + const getProfileEdit = await this.profileEditRepository.findOne({ + where: { id: Id }, + }); + if (!getProfileEdit) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + return new HttpSuccess(getProfileEdit); + } + @Get("{profileEmployeeId}") public async detailProfileEdit(@Path() profileEmployeeId: string) { const getProfileEdit = await this.profileEditRepository.findOne({ @@ -172,7 +231,10 @@ export class ProfileEditEmployeeController extends Controller { @Request() req: RequestWithUser, @Path() editId: string, ) { - const record = await this.profileEditRepository.findOneBy({ id: editId }); + // const record = await this.profileEditRepository.findOneBy({ id: editId }); + const record = await this.profileEditRepository.findOne({ + where: { id: editId } + }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); Object.assign(record, requestBody); diff --git a/src/entities/ProfileEdit.ts b/src/entities/ProfileEdit.ts index b3980a2c..f15f4842 100644 --- a/src/entities/ProfileEdit.ts +++ b/src/entities/ProfileEdit.ts @@ -61,6 +61,7 @@ export class ProfileEdit extends EntityBase { } export class CreateProfileEdit { + profileId: string; topic: string | null; detail: string | null; } @@ -73,6 +74,7 @@ export class EditProfileEdit { } export class CreateProfileEmployeeEdit { + profileEmployeeId: string; topic: string | null; detail: string | null; }