ปรับเพิ่ม api คำร้องขอแก้ไขข้อมูล

This commit is contained in:
Bright 2024-07-19 17:01:17 +07:00
parent 92c6c01438
commit df0aa633db
3 changed files with 139 additions and 13 deletions

View file

@ -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);

View file

@ -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);

View file

@ -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;
}