fixing exception error
This commit is contained in:
parent
1c9f276cfb
commit
6585520af7
5 changed files with 1343 additions and 1085 deletions
|
|
@ -46,36 +46,45 @@ export class DirectorController {
|
|||
@Query("pageSize") pageSize: number = 10,
|
||||
@Query("keyword") keyword?: string,
|
||||
) {
|
||||
await new permission().PermissionList(request, "SYS_EVA_INFO");
|
||||
const directors = await AppDataSource.getRepository(Director)
|
||||
.createQueryBuilder("director")
|
||||
.andWhere(
|
||||
new Brackets((qb) => {
|
||||
qb.where(
|
||||
keyword != null && keyword != ""
|
||||
? "CONCAT(director.prefix, director.firstName, ' ', director.lastName) LIKE :keyword"
|
||||
: "1=1",
|
||||
{
|
||||
keyword: `%${keyword}%`,
|
||||
},
|
||||
)
|
||||
.orWhere(keyword != null && keyword != "" ? "director.position LIKE :keyword" : "1=1", {
|
||||
keyword: `%${keyword}%`,
|
||||
})
|
||||
.orWhere(keyword != null && keyword != "" ? "director.email LIKE :keyword" : "1=1", {
|
||||
keyword: `%${keyword}%`,
|
||||
})
|
||||
.orWhere(keyword != null && keyword != "" ? "director.phone LIKE :keyword" : "1=1", {
|
||||
keyword: `%${keyword}%`,
|
||||
});
|
||||
}),
|
||||
)
|
||||
.orderBy("director.createdAt", "DESC")
|
||||
.skip((page - 1) * pageSize)
|
||||
.take(pageSize)
|
||||
.getMany();
|
||||
try {
|
||||
await new permission().PermissionList(request, "SYS_EVA_INFO");
|
||||
const directors = await AppDataSource.getRepository(Director)
|
||||
.createQueryBuilder("director")
|
||||
.andWhere(
|
||||
new Brackets((qb) => {
|
||||
qb.where(
|
||||
keyword != null && keyword != ""
|
||||
? "CONCAT(director.prefix, director.firstName, ' ', director.lastName) LIKE :keyword"
|
||||
: "1=1",
|
||||
{
|
||||
keyword: `%${keyword}%`,
|
||||
},
|
||||
)
|
||||
.orWhere(
|
||||
keyword != null && keyword != "" ? "director.position LIKE :keyword" : "1=1",
|
||||
{
|
||||
keyword: `%${keyword}%`,
|
||||
},
|
||||
)
|
||||
.orWhere(keyword != null && keyword != "" ? "director.email LIKE :keyword" : "1=1", {
|
||||
keyword: `%${keyword}%`,
|
||||
})
|
||||
.orWhere(keyword != null && keyword != "" ? "director.phone LIKE :keyword" : "1=1", {
|
||||
keyword: `%${keyword}%`,
|
||||
});
|
||||
}),
|
||||
)
|
||||
.orderBy("director.createdAt", "DESC")
|
||||
.skip((page - 1) * pageSize)
|
||||
.take(pageSize)
|
||||
.getMany();
|
||||
|
||||
return new HttpSuccess(directors);
|
||||
return new HttpSuccess(directors);
|
||||
} catch (error: any) {
|
||||
if (error instanceof HttpError) {
|
||||
throw error;
|
||||
} else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error);
|
||||
}
|
||||
}
|
||||
|
||||
@Get("admin")
|
||||
|
|
@ -85,34 +94,43 @@ export class DirectorController {
|
|||
@Query("pageSize") pageSize: number = 10,
|
||||
@Query("keyword") keyword?: string,
|
||||
) {
|
||||
const directors = await AppDataSource.getRepository(Director)
|
||||
.createQueryBuilder("director")
|
||||
.andWhere(
|
||||
new Brackets((qb) => {
|
||||
qb.where(
|
||||
keyword != null && keyword != ""
|
||||
? "CONCAT(director.prefix, director.firstName, ' ', director.lastName) LIKE :keyword"
|
||||
: "1=1",
|
||||
{
|
||||
keyword: `%${keyword}%`,
|
||||
},
|
||||
)
|
||||
.orWhere(keyword != null && keyword != "" ? "director.position LIKE :keyword" : "1=1", {
|
||||
keyword: `%${keyword}%`,
|
||||
})
|
||||
.orWhere(keyword != null && keyword != "" ? "director.email LIKE :keyword" : "1=1", {
|
||||
keyword: `%${keyword}%`,
|
||||
})
|
||||
.orWhere(keyword != null && keyword != "" ? "director.phone LIKE :keyword" : "1=1", {
|
||||
keyword: `%${keyword}%`,
|
||||
});
|
||||
}),
|
||||
)
|
||||
.orderBy("director.createdAt", "DESC")
|
||||
.skip((page - 1) * pageSize)
|
||||
.take(pageSize)
|
||||
.getMany();
|
||||
return new HttpSuccess(directors);
|
||||
try {
|
||||
const directors = await AppDataSource.getRepository(Director)
|
||||
.createQueryBuilder("director")
|
||||
.andWhere(
|
||||
new Brackets((qb) => {
|
||||
qb.where(
|
||||
keyword != null && keyword != ""
|
||||
? "CONCAT(director.prefix, director.firstName, ' ', director.lastName) LIKE :keyword"
|
||||
: "1=1",
|
||||
{
|
||||
keyword: `%${keyword}%`,
|
||||
},
|
||||
)
|
||||
.orWhere(
|
||||
keyword != null && keyword != "" ? "director.position LIKE :keyword" : "1=1",
|
||||
{
|
||||
keyword: `%${keyword}%`,
|
||||
},
|
||||
)
|
||||
.orWhere(keyword != null && keyword != "" ? "director.email LIKE :keyword" : "1=1", {
|
||||
keyword: `%${keyword}%`,
|
||||
})
|
||||
.orWhere(keyword != null && keyword != "" ? "director.phone LIKE :keyword" : "1=1", {
|
||||
keyword: `%${keyword}%`,
|
||||
});
|
||||
}),
|
||||
)
|
||||
.orderBy("director.createdAt", "DESC")
|
||||
.skip((page - 1) * pageSize)
|
||||
.take(pageSize)
|
||||
.getMany();
|
||||
return new HttpSuccess(directors);
|
||||
} catch (error: any) {
|
||||
if (error instanceof HttpError) {
|
||||
throw error;
|
||||
} else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -123,13 +141,19 @@ export class DirectorController {
|
|||
*/
|
||||
@Get("{id}")
|
||||
async one(@Path() id: string, @Request() request: RequestWithUser) {
|
||||
let _workflow = await new permission().Workflow(request, id, "SYS_EVA_INFO");
|
||||
if (_workflow == false) await new permission().PermissionGet(request, "SYS_EVA_INFO");
|
||||
const director = await this.directorRepository.findOne({ where: { id } });
|
||||
if (!director) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
|
||||
try {
|
||||
let _workflow = await new permission().Workflow(request, id, "SYS_EVA_INFO");
|
||||
if (_workflow == false) await new permission().PermissionGet(request, "SYS_EVA_INFO");
|
||||
const director = await this.directorRepository.findOne({ where: { id } });
|
||||
if (!director) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
|
||||
}
|
||||
return new HttpSuccess(director);
|
||||
} catch (error: any) {
|
||||
if (error instanceof HttpError) {
|
||||
throw error;
|
||||
} else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error);
|
||||
}
|
||||
return new HttpSuccess(director);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -140,27 +164,33 @@ export class DirectorController {
|
|||
*/
|
||||
@Post()
|
||||
async save(@Body() requestBody: CreateDirector, @Request() request: RequestWithUser) {
|
||||
await new permission().PermissionCreate(request, "SYS_EVA_INFO");
|
||||
let directorDup = await this.directorRepository.findOne({
|
||||
where: { firstName: requestBody.firstName, lastName: requestBody.lastName },
|
||||
});
|
||||
if (directorDup != null) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อกรรมการนี้มีอยู่ในระบบแล้ว");
|
||||
try {
|
||||
await new permission().PermissionCreate(request, "SYS_EVA_INFO");
|
||||
let directorDup = await this.directorRepository.findOne({
|
||||
where: { firstName: requestBody.firstName, lastName: requestBody.lastName },
|
||||
});
|
||||
if (directorDup != null) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อกรรมการนี้มีอยู่ในระบบแล้ว");
|
||||
}
|
||||
const director = Object.assign(new Director(), requestBody);
|
||||
director.createdUserId = request.user.sub;
|
||||
director.createdFullName = request.user.name;
|
||||
director.createdAt = new Date();
|
||||
director.lastUpdateUserId = request.user.sub;
|
||||
director.lastUpdateFullName = request.user.name;
|
||||
director.lastUpdatedAt = new Date();
|
||||
|
||||
const before = null;
|
||||
|
||||
await this.directorRepository.save(director, { data: request });
|
||||
setLogDataDiff(request, { before, after: director });
|
||||
|
||||
return new HttpSuccess();
|
||||
} catch (error: any) {
|
||||
if (error instanceof HttpError) {
|
||||
throw error;
|
||||
} else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error);
|
||||
}
|
||||
const director = Object.assign(new Director(), requestBody);
|
||||
director.createdUserId = request.user.sub;
|
||||
director.createdFullName = request.user.name;
|
||||
director.createdAt = new Date();
|
||||
director.lastUpdateUserId = request.user.sub;
|
||||
director.lastUpdateFullName = request.user.name;
|
||||
director.lastUpdatedAt = new Date();
|
||||
|
||||
const before = null;
|
||||
|
||||
await this.directorRepository.save(director, { data: request });
|
||||
setLogDataDiff(request, { before, after: director });
|
||||
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -171,25 +201,31 @@ export class DirectorController {
|
|||
*/
|
||||
@Put("{id}")
|
||||
async update(@Path() id: string, @Body() u: CreateDirector, @Request() request: RequestWithUser) {
|
||||
await new permission().PermissionUpdate(request, "SYS_EVA_INFO");
|
||||
let directorDup = await this.directorRepository.findOne({
|
||||
where: { firstName: u.firstName, lastName: u.lastName, id: Not(id) },
|
||||
});
|
||||
if (directorDup != null) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อกรรมการนี้มีอยู่ในระบบแล้ว");
|
||||
try {
|
||||
await new permission().PermissionUpdate(request, "SYS_EVA_INFO");
|
||||
let directorDup = await this.directorRepository.findOne({
|
||||
where: { firstName: u.firstName, lastName: u.lastName, id: Not(id) },
|
||||
});
|
||||
if (directorDup != null) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อกรรมการนี้มีอยู่ในระบบแล้ว");
|
||||
}
|
||||
let director = await this.directorRepository.findOneBy({ id });
|
||||
if (!director) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
|
||||
}
|
||||
const before = structuredClone(directorDup);
|
||||
director.lastUpdateUserId = request.user.sub;
|
||||
director.lastUpdateFullName = request.user.name;
|
||||
director.lastUpdatedAt = new Date();
|
||||
this.directorRepository.merge(director, u);
|
||||
await this.directorRepository.save(director, { data: request });
|
||||
setLogDataDiff(request, { before, after: director });
|
||||
return new HttpSuccess();
|
||||
} catch (error: any) {
|
||||
if (error instanceof HttpError) {
|
||||
throw error;
|
||||
} else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error);
|
||||
}
|
||||
let director = await this.directorRepository.findOneBy({ id });
|
||||
if (!director) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
|
||||
}
|
||||
const before = structuredClone(directorDup);
|
||||
director.lastUpdateUserId = request.user.sub;
|
||||
director.lastUpdateFullName = request.user.name;
|
||||
director.lastUpdatedAt = new Date();
|
||||
this.directorRepository.merge(director, u);
|
||||
await this.directorRepository.save(director, { data: request });
|
||||
setLogDataDiff(request, { before, after: director });
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -200,12 +236,18 @@ export class DirectorController {
|
|||
*/
|
||||
@Delete("{id}")
|
||||
async remove(id: string, @Request() request: RequestWithUser) {
|
||||
await new permission().PermissionDelete(request, "SYS_EVA_INFO");
|
||||
let director = await this.directorRepository.findOneBy({ id });
|
||||
if (!director) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
|
||||
try {
|
||||
await new permission().PermissionDelete(request, "SYS_EVA_INFO");
|
||||
let director = await this.directorRepository.findOneBy({ id });
|
||||
if (!director) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
|
||||
}
|
||||
await this.directorRepository.remove(director, { data: request });
|
||||
return new HttpSuccess();
|
||||
} catch (error: any) {
|
||||
if (error instanceof HttpError) {
|
||||
throw error;
|
||||
} else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error);
|
||||
}
|
||||
await this.directorRepository.remove(director, { data: request });
|
||||
return new HttpSuccess();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue