From f0e5a83d02aaa1c9051f85547c0bdf5f38d17983 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Thu, 17 Oct 2024 16:39:42 +0700 Subject: [PATCH] =?UTF-8?q?log=20admin=20,=20master=20(=E0=B8=A2=E0=B8=81?= =?UTF-8?q?=E0=B9=80=E0=B8=A7=E0=B9=89=E0=B8=99=20menu=20=E0=B8=82?= =?UTF-8?q?=E0=B9=89=E0=B8=AD=E0=B8=A1=E0=B8=B9=E0=B8=A5=E0=B8=9B=E0=B8=8E?= =?UTF-8?q?=E0=B8=B4=E0=B8=97=E0=B8=B4=E0=B8=99=E0=B8=A7=E0=B8=B1=E0=B8=99?= =?UTF-8?q?=E0=B8=AB=E0=B8=A2=E0=B8=B8=E0=B8=94)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.ts | 2 +- src/controllers/AuthRoleController.ts | 35 +++++++++++-------- src/controllers/EmployeePosTypeController.ts | 21 +++++++---- src/controllers/EmployeePositionController.ts | 12 ++++--- src/controllers/InsigniaTypeController.ts | 19 +++++----- src/controllers/PermissionOrgController.ts | 12 ++++--- src/controllers/PosExecutiveController.ts | 16 ++++++--- src/controllers/PosTypeController.ts | 26 +++++++++----- src/controllers/PositionController.ts | 26 ++++++++++---- src/controllers/UserController.ts | 2 +- src/middlewares/logs.ts | 14 ++++++-- 11 files changed, 122 insertions(+), 63 deletions(-) diff --git a/src/app.ts b/src/app.ts index 360977a7..ee4eced4 100644 --- a/src/app.ts +++ b/src/app.ts @@ -26,7 +26,7 @@ async function main() { ); app.use(express.json()); app.use(express.urlencoded({ extended: true })); - // app.use(logMiddleware); + app.use(logMiddleware); app.use("/", express.static("static")); app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument)); diff --git a/src/controllers/AuthRoleController.ts b/src/controllers/AuthRoleController.ts index 4d78ae93..75e23541 100644 --- a/src/controllers/AuthRoleController.ts +++ b/src/controllers/AuthRoleController.ts @@ -22,6 +22,7 @@ import { AuthRoleAttr } from "../entities/AuthRoleAttr"; import { PosMaster } from "../entities/PosMaster"; import { EmployeePosMaster } from "../entities/EmployeePosMaster"; import { promisify } from "util"; +import { setLogDataDiff } from "../interfaces/utils"; const REDIS_HOST = process.env.REDIS_HOST; const REDIS_PORT = process.env.REDIS_PORT; @@ -74,11 +75,11 @@ export class AuthRoleController extends Controller { createdAt: new Date(), lastUpdatedAt: new Date(), }; - + const before = null; Object.assign(data, { ...body, ...meta }); - await this.authRoleRepo.save(data); - + await this.authRoleRepo.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); return new HttpSuccess(data.id); } @@ -96,7 +97,7 @@ export class AuthRoleController extends Controller { getDetail = await this.authRoleRepo.findOneBy({ id: body.authRoleId }); if (!getDetail) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์"); } - + const before = null; const posMaster = await this.posMasterRepository.findOneBy({ id: body.posMasterId }); if (!posMaster) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลตำแหน่ง"); @@ -104,7 +105,8 @@ export class AuthRoleController extends Controller { posMaster.lastUpdateFullName = req.user.name; posMaster.lastUpdatedAt = new Date(); posMaster.authRoleId = body.authRoleId; - await this.posMasterRepository.save(posMaster); + await this.posMasterRepository.save(posMaster, {data: req}); + setLogDataDiff(req, { before, after: posMaster }); // เช็คว่าถ้ามีค่า current_holderId ให้ลบ key สิทธิ์ใน redis if (posMaster.current_holderId) { @@ -139,7 +141,7 @@ export class AuthRoleController extends Controller { getDetail = await this.authRoleRepo.findOneBy({ id: body.authRoleId }); if (!getDetail) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์"); } - + const before = null; const posMaster = await this.employeePosMasterRepository.findOneBy({ id: body.posMasterId }); if (!posMaster) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลตำแหน่ง"); @@ -147,7 +149,8 @@ export class AuthRoleController extends Controller { posMaster.lastUpdateFullName = req.user.name; posMaster.lastUpdatedAt = new Date(); posMaster.authRoleId = body.authRoleId; - await this.employeePosMasterRepository.save(posMaster); + await this.employeePosMasterRepository.save(posMaster, {data: req}); + setLogDataDiff(req, { before, after: posMaster }); return new HttpSuccess(); } @@ -226,7 +229,7 @@ export class AuthRoleController extends Controller { // ...newAttrs.map((attr) => this.authRoleAttrRepo.save(attr)), // ]); - await this.authRoleAttrRepo.remove(roleAttrData); + await this.authRoleAttrRepo.remove(roleAttrData, {data: req}); const newAttrs = body.authRoleAttrs.map((attr) => { const newAttr = new AuthRoleAttr(); @@ -241,9 +244,10 @@ export class AuthRoleController extends Controller { }); return newAttr; }); - + const before = structuredClone(record); await Promise.all([ - this.authRoleRepo.save(record), + this.authRoleRepo.save(record, {data: req}), + setLogDataDiff(req, { before, after: record }), ...newAttrs.map((attr) => this.authRoleAttrRepo.save(attr)), ]); @@ -260,16 +264,17 @@ export class AuthRoleController extends Controller { } @Delete("{roleId}") - public async deleteRole(@Path() roleId: string) { + public async deleteRole(@Path() roleId: string, @Request() req: RequestWithUser) { let result: any; try { - result = await this.authRoleRepo.delete({ id: roleId }); + result = await this.authRoleRepo.findOneBy({ id: roleId }); + if (!result) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + await this.authRoleRepo.remove(result, {data: req}); } catch { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถลบข้อมูลได้"); } - if (result.affected == undefined || result.affected <= 0) - throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - return new HttpSuccess(); } } diff --git a/src/controllers/EmployeePosTypeController.ts b/src/controllers/EmployeePosTypeController.ts index 24261118..7fd73ae1 100644 --- a/src/controllers/EmployeePosTypeController.ts +++ b/src/controllers/EmployeePosTypeController.ts @@ -25,6 +25,8 @@ import { } from "../entities/EmployeePosType"; import { EmployeePosLevel } from "../entities/EmployeePosLevel"; import { EmployeePosDict } from "../entities/EmployeePosDict"; +import { RequestWithUser } from "../middlewares/user"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/employee/pos/type") @Tags("EmployeePosType") @@ -48,7 +50,7 @@ export class EmployeePosTypeController extends Controller { async CreateEmpType( @Body() requestBody: CreateEmployeePosType, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const EmpPosType = Object.assign(new EmployeePosType(), requestBody); @@ -72,13 +74,15 @@ export class EmployeePosTypeController extends Controller { "ระดับของกลุ่มงานลูกจ้างประจำนี้มีอยู่ในระบบแล้ว", ); } + const before = null; EmpPosType.createdUserId = request.user.sub; EmpPosType.createdFullName = request.user.name; EmpPosType.lastUpdateUserId = request.user.sub; EmpPosType.lastUpdateFullName = request.user.name; EmpPosType.createdAt = new Date(); EmpPosType.lastUpdatedAt = new Date(); - await this.employeePosTypeRepository.save(EmpPosType); + await this.employeePosTypeRepository.save(EmpPosType, {data: request}); + setLogDataDiff(request, {before, after: EmpPosType}); return new HttpSuccess(EmpPosType.id); } @@ -93,7 +97,7 @@ export class EmployeePosTypeController extends Controller { async EditEmpType( @Path() id: string, @Body() requestBody: UpdateEmployeePosType, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const EmpPosType = await this.employeePosTypeRepository.findOne({ where: { id } }); if (!EmpPosType) { @@ -120,11 +124,13 @@ export class EmployeePosTypeController extends Controller { "ระดับของกลุ่มงานลูกจ้างประจำนี้มีอยู่ในระบบแล้ว", ); } + const before = structuredClone(EmpPosType); EmpPosType.lastUpdateUserId = request.user.sub; EmpPosType.lastUpdateFullName = request.user.name; EmpPosType.lastUpdatedAt = new Date(); this.employeePosTypeRepository.merge(EmpPosType, requestBody); - await this.employeePosTypeRepository.save(EmpPosType); + await this.employeePosTypeRepository.save(EmpPosType, {data: request}); + setLogDataDiff(request, {before, after: EmpPosType}); return new HttpSuccess(EmpPosType.id); } @@ -136,10 +142,13 @@ export class EmployeePosTypeController extends Controller { * @param {string} id Id กลุ่มงานลูกจ้างประจำ */ @Delete("{id}") - async deleteType(@Path() id: string) { + async deleteType(@Path() id: string, @Request() request: RequestWithUser) { let result: any; try { - result = await this.employeePosTypeRepository.delete({ id: id }); + result = await this.employeePosTypeRepository.findOne({ + where: { id: id }, + }) + await this.employeePosTypeRepository.remove(result, {data: request}); } catch { throw new HttpError( HttpStatusCode.NOT_FOUND, diff --git a/src/controllers/EmployeePositionController.ts b/src/controllers/EmployeePositionController.ts index c5cc5299..3f601929 100644 --- a/src/controllers/EmployeePositionController.ts +++ b/src/controllers/EmployeePositionController.ts @@ -106,14 +106,15 @@ export class EmployeePositionController extends Controller { if (rowRepeated) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ข้อมูล Row นี้มีอยู่ในระบบแล้ว"); } - + const before = null; empPosDict.createdUserId = request.user.sub; empPosDict.createdFullName = request.user.name; empPosDict.createdAt = new Date(); empPosDict.lastUpdateUserId = request.user.sub; empPosDict.lastUpdateFullName = request.user.name; empPosDict.lastUpdatedAt = new Date(); - await this.employeePosDictRepository.save(empPosDict); + await this.employeePosDictRepository.save(empPosDict, { data: request }); + setLogDataDiff(request, { before, after: empPosDict }); return new HttpSuccess(empPosDict.id); } @@ -163,12 +164,13 @@ export class EmployeePositionController extends Controller { if (rowRepeated) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ข้อมูล Row นี้มีอยู่ในระบบแล้ว"); } - + const before = structuredClone(empPosDict); empPosDict.lastUpdateUserId = request.user.sub; empPosDict.lastUpdateFullName = request.user.name; empPosDict.lastUpdatedAt = new Date(); this.employeePosDictRepository.merge(empPosDict, requestBody); - await this.employeePosDictRepository.save(empPosDict); + await this.employeePosDictRepository.save(empPosDict,{data: request}); + setLogDataDiff( request, { before, after: empPosDict }); return new HttpSuccess(empPosDict.id); } @@ -186,7 +188,7 @@ export class EmployeePositionController extends Controller { if (!delEmpPosDict) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งลูกจ้างประจำนี้"); } - await this.employeePosDictRepository.remove(delEmpPosDict); + await this.employeePosDictRepository.remove(delEmpPosDict, { data: request }); return new HttpSuccess(); } diff --git a/src/controllers/InsigniaTypeController.ts b/src/controllers/InsigniaTypeController.ts index 07d85b66..074e0b80 100644 --- a/src/controllers/InsigniaTypeController.ts +++ b/src/controllers/InsigniaTypeController.ts @@ -25,6 +25,7 @@ import { InsigniaType, CreateInsigniaType, UpdateInsigniaType } from "../entitie import { Insignia } from "../entities/Insignia"; import permission from "../interfaces/permission"; import { RequestWithUser } from "../middlewares/user"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/insignia/insignia-type") @Tags("InsigniaType") @Security("bearerAuth") @@ -46,7 +47,7 @@ export class InsigniaTypeController extends Controller { @Post("") async CreateInsigniaType( @Body() requestBody: CreateInsigniaType, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const insigniaType = Object.assign(new InsigniaType(), requestBody); if (!insigniaType) { @@ -62,14 +63,15 @@ export class InsigniaTypeController extends Controller { if (rowRepeated) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ข้อมูล Row นี้มีอยู่ในระบบแล้ว"); } - + const before = null; insigniaType.createdUserId = request.user.sub; insigniaType.createdFullName = request.user.name; insigniaType.lastUpdateUserId = request.user.sub; insigniaType.lastUpdateFullName = request.user.name; insigniaType.createdAt = new Date(); insigniaType.lastUpdatedAt = new Date(); - await this.insigniaTypeRepository.save(insigniaType); + await this.insigniaTypeRepository.save(insigniaType, { data: request }); + setLogDataDiff(request, { before, after: insigniaType }); return new HttpSuccess(insigniaType.id); } @@ -84,7 +86,7 @@ export class InsigniaTypeController extends Controller { async UpdateInsigniaType( @Path() id: string, @Body() requestBody: UpdateInsigniaType, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const insigniaType = await this.insigniaTypeRepository.findOne({ where: { id: id }, @@ -102,12 +104,13 @@ export class InsigniaTypeController extends Controller { if (rowRepeated) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ข้อมูล Row นี้มีอยู่ในระบบแล้ว"); } - + const before = structuredClone(insigniaType); insigniaType.lastUpdateUserId = request.user.sub; insigniaType.lastUpdateFullName = request.user.name; insigniaType.lastUpdatedAt = new Date(); this.insigniaTypeRepository.merge(insigniaType, requestBody); - await this.insigniaTypeRepository.save(insigniaType); + await this.insigniaTypeRepository.save(insigniaType, { data: request }); + setLogDataDiff( request, { before, after: insigniaType }); return new HttpSuccess(insigniaType.id); } @@ -119,7 +122,7 @@ export class InsigniaTypeController extends Controller { * @param {string} id Id ลำดับชั้นเครื่องราชอิสริยาภรณ์ */ @Delete("{id}") - async delete(@Path() id: string) { + async delete(@Path() id: string, @Request() request: RequestWithUser) { const delInsigniaType = await this.insigniaTypeRepository.findOne({ where: { id } }); if (!delInsigniaType) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลลำดับชั้นเครื่องราชอิสริยาภรณ์นี้"); @@ -133,7 +136,7 @@ export class InsigniaTypeController extends Controller { "ไม่สามารถลบได้ เนื่องจากพบข้อมูลที่ตารางเครื่องราชอิสริยาภรณ์", ); } - await this.insigniaTypeRepository.remove(delInsigniaType); + await this.insigniaTypeRepository.remove(delInsigniaType, {data: request}); return new HttpSuccess(); } diff --git a/src/controllers/PermissionOrgController.ts b/src/controllers/PermissionOrgController.ts index 1e17ac8e..4481a8ee 100644 --- a/src/controllers/PermissionOrgController.ts +++ b/src/controllers/PermissionOrgController.ts @@ -29,6 +29,7 @@ import permission from "../interfaces/permission"; import { PosMaster } from "../entities/PosMaster"; import { EmployeePosMaster } from "../entities/EmployeePosMaster"; import { ProfileEmployee } from "../entities/ProfileEmployee"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/permission-org") @Tags("PermissionOrg") @@ -470,7 +471,7 @@ export class PermissionOrgController extends Controller { if (checkDup) { throw new HttpError(HttpStatusCode.NOT_FOUND, "มีสิทธิ์นี้อยู่ในระบบแล้ว"); } - + const before = null; const _permissionOrg = new PermissionOrg(); _permissionOrg.orgRootTree = orgRoot; _permissionOrg.profileTree = profile; @@ -480,7 +481,8 @@ export class PermissionOrgController extends Controller { _permissionOrg.lastUpdateFullName = request.user.name; _permissionOrg.createdAt = new Date(); _permissionOrg.lastUpdatedAt = new Date(); - await this.permissionOrgRepository.save(_permissionOrg); + await this.permissionOrgRepository.save(_permissionOrg, {data:request}); + setLogDataDiff(request, { before, after: _permissionOrg }); return new HttpSuccess(); } @@ -492,7 +494,7 @@ export class PermissionOrgController extends Controller { * @param {string} id Id สิทธิ์โครงสร้าง */ @Delete("{id}") - async Delete(@Request() request: RequestWithUser, @Path() id: string) { + async Delete(@Request() req: RequestWithUser, @Path() id: string) { // if (!request.user.role.includes("SUPER_ADMIN")) { // throw new HttpError(HttpStatus.FORBIDDEN, "ไม่มีสิทธิ์ใช้งานระบบนี้"); // } @@ -518,9 +520,11 @@ export class PermissionOrgController extends Controller { if (!_delPermissionOrg) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบสิทธิ์นี้อยู่ในระบบแล้ว"); } - await this.permissionOrgRepository.delete(_delPermissionOrg.id); + + await this.permissionOrgRepository.remove(_delPermissionOrg, {data:req}); return new HttpSuccess(); } + public async listAuthSysOrgFuncByRevisionId( request: RequestWithUser, system: string, diff --git a/src/controllers/PosExecutiveController.ts b/src/controllers/PosExecutiveController.ts index bee1c224..c7636b62 100644 --- a/src/controllers/PosExecutiveController.ts +++ b/src/controllers/PosExecutiveController.ts @@ -21,6 +21,9 @@ import HttpStatusCode from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { CreatePosExecutive, PosExecutive } from "../entities/PosExecutive"; import { Position } from "../entities/Position"; +import { RequestWithUser } from "../middlewares/user"; +import { after } from "node:test"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/pos/executive") @Tags("PosExecutive") @Security("bearerAuth") @@ -43,7 +46,7 @@ export class PosExecutiveController extends Controller { async createPosExecutive( @Body() requestBody: CreatePosExecutive, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const checkName = await this.posExecutiveRepository.findOne({ where: { posExecutiveName: requestBody.posExecutiveName }, @@ -75,13 +78,15 @@ export class PosExecutiveController extends Controller { if (_checkPriority) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ลำดับนี้มีอยู่ในระบบแล้ว"); } + const before = null; posExecutive.createdUserId = request.user.sub; posExecutive.createdFullName = request.user.name; posExecutive.lastUpdateUserId = request.user.sub; posExecutive.lastUpdateFullName = request.user.name; posExecutive.createdAt = new Date(); posExecutive.lastUpdatedAt = new Date(); - await this.posExecutiveRepository.save(posExecutive); + await this.posExecutiveRepository.save(posExecutive, {data: request}); + setLogDataDiff(request, {before, after: posExecutive}); return new HttpSuccess(posExecutive.id); } @@ -97,7 +102,7 @@ export class PosExecutiveController extends Controller { @Path() id: string, @Body() requestBody: CreatePosExecutive, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const posExecutive = await this.posExecutiveRepository.findOne({ where: { id: id } }); if (!posExecutive) { @@ -136,13 +141,14 @@ export class PosExecutiveController extends Controller { if (_checkPriority) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ลำดับนี้มีอยู่ในระบบแล้ว"); } - + const before = structuredClone(posExecutive); posExecutive.posExecutiveName = requestBody.posExecutiveName; posExecutive.lastUpdateUserId = request.user.sub; posExecutive.lastUpdateFullName = request.user.name; posExecutive.lastUpdatedAt = new Date(); // this.posExecutiveRepository.merge(posExecutive, requestBody); - await this.posExecutiveRepository.save(posExecutive); + await this.posExecutiveRepository.save(posExecutive, {data: request}); + setLogDataDiff(request, {before, after: posExecutive}); return new HttpSuccess(); } diff --git a/src/controllers/PosTypeController.ts b/src/controllers/PosTypeController.ts index b3228a1e..bd762af6 100644 --- a/src/controllers/PosTypeController.ts +++ b/src/controllers/PosTypeController.ts @@ -23,6 +23,8 @@ import { PosType, CreatePosType, UpdatePosType } from "../entities/PosType"; import { PosLevel } from "../entities/PosLevel"; import HttpError from "../interfaces/http-error"; import { Not } from "typeorm"; +import { setLogDataDiff } from "../interfaces/utils"; +import { RequestWithUser } from "../middlewares/user"; @Route("api/v1/org/pos/type") @Tags("PosType") @@ -50,7 +52,7 @@ export class PosTypeController extends Controller { async createType( @Body() requestBody: CreatePosType, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const posType = Object.assign(new PosType(), requestBody); if (!posType) { @@ -64,13 +66,15 @@ export class PosTypeController extends Controller { if (chkPosTypeName) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อประเภทตำแหน่งนี้มีอยู่ในระบบแล้ว"); } + const before = null; posType.createdUserId = request.user.sub; posType.createdFullName = request.user.name; posType.lastUpdateUserId = request.user.sub; posType.lastUpdateFullName = request.user.name; posType.createdAt = new Date(); posType.lastUpdatedAt = new Date(); - await this.posTypeRepository.save(posType); + await this.posTypeRepository.save(posType, {data: request}); + setLogDataDiff(request, {before , after: posType}); return new HttpSuccess(posType); } @@ -89,7 +93,7 @@ export class PosTypeController extends Controller { async editType( @Path() id: string, @Body() requestBody: UpdatePosType, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const posType = await this.posTypeRepository.findOne({ where: { id } }); if (!posType) { @@ -104,11 +108,13 @@ export class PosTypeController extends Controller { if (chkPosTypeName) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อประเภทตำแหน่งนี้มีอยู่ในระบบแล้ว"); } + const before = structuredClone(posType); posType.lastUpdateUserId = request.user.sub; posType.lastUpdateFullName = request.user.name; posType.lastUpdatedAt = new Date(); this.posTypeRepository.merge(posType, requestBody); - await this.posTypeRepository.save(posType); + await this.posTypeRepository.save(posType, {data: request}); + setLogDataDiff(request,{ before, after:posType }); return new HttpSuccess(posType.id); } @@ -123,16 +129,20 @@ export class PosTypeController extends Controller { async deleteType(@Path() id: string) { let result: any; try { - result = await this.posTypeRepository.delete({ id: id }); + result = await this.posTypeRepository.findOne({ where: { id } }); + if(!result){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); + } + await this.posTypeRepository.remove(result, {data: result}); } catch { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่สามารถลบได้เนื่องจากมีการใช้งานประเภทตำแหน่งนี้อยู่", ); } - if (result.affected == undefined || result.affected <= 0) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); - } + // if (result.affected == undefined || result.affected <= 0) { + // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + // } // const delPosType = await this.posTypeRepository.findOne({ where: { id } }); // if (!delPosType) { // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 7dc8f8d9..74cfbc9f 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -144,14 +144,15 @@ export class PositionController extends Controller { if (rowRepeated) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ข้อมูล Row นี้มีอยู่ในระบบแล้ว"); } - + const before = null; posDict.createdUserId = request.user.sub; posDict.createdFullName = request.user.name; posDict.lastUpdateUserId = request.user.sub; posDict.lastUpdateFullName = request.user.name; posDict.createdAt = new Date(); posDict.lastUpdatedAt = new Date(); - await this.posDictRepository.save(posDict); + await this.posDictRepository.save(posDict, {data: request}); + setLogDataDiff(request, { before, after: posDict }); return new HttpSuccess(posDict.id); } @@ -218,7 +219,13 @@ export class PositionController extends Controller { if (!posMaster) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่ง"); } - await this.posMasterAssignRepo.delete({ posMasterId: posMaster.id }); + const posMasterAssigns = await this.posMasterAssignRepo.find({ + where: { posMasterId: posMaster.id }, + }); + if (posMasterAssigns.length > 0) { + await this.posMasterAssignRepo.remove(posMasterAssigns, {data:request}); + } + // await this.posMasterAssignRepo.delete({ posMasterId: posMaster.id }); return new HttpSuccess(); } @@ -270,6 +277,7 @@ export class PositionController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล PosLevelId"); } + const before = null; let posExecutive: any = new PosExecutive(); if (requestBody.posExecutive != null && requestBody.posExecutive != "") { const checkName = await this.posExecutiveRepository.findOne({ @@ -299,7 +307,8 @@ export class PositionController extends Controller { posExecutive.lastUpdateFullName = request.user.name; posExecutive.createdAt = new Date(); posExecutive.lastUpdatedAt = new Date(); - await this.posExecutiveRepository.save(posExecutive); + await this.posExecutiveRepository.save(posExecutive, {data:request}); + setLogDataDiff(request, {before, after: posExecutive}); } const rowRepeated = await this.posDictRepository.findOne({ @@ -325,7 +334,8 @@ export class PositionController extends Controller { posDict.lastUpdateFullName = request.user.name; posDict.createdAt = new Date(); posDict.lastUpdatedAt = new Date(); - await this.posDictRepository.save(posDict); + await this.posDictRepository.save(posDict, {data:request}); + setLogDataDiff(request, {before, after: posDict}); return new HttpSuccess(posDict.id); } @@ -407,6 +417,7 @@ export class PositionController extends Controller { if (rowRepeated) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ข้อมูล Row นี้มีอยู่ในระบบแล้ว"); } + const before = structuredClone(posDict); Object.assign(posDict, requestBody); posDict.lastUpdateUserId = request.user.sub; posDict.lastUpdateFullName = request.user.name; @@ -422,7 +433,8 @@ export class PositionController extends Controller { posDict.posDictArea = requestBody.posDictArea ? requestBody.posDictArea : ""; posDict.isSpecial = requestBody.isSpecial; // this.posDictRepository.merge(posDict, requestBody); - await this.posDictRepository.save(posDict); + await this.posDictRepository.save(posDict, {data:request}); + setLogDataDiff(request, {before, after: posDict}); return new HttpSuccess(); } @@ -440,7 +452,7 @@ export class PositionController extends Controller { if (!delPosDict) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งในสายงานนี้"); } - await this.posDictRepository.remove(delPosDict); + await this.posDictRepository.remove(delPosDict, {data: request}); return new HttpSuccess(); } diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index 456de900..bb6ece7d 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -401,7 +401,7 @@ export class KeycloakController extends Controller { return result; } - @Put("user/{userId}/enableStatus/{status}") + @Put("user/{userId}/enableStatus/{status}") //#log? async changeEnableStatus(@Path() userId: string, @Path() status: boolean) { const result = await enableStatus(userId, status); if (!result) { diff --git a/src/middlewares/logs.ts b/src/middlewares/logs.ts index 43120556..ab65fdb9 100644 --- a/src/middlewares/logs.ts +++ b/src/middlewares/logs.ts @@ -39,13 +39,21 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) { res.on("finish", () => { if (!req.url.startsWith("/api/")) return; let system = "organization"; - if (req.url.startsWith("/api/v1/org/metadata/")) system = "metadata"; + if (req.url.startsWith("/api/v1/org/metadata/")) system = "master"; + if (req.url.startsWith("/api/v1/org/pos/position/")) system = "master"; + if (req.url.startsWith("/api/v1/org/pos/type/")) system = "master"; + if (req.url.startsWith("/api/v1/org/employee/pos/position/")) system = "master"; + if (req.url.startsWith("api/v1/org/employee/pos/type/")) system = "master"; if (req.url.startsWith("/api/v1/org/auth/authRoleAttr/")) system = "admin"; + if (req.url.startsWith("/api/v1/org/auth/authRole/")) system = "admin"; + // if (req.url.startsWith("/api/v1/org/keycloak")) system = "admin"; + if (req.url.startsWith("/api/v1/org/pos/admin/master/list")) system = "admin"; + if (req.url.startsWith("/api/v1/org/super-admin/{id}")) system = "admin"; + if (req.url.startsWith("/api/v1/org/permission-org/")) system = "admin"; + if (req.url.startsWith("/api/v1/org/pos/assign/")) system = "admin"; if (req.url.startsWith("/api/v1/org/profile/")) system = "registry"; if (req.url.startsWith("/api/v1/org/profile-employee/")) system = "registry"; if (req.url.startsWith("/api/v1/org/profile-temp/")) system = "registry"; - // if (req.url.startsWith("/api/v1/org/auth/authRoleAttr/")) system = "admin"; - // if (req.url.startsWith("/api/v1/org/auth/authRoleAttr/")) system = "admin"; const level = LOG_LEVEL_MAP[process.env.LOG_LEVEL ?? "debug"] || 4;