From 6539804937a7b3163907192316152b65cc4f540c Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Thu, 3 Oct 2024 15:57:44 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B8=9C=E0=B8=B9=E0=B8=81=20log=20=E0=B9=80?= =?UTF-8?q?=E0=B8=A1=E0=B8=99=E0=B8=B9=E0=B8=97=E0=B8=B0=E0=B9=80=E0=B8=9A?= =?UTF-8?q?=E0=B8=B5=E0=B8=A2=E0=B8=99=E0=B8=9B=E0=B8=A3=E0=B8=B0=E0=B8=A7?= =?UTF-8?q?=E0=B8=B1=E0=B8=95=E0=B8=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.ts | 2 + src/controllers/BloodGroupController.ts | 20 +++++---- src/controllers/DistrictController.ts | 16 ++++--- src/controllers/EducationLevelController.ts | 20 +++++---- src/controllers/GenderController.ts | 20 +++++---- src/controllers/PrefixController.ts | 19 +++++--- src/controllers/ProfileAbilityController.ts | 31 +++++++------ .../ProfileAbilityEmployeeController.ts | 22 ++++++---- .../ProfileAbilityEmployeeTempController.ts | 22 ++++++---- src/controllers/ProfileAddressController.ts | 10 +++-- .../ProfileAddressEmployeeController.ts | 10 +++-- .../ProfileAddressEmployeeTempController.ts | 10 +++-- .../ProfileAssessmentsController.ts | 18 +++++--- .../ProfileAssessmentsEmployeeController.ts | 17 +++++--- ...rofileAssessmentsEmployeeTempController.ts | 22 ++++++---- .../ProfileCertificateController.ts | 23 +++++++--- .../ProfileCertificateEmployeeController.ts | 24 ++++++++--- ...rofileCertificateEmployeeTempController.ts | 20 ++++++--- .../ProfileChangeNameController.ts | 26 ++++++----- .../ProfileChangeNameEmployeeController.ts | 12 ++++-- ...ProfileChangeNameEmployeeTempController.ts | 21 ++++++--- src/controllers/ProfileChildrenController.ts | 18 +++++--- .../ProfileChildrenEmployeeController.ts | 19 +++++--- .../ProfileChildrenEmployeeTempController.ts | 18 +++++--- .../ProfileDisciplineController.ts | 25 +++++++---- .../ProfileDisciplineEmployeeController.ts | 24 +++++++---- ...ProfileDisciplineEmployeeTempController.ts | 23 +++++++--- src/controllers/ProfileDutyController.ts | 22 +++++++--- .../ProfileDutyEmployeeController.ts | 19 +++++--- .../ProfileDutyEmployeeTempController.ts | 19 +++++--- .../ProfileEducationsController.ts | 22 ++++++---- .../ProfileEducationsEmployeeController.ts | 22 ++++++---- ...ProfileEducationsEmployeeTempController.ts | 23 +++++----- .../ProfileFamilyCoupleController.ts | 21 ++++++--- .../ProfileFamilyCoupleEmployeeController.ts | 21 ++++++--- ...ofileFamilyCoupleEmployeeTempController.ts | 20 ++++++--- .../ProfileFamilyFatherController.ts | 18 +++++--- .../ProfileFamilyFatherEmployeeController.ts | 17 +++++--- ...ofileFamilyFatherEmployeeTempController.ts | 17 +++++--- .../ProfileFamilyMotherController.ts | 17 +++++--- .../ProfileFamilyMotherEmployeeController.ts | 18 +++++--- ...ofileFamilyMotherEmployeeTempController.ts | 18 +++++--- .../ProfileGovernmentController.ts | 10 +++-- .../ProfileGovernmentEmployeeController.ts | 8 +++- ...ProfileGovernmentEmployeeTempController.ts | 9 +++- src/controllers/ProfileHonorController.ts | 16 ++++--- .../ProfileHonorEmployeeController.ts | 16 ++++--- .../ProfileHonorEmployeeTempController.ts | 16 ++++--- src/controllers/ProfileInsigniaController.ts | 16 ++++--- .../ProfileInsigniaEmployeeController.ts | 16 ++++--- .../ProfileInsigniaEmployeeTempController.ts | 16 ++++--- src/controllers/ProfileLeaveController.ts | 16 ++++--- .../ProfileLeaveEmployeeController.ts | 16 ++++--- .../ProfileLeaveEmployeeTempController.ts | 11 +++-- src/controllers/ProfileNopaidController.ts | 15 ++++--- .../ProfileNopaidEmployeeController.ts | 14 +++--- .../ProfileNopaidEmployeeTempController.ts | 15 ++++--- src/controllers/ProfileOtherController.ts | 16 ++++--- .../ProfileOtherEmployeeController.ts | 15 ++++--- .../ProfileOtherEmployeeTempController.ts | 8 ++-- src/controllers/ProfileSalaryController.ts | 16 ++++--- .../ProfileSalaryEmployeeController.ts | 16 ++++--- .../ProfileSalaryEmployeeTempController.ts | 16 ++++--- src/controllers/ProfileTrainingController.ts | 16 ++++--- .../ProfileTrainingEmployeeController.ts | 18 +++++--- .../ProfileTrainingEmployeeTempController.ts | 16 ++++--- src/controllers/ProvinceController.ts | 16 ++++--- src/controllers/RankController.ts | 19 +++++--- src/controllers/RelationshipController.ts | 20 ++++++--- src/controllers/ReligionController.ts | 20 +++++---- src/controllers/SubDistrictController.ts | 16 ++++--- src/database/data-source.ts | 33 +++++++++++++- src/interfaces/call-api.ts | 43 +++++++++++++++++++ src/middlewares/auth.ts | 8 ++++ src/middlewares/error.ts | 6 +++ src/middlewares/logs.ts | 6 +-- 76 files changed, 909 insertions(+), 431 deletions(-) diff --git a/src/app.ts b/src/app.ts index 876721b7..40c284ac 100644 --- a/src/app.ts +++ b/src/app.ts @@ -9,6 +9,7 @@ import error from "./middlewares/error"; import { AppDataSource } from "./database/data-source"; import { RegisterRoutes } from "./routes"; import { OrganizationController } from "./controllers/OrganizationController"; +import logMiddleware from "./middlewares/logs"; async function main() { await AppDataSource.initialize(); @@ -22,6 +23,7 @@ async function main() { ); app.use(express.json()); app.use(express.urlencoded({ extended: true })); + app.use(logMiddleware); app.use("/", express.static("static")); app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument)); diff --git a/src/controllers/BloodGroupController.ts b/src/controllers/BloodGroupController.ts index 02451bc0..a4f86be2 100644 --- a/src/controllers/BloodGroupController.ts +++ b/src/controllers/BloodGroupController.ts @@ -19,6 +19,8 @@ import HttpStatusCode from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { CreateBloodGroup, BloodGroup } from "../entities/BloodGroup"; import { Not } from "typeorm"; +import { setLogDataDiff } from "../interfaces/utils"; +import { RequestWithUser } from "../middlewares/user"; @Route("api/v1/org/metadata/bloodGroup") @Tags("BloodGroup") @Security("bearerAuth") @@ -40,7 +42,7 @@ export class BloodGroupController extends Controller { async createBloodGroup( @Body() requestBody: CreateBloodGroup, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const checkName = await this.bloodGroupRepository.findOne({ where: { name: requestBody.name }, @@ -49,7 +51,7 @@ export class BloodGroupController extends Controller { if (checkName) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } - + const before = null; const bloodGroup = Object.assign(new BloodGroup(), requestBody); bloodGroup.createdUserId = request.user.sub; bloodGroup.createdFullName = request.user.name; @@ -57,7 +59,8 @@ export class BloodGroupController extends Controller { bloodGroup.lastUpdateUserId = request.user.sub; bloodGroup.lastUpdateFullName = request.user.name; bloodGroup.lastUpdatedAt = new Date(); - await this.bloodGroupRepository.save(bloodGroup); + await this.bloodGroupRepository.save(bloodGroup, { data: request }); + setLogDataDiff(request, { before, after: bloodGroup }); return new HttpSuccess(); } @@ -73,7 +76,7 @@ export class BloodGroupController extends Controller { @Path() id: string, @Body() requestBody: CreateBloodGroup, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const bloodGroup = await this.bloodGroupRepository.findOne({ where: { id: id } }); if (!bloodGroup) { @@ -87,12 +90,13 @@ export class BloodGroupController extends Controller { if (checkName) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } - + const before = structuredClone(bloodGroup); bloodGroup.lastUpdateUserId = request.user.sub; bloodGroup.lastUpdateFullName = request.user.name; bloodGroup.lastUpdatedAt = new Date(); this.bloodGroupRepository.merge(bloodGroup, requestBody); - await this.bloodGroupRepository.save(bloodGroup); + await this.bloodGroupRepository.save(bloodGroup, { data: request }); + setLogDataDiff(request, { before, after: bloodGroup }); return new HttpSuccess(); } @@ -104,7 +108,7 @@ export class BloodGroupController extends Controller { * @param {string} id Id กลุ่มเลือด */ @Delete("{id}") - async deleteBloodGroup(@Path() id: string) { + async deleteBloodGroup(@Path() id: string, @Request() request: RequestWithUser) { const delBloodGroup = await this.bloodGroupRepository.findOne({ where: { id }, }); @@ -112,7 +116,7 @@ export class BloodGroupController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกรุ๊ปเลือดนี้"); } - await this.bloodGroupRepository.delete({ id: id }); + await this.bloodGroupRepository.remove(delBloodGroup,{ data: request }); return new HttpSuccess(); } diff --git a/src/controllers/DistrictController.ts b/src/controllers/DistrictController.ts index d244e53c..18929d05 100644 --- a/src/controllers/DistrictController.ts +++ b/src/controllers/DistrictController.ts @@ -20,6 +20,8 @@ import HttpError from "../interfaces/http-error"; import { District, CreateDistrict, UpdateDistrict } from "../entities/District"; import { Province } from "../entities/Province"; import { Not } from "typeorm"; +import { setLogDataDiff } from "../interfaces/utils"; +import { RequestWithUser } from "../middlewares/user"; @Route("api/v1/org/metadata/district") @Tags("District") @@ -79,7 +81,7 @@ export class DistrictController extends Controller { async Post( @Body() requestBody: CreateDistrict, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const _district = Object.assign(new District(), requestBody); if (!_district) { @@ -100,14 +102,15 @@ export class DistrictController extends Controller { if (checkName) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } - + const before = null; _district.createdUserId = request.user.sub; _district.createdFullName = request.user.name; _district.createdAt = new Date(); _district.lastUpdateUserId = request.user.sub; _district.lastUpdateFullName = request.user.name; _district.lastUpdatedAt = new Date(); - await this.districtRepository.save(_district); + await this.districtRepository.save(_district, { data: request}); + setLogDataDiff(request, { before, after: _district }); return new HttpSuccess(); } @@ -123,7 +126,7 @@ export class DistrictController extends Controller { @Path() id: string, @Body() requestBody: UpdateDistrict, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const _district = await this.districtRepository.findOne({ where: { id: id } }); if (!_district) { @@ -143,12 +146,13 @@ export class DistrictController extends Controller { if (checkName) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } - + const before = structuredClone(_district); _district.lastUpdateUserId = request.user.sub; _district.lastUpdateFullName = request.user.name; _district.lastUpdatedAt = new Date(); this.districtRepository.merge(_district, requestBody); - await this.districtRepository.save(_district); + await this.districtRepository.save(_district, { data: request }); + setLogDataDiff(request, { before, after: _district }); return new HttpSuccess(); } diff --git a/src/controllers/EducationLevelController.ts b/src/controllers/EducationLevelController.ts index 29281e78..b3e6e868 100644 --- a/src/controllers/EducationLevelController.ts +++ b/src/controllers/EducationLevelController.ts @@ -19,6 +19,8 @@ import HttpStatusCode from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { CreateEducationLevel, EducationLevel } from "../entities/EducationLevel"; import { Not } from "typeorm"; +import { RequestWithUser } from "../middlewares/user"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/metadata/educationLevel") @Tags("EducationLevel") @Security("bearerAuth") @@ -40,7 +42,7 @@ export class EducationLevelController extends Controller { async createEducationLevel( @Body() requestBody: CreateEducationLevel, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const checkName = await this.educationLevelRepository.findOne({ where: { name: requestBody.name }, @@ -49,7 +51,7 @@ export class EducationLevelController extends Controller { if (checkName) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } - + const before = null; const educationLevel = Object.assign(new EducationLevel(), requestBody); educationLevel.createdUserId = request.user.sub; educationLevel.createdFullName = request.user.name; @@ -57,7 +59,8 @@ export class EducationLevelController extends Controller { educationLevel.lastUpdateUserId = request.user.sub; educationLevel.lastUpdateFullName = request.user.name; educationLevel.lastUpdatedAt = new Date(); - await this.educationLevelRepository.save(educationLevel); + await this.educationLevelRepository.save(educationLevel, { data: request }); + setLogDataDiff( request, { before, after: educationLevel } ); return new HttpSuccess(); } @@ -73,7 +76,7 @@ export class EducationLevelController extends Controller { @Path() id: string, @Body() requestBody: CreateEducationLevel, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const educationLevel = await this.educationLevelRepository.findOne({ where: { id: id } }); if (!educationLevel) { @@ -87,12 +90,13 @@ export class EducationLevelController extends Controller { if (checkName) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } - + const before = structuredClone(educationLevel); educationLevel.lastUpdateUserId = request.user.sub; educationLevel.lastUpdateFullName = request.user.name; educationLevel.lastUpdatedAt = new Date(); this.educationLevelRepository.merge(educationLevel, requestBody); - await this.educationLevelRepository.save(educationLevel); + await this.educationLevelRepository.save(educationLevel, { data: request }); + setLogDataDiff( request, { before, after: educationLevel } ); return new HttpSuccess(); } @@ -104,14 +108,14 @@ export class EducationLevelController extends Controller { * @param {string} id Id ระดับการศึกษา */ @Delete("{id}") - async deleteEducationLevel(@Path() id: string) { + async deleteEducationLevel(@Path() id: string , @Request() request: RequestWithUser) { const delEducationLevel = await this.educationLevelRepository.findOne({ where: { id }, }); if (!delEducationLevel) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับการศึกษานี้"); } - await this.educationLevelRepository.delete({ id: id }); + await this.educationLevelRepository.remove(delEducationLevel,{ data: request }); return new HttpSuccess(); } diff --git a/src/controllers/GenderController.ts b/src/controllers/GenderController.ts index 5e373d83..75d809b1 100644 --- a/src/controllers/GenderController.ts +++ b/src/controllers/GenderController.ts @@ -19,6 +19,8 @@ import HttpStatusCode from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { Gender, CreateGender, UpdateGender } from "../entities/Gender"; import { Not } from "typeorm"; +import { setLogDataDiff } from "../interfaces/utils"; +import { RequestWithUser } from "../middlewares/user"; @Route("api/v1/org/metadata/gender") @Tags("Gender") @@ -79,7 +81,7 @@ export class GenderController extends Controller { async Post( @Body() requestBody: CreateGender, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const _gender = Object.assign(new Gender(), requestBody); if (!_gender) { @@ -93,14 +95,15 @@ export class GenderController extends Controller { if (checkName) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } - + const before = null; _gender.createdUserId = request.user.sub; _gender.createdFullName = request.user.name; _gender.lastUpdateUserId = request.user.sub; _gender.lastUpdateFullName = request.user.name; _gender.createdAt = new Date(); _gender.lastUpdatedAt = new Date(); - await this.genderRepository.save(_gender); + await this.genderRepository.save(_gender, { data: request }); + setLogDataDiff( request, { before, after: _gender } ); return new HttpSuccess(); } @@ -116,7 +119,7 @@ export class GenderController extends Controller { @Path() id: string, @Body() requestBody: UpdateGender, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const _gender = await this.genderRepository.findOne({ where: { id: id } }); if (!_gender) { @@ -128,12 +131,13 @@ export class GenderController extends Controller { if (checkName) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } - + const before = structuredClone(_gender); _gender.lastUpdateUserId = request.user.sub; _gender.lastUpdateFullName = request.user.name; _gender.lastUpdatedAt = new Date(); this.genderRepository.merge(_gender, requestBody); - await this.genderRepository.save(_gender); + await this.genderRepository.save(_gender, { data: request }); + setLogDataDiff( request, { before, after: _gender } ); return new HttpSuccess(); } @@ -145,14 +149,14 @@ export class GenderController extends Controller { * @param {string} id Id เพศ */ @Delete("{id}") - async Delete(@Path() id: string) { + async Delete(@Path() id: string, @Request() request: RequestWithUser) { const _delGender = await this.genderRepository.findOne({ where: { id: id }, }); if (!_delGender) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลเพศนี้"); } - await this.genderRepository.delete(_delGender.id); + await this.genderRepository.remove(_delGender, {data: request}); return new HttpSuccess(); } } diff --git a/src/controllers/PrefixController.ts b/src/controllers/PrefixController.ts index d85d6437..9165cfee 100644 --- a/src/controllers/PrefixController.ts +++ b/src/controllers/PrefixController.ts @@ -19,6 +19,9 @@ import HttpStatusCode from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { Prefixe, CreatePrefixe, UpdatePrefixe } from "../entities/Prefixe"; import { Not } from "typeorm"; +import { setLogDataDiff } from "../interfaces/utils"; +import { RequestWithUser } from "../middlewares/user"; +import { request } from "axios"; @Route("api/v1/org/metadata/prefix") @Tags("Prefix") @@ -78,7 +81,7 @@ export class PrefixController extends Controller { async Post( @Body() requestBody: CreatePrefixe, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const _prefix = Object.assign(new Prefixe(), requestBody); if (!_prefix) { @@ -93,13 +96,15 @@ export class PrefixController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } + const before = null; _prefix.createdUserId = request.user.sub; _prefix.createdFullName = request.user.name; _prefix.lastUpdateUserId = request.user.sub; _prefix.lastUpdateFullName = request.user.name; _prefix.createdAt = new Date(); _prefix.lastUpdatedAt = new Date(); - await this.prefixRepository.save(_prefix); + await this.prefixRepository.save(_prefix, { data: request }); + setLogDataDiff( request, { before, after: _prefix} ) return new HttpSuccess(); } @@ -115,7 +120,7 @@ export class PrefixController extends Controller { @Path() id: string, @Body() requestBody: UpdatePrefixe, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const _prefix = await this.prefixRepository.findOne({ where: { id: id } }); if (!_prefix) { @@ -128,11 +133,13 @@ export class PrefixController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } + const before = structuredClone(_prefix); _prefix.lastUpdateUserId = request.user.sub; _prefix.lastUpdateFullName = request.user.name; _prefix.lastUpdatedAt = new Date(); this.prefixRepository.merge(_prefix, requestBody); - await this.prefixRepository.save(_prefix); + await this.prefixRepository.save(_prefix, { data: request }); + setLogDataDiff(request, { before, after: _prefix }); return new HttpSuccess(); } @@ -144,14 +151,14 @@ export class PrefixController extends Controller { * @param {string} id Id คำนำหน้า */ @Delete("{id}") - async Delete(@Path() id: string) { + async Delete(@Path() id: string, @Request() request: RequestWithUser) { const _delPrefix = await this.prefixRepository.findOne({ where: { id: id }, }); if (!_delPrefix) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำนำหน้าชื่อนี้"); } - await this.prefixRepository.delete(_delPrefix.id); + await this.prefixRepository.remove(_delPrefix,{ data: request }); return new HttpSuccess(); } } diff --git a/src/controllers/ProfileAbilityController.ts b/src/controllers/ProfileAbilityController.ts index 108ab51f..54bb531c 100644 --- a/src/controllers/ProfileAbilityController.ts +++ b/src/controllers/ProfileAbilityController.ts @@ -24,6 +24,7 @@ import HttpError from "../interfaces/http-error"; import HttpStatus from "../interfaces/http-status"; import HttpSuccess from "../interfaces/http-success"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile/ability") @Tags("ProfileAbility") @Security("bearerAuth") @@ -66,10 +67,10 @@ export class ProfileAbilityController extends Controller { @Path() abilityId: string, @Request() req: RequestWithUser, ) { - const _record = await this.profileAbilityRepo.findOneBy({ id: abilityId }); - if (_record) { - await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", _record.profileId); - } + const _record = await this.profileAbilityRepo.findOneBy({ id: abilityId }); + if (_record) { + await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", _record.profileId); + } const record = await this.profileAbilityHistoryRepo.find({ where: { profileAbilityId: abilityId }, @@ -83,9 +84,7 @@ export class ProfileAbilityController extends Controller { } @Get("history/{abilityId}") - public async getProfileAbilityHistory( - @Path() abilityId: string, - ) { + public async getProfileAbilityHistory(@Path() abilityId: string) { const record = await this.profileAbilityHistoryRepo.find({ where: { profileAbilityId: abilityId }, order: { createdAt: "DESC" }, @@ -111,7 +110,7 @@ export class ProfileAbilityController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profile.id); - + const before = null; const data = new ProfileAbility(); const meta = { createdUserId: req.user.sub, @@ -126,10 +125,11 @@ export class ProfileAbilityController extends Controller { const history = new ProfileAbilityHistory(); Object.assign(history, { ...data, id: undefined }); - await this.profileAbilityRepo.save(data); + await this.profileAbilityRepo.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileAbilityId = data.id; - await this.profileAbilityHistoryRepo.save(history); - + await this.profileAbilityHistoryRepo.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(); } @@ -144,7 +144,8 @@ export class ProfileAbilityController extends Controller { await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", record.profileId); const history = new ProfileAbilityHistory(); - + const before = structuredClone(record); + // const before_null = null; Object.assign(record, body); Object.assign(history, { ...record, id: undefined }); @@ -160,8 +161,10 @@ export class ProfileAbilityController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.profileAbilityRepo.save(record), - this.profileAbilityHistoryRepo.save(history), + this.profileAbilityRepo.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.profileAbilityHistoryRepo.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileAbilityEmployeeController.ts b/src/controllers/ProfileAbilityEmployeeController.ts index aaa49f16..9287a9d5 100644 --- a/src/controllers/ProfileAbilityEmployeeController.ts +++ b/src/controllers/ProfileAbilityEmployeeController.ts @@ -24,6 +24,7 @@ import HttpStatus from "../interfaces/http-status"; import HttpSuccess from "../interfaces/http-success"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-employee/ability") @Tags("ProfileAbilityEmployee") @Security("bearerAuth") @@ -88,9 +89,7 @@ export class ProfileAbilityEmployeeController extends Controller { } @Get("history/{abilityId}") - public async getProfileAbilityHistory( - @Path() abilityId: string, - ) { + public async getProfileAbilityHistory(@Path() abilityId: string) { const record = await this.profileAbilityHistoryRepo.find({ where: { profileAbilityId: abilityId }, order: { createdAt: "DESC" }, @@ -115,7 +114,7 @@ export class ProfileAbilityEmployeeController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_EMP", profile.id); - + const before = null; const data = new ProfileAbility(); const meta = { createdUserId: req.user.sub, @@ -130,9 +129,11 @@ export class ProfileAbilityEmployeeController extends Controller { const history = new ProfileAbilityHistory(); Object.assign(history, { ...data, id: undefined }); - await this.profileAbilityRepo.save(data); + await this.profileAbilityRepo.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileAbilityId = data.id; - await this.profileAbilityHistoryRepo.save(history); + await this.profileAbilityHistoryRepo.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(); } @@ -150,7 +151,8 @@ export class ProfileAbilityEmployeeController extends Controller { "SYS_REGISTRY_EMP", record.profileEmployeeId, ); - + const before = structuredClone(record); + // const before_null = null; const history = new ProfileAbilityHistory(); Object.assign(record, body); @@ -168,8 +170,10 @@ export class ProfileAbilityEmployeeController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.profileAbilityRepo.save(record), - this.profileAbilityHistoryRepo.save(history), + this.profileAbilityRepo.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.profileAbilityHistoryRepo.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileAbilityEmployeeTempController.ts b/src/controllers/ProfileAbilityEmployeeTempController.ts index dc03509f..d49d91c0 100644 --- a/src/controllers/ProfileAbilityEmployeeTempController.ts +++ b/src/controllers/ProfileAbilityEmployeeTempController.ts @@ -24,6 +24,7 @@ import HttpStatus from "../interfaces/http-status"; import HttpSuccess from "../interfaces/http-success"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-temp/ability") @Tags("ProfileAbilityEmployee") @Security("bearerAuth") @@ -84,9 +85,7 @@ export class ProfileAbilityEmployeeTempController extends Controller { } @Get("history/{abilityId}") - public async getProfileAbilityHistory( - @Path() abilityId: string, - ) { + public async getProfileAbilityHistory(@Path() abilityId: string) { const record = await this.profileAbilityHistoryRepo.find({ where: { profileAbilityId: abilityId }, order: { createdAt: "DESC" }, @@ -111,7 +110,7 @@ export class ProfileAbilityEmployeeTempController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionUpdate(req, "SYS_REGISTRY_TEMP"); - + const before = null; const data = new ProfileAbility(); const meta = { createdUserId: req.user.sub, @@ -126,9 +125,11 @@ export class ProfileAbilityEmployeeTempController extends Controller { const history = new ProfileAbilityHistory(); Object.assign(history, { ...data, id: undefined }); - await this.profileAbilityRepo.save(data); + await this.profileAbilityRepo.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileAbilityId = data.id; - await this.profileAbilityHistoryRepo.save(history); + await this.profileAbilityHistoryRepo.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(); } @@ -142,7 +143,8 @@ export class ProfileAbilityEmployeeTempController extends Controller { const record = await this.profileAbilityRepo.findOneBy({ id: abilityId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); await new permission().PermissionUpdate(req, "SYS_REGISTRY_TEMP"); - + const before = structuredClone(record); + // const before_null = null; const history = new ProfileAbilityHistory(); Object.assign(record, body); @@ -160,8 +162,10 @@ export class ProfileAbilityEmployeeTempController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.profileAbilityRepo.save(record), - this.profileAbilityHistoryRepo.save(history), + this.profileAbilityRepo.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.profileAbilityHistoryRepo.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileAddressController.ts b/src/controllers/ProfileAddressController.ts index 7226bbbd..4adccbbe 100644 --- a/src/controllers/ProfileAddressController.ts +++ b/src/controllers/ProfileAddressController.ts @@ -7,6 +7,7 @@ import { RequestWithUser } from "../middlewares/user"; import { Profile, ProfileAddressHistory, UpdateProfileAddress } from "../entities/Profile"; import { AppDataSource } from "../database/data-source"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile/address") @Tags("ProfileAddress") @Security("bearerAuth") @@ -169,7 +170,8 @@ export class ProfileAddressController extends Controller { await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profileId); const record = await this.profileRepo.findOneBy({ id: profileId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - + const before = structuredClone(record); + // const before_null = null; const history = new ProfileAddressHistory(); Object.assign(record, body); @@ -187,8 +189,10 @@ export class ProfileAddressController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.profileRepo.save(record), - this.profileAddressHistoryRepo.save(history), + this.profileRepo.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.profileAddressHistoryRepo.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileAddressEmployeeController.ts b/src/controllers/ProfileAddressEmployeeController.ts index 3728b3f9..f9e4d80c 100644 --- a/src/controllers/ProfileAddressEmployeeController.ts +++ b/src/controllers/ProfileAddressEmployeeController.ts @@ -8,6 +8,7 @@ import { ProfileAddressHistory } from "../entities/Profile"; import { AppDataSource } from "../database/data-source"; import { ProfileEmployee, UpdateProfileAddressEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-employee/address") @Tags("ProfileAddressEmployee") @Security("bearerAuth") @@ -173,7 +174,8 @@ export class ProfileAddressEmployeeController extends Controller { const record = await this.profileEmployeeRepo.findOneBy({ id: profileId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_EMP", record.id); - + const before = structuredClone(record); + // const before_null = null; const history = new ProfileAddressHistory(); Object.assign(record, body); @@ -191,8 +193,10 @@ export class ProfileAddressEmployeeController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.profileEmployeeRepo.save(record), - this.profileAddressHistoryRepo.save(history), + this.profileEmployeeRepo.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.profileAddressHistoryRepo.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileAddressEmployeeTempController.ts b/src/controllers/ProfileAddressEmployeeTempController.ts index 07359243..8802ae55 100644 --- a/src/controllers/ProfileAddressEmployeeTempController.ts +++ b/src/controllers/ProfileAddressEmployeeTempController.ts @@ -8,6 +8,7 @@ import { ProfileAddressHistory } from "../entities/Profile"; import { AppDataSource } from "../database/data-source"; import { ProfileEmployee, UpdateProfileAddressEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-temp/address") @Tags("ProfileAddressEmployee") @Security("bearerAuth") @@ -173,7 +174,8 @@ export class ProfileAddressEmployeeTempController extends Controller { await new permission().PermissionUpdate(req, "SYS_REGISTRY_TEMP"); const record = await this.profileEmployeeRepo.findOneBy({ id: profileId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - + const before = structuredClone(record); + // const before_null = null; const history = new ProfileAddressHistory(); Object.assign(record, body); @@ -191,8 +193,10 @@ export class ProfileAddressEmployeeTempController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.profileEmployeeRepo.save(record), - this.profileAddressHistoryRepo.save(history), + this.profileEmployeeRepo.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.profileAddressHistoryRepo.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileAssessmentsController.ts b/src/controllers/ProfileAssessmentsController.ts index 3e8e084f..0c1dfe82 100644 --- a/src/controllers/ProfileAssessmentsController.ts +++ b/src/controllers/ProfileAssessmentsController.ts @@ -24,6 +24,7 @@ import { ProfileAssessmentHistory } from "../entities/ProfileAssessmentHistory"; import { Profile } from "../entities/Profile"; import { RequestWithUser } from "../middlewares/user"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile/assessments") @Tags("ProfileAssessments") @Security("bearerAuth") @@ -123,7 +124,7 @@ export class ProfileAssessmentsController extends Controller { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); } await new permission().PermissionOrgUserCreate(req, "SYS_REGISTRY_OFFICER", profile.id); - + const before = null; const data = new ProfileAssessment(); const meta = { createdUserId: req.user.sub, @@ -137,9 +138,11 @@ export class ProfileAssessmentsController extends Controller { const history = new ProfileAssessmentHistory(); Object.assign(history, { ...data, id: undefined }); - await this.profileAssessmentsRepository.save(data); + await this.profileAssessmentsRepository.save(data, { data: req }); + setLogDataDiff( req, { before, after: data } ); history.profileAssessmentId = data.id; - await this.profileAssessmentsHistoryRepository.save(history); + await this.profileAssessmentsHistoryRepository.save(history, { data: req }); + setLogDataDiff( req, { before, after: history } ); return new HttpSuccess(); } @@ -153,7 +156,8 @@ export class ProfileAssessmentsController extends Controller { const record = await this.profileAssessmentsRepository.findOneBy({ id: assessmentId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", record.profileId); - + const before = structuredClone(record); + const before_null = null; const history = new ProfileAssessmentHistory(); Object.assign(record, body); @@ -171,8 +175,10 @@ export class ProfileAssessmentsController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.profileAssessmentsRepository.save(record), - this.profileAssessmentsHistoryRepository.save(history), + this.profileAssessmentsRepository.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.profileAssessmentsHistoryRepository.save(history, { data: req }), + setLogDataDiff(req, { before, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileAssessmentsEmployeeController.ts b/src/controllers/ProfileAssessmentsEmployeeController.ts index d187dea9..97774c79 100644 --- a/src/controllers/ProfileAssessmentsEmployeeController.ts +++ b/src/controllers/ProfileAssessmentsEmployeeController.ts @@ -24,6 +24,7 @@ import { ProfileAssessmentHistory } from "../entities/ProfileAssessmentHistory"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import { RequestWithUser } from "../middlewares/user"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-employee/assessments") @Tags("ProfileEmployeeAssessments") @Security("bearerAuth") @@ -120,7 +121,7 @@ export class ProfileAssessmentsEmployeeController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_EMP", profile.id); - + const before = null; const data = new ProfileAssessment(); const meta = { createdUserId: req.user.sub, @@ -134,9 +135,11 @@ export class ProfileAssessmentsEmployeeController extends Controller { const history = new ProfileAssessmentHistory(); Object.assign(history, { ...data, id: undefined }); - await this.profileAssessmentsRepository.save(data); + await this.profileAssessmentsRepository.save(data, { data: req }); + setLogDataDiff( req, { before, after: data }); history.profileAssessmentId = data.id; - await this.profileAssessmentsHistoryRepository.save(history); + await this.profileAssessmentsHistoryRepository.save(history, { data: req }); + setLogDataDiff( req, { before, after: history }); return new HttpSuccess(); } @@ -155,6 +158,8 @@ export class ProfileAssessmentsEmployeeController extends Controller { record.profileEmployeeId, ); + const before = structuredClone(record); + // const before_null = null; const history = new ProfileAssessmentHistory(); Object.assign(record, body); @@ -172,8 +177,10 @@ export class ProfileAssessmentsEmployeeController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.profileAssessmentsRepository.save(record), - this.profileAssessmentsHistoryRepository.save(history), + this.profileAssessmentsRepository.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.profileAssessmentsHistoryRepository.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileAssessmentsEmployeeTempController.ts b/src/controllers/ProfileAssessmentsEmployeeTempController.ts index 9d4e63c8..e0f0ba57 100644 --- a/src/controllers/ProfileAssessmentsEmployeeTempController.ts +++ b/src/controllers/ProfileAssessmentsEmployeeTempController.ts @@ -24,6 +24,7 @@ import { ProfileAssessmentHistory } from "../entities/ProfileAssessmentHistory"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import { RequestWithUser } from "../middlewares/user"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-temp/assessments") @Tags("ProfileEmployeeAssessments") @Security("bearerAuth") @@ -88,9 +89,7 @@ export class ProfileAssessmentsEmployeeTempController extends Controller { } @Get("history/{assessmentId}") - public async getProfileAssessmentsHistory( - @Path() assessmentId: string, - ) { + public async getProfileAssessmentsHistory(@Path() assessmentId: string) { const record = await this.profileAssessmentsHistoryRepository.find({ where: { profileAssessmentId: assessmentId, @@ -119,7 +118,7 @@ export class ProfileAssessmentsEmployeeTempController extends Controller { if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } - + const before = null; const data = new ProfileAssessment(); const meta = { createdUserId: req.user.sub, @@ -133,9 +132,11 @@ export class ProfileAssessmentsEmployeeTempController extends Controller { const history = new ProfileAssessmentHistory(); Object.assign(history, { ...data, id: undefined }); - await this.profileAssessmentsRepository.save(data); + await this.profileAssessmentsRepository.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileAssessmentId = data.id; - await this.profileAssessmentsHistoryRepository.save(history); + await this.profileAssessmentsHistoryRepository.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(); } @@ -149,7 +150,8 @@ export class ProfileAssessmentsEmployeeTempController extends Controller { await new permission().PermissionUpdate(req, "SYS_REGISTRY_TEMP"); const record = await this.profileAssessmentsRepository.findOneBy({ id: assessmentId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - + const before = structuredClone(record); + // const before_null = null; const history = new ProfileAssessmentHistory(); Object.assign(record, body); @@ -167,8 +169,10 @@ export class ProfileAssessmentsEmployeeTempController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.profileAssessmentsRepository.save(record), - this.profileAssessmentsHistoryRepository.save(history), + this.profileAssessmentsRepository.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.profileAssessmentsHistoryRepository.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileCertificateController.ts b/src/controllers/ProfileCertificateController.ts index 691f1290..1109f1f2 100644 --- a/src/controllers/ProfileCertificateController.ts +++ b/src/controllers/ProfileCertificateController.ts @@ -24,6 +24,7 @@ import { ProfileCertificateHistory } from "../entities/ProfileCertificateHistory import { RequestWithUser } from "../middlewares/user"; import { Profile } from "../entities/Profile"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile/certificate") @Tags("ProfileCertificate") @Security("bearerAuth") @@ -56,7 +57,10 @@ export class ProfileCertificateController extends Controller { } @Get("admin/history/{certificateId}") - public async certificateAdminHistory(@Path() certificateId: string, @Request() req: RequestWithUser) { + public async certificateAdminHistory( + @Path() certificateId: string, + @Request() req: RequestWithUser, + ) { const _record = await this.certificateRepo.findOneBy({ id: certificateId }); if (_record) { await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", _record.profileId); @@ -96,7 +100,7 @@ export class ProfileCertificateController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profile.id); - + const before = null; const data = new ProfileCertificate(); const meta = { @@ -112,9 +116,11 @@ export class ProfileCertificateController extends Controller { const history = new ProfileCertificateHistory(); Object.assign(history, { ...data, id: undefined }); - await this.certificateRepo.save(data); + await this.certificateRepo.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileCertificateId = data.id; - await this.certificateHistoryRepo.save(history); + await this.certificateHistoryRepo.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(); } @@ -128,7 +134,8 @@ export class ProfileCertificateController extends Controller { const record = await this.certificateRepo.findOneBy({ id: certificateId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", record.profileId); - + const before = structuredClone(record); + // const before_null = null; const history = new ProfileCertificateHistory(); Object.assign(record, body); @@ -146,8 +153,10 @@ export class ProfileCertificateController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.certificateRepo.save(record), - this.certificateHistoryRepo.save(history), + this.certificateRepo.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.certificateHistoryRepo.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileCertificateEmployeeController.ts b/src/controllers/ProfileCertificateEmployeeController.ts index 8ede9f2f..04d045e8 100644 --- a/src/controllers/ProfileCertificateEmployeeController.ts +++ b/src/controllers/ProfileCertificateEmployeeController.ts @@ -24,6 +24,7 @@ import { ProfileCertificateHistory } from "../entities/ProfileCertificateHistory import { RequestWithUser } from "../middlewares/user"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-employee/certificate") @Tags("ProfileEmployeeCertificate") @Security("bearerAuth") @@ -56,7 +57,10 @@ export class ProfileCertificateEmployeeController extends Controller { } @Get("admin/history/{certificateId}") - public async certificateAdminHistory(@Path() certificateId: string, @Request() req: RequestWithUser) { + public async certificateAdminHistory( + @Path() certificateId: string, + @Request() req: RequestWithUser, + ) { const _record = await this.certificateRepo.findOneBy({ id: certificateId }); if (_record) { await new permission().PermissionOrgUserGet( @@ -73,7 +77,7 @@ export class ProfileCertificateEmployeeController extends Controller { }); return new HttpSuccess(record); } - + @Get("history/{certificateId}") public async certificateHistory(@Path() certificateId: string) { const record = await this.certificateHistoryRepo.find({ @@ -100,6 +104,7 @@ export class ProfileCertificateEmployeeController extends Controller { } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_EMP", profile.id); + const before = null; const data = new ProfileCertificate(); const meta = { @@ -115,9 +120,11 @@ export class ProfileCertificateEmployeeController extends Controller { const history = new ProfileCertificateHistory(); Object.assign(history, { ...data, id: undefined }); - await this.certificateRepo.save(data); + await this.certificateRepo.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileCertificateId = data.id; - await this.certificateHistoryRepo.save(history); + await this.certificateHistoryRepo.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(); } @@ -135,7 +142,8 @@ export class ProfileCertificateEmployeeController extends Controller { "SYS_REGISTRY_EMP", record.profileEmployeeId, ); - + const before = structuredClone(record); + // const before_null = null; const history = new ProfileCertificateHistory(); Object.assign(record, body); @@ -153,8 +161,10 @@ export class ProfileCertificateEmployeeController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.certificateRepo.save(record), - this.certificateHistoryRepo.save(history), + this.certificateRepo.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.certificateHistoryRepo.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileCertificateEmployeeTempController.ts b/src/controllers/ProfileCertificateEmployeeTempController.ts index 6b5f7e17..e7a073fe 100644 --- a/src/controllers/ProfileCertificateEmployeeTempController.ts +++ b/src/controllers/ProfileCertificateEmployeeTempController.ts @@ -24,6 +24,7 @@ import { ProfileCertificateHistory } from "../entities/ProfileCertificateHistory import { RequestWithUser } from "../middlewares/user"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-temp/certificate") @Tags("ProfileEmployeeCertificate") @Security("bearerAuth") @@ -87,7 +88,7 @@ export class ProfileCertificateEmployeeTempController extends Controller { if (!body.profileEmployeeId) { throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileEmployeeId"); } - + const before = null; const profile = await this.profileEmployeeRepo.findOneBy({ id: body.profileEmployeeId }); if (!profile) { @@ -109,9 +110,11 @@ export class ProfileCertificateEmployeeTempController extends Controller { const history = new ProfileCertificateHistory(); Object.assign(history, { ...data, id: undefined }); - await this.certificateRepo.save(data); + await this.certificateRepo.save(data, { data: req }); + setLogDataDiff( req , {before, after: data}); history.profileCertificateId = data.id; - await this.certificateHistoryRepo.save(history); + await this.certificateHistoryRepo.save(history, { data: req }); + setLogDataDiff( req , {before, after: history}); return new HttpSuccess(); } @@ -126,7 +129,8 @@ export class ProfileCertificateEmployeeTempController extends Controller { const record = await this.certificateRepo.findOneBy({ id: certificateId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - + const before = structuredClone(record); + const before_null = null; const history = new ProfileCertificateHistory(); Object.assign(record, body); @@ -144,10 +148,12 @@ export class ProfileCertificateEmployeeTempController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.certificateRepo.save(record), - this.certificateHistoryRepo.save(history), + this.certificateRepo.save(record, { data: req }), + setLogDataDiff( req , {before, after: record}), + this.certificateHistoryRepo.save(history, { data: req }), + setLogDataDiff( req , {before: before_null, after: history}), ]); - + return new HttpSuccess(); } diff --git a/src/controllers/ProfileChangeNameController.ts b/src/controllers/ProfileChangeNameController.ts index 85100d84..8a4bc4df 100644 --- a/src/controllers/ProfileChangeNameController.ts +++ b/src/controllers/ProfileChangeNameController.ts @@ -25,6 +25,7 @@ import { } from "../entities/ProfileChangeName"; import { updateName } from "../keycloak"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile/changeName") @Tags("ProfileChangeName") @Security("bearerAuth") @@ -83,7 +84,7 @@ export class ProfileChangeNameController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profile.id); - + const before = null; const data = new ProfileChangeName(); const meta = { @@ -101,12 +102,13 @@ export class ProfileChangeNameController extends Controller { await this.changeNameRepository.save(data); history.profileChangeNameId = data.id; - await this.changeNameHistoryRepository.save(history); - + await this.changeNameHistoryRepository.save(history, { data: req }); + setLogDataDiff( req, { before, after: history }) profile.firstName = body.firstName ?? profile.firstName; profile.lastName = body.lastName ?? profile.lastName; profile.prefix = body.prefix ?? profile.prefix; - await this.profileRepository.save(profile); + await this.profileRepository.save(profile, { data:req }); + setLogDataDiff(req, { before, after: profile }); if (profile != null && profile.keycloak != null) { const result = await updateName(profile.keycloak, profile.firstName, profile.lastName); @@ -127,7 +129,8 @@ export class ProfileChangeNameController extends Controller { const record = await this.changeNameRepository.findOneBy({ id: changeNameId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", record.profileId); - + const before = structuredClone(record); + const before_null = null; const history = new ProfileChangeNameHistory(); Object.assign(record, body); @@ -145,8 +148,10 @@ export class ProfileChangeNameController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.changeNameRepository.save(record), - this.changeNameHistoryRepository.save(history), + this.changeNameRepository.save(record, { data: req }), + setLogDataDiff( req , {before, after: record}), + this.changeNameHistoryRepository.save(history, { data: req }), + setLogDataDiff( req , {before: before_null, after: history}), ]); const chkLastRecord = await this.changeNameRepository.findOne({ @@ -158,14 +163,15 @@ export class ProfileChangeNameController extends Controller { }, }); if (!chkLastRecord) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - + const profile = await this.profileRepository.findOneBy({ id: record.profileId }); - + const before_profile = structuredClone(profile); if (profile && chkLastRecord.id === record.id) { profile.firstName = body.firstName ?? profile.firstName; profile.lastName = body.lastName ?? profile.lastName; profile.prefix = body.prefix ?? profile.prefix; - await this.profileRepository.save(profile); + await this.profileRepository.save(profile, { data: req }); + setLogDataDiff( req , {before: before_profile, after: profile}); } // ปิดไว้ก่อนเพราะ error ต้องใช้ keycloak ที่มีสิทธิ์ในการ update //update 17/07 diff --git a/src/controllers/ProfileChangeNameEmployeeController.ts b/src/controllers/ProfileChangeNameEmployeeController.ts index 8accbad2..b2ca5892 100644 --- a/src/controllers/ProfileChangeNameEmployeeController.ts +++ b/src/controllers/ProfileChangeNameEmployeeController.ts @@ -25,6 +25,7 @@ import { import { ProfileEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; import { updateName } from "../keycloak"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-employee/changeName") @Tags("ProfileChangeNameEmployee") @Security("bearerAuth") @@ -87,7 +88,7 @@ export class ProfileChangeNameEmployeeController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_EMP", profile.id); - + const before = null; const data = new ProfileChangeName(); const meta = { @@ -103,14 +104,17 @@ export class ProfileChangeNameEmployeeController extends Controller { const history = new ProfileChangeNameHistory(); Object.assign(history, { ...data, id: undefined }); - await this.changeNameRepository.save(data); + await this.changeNameRepository.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileChangeNameId = data.id; - await this.changeNameHistoryRepository.save(history); + await this.changeNameHistoryRepository.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); profile.firstName = body.firstName ?? profile.firstName; profile.lastName = body.lastName ?? profile.lastName; profile.prefix = body.prefix ?? profile.prefix; - await this.profileEmployeeRepo.save(profile); + await this.profileEmployeeRepo.save(profile, { data: req }); + setLogDataDiff(req, { before, after: profile }); if (profile != null && profile.keycloak != null) { const result = await updateName(profile.keycloak, profile.firstName, profile.lastName); diff --git a/src/controllers/ProfileChangeNameEmployeeTempController.ts b/src/controllers/ProfileChangeNameEmployeeTempController.ts index 69e27fd7..966afb6e 100644 --- a/src/controllers/ProfileChangeNameEmployeeTempController.ts +++ b/src/controllers/ProfileChangeNameEmployeeTempController.ts @@ -25,6 +25,7 @@ import { import { ProfileEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; import { updateName } from "../keycloak"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-temp/changeName") @Tags("ProfileChangeNameEmployee") @Security("bearerAuth") @@ -81,7 +82,7 @@ export class ProfileChangeNameEmployeeTempController extends Controller { if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } - + const before = null; const data = new ProfileChangeName(); const meta = { @@ -97,14 +98,17 @@ export class ProfileChangeNameEmployeeTempController extends Controller { const history = new ProfileChangeNameHistory(); Object.assign(history, { ...data, id: undefined }); - await this.changeNameRepository.save(data); + await this.changeNameRepository.save(data, { data: req }); + setLogDataDiff( req , {before, after: data}); history.profileChangeNameId = data.id; - await this.changeNameHistoryRepository.save(history); + await this.changeNameHistoryRepository.save(history, { data: req }); + setLogDataDiff( req , {before, after: history}); profile.firstName = body.firstName ?? profile.firstName; profile.lastName = body.lastName ?? profile.lastName; profile.prefix = body.prefix ?? profile.prefix; - await this.profileEmployeeRepo.save(profile); + await this.profileEmployeeRepo.save(profile, {data: req}); + setLogDataDiff( req, {before, after: profile}); if (profile != null && profile.keycloak != null) { const result = await updateName(profile.keycloak, profile.firstName, profile.lastName); @@ -126,7 +130,8 @@ export class ProfileChangeNameEmployeeTempController extends Controller { const record = await this.changeNameRepository.findOneBy({ id: changeNameId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - + const before = structuredClone(record); + // const before_null = null; const history = new ProfileChangeNameHistory(); Object.assign(record, body); @@ -144,8 +149,10 @@ export class ProfileChangeNameEmployeeTempController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.changeNameRepository.save(record), - this.changeNameHistoryRepository.save(history), + this.changeNameRepository.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.changeNameHistoryRepository.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); const chkLastRecord = await this.changeNameRepository.findOne({ diff --git a/src/controllers/ProfileChildrenController.ts b/src/controllers/ProfileChildrenController.ts index b9b010bd..d98ee6d5 100644 --- a/src/controllers/ProfileChildrenController.ts +++ b/src/controllers/ProfileChildrenController.ts @@ -25,6 +25,7 @@ import { } from "../entities/ProfileChildren"; import Extension from "../interfaces/extension"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile/family/children") @Tags("ProfileChildren") @Security("bearerAuth") @@ -76,7 +77,7 @@ export class ProfileChildrenController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profile.id); - + const before = null; const data = new ProfileChildren(); const meta = { createdUserId: req.user.sub, @@ -93,9 +94,11 @@ export class ProfileChildrenController extends Controller { const history = new ProfileChildrenHistory(); Object.assign(history, { ...data, id: undefined }); - await this.childrenRepository.save(data); + await this.childrenRepository.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileChildrenId = data.id; - await this.childrenHistoryRepository.save(history); + await this.childrenHistoryRepository.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(); } @@ -109,7 +112,8 @@ export class ProfileChildrenController extends Controller { const record = await this.childrenRepository.findOneBy({ id: childrenId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", record.profileId); - + const before = structuredClone(record); + // const before_null = null; const history = new ProfileChildrenHistory(); Object.assign(record, body); Object.assign(history, { ...record, id: undefined }); @@ -126,8 +130,10 @@ export class ProfileChildrenController extends Controller { history.createdAt = new Date(); history.lastUpdatedAt = new Date(); await Promise.all([ - this.childrenRepository.save(record), - this.childrenHistoryRepository.save(history), + this.childrenRepository.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.childrenHistoryRepository.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileChildrenEmployeeController.ts b/src/controllers/ProfileChildrenEmployeeController.ts index 5c2510fc..5d01f683 100644 --- a/src/controllers/ProfileChildrenEmployeeController.ts +++ b/src/controllers/ProfileChildrenEmployeeController.ts @@ -25,6 +25,7 @@ import { import { ProfileEmployee } from "../entities/ProfileEmployee"; import Extension from "../interfaces/extension"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-employee/family/children") @Tags("ProfileChildren") @Security("bearerAuth") @@ -83,7 +84,7 @@ export class ProfileChildrenEmployeeController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_EMP", profile.id); - + const before = null; const data = new ProfileChildren(); const meta = { @@ -101,10 +102,11 @@ export class ProfileChildrenEmployeeController extends Controller { const history = new ProfileChildrenHistory(); Object.assign(history, { ...data, id: undefined }); - await this.childrenRepository.save(data); + await this.childrenRepository.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileChildrenId = data.id; - await this.childrenHistoryRepository.save(history); - + await this.childrenHistoryRepository.save(history, { data: req }); + setLogDataDiff( req, { before, after: history }); return new HttpSuccess(); } @@ -121,7 +123,8 @@ export class ProfileChildrenEmployeeController extends Controller { "SYS_REGISTRY_EMP", record.profileEmployeeId, ); - + const before = structuredClone(record); + // const before_null = null; const history = new ProfileChildrenHistory(); Object.assign(record, body); Object.assign(history, { ...record, id: undefined }); @@ -139,8 +142,10 @@ export class ProfileChildrenEmployeeController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.childrenRepository.save(record), - this.childrenHistoryRepository.save(history), + this.childrenRepository.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.childrenHistoryRepository.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileChildrenEmployeeTempController.ts b/src/controllers/ProfileChildrenEmployeeTempController.ts index 9a6ce934..8d232c0a 100644 --- a/src/controllers/ProfileChildrenEmployeeTempController.ts +++ b/src/controllers/ProfileChildrenEmployeeTempController.ts @@ -25,6 +25,7 @@ import { import { ProfileEmployee } from "../entities/ProfileEmployee"; import Extension from "../interfaces/extension"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-temp/family/children") @Tags("ProfileChildren") @Security("bearerAuth") @@ -77,7 +78,7 @@ export class ProfileChildrenEmployeeTempController extends Controller { if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } - + const before = null; const data = new ProfileChildren(); const meta = { @@ -94,9 +95,11 @@ export class ProfileChildrenEmployeeTempController extends Controller { const history = new ProfileChildrenHistory(); Object.assign(history, { ...data, id: undefined }); - await this.childrenRepository.save(data); + await this.childrenRepository.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileChildrenId = data.id; - await this.childrenHistoryRepository.save(history); + await this.childrenHistoryRepository.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(); } @@ -110,7 +113,8 @@ export class ProfileChildrenEmployeeTempController extends Controller { await new permission().PermissionUpdate(req, "SYS_REGISTRY_TEMP"); const record = await this.childrenRepository.findOneBy({ id: childrenId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - + const before = structuredClone(record); + // const before_null = null; const history = new ProfileChildrenHistory(); Object.assign(record, body); Object.assign(history, { ...record, id: undefined }); @@ -128,8 +132,10 @@ export class ProfileChildrenEmployeeTempController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.childrenRepository.save(record), - this.childrenHistoryRepository.save(history), + this.childrenRepository.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.childrenHistoryRepository.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileDisciplineController.ts b/src/controllers/ProfileDisciplineController.ts index 65687996..5dd76287 100644 --- a/src/controllers/ProfileDisciplineController.ts +++ b/src/controllers/ProfileDisciplineController.ts @@ -24,6 +24,7 @@ import { UpdateProfileDiscipline, } from "../entities/ProfileDiscipline"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile/discipline") @Tags("ProfileDiscipline") @Security("bearerAuth") @@ -66,7 +67,10 @@ export class ProfileDisciplineController extends Controller { } @Get("admin/history/{disciplineId}") - public async disciplineAdminHistory(@Path() disciplineId: string, @Request() req: RequestWithUser) { + public async disciplineAdminHistory( + @Path() disciplineId: string, + @Request() req: RequestWithUser, + ) { const _record = await this.disciplineRepository.findOneBy({ id: disciplineId }); if (_record) { await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", _record.profileId); @@ -87,7 +91,7 @@ export class ProfileDisciplineController extends Controller { return new HttpSuccess(record); } - @Post() + @Post() public async newDiscipline( @Request() req: RequestWithUser, @Body() body: CreateProfileDiscipline, @@ -102,7 +106,7 @@ export class ProfileDisciplineController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profile.id); - + const before = null; const data = new ProfileDiscipline(); const meta = { @@ -118,9 +122,11 @@ export class ProfileDisciplineController extends Controller { const history = new ProfileDisciplineHistory(); Object.assign(history, { ...data, id: undefined }); - await this.disciplineRepository.save(data); + await this.disciplineRepository.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileDisciplineId = data.id; - await this.disciplineHistoryRepository.save(history); + await this.disciplineHistoryRepository.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(); } @@ -134,7 +140,8 @@ export class ProfileDisciplineController extends Controller { const record = await this.disciplineRepository.findOneBy({ id: disciplineId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", record.profileId); - + const before = structuredClone(record); + const before_null = null; const history = new ProfileDisciplineHistory(); Object.assign(record, body); @@ -152,8 +159,10 @@ export class ProfileDisciplineController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.disciplineRepository.save(record), - this.disciplineHistoryRepository.save(history), + this.disciplineRepository.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.disciplineHistoryRepository.save(history, { data: req }), + setLogDataDiff(req, { before, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileDisciplineEmployeeController.ts b/src/controllers/ProfileDisciplineEmployeeController.ts index 3883288b..29d05050 100644 --- a/src/controllers/ProfileDisciplineEmployeeController.ts +++ b/src/controllers/ProfileDisciplineEmployeeController.ts @@ -24,6 +24,7 @@ import { } from "../entities/ProfileDiscipline"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-employee/discipline") @Tags("ProfileDisciplineEmployee") @Security("bearerAuth") @@ -66,7 +67,10 @@ export class ProfileDisciplineEmployeeController extends Controller { } @Get("admin/history/{disciplineId}") - public async disciplineAdminHistory(@Path() disciplineId: string, @Request() req: RequestWithUser) { + public async disciplineAdminHistory( + @Path() disciplineId: string, + @Request() req: RequestWithUser, + ) { const _record = await this.disciplineRepository.findOneBy({ id: disciplineId }); if (_record) { await new permission().PermissionOrgUserGet( @@ -105,7 +109,7 @@ export class ProfileDisciplineEmployeeController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_EMP", profile.id); - + const before = null; const data = new ProfileDiscipline(); const meta = { @@ -121,10 +125,11 @@ export class ProfileDisciplineEmployeeController extends Controller { const history = new ProfileDisciplineHistory(); Object.assign(history, { ...data, id: undefined }); - await this.disciplineRepository.save(data); + await this.disciplineRepository.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileDisciplineId = data.id; - await this.disciplineHistoryRepository.save(history); - + await this.disciplineHistoryRepository.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(); } @@ -141,7 +146,8 @@ export class ProfileDisciplineEmployeeController extends Controller { "SYS_REGISTRY_EMP", record.profileEmployeeId, ); - + const before = structuredClone(record); + // const before_null = null; const history = new ProfileDisciplineHistory(); Object.assign(record, body); @@ -159,8 +165,10 @@ export class ProfileDisciplineEmployeeController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.disciplineRepository.save(record), - this.disciplineHistoryRepository.save(history), + this.disciplineRepository.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.disciplineHistoryRepository.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileDisciplineEmployeeTempController.ts b/src/controllers/ProfileDisciplineEmployeeTempController.ts index c9a3cfa4..af900ad9 100644 --- a/src/controllers/ProfileDisciplineEmployeeTempController.ts +++ b/src/controllers/ProfileDisciplineEmployeeTempController.ts @@ -24,6 +24,7 @@ import { } from "../entities/ProfileDiscipline"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-temp/discipline") @Tags("ProfileDisciplineEmployee") @Security("bearerAuth") @@ -66,7 +67,10 @@ export class ProfileDisciplineEmployeeTempController extends Controller { } @Get("admin/history/{disciplineId}") - public async disciplineAdminHistory(@Path() disciplineId: string, @Request() req: RequestWithUser) { + public async disciplineAdminHistory( + @Path() disciplineId: string, + @Request() req: RequestWithUser, + ) { await new permission().PermissionGet(req, "SYS_REGISTRY_TEMP"); const record = await this.disciplineHistoryRepository.find({ where: { profileDisciplineId: disciplineId }, @@ -99,7 +103,7 @@ export class ProfileDisciplineEmployeeTempController extends Controller { if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } - + const before = null; const data = new ProfileDiscipline(); const meta = { @@ -115,9 +119,11 @@ export class ProfileDisciplineEmployeeTempController extends Controller { const history = new ProfileDisciplineHistory(); Object.assign(history, { ...data, id: undefined }); - await this.disciplineRepository.save(data); + await this.disciplineRepository.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileDisciplineId = data.id; - await this.disciplineHistoryRepository.save(history); + await this.disciplineHistoryRepository.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(); } @@ -132,7 +138,8 @@ export class ProfileDisciplineEmployeeTempController extends Controller { const record = await this.disciplineRepository.findOneBy({ id: disciplineId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - + const before = structuredClone(record); + // const before_null = null; const history = new ProfileDisciplineHistory(); Object.assign(record, body); @@ -150,8 +157,10 @@ export class ProfileDisciplineEmployeeTempController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.disciplineRepository.save(record), - this.disciplineHistoryRepository.save(history), + this.disciplineRepository.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.disciplineHistoryRepository.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileDutyController.ts b/src/controllers/ProfileDutyController.ts index 16673740..5981e1db 100644 --- a/src/controllers/ProfileDutyController.ts +++ b/src/controllers/ProfileDutyController.ts @@ -20,6 +20,7 @@ import { RequestWithUser } from "../middlewares/user"; import { Profile } from "../entities/Profile"; import { CreateProfileDuty, ProfileDuty, UpdateProfileDuty } from "../entities/ProfileDuty"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile/duty") @Tags("ProfileDuty") @Security("bearerAuth") @@ -65,7 +66,7 @@ export class ProfileDutyController extends Controller { } @Get("history/{dutyId}") - public async dutyHistory(@Path() dutyId: string,) { + public async dutyHistory(@Path() dutyId: string) { const record = await this.dutyHistoryRepository.find({ where: { profileDutyId: dutyId }, order: { createdAt: "DESC" }, @@ -84,7 +85,7 @@ export class ProfileDutyController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profile.id); - + const before = null; const data = new ProfileDuty(); const meta = { @@ -100,10 +101,11 @@ export class ProfileDutyController extends Controller { const history = new ProfileDutyHistory(); Object.assign(history, { ...data, id: undefined }); - await this.dutyRepository.save(data); + await this.dutyRepository.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileDutyId = data.id; - await this.dutyHistoryRepository.save(history); - + await this.dutyHistoryRepository.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(); } @@ -116,7 +118,8 @@ export class ProfileDutyController extends Controller { const record = await this.dutyRepository.findOneBy({ id: dutyId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", record.profileId); - + const before = structuredClone(record); + // const before_null = null; const history = new ProfileDutyHistory(); Object.assign(record, body); @@ -133,7 +136,12 @@ export class ProfileDutyController extends Controller { history.createdAt = new Date(); history.lastUpdatedAt = new Date(); - await Promise.all([this.dutyRepository.save(record), this.dutyHistoryRepository.save(history)]); + await Promise.all([ + this.dutyRepository.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.dutyHistoryRepository.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), + ]); return new HttpSuccess(); } diff --git a/src/controllers/ProfileDutyEmployeeController.ts b/src/controllers/ProfileDutyEmployeeController.ts index 9bdf00ef..13b692a0 100644 --- a/src/controllers/ProfileDutyEmployeeController.ts +++ b/src/controllers/ProfileDutyEmployeeController.ts @@ -20,6 +20,7 @@ import { RequestWithUser } from "../middlewares/user"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import { CreateProfileEmployeeDuty, ProfileDuty, UpdateProfileDuty } from "../entities/ProfileDuty"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-employee/duty") @Tags("ProfileEmployeeDuty") @Security("bearerAuth") @@ -88,7 +89,7 @@ export class ProfileDutyEmployeeController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_EMP", profile.id); - + const before = null; const data = new ProfileDuty(); const meta = { @@ -104,9 +105,11 @@ export class ProfileDutyEmployeeController extends Controller { const history = new ProfileDutyHistory(); Object.assign(history, { ...data, id: undefined }); - await this.dutyRepository.save(data); + await this.dutyRepository.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileDutyId = data.id; - await this.dutyHistoryRepository.save(history); + await this.dutyHistoryRepository.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(); } @@ -124,7 +127,8 @@ export class ProfileDutyEmployeeController extends Controller { "SYS_REGISTRY_EMP", record.profileEmployeeId, ); - + const before = structuredClone(record); + const before_null = null; const history = new ProfileDutyHistory(); Object.assign(record, body); @@ -141,7 +145,12 @@ export class ProfileDutyEmployeeController extends Controller { history.createdAt = new Date(); history.lastUpdatedAt = new Date(); - await Promise.all([this.dutyRepository.save(record), this.dutyHistoryRepository.save(history)]); + await Promise.all([ + this.dutyRepository.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.dutyHistoryRepository.save(history, { data: req }), + setLogDataDiff(req, { before, after: history }), + ]); return new HttpSuccess(); } diff --git a/src/controllers/ProfileDutyEmployeeTempController.ts b/src/controllers/ProfileDutyEmployeeTempController.ts index b4dcbb4f..dc34f8b9 100644 --- a/src/controllers/ProfileDutyEmployeeTempController.ts +++ b/src/controllers/ProfileDutyEmployeeTempController.ts @@ -20,6 +20,7 @@ import { RequestWithUser } from "../middlewares/user"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import { CreateProfileEmployeeDuty, ProfileDuty, UpdateProfileDuty } from "../entities/ProfileDuty"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-temp/duty") @Tags("ProfileEmployeeDuty") @Security("bearerAuth") @@ -82,7 +83,7 @@ export class ProfileDutyEmployeeTempController extends Controller { if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } - + const before = null; const data = new ProfileDuty(); const meta = { @@ -98,9 +99,11 @@ export class ProfileDutyEmployeeTempController extends Controller { const history = new ProfileDutyHistory(); Object.assign(history, { ...data, id: undefined }); - await this.dutyRepository.save(data); + await this.dutyRepository.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileDutyId = data.id; - await this.dutyHistoryRepository.save(history); + await this.dutyHistoryRepository.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(); } @@ -115,7 +118,8 @@ export class ProfileDutyEmployeeTempController extends Controller { const record = await this.dutyRepository.findOneBy({ id: dutyId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - + const before = structuredClone(record); + // const before_null = null; const history = new ProfileDutyHistory(); Object.assign(record, body); @@ -132,7 +136,12 @@ export class ProfileDutyEmployeeTempController extends Controller { history.createdAt = new Date(); history.lastUpdatedAt = new Date(); - await Promise.all([this.dutyRepository.save(record), this.dutyHistoryRepository.save(history)]); + await Promise.all([ + this.dutyRepository.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.dutyHistoryRepository.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), + ]); return new HttpSuccess(); } diff --git a/src/controllers/ProfileEducationsController.ts b/src/controllers/ProfileEducationsController.ts index d1462793..4c9d702b 100644 --- a/src/controllers/ProfileEducationsController.ts +++ b/src/controllers/ProfileEducationsController.ts @@ -26,6 +26,7 @@ import { Profile } from "../entities/Profile"; import { ProfileEducationHistory } from "../entities/ProfileEducationHistory"; import { AppDataSource } from "../database/data-source"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile/educations") @Tags("ProfileEducations") @Security("bearerAuth") @@ -85,9 +86,7 @@ export class ProfileEducationsController extends Controller { } @Get("history/{educationId}") - public async getProfileEducationHistory( - @Path() educationId: string, - ) { + public async getProfileEducationHistory(@Path() educationId: string) { const record = await this.profileEducationHistoryRepo.find({ where: { profileEducationId: educationId, @@ -114,7 +113,7 @@ export class ProfileEducationsController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profile.id); - + const before = null; const data = new ProfileEducation(); const meta = { createdUserId: req.user.sub, @@ -129,9 +128,11 @@ export class ProfileEducationsController extends Controller { const history = new ProfileEducationHistory(); Object.assign(history, { ...data, id: undefined }); - await this.profileEducationRepo.save(data); + await this.profileEducationRepo.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileEducationId = data.id; - await this.profileEducationHistoryRepo.save(history); + await this.profileEducationHistoryRepo.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(); } @@ -145,7 +146,8 @@ export class ProfileEducationsController extends Controller { const record = await this.profileEducationRepo.findOneBy({ id: educationId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", record.profileId); - + const before = structuredClone(record); + // const before_null = null; const history = new ProfileEducationHistory(); Object.assign(record, body); @@ -163,8 +165,10 @@ export class ProfileEducationsController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.profileEducationRepo.save(record), - this.profileEducationHistoryRepo.save(history), + this.profileEducationRepo.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.profileEducationHistoryRepo.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileEducationsEmployeeController.ts b/src/controllers/ProfileEducationsEmployeeController.ts index 6631cab8..189e8889 100644 --- a/src/controllers/ProfileEducationsEmployeeController.ts +++ b/src/controllers/ProfileEducationsEmployeeController.ts @@ -24,6 +24,7 @@ import { ProfileEducationHistory } from "../entities/ProfileEducationHistory"; import { AppDataSource } from "../database/data-source"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-employee/educations") @Tags("ProfileEducationsEmployee") @Security("bearerAuth") @@ -91,9 +92,7 @@ export class ProfileEducationsEmployeeController extends Controller { } @Get("history/{educationId}") - public async getProfileEducationHistory( - @Path() educationId: string, - ) { + public async getProfileEducationHistory(@Path() educationId: string) { const record = await this.profileEducationHistoryRepo.find({ where: { profileEducationId: educationId, @@ -120,7 +119,7 @@ export class ProfileEducationsEmployeeController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_EMP", profile.id); - + const before = null; const data = new ProfileEducation(); const meta = { createdUserId: req.user.sub, @@ -135,9 +134,11 @@ export class ProfileEducationsEmployeeController extends Controller { const history = new ProfileEducationHistory(); Object.assign(history, { ...data, id: undefined }); - await this.profileEducationRepo.save(data); + await this.profileEducationRepo.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileEducationId = data.id; - await this.profileEducationHistoryRepo.save(history); + await this.profileEducationHistoryRepo.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(); } @@ -155,7 +156,8 @@ export class ProfileEducationsEmployeeController extends Controller { "SYS_REGISTRY_EMP", record.profileEmployeeId, ); - + const before = structuredClone(record); + const before_null = null; const history = new ProfileEducationHistory(); Object.assign(record, body); @@ -173,8 +175,10 @@ export class ProfileEducationsEmployeeController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.profileEducationRepo.save(record), - this.profileEducationHistoryRepo.save(history), + this.profileEducationRepo.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.profileEducationHistoryRepo.save(history, { data: req }), + setLogDataDiff(req, { before, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileEducationsEmployeeTempController.ts b/src/controllers/ProfileEducationsEmployeeTempController.ts index aa6cfda1..61c2325d 100644 --- a/src/controllers/ProfileEducationsEmployeeTempController.ts +++ b/src/controllers/ProfileEducationsEmployeeTempController.ts @@ -24,6 +24,7 @@ import { ProfileEducationHistory } from "../entities/ProfileEducationHistory"; import { AppDataSource } from "../database/data-source"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-temp/educations") @Tags("ProfileEducationsEmployee") @Security("bearerAuth") @@ -83,9 +84,7 @@ export class ProfileEducationsEmployeeTempController extends Controller { } @Get("history/{educationId}") - public async getProfileEducationHistory( - @Path() educationId: string, - ) { + public async getProfileEducationHistory(@Path() educationId: string) { const record = await this.profileEducationHistoryRepo.find({ where: { profileEducationId: educationId, @@ -112,7 +111,7 @@ export class ProfileEducationsEmployeeTempController extends Controller { if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } - + const before = null; const data = new ProfileEducation(); const meta = { createdUserId: req.user.sub, @@ -127,10 +126,11 @@ export class ProfileEducationsEmployeeTempController extends Controller { const history = new ProfileEducationHistory(); Object.assign(history, { ...data, id: undefined }); - await this.profileEducationRepo.save(data); + await this.profileEducationRepo.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileEducationId = data.id; - await this.profileEducationHistoryRepo.save(history); - + await this.profileEducationHistoryRepo.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(); } @@ -143,7 +143,8 @@ export class ProfileEducationsEmployeeTempController extends Controller { await new permission().PermissionUpdate(req, "SYS_REGISTRY_TEMP"); const record = await this.profileEducationRepo.findOneBy({ id: educationId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - + const before = structuredClone(record); + // const before_null = null; const history = new ProfileEducationHistory(); Object.assign(record, body); @@ -161,8 +162,10 @@ export class ProfileEducationsEmployeeTempController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.profileEducationRepo.save(record), - this.profileEducationHistoryRepo.save(history), + this.profileEducationRepo.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.profileEducationHistoryRepo.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileFamilyCoupleController.ts b/src/controllers/ProfileFamilyCoupleController.ts index 5b7a82cf..e26c6ca7 100644 --- a/src/controllers/ProfileFamilyCoupleController.ts +++ b/src/controllers/ProfileFamilyCoupleController.ts @@ -25,6 +25,7 @@ import { import { ProfileFamilyCoupleHistory } from "../entities/ProfileFamilyCoupleHistory"; import Extension from "../interfaces/extension"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile/family/couple") @Tags("ProfileFamilyCouple") @Security("bearerAuth") @@ -165,6 +166,7 @@ export class ProfileFamilyCoupleController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profile.id); + const before = null; familyCouple.coupleCitizenId = Extension.CheckCitizen(String(body.coupleCitizenId)); familyCouple.createdUserId = req.user.sub; familyCouple.createdFullName = req.user.name; @@ -177,11 +179,13 @@ export class ProfileFamilyCoupleController extends Controller { const history = new ProfileFamilyCoupleHistory(); Object.assign(history, { ...familyCouple, id: undefined }); - await this.profileRepo.save(profile); - await this.ProfileFamilyCouple.save(familyCouple); + await this.profileRepo.save(profile, { data: req }); + setLogDataDiff(req, { before, after: profile }); + await this.ProfileFamilyCouple.save(familyCouple, { data: req }); + setLogDataDiff(req, { before, after: familyCouple }); history.profileFamilyCoupleId = familyCouple.id; - await this.ProfileFamilyCoupleHistory.save(history); - + await this.ProfileFamilyCoupleHistory.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(familyCouple.id); } @@ -194,7 +198,8 @@ export class ProfileFamilyCoupleController extends Controller { await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profileId); const familyCouple = await this.ProfileFamilyCouple.findOneBy({ profileId: profileId }); if (!familyCouple) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - + const before = structuredClone(familyCouple); + // const before_null = null; const history = new ProfileFamilyCoupleHistory(); Object.assign(familyCouple, body); Object.assign(history, { ...familyCouple, id: undefined }); @@ -212,8 +217,10 @@ export class ProfileFamilyCoupleController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.ProfileFamilyCouple.save(familyCouple), - this.ProfileFamilyCoupleHistory.save(history), + this.ProfileFamilyCouple.save(familyCouple, { data: req }), + setLogDataDiff(req, { before, after: familyCouple }), + this.ProfileFamilyCoupleHistory.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileFamilyCoupleEmployeeController.ts b/src/controllers/ProfileFamilyCoupleEmployeeController.ts index 425d6534..be5b2e79 100644 --- a/src/controllers/ProfileFamilyCoupleEmployeeController.ts +++ b/src/controllers/ProfileFamilyCoupleEmployeeController.ts @@ -25,6 +25,7 @@ import { import { ProfileFamilyCoupleHistory } from "../entities/ProfileFamilyCoupleHistory"; import Extension from "../interfaces/extension"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-employee/family/couple") @Tags("ProfileEmployeeFamilyCouple") @Security("bearerAuth") @@ -168,7 +169,7 @@ export class ProfileFamilyCoupleEmployeeController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_EMP", profile.id); - + const before = null; familyCouple.coupleCitizenId = Extension.CheckCitizen(String(body.coupleCitizenId)); familyCouple.createdUserId = req.user.sub; familyCouple.createdFullName = req.user.name; @@ -181,10 +182,13 @@ export class ProfileFamilyCoupleEmployeeController extends Controller { const history = new ProfileFamilyCoupleHistory(); Object.assign(history, { ...familyCouple, id: undefined }); - await this.profileRepo.save(profile); - await this.ProfileFamilyCouple.save(familyCouple); + await this.profileRepo.save(profile, { data: req }); + setLogDataDiff(req, { before, after: profile }); + await this.ProfileFamilyCouple.save(familyCouple, { data: req }); + setLogDataDiff(req, { before, after: familyCouple }); history.profileFamilyCoupleId = familyCouple.id; - await this.ProfileFamilyCoupleHistory.save(history); + await this.ProfileFamilyCoupleHistory.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(familyCouple.id); } @@ -200,7 +204,8 @@ export class ProfileFamilyCoupleEmployeeController extends Controller { profileEmployeeId: profileEmployeeId, }); if (!familyCouple) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - + const before = structuredClone(familyCouple); + // const before_null = null; const history = new ProfileFamilyCoupleHistory(); Object.assign(familyCouple, body); Object.assign(history, { ...familyCouple, id: undefined }); @@ -218,8 +223,10 @@ export class ProfileFamilyCoupleEmployeeController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.ProfileFamilyCouple.save(familyCouple), - this.ProfileFamilyCoupleHistory.save(history), + this.ProfileFamilyCouple.save(familyCouple, { data: req }), + setLogDataDiff(req, { before, after: familyCouple }), + this.ProfileFamilyCoupleHistory.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileFamilyCoupleEmployeeTempController.ts b/src/controllers/ProfileFamilyCoupleEmployeeTempController.ts index 142fea29..55262930 100644 --- a/src/controllers/ProfileFamilyCoupleEmployeeTempController.ts +++ b/src/controllers/ProfileFamilyCoupleEmployeeTempController.ts @@ -25,6 +25,7 @@ import { import { ProfileFamilyCoupleHistory } from "../entities/ProfileFamilyCoupleHistory"; import Extension from "../interfaces/extension"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-temp/family/couple") @Tags("ProfileEmployeeFamilyCouple") @Security("bearerAuth") @@ -161,6 +162,7 @@ export class ProfileFamilyCoupleEmployeeTempController extends Controller { @Body() body: CreateProfileEmployeeFamilyCouple, ) { await new permission().PermissionUpdate(req, "SYS_REGISTRY_TEMP"); + const before = null; const familyCouple = Object.assign(new ProfileFamilyCouple(), body); if (!familyCouple) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); @@ -182,10 +184,13 @@ export class ProfileFamilyCoupleEmployeeTempController extends Controller { const history = new ProfileFamilyCoupleHistory(); Object.assign(history, { ...familyCouple, id: undefined }); - await this.profileRepo.save(profile); - await this.ProfileFamilyCouple.save(familyCouple); + await this.profileRepo.save(profile, { data: req }); + setLogDataDiff(req, { before, after: profile }); + await this.ProfileFamilyCouple.save(familyCouple, { data: req }); + setLogDataDiff(req, { before, after: familyCouple }); history.profileFamilyCoupleId = familyCouple.id; - await this.ProfileFamilyCoupleHistory.save(history); + await this.ProfileFamilyCoupleHistory.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(familyCouple.id); } @@ -201,7 +206,8 @@ export class ProfileFamilyCoupleEmployeeTempController extends Controller { profileEmployeeId: profileEmployeeId, }); if (!familyCouple) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - + const before = structuredClone(familyCouple); + // const before_null = null; const history = new ProfileFamilyCoupleHistory(); Object.assign(familyCouple, body); Object.assign(history, { ...familyCouple, id: undefined }); @@ -219,8 +225,10 @@ export class ProfileFamilyCoupleEmployeeTempController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.ProfileFamilyCouple.save(familyCouple), - this.ProfileFamilyCoupleHistory.save(history), + this.ProfileFamilyCouple.save(familyCouple, { data: req }), + setLogDataDiff(req, { before, after: familyCouple }), + this.ProfileFamilyCoupleHistory.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileFamilyFatherController.ts b/src/controllers/ProfileFamilyFatherController.ts index b9d1b65e..2a8ea184 100644 --- a/src/controllers/ProfileFamilyFatherController.ts +++ b/src/controllers/ProfileFamilyFatherController.ts @@ -25,6 +25,7 @@ import { import { ProfileFamilyFatherHistory } from "../entities/ProfileFamilyFatherHistory"; import Extension from "../interfaces/extension"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile/family/father") @Tags("ProfileFamilyFather") @Security("bearerAuth") @@ -158,6 +159,7 @@ export class ProfileFamilyFatherController extends Controller { if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } + const before = null; await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profile.id); familyFather.fatherCitizenId = Extension.CheckCitizen(String(body.fatherCitizenId)); familyFather.createdUserId = req.user.sub; @@ -170,10 +172,11 @@ export class ProfileFamilyFatherController extends Controller { const history = new ProfileFamilyFatherHistory(); Object.assign(history, { ...familyFather, id: undefined }); - await this.ProfileFamilyFather.save(familyFather); + await this.ProfileFamilyFather.save(familyFather, { data: req }); + setLogDataDiff(req, { before, after: familyFather }); history.profileFamilyFatherId = familyFather.id; - await this.ProfileFamilyFatherHistory.save(history); - + await this.ProfileFamilyFatherHistory.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(familyFather.id); } @@ -186,7 +189,8 @@ export class ProfileFamilyFatherController extends Controller { await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profileId); const familyFather = await this.ProfileFamilyFather.findOneBy({ profileId: profileId }); if (!familyFather) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - + const before = structuredClone(familyFather); + // const before_null = null; const history = new ProfileFamilyFatherHistory(); Object.assign(familyFather, body); Object.assign(history, { ...familyFather, id: undefined }); @@ -204,8 +208,10 @@ export class ProfileFamilyFatherController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.ProfileFamilyFather.save(familyFather), - this.ProfileFamilyFatherHistory.save(history), + this.ProfileFamilyFather.save(familyFather, { data: req }), + setLogDataDiff(req, { before, after: familyFather }), + this.ProfileFamilyFatherHistory.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileFamilyFatherEmployeeController.ts b/src/controllers/ProfileFamilyFatherEmployeeController.ts index fc42cdf7..3bd3f44d 100644 --- a/src/controllers/ProfileFamilyFatherEmployeeController.ts +++ b/src/controllers/ProfileFamilyFatherEmployeeController.ts @@ -25,6 +25,7 @@ import { import { ProfileFamilyFatherHistory } from "../entities/ProfileFamilyFatherHistory"; import Extension from "../interfaces/extension"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-employee/family/father") @Tags("ProfileEmployeeFamilyFather") @Security("bearerAuth") @@ -161,6 +162,7 @@ export class ProfileFamilyFatherEmployeeController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_EMP", profile.id); + const before = null; familyFather.fatherCitizenId = Extension.CheckCitizen(String(body.fatherCitizenId)); familyFather.createdUserId = req.user.sub; familyFather.createdFullName = req.user.name; @@ -172,9 +174,11 @@ export class ProfileFamilyFatherEmployeeController extends Controller { const history = new ProfileFamilyFatherHistory(); Object.assign(history, { ...familyFather, id: undefined }); - await this.ProfileFamilyFather.save(familyFather); + await this.ProfileFamilyFather.save(familyFather, { data: req }); + setLogDataDiff(req, { before, after: familyFather }); history.profileFamilyFatherId = familyFather.id; - await this.ProfileFamilyFatherHistory.save(history); + await this.ProfileFamilyFatherHistory.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(familyFather.id); } @@ -190,7 +194,8 @@ export class ProfileFamilyFatherEmployeeController extends Controller { profileEmployeeId: profileEmployeeId, }); if (!familyFather) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - + const before = structuredClone(familyFather); + // const before_null = null; const history = new ProfileFamilyFatherHistory(); Object.assign(familyFather, body); Object.assign(history, { ...familyFather, id: undefined }); @@ -208,8 +213,10 @@ export class ProfileFamilyFatherEmployeeController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.ProfileFamilyFather.save(familyFather), - this.ProfileFamilyFatherHistory.save(history), + this.ProfileFamilyFather.save(familyFather, { data: req }), + setLogDataDiff(req, { before, after: familyFather }), + this.ProfileFamilyFatherHistory.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileFamilyFatherEmployeeTempController.ts b/src/controllers/ProfileFamilyFatherEmployeeTempController.ts index ed19610b..3436df28 100644 --- a/src/controllers/ProfileFamilyFatherEmployeeTempController.ts +++ b/src/controllers/ProfileFamilyFatherEmployeeTempController.ts @@ -25,6 +25,7 @@ import { import { ProfileFamilyFatherHistory } from "../entities/ProfileFamilyFatherHistory"; import Extension from "../interfaces/extension"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-temp/family/father") @Tags("ProfileEmployeeFamilyFather") @Security("bearerAuth") @@ -162,6 +163,7 @@ export class ProfileFamilyFatherEmployeeTempController extends Controller { if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } + const before = null; familyFather.fatherCitizenId = Extension.CheckCitizen(String(body.fatherCitizenId)); familyFather.createdUserId = req.user.sub; familyFather.createdFullName = req.user.name; @@ -173,9 +175,11 @@ export class ProfileFamilyFatherEmployeeTempController extends Controller { const history = new ProfileFamilyFatherHistory(); Object.assign(history, { ...familyFather, id: undefined }); - await this.ProfileFamilyFather.save(familyFather); + await this.ProfileFamilyFather.save(familyFather, { data: req }); + setLogDataDiff(req, { before, after: familyFather }); history.profileFamilyFatherId = familyFather.id; - await this.ProfileFamilyFatherHistory.save(history); + await this.ProfileFamilyFatherHistory.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(familyFather.id); } @@ -195,7 +199,8 @@ export class ProfileFamilyFatherEmployeeTempController extends Controller { const history = new ProfileFamilyFatherHistory(); Object.assign(familyFather, body); Object.assign(history, { ...familyFather, id: undefined }); - + const before = structuredClone(familyFather); + // const before_null = null; familyFather.fatherCitizenId = Extension.CheckCitizen(String(body.fatherCitizenId)); history.profileFamilyFatherId = familyFather.id; familyFather.lastUpdateUserId = req.user.sub; @@ -209,8 +214,10 @@ export class ProfileFamilyFatherEmployeeTempController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.ProfileFamilyFather.save(familyFather), - this.ProfileFamilyFatherHistory.save(history), + this.ProfileFamilyFather.save(familyFather, { data: req }), + setLogDataDiff(req, { before, after: familyFather }), + this.ProfileFamilyFatherHistory.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileFamilyMotherController.ts b/src/controllers/ProfileFamilyMotherController.ts index dc9ac3a1..00c8e9b8 100644 --- a/src/controllers/ProfileFamilyMotherController.ts +++ b/src/controllers/ProfileFamilyMotherController.ts @@ -25,6 +25,7 @@ import { import { ProfileFamilyMotherHistory } from "../entities/ProfileFamilyMotherHistory"; import Extension from "../interfaces/extension"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile/family/mother") @Tags("ProfileFamilyMother") @Security("bearerAuth") @@ -158,6 +159,7 @@ export class ProfileFamilyMotherController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profile.id); + const before = null; familyMother.motherCitizenId = Extension.CheckCitizen(String(body.motherCitizenId)); familyMother.createdUserId = req.user.sub; familyMother.createdFullName = req.user.name; @@ -169,9 +171,11 @@ export class ProfileFamilyMotherController extends Controller { const history = new ProfileFamilyMotherHistory(); Object.assign(history, { ...familyMother, id: undefined }); - await this.ProfileFamilyMother.save(familyMother); + await this.ProfileFamilyMother.save(familyMother, { data: req }); + setLogDataDiff(req, { before, after: familyMother }); history.profileFamilyMotherId = familyMother.id; - await this.ProfileFamilyMotherHistory.save(history); + await this.ProfileFamilyMotherHistory.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(familyMother.id); } @@ -185,7 +189,8 @@ export class ProfileFamilyMotherController extends Controller { await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profileId); const familyMother = await this.ProfileFamilyMother.findOneBy({ profileId: profileId }); if (!familyMother) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - + const before = structuredClone(familyMother); + // const before_null = null; const history = new ProfileFamilyMotherHistory(); Object.assign(familyMother, body); Object.assign(history, { ...familyMother, id: undefined }); @@ -203,8 +208,10 @@ export class ProfileFamilyMotherController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.ProfileFamilyMother.save(familyMother), - this.ProfileFamilyMotherHistory.save(history), + this.ProfileFamilyMother.save(familyMother, { data: req }), + setLogDataDiff(req, { before, after: familyMother }), + this.ProfileFamilyMotherHistory.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileFamilyMotherEmployeeController.ts b/src/controllers/ProfileFamilyMotherEmployeeController.ts index 90961151..1afe0344 100644 --- a/src/controllers/ProfileFamilyMotherEmployeeController.ts +++ b/src/controllers/ProfileFamilyMotherEmployeeController.ts @@ -25,6 +25,7 @@ import { import { ProfileFamilyMotherHistory } from "../entities/ProfileFamilyMotherHistory"; import Extension from "../interfaces/extension"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-employee/family/mother") @Tags("ProfileEmployeeFamilyMother") @Security("bearerAuth") @@ -161,6 +162,7 @@ export class ProfileFamilyMotherEmployeeController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_EMP", profile.id); + const before = null; familyMother.motherCitizenId = Extension.CheckCitizen(String(body.motherCitizenId)); familyMother.createdUserId = req.user.sub; familyMother.createdFullName = req.user.name; @@ -172,10 +174,11 @@ export class ProfileFamilyMotherEmployeeController extends Controller { const history = new ProfileFamilyMotherHistory(); Object.assign(history, { ...familyMother, id: undefined }); - await this.ProfileFamilyMother.save(familyMother); + await this.ProfileFamilyMother.save(familyMother, { data: req }); + setLogDataDiff(req, { before, after: familyMother }); history.profileFamilyMotherId = familyMother.id; - await this.ProfileFamilyMotherHistory.save(history); - + await this.ProfileFamilyMotherHistory.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(familyMother.id); } @@ -194,7 +197,8 @@ export class ProfileFamilyMotherEmployeeController extends Controller { const history = new ProfileFamilyMotherHistory(); Object.assign(familyMother, body); Object.assign(history, { ...familyMother, id: undefined }); - + const before = structuredClone(familyMother); + // const before_null = null; familyMother.motherCitizenId = Extension.CheckCitizen(String(body.motherCitizenId)); history.profileFamilyMotherId = familyMother.id; familyMother.lastUpdateUserId = req.user.sub; @@ -208,8 +212,10 @@ export class ProfileFamilyMotherEmployeeController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.ProfileFamilyMother.save(familyMother), - this.ProfileFamilyMotherHistory.save(history), + this.ProfileFamilyMother.save(familyMother, { data: req }), + setLogDataDiff(req, { before, after: familyMother }), + this.ProfileFamilyMotherHistory.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileFamilyMotherEmployeeTempController.ts b/src/controllers/ProfileFamilyMotherEmployeeTempController.ts index 401443bc..6884cd5d 100644 --- a/src/controllers/ProfileFamilyMotherEmployeeTempController.ts +++ b/src/controllers/ProfileFamilyMotherEmployeeTempController.ts @@ -25,6 +25,7 @@ import { import { ProfileFamilyMotherHistory } from "../entities/ProfileFamilyMotherHistory"; import Extension from "../interfaces/extension"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-temp/family/mother") @Tags("ProfileEmployeeFamilyMother") @Security("bearerAuth") @@ -162,6 +163,7 @@ export class ProfileFamilyMotherEmployeeTempController extends Controller { if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } + const before = null; familyMother.motherCitizenId = Extension.CheckCitizen(String(body.motherCitizenId)); familyMother.createdUserId = req.user.sub; familyMother.createdFullName = req.user.name; @@ -173,10 +175,11 @@ export class ProfileFamilyMotherEmployeeTempController extends Controller { const history = new ProfileFamilyMotherHistory(); Object.assign(history, { ...familyMother, id: undefined }); - await this.ProfileFamilyMother.save(familyMother); + await this.ProfileFamilyMother.save(familyMother, { data: req }); + setLogDataDiff(req, { before, after: familyMother }); history.profileFamilyMotherId = familyMother.id; - await this.ProfileFamilyMotherHistory.save(history); - + await this.ProfileFamilyMotherHistory.save(history, { data: req }); + //setLogDataDiff(req, { before, after: history }); return new HttpSuccess(familyMother.id); } @@ -191,7 +194,8 @@ export class ProfileFamilyMotherEmployeeTempController extends Controller { profileEmployeeId: profileEmployeeId, }); if (!familyMother) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - + const before = structuredClone(familyMother); + // const before_null = null; const history = new ProfileFamilyMotherHistory(); Object.assign(familyMother, body); Object.assign(history, { ...familyMother, id: undefined }); @@ -209,8 +213,10 @@ export class ProfileFamilyMotherEmployeeTempController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.ProfileFamilyMother.save(familyMother), - this.ProfileFamilyMotherHistory.save(history), + this.ProfileFamilyMother.save(familyMother, { data: req }), + setLogDataDiff(req, { before, after: familyMother }), + this.ProfileFamilyMotherHistory.save(history, { data: req }), + // setLogDataDiff(req, { before: before_null, after: history }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileGovernmentController.ts b/src/controllers/ProfileGovernmentController.ts index 2a4645a5..5c138701 100644 --- a/src/controllers/ProfileGovernmentController.ts +++ b/src/controllers/ProfileGovernmentController.ts @@ -8,7 +8,7 @@ import { Profile } from "../entities/Profile"; import { ProfileGovernment, UpdateProfileGovernment } from "../entities/ProfileGovernment"; import { Position } from "../entities/Position"; import { PosMaster } from "../entities/PosMaster"; -import { calculateAge, calculateRetireDate } from "../interfaces/utils"; +import { calculateAge, calculateRetireDate, setLogDataDiff } from "../interfaces/utils"; import permission from "../interfaces/permission"; import { OrgRevision } from "../entities/OrgRevision"; @Route("api/v1/org/profile/government") @@ -385,7 +385,7 @@ export class ProfileGovernmentHistoryController extends Controller { }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - + const before = structuredClone(record); const history = new ProfileGovernment(); Object.assign(record, body); @@ -402,7 +402,11 @@ export class ProfileGovernmentHistoryController extends Controller { history.createdAt = new Date(); history.lastUpdatedAt = new Date(); - await Promise.all([this.profileRepo.save(record), this.govRepo.save(history)]); + await Promise.all([ + this.profileRepo.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.govRepo.save(history, { data:req }) + ]); return new HttpSuccess(); } diff --git a/src/controllers/ProfileGovernmentEmployeeController.ts b/src/controllers/ProfileGovernmentEmployeeController.ts index 7aac86c9..fb47a767 100644 --- a/src/controllers/ProfileGovernmentEmployeeController.ts +++ b/src/controllers/ProfileGovernmentEmployeeController.ts @@ -25,7 +25,7 @@ import { } from "../entities/ProfileGovernment"; import { EmployeePosition } from "../entities/EmployeePosition"; import { EmployeePosMaster } from "../entities/EmployeePosMaster"; -import { calculateAge, calculateRetireDate } from "../interfaces/utils"; +import { calculateAge, calculateRetireDate, setLogDataDiff } from "../interfaces/utils"; import permission from "../interfaces/permission"; import { OrgRevision } from "../entities/OrgRevision"; @Route("api/v1/org/profile-employee/government") @@ -368,6 +368,7 @@ export class ProfileGovernmentEmployeeController extends Controller { }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + const before = structuredClone(record); const history = new ProfileGovernment(); Object.assign(record, body); @@ -384,7 +385,10 @@ export class ProfileGovernmentEmployeeController extends Controller { history.createdAt = new Date(); history.lastUpdatedAt = new Date(); - await Promise.all([this.profileEmployeeRepo.save(record), this.govRepo.save(history)]); + await Promise.all([ + this.profileEmployeeRepo.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.govRepo.save(history, { data: req })]); return new HttpSuccess(); } } diff --git a/src/controllers/ProfileGovernmentEmployeeTempController.ts b/src/controllers/ProfileGovernmentEmployeeTempController.ts index 57c223a0..d6e60fb3 100644 --- a/src/controllers/ProfileGovernmentEmployeeTempController.ts +++ b/src/controllers/ProfileGovernmentEmployeeTempController.ts @@ -25,7 +25,7 @@ import { } from "../entities/ProfileGovernment"; import { EmployeePosition } from "../entities/EmployeePosition"; import { EmployeePosMaster } from "../entities/EmployeePosMaster"; -import { calculateAge, calculateRetireDate } from "../interfaces/utils"; +import { calculateAge, calculateRetireDate, setLogDataDiff } from "../interfaces/utils"; import permission from "../interfaces/permission"; import { OrgRevision } from "../entities/OrgRevision"; @Route("api/v1/org/profile-temp/government") @@ -369,6 +369,7 @@ export class ProfileGovernmentEmployeeTempController extends Controller { }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + const before = structuredClone(record); const history = new ProfileGovernment(); Object.assign(record, body); @@ -385,7 +386,11 @@ export class ProfileGovernmentEmployeeTempController extends Controller { history.createdAt = new Date(); history.lastUpdatedAt = new Date(); - await Promise.all([this.profileEmployeeRepo.save(record), this.govRepo.save(history)]); + await Promise.all([ + this.profileEmployeeRepo.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.govRepo.save(history, { data: req }), + ]); return new HttpSuccess(); } } diff --git a/src/controllers/ProfileHonorController.ts b/src/controllers/ProfileHonorController.ts index 8d34c1d0..0b4a9952 100644 --- a/src/controllers/ProfileHonorController.ts +++ b/src/controllers/ProfileHonorController.ts @@ -20,6 +20,7 @@ import { ProfileHonorHistory } from "../entities/ProfileHonorHistory"; import { RequestWithUser } from "../middlewares/user"; import { Profile } from "../entities/Profile"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile/honor") @Tags("ProfileHonor") @Security("bearerAuth") @@ -111,7 +112,7 @@ export class ProfileHonorController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profile.id); - + const before = null; const data = new ProfileHonor(); const meta = { @@ -127,9 +128,10 @@ export class ProfileHonorController extends Controller { const history = new ProfileHonorHistory(); Object.assign(history, { ...data, id: undefined }); - await this.honorRepo.save(data); + await this.honorRepo.save(data, {data: req}); + setLogDataDiff(req, { before, after: data }); history.profileHonorId = data.id; - await this.honorHistoryRepo.save(history); + await this.honorHistoryRepo.save(history, {data: req}); return new HttpSuccess(); } @@ -148,7 +150,7 @@ export class ProfileHonorController extends Controller { Object.assign(record, body); Object.assign(history, { ...record, id: undefined }); - + const before = structuredClone(record); history.profileHonorId = honorId; record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; @@ -160,7 +162,11 @@ export class ProfileHonorController extends Controller { history.createdAt = new Date(); history.lastUpdatedAt = new Date(); - await Promise.all([this.honorRepo.save(record), this.honorHistoryRepo.save(history)]); + await Promise.all([ + this.honorRepo.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.honorHistoryRepo.save(history, { data: req }), + ]); return new HttpSuccess(); } diff --git a/src/controllers/ProfileHonorEmployeeController.ts b/src/controllers/ProfileHonorEmployeeController.ts index 4bdb7aa0..c2b90631 100644 --- a/src/controllers/ProfileHonorEmployeeController.ts +++ b/src/controllers/ProfileHonorEmployeeController.ts @@ -24,6 +24,7 @@ import { ProfileHonorHistory } from "../entities/ProfileHonorHistory"; import { RequestWithUser } from "../middlewares/user"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-employee/honor") @Tags("ProfileEmployeeHonor") @Security("bearerAuth") @@ -119,7 +120,7 @@ export class ProfileHonorEmployeeController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_EMP", profile.id); - + const before = null; const data = new ProfileHonor(); const meta = { @@ -135,9 +136,10 @@ export class ProfileHonorEmployeeController extends Controller { const history = new ProfileHonorHistory(); Object.assign(history, { ...data, id: undefined }); - await this.honorRepo.save(data); + await this.honorRepo.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileHonorId = data.id; - await this.honorHistoryRepo.save(history); + await this.honorHistoryRepo.save(history, { data: req }); return new HttpSuccess(); } @@ -155,7 +157,7 @@ export class ProfileHonorEmployeeController extends Controller { "SYS_REGISTRY_EMP", record.profileEmployeeId, ); - + const before = structuredClone(record); const history = new ProfileHonorHistory(); Object.assign(record, body); @@ -172,7 +174,11 @@ export class ProfileHonorEmployeeController extends Controller { history.createdAt = new Date(); history.lastUpdatedAt = new Date(); - await Promise.all([this.honorRepo.save(record), this.honorHistoryRepo.save(history)]); + await Promise.all([ + this.honorRepo.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.honorHistoryRepo.save(history, { data: req }), + ]); return new HttpSuccess(); } diff --git a/src/controllers/ProfileHonorEmployeeTempController.ts b/src/controllers/ProfileHonorEmployeeTempController.ts index d653d348..0d619ae5 100644 --- a/src/controllers/ProfileHonorEmployeeTempController.ts +++ b/src/controllers/ProfileHonorEmployeeTempController.ts @@ -24,6 +24,7 @@ import { ProfileHonorHistory } from "../entities/ProfileHonorHistory"; import { RequestWithUser } from "../middlewares/user"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-temp/honor") @Tags("ProfileEmployeeHonor") @Security("bearerAuth") @@ -112,7 +113,7 @@ export class ProfileHonorEmployeeTempController extends Controller { if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } - + const before = null; const data = new ProfileHonor(); const meta = { @@ -128,9 +129,10 @@ export class ProfileHonorEmployeeTempController extends Controller { const history = new ProfileHonorHistory(); Object.assign(history, { ...data, id: undefined }); - await this.honorRepo.save(data); + await this.honorRepo.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileHonorId = data.id; - await this.honorHistoryRepo.save(history); + await this.honorHistoryRepo.save(history, { data: req }); return new HttpSuccess(); } @@ -145,7 +147,7 @@ export class ProfileHonorEmployeeTempController extends Controller { const record = await this.honorRepo.findOneBy({ id: honorId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - + const before = structuredClone(record); const history = new ProfileHonorHistory(); Object.assign(record, body); @@ -162,7 +164,11 @@ export class ProfileHonorEmployeeTempController extends Controller { history.createdAt = new Date(); history.lastUpdatedAt = new Date(); - await Promise.all([this.honorRepo.save(record), this.honorHistoryRepo.save(history)]); + await Promise.all([ + this.honorRepo.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.honorHistoryRepo.save(history, { data: req }), + ]); return new HttpSuccess(); } diff --git a/src/controllers/ProfileInsigniaController.ts b/src/controllers/ProfileInsigniaController.ts index 6b6cd43f..b0cb8693 100644 --- a/src/controllers/ProfileInsigniaController.ts +++ b/src/controllers/ProfileInsigniaController.ts @@ -25,6 +25,7 @@ import { RequestWithUser } from "../middlewares/user"; import { Profile } from "../entities/Profile"; import { Insignia } from "../entities/Insignia"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile/insignia") @Tags("ProfileInsignia") @Security("bearerAuth") @@ -124,7 +125,7 @@ export class ProfileInsigniaController extends Controller { if (!insignia) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลเครื่องราชฯ นี้"); } - + const before = null; const data = new ProfileInsignia(); const meta = { @@ -140,9 +141,10 @@ export class ProfileInsigniaController extends Controller { const history = new ProfileInsigniaHistory(); Object.assign(history, { ...data, id: undefined }); - await this.insigniaRepo.save(data); + await this.insigniaRepo.save(data, { data: req }); + setLogDataDiff( req, { before, after: data } ); history.profileInsigniaId = data.id; - await this.insigniaHistoryRepo.save(history); + await this.insigniaHistoryRepo.save(history, { data: req }); return new HttpSuccess(); } @@ -163,7 +165,7 @@ export class ProfileInsigniaController extends Controller { if (!insignia) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลเครื่องราชฯ นี้"); } - + const before = structuredClone(record); const history = new ProfileInsigniaHistory(); Object.assign(record, body); @@ -180,7 +182,11 @@ export class ProfileInsigniaController extends Controller { history.createdAt = new Date(); history.lastUpdatedAt = new Date(); - await Promise.all([this.insigniaRepo.save(record), this.insigniaHistoryRepo.save(history)]); + await Promise.all([ + this.insigniaRepo.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.insigniaHistoryRepo.save(history, { data: req }) + ]); return new HttpSuccess(); } diff --git a/src/controllers/ProfileInsigniaEmployeeController.ts b/src/controllers/ProfileInsigniaEmployeeController.ts index 2e522faa..5c37a189 100644 --- a/src/controllers/ProfileInsigniaEmployeeController.ts +++ b/src/controllers/ProfileInsigniaEmployeeController.ts @@ -25,6 +25,7 @@ import { RequestWithUser } from "../middlewares/user"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import { Insignia } from "../entities/Insignia"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-employee/insignia") @Tags("ProfileEmployeeInsignia") @Security("bearerAuth") @@ -128,7 +129,7 @@ export class ProfileInsigniaEmployeeController extends Controller { if (!insignia) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลเครื่องราชฯ นี้"); } - + const before = null; const data = new ProfileInsignia(); const meta = { @@ -144,9 +145,10 @@ export class ProfileInsigniaEmployeeController extends Controller { const history = new ProfileInsigniaHistory(); Object.assign(history, { ...data, id: undefined }); - await this.insigniaRepo.save(data); + await this.insigniaRepo.save(data, { data: req }); + setLogDataDiff( req, { before, after: data } ) history.profileInsigniaId = data.id; - await this.insigniaHistoryRepo.save(history); + await this.insigniaHistoryRepo.save(history, { data: req }); return new HttpSuccess(); } @@ -171,7 +173,7 @@ export class ProfileInsigniaEmployeeController extends Controller { if (!insignia) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลเครื่องราชฯ นี้"); } - + const before = structuredClone(record); const history = new ProfileInsigniaHistory(); Object.assign(record, body); @@ -188,7 +190,11 @@ export class ProfileInsigniaEmployeeController extends Controller { history.createdAt = new Date(); history.lastUpdatedAt = new Date(); - await Promise.all([this.insigniaRepo.save(record), this.insigniaHistoryRepo.save(history)]); + await Promise.all([ + this.insigniaRepo.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.insigniaHistoryRepo.save(history, { data: req }), + ]); return new HttpSuccess(); } diff --git a/src/controllers/ProfileInsigniaEmployeeTempController.ts b/src/controllers/ProfileInsigniaEmployeeTempController.ts index 962e085a..78e19973 100644 --- a/src/controllers/ProfileInsigniaEmployeeTempController.ts +++ b/src/controllers/ProfileInsigniaEmployeeTempController.ts @@ -25,6 +25,7 @@ import { RequestWithUser } from "../middlewares/user"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import { Insignia } from "../entities/Insignia"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-temp/insignia") @Tags("ProfileEmployeeInsignia") @Security("bearerAuth") @@ -122,7 +123,7 @@ export class ProfileInsigniaEmployeeTempController extends Controller { if (!insignia) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลเครื่องราชฯ นี้"); } - + const before = null; const data = new ProfileInsignia(); const meta = { @@ -138,9 +139,10 @@ export class ProfileInsigniaEmployeeTempController extends Controller { const history = new ProfileInsigniaHistory(); Object.assign(history, { ...data, id: undefined }); - await this.insigniaRepo.save(data); + await this.insigniaRepo.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileInsigniaId = data.id; - await this.insigniaHistoryRepo.save(history); + await this.insigniaHistoryRepo.save(history, { data:req }); return new HttpSuccess(); } @@ -162,7 +164,7 @@ export class ProfileInsigniaEmployeeTempController extends Controller { if (!insignia) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลเครื่องราชฯ นี้"); } - + const before = structuredClone(record); const history = new ProfileInsigniaHistory(); Object.assign(record, body); @@ -179,7 +181,11 @@ export class ProfileInsigniaEmployeeTempController extends Controller { history.createdAt = new Date(); history.lastUpdatedAt = new Date(); - await Promise.all([this.insigniaRepo.save(record), this.insigniaHistoryRepo.save(history)]); + await Promise.all([ + this.insigniaRepo.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.insigniaHistoryRepo.save(history, { data: req }), + ]); return new HttpSuccess(); } diff --git a/src/controllers/ProfileLeaveController.ts b/src/controllers/ProfileLeaveController.ts index c78d331e..36cfa1bd 100644 --- a/src/controllers/ProfileLeaveController.ts +++ b/src/controllers/ProfileLeaveController.ts @@ -25,6 +25,7 @@ import { RequestWithUser } from "../middlewares/user"; import { Profile } from "../entities/Profile"; import { LeaveType } from "../entities/LeaveType"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile/leave") @Tags("ProfileLeave") @Security("bearerAuth") @@ -188,7 +189,7 @@ export class ProfileLeaveController extends Controller { if (!leaveType) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทลานี้"); } - + const before = null; const data = new ProfileLeave(); const meta = { @@ -204,9 +205,10 @@ export class ProfileLeaveController extends Controller { const history = new ProfileLeaveHistory(); Object.assign(history, { ...data, id: undefined }); - await this.leaveRepo.save(data); + await this.leaveRepo.save(data, { data: req }); + setLogDataDiff( req, { before, after: data }); history.profileLeaveId = data.id; - await this.leaveHistoryRepo.save(history); + await this.leaveHistoryRepo.save(history, { data: req }); return new HttpSuccess(); } @@ -227,7 +229,7 @@ export class ProfileLeaveController extends Controller { if (!leaveType) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทลานี้"); } - + const before = structuredClone(record); const history = new ProfileLeaveHistory(); Object.assign(record, body); @@ -244,7 +246,11 @@ export class ProfileLeaveController extends Controller { history.createdAt = new Date(); history.lastUpdatedAt = new Date(); - await Promise.all([this.leaveRepo.save(record), this.leaveHistoryRepo.save(history)]); + await Promise.all([ + this.leaveRepo.save(record, { data: req }), + setLogDataDiff( req, { before, after: record }), + this.leaveHistoryRepo.save(history, { data: req }), + ]); return new HttpSuccess(); } diff --git a/src/controllers/ProfileLeaveEmployeeController.ts b/src/controllers/ProfileLeaveEmployeeController.ts index 7320d6fe..7ded7aed 100644 --- a/src/controllers/ProfileLeaveEmployeeController.ts +++ b/src/controllers/ProfileLeaveEmployeeController.ts @@ -25,6 +25,7 @@ import { RequestWithUser } from "../middlewares/user"; import { LeaveType } from "../entities/LeaveType"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-employee/leave") @Tags("ProfileLeave") @Security("bearerAuth") @@ -116,7 +117,7 @@ export class ProfileLeaveEmployeeController extends Controller { if (!leaveType) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทลานี้"); } - + const before = null; const data = new ProfileLeave(); const meta = { @@ -132,9 +133,10 @@ export class ProfileLeaveEmployeeController extends Controller { const history = new ProfileLeaveHistory(); Object.assign(history, { ...data, id: undefined }); - await this.leaveRepo.save(data); + await this.leaveRepo.save(data, { data: req }); + setLogDataDiff( req, { before, after: data } ) history.profileLeaveId = data.id; - await this.leaveHistoryRepo.save(history); + await this.leaveHistoryRepo.save(history, { data: req }); return new HttpSuccess(); } @@ -159,7 +161,7 @@ export class ProfileLeaveEmployeeController extends Controller { if (!leaveType) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทลานี้"); } - + const before = structuredClone(record); const history = new ProfileLeaveHistory(); Object.assign(record, body); @@ -176,7 +178,11 @@ export class ProfileLeaveEmployeeController extends Controller { history.createdAt = new Date(); history.lastUpdatedAt = new Date(); - await Promise.all([this.leaveRepo.save(record), this.leaveHistoryRepo.save(history)]); + await Promise.all([ + this.leaveRepo.save(record, { data: req }), + setLogDataDiff( req, { before, after: record } ), + this.leaveHistoryRepo.save(history, { data: req }), + ]); return new HttpSuccess(); } diff --git a/src/controllers/ProfileLeaveEmployeeTempController.ts b/src/controllers/ProfileLeaveEmployeeTempController.ts index b0f77d3c..ccba6411 100644 --- a/src/controllers/ProfileLeaveEmployeeTempController.ts +++ b/src/controllers/ProfileLeaveEmployeeTempController.ts @@ -25,6 +25,7 @@ import { RequestWithUser } from "../middlewares/user"; import { LeaveType } from "../entities/LeaveType"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-temp/leave") @Tags("ProfileLeave") @Security("bearerAuth") @@ -109,7 +110,7 @@ export class ProfileLeaveEmployeeTempController extends Controller { if (!leaveType) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทลานี้"); } - + const before = null; const data = new ProfileLeave(); const meta = { @@ -149,7 +150,7 @@ export class ProfileLeaveEmployeeTempController extends Controller { if (!leaveType) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทลานี้"); } - + const before = structuredClone(record); const history = new ProfileLeaveHistory(); Object.assign(record, body); @@ -166,7 +167,11 @@ export class ProfileLeaveEmployeeTempController extends Controller { history.createdAt = new Date(); history.lastUpdatedAt = new Date(); - await Promise.all([this.leaveRepo.save(record), this.leaveHistoryRepo.save(history)]); + await Promise.all([ + this.leaveRepo.save(record), + setLogDataDiff( req, { before, after: record} ), + this.leaveHistoryRepo.save(history) + ]); return new HttpSuccess(); } diff --git a/src/controllers/ProfileNopaidController.ts b/src/controllers/ProfileNopaidController.ts index e934bda9..6d351eb1 100644 --- a/src/controllers/ProfileNopaidController.ts +++ b/src/controllers/ProfileNopaidController.ts @@ -20,6 +20,7 @@ import { RequestWithUser } from "../middlewares/user"; import { Profile } from "../entities/Profile"; import { CreateProfileNopaid, ProfileNopaid, UpdateProfileNopaid } from "../entities/ProfileNopaid"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile/nopaid") @Tags("ProfileNopaid") @Security("bearerAuth") @@ -75,7 +76,7 @@ export class ProfileNopaidController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profile.id); - + const before = null; const data = new ProfileNopaid(); const meta = { @@ -91,9 +92,10 @@ export class ProfileNopaidController extends Controller { const history = new ProfileNopaidHistory(); Object.assign(history, { ...data, id: undefined }); - await this.nopaidRepository.save(data); + await this.nopaidRepository.save(data, { data: req }); + setLogDataDiff( req, { before, after: data } ) history.profileNopaidId = data.id; - await this.nopaidHistoryRepository.save(history); + await this.nopaidHistoryRepository.save(history, { data: req }); return new HttpSuccess(); } @@ -107,7 +109,7 @@ export class ProfileNopaidController extends Controller { const record = await this.nopaidRepository.findOneBy({ id: nopaidId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", record.profileId); - + const before = structuredClone(record); const history = new ProfileNopaidHistory(); Object.assign(record, body); @@ -125,8 +127,9 @@ export class ProfileNopaidController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.nopaidRepository.save(record), - this.nopaidHistoryRepository.save(history), + this.nopaidRepository.save(record, { data: req }), + setLogDataDiff( req, { before, after: record } ), + this.nopaidHistoryRepository.save(history, { data: req }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileNopaidEmployeeController.ts b/src/controllers/ProfileNopaidEmployeeController.ts index a337ca7a..cdd1352c 100644 --- a/src/controllers/ProfileNopaidEmployeeController.ts +++ b/src/controllers/ProfileNopaidEmployeeController.ts @@ -24,6 +24,7 @@ import { UpdateProfileNopaid, } from "../entities/ProfileNopaid"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-employee/nopaid") @Tags("ProfileNopaid") @Security("bearerAuth") @@ -79,7 +80,7 @@ export class ProfileNopaidEmployeeController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_EMP", profile.id); - + const before = null; const data = new ProfileNopaid(); const meta = { @@ -95,9 +96,10 @@ export class ProfileNopaidEmployeeController extends Controller { const history = new ProfileNopaidHistory(); Object.assign(history, { ...data, id: undefined }); - await this.nopaidRepository.save(data); + await this.nopaidRepository.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileNopaidId = data.id; - await this.nopaidHistoryRepository.save(history); + await this.nopaidHistoryRepository.save(history, { data: req }); return new HttpSuccess(); } @@ -116,6 +118,7 @@ export class ProfileNopaidEmployeeController extends Controller { record.profileEmployeeId, ); + const before = structuredClone(record); const history = new ProfileNopaidHistory(); Object.assign(record, body); @@ -133,8 +136,9 @@ export class ProfileNopaidEmployeeController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.nopaidRepository.save(record), - this.nopaidHistoryRepository.save(history), + this.nopaidRepository.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.nopaidHistoryRepository.save(history, { data: req }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileNopaidEmployeeTempController.ts b/src/controllers/ProfileNopaidEmployeeTempController.ts index aad9f443..011e7043 100644 --- a/src/controllers/ProfileNopaidEmployeeTempController.ts +++ b/src/controllers/ProfileNopaidEmployeeTempController.ts @@ -24,6 +24,7 @@ import { UpdateProfileNopaid, } from "../entities/ProfileNopaid"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-temp/nopaid") @Tags("ProfileNopaid") @Security("bearerAuth") @@ -80,7 +81,7 @@ export class ProfileNopaidEmployeeTempController extends Controller { if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } - + const before = null; const data = new ProfileNopaid(); const meta = { @@ -96,9 +97,10 @@ export class ProfileNopaidEmployeeTempController extends Controller { const history = new ProfileNopaidHistory(); Object.assign(history, { ...data, id: undefined }); - await this.nopaidRepository.save(data); + await this.nopaidRepository.save(data, {data: req}); + setLogDataDiff( req, { before, after: data}) history.profileNopaidId = data.id; - await this.nopaidHistoryRepository.save(history); + await this.nopaidHistoryRepository.save(history, {data: req}); return new HttpSuccess(); } @@ -113,7 +115,7 @@ export class ProfileNopaidEmployeeTempController extends Controller { const record = await this.nopaidRepository.findOneBy({ id: nopaidId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - + const before = structuredClone(record); const history = new ProfileNopaidHistory(); Object.assign(record, body); @@ -131,8 +133,9 @@ export class ProfileNopaidEmployeeTempController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.nopaidRepository.save(record), - this.nopaidHistoryRepository.save(history), + this.nopaidRepository.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.nopaidHistoryRepository.save(history, { data: req }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileOtherController.ts b/src/controllers/ProfileOtherController.ts index bd7e38a6..e5253a94 100644 --- a/src/controllers/ProfileOtherController.ts +++ b/src/controllers/ProfileOtherController.ts @@ -21,6 +21,7 @@ import { RequestWithUser } from "../middlewares/user"; import { Profile } from "../entities/Profile"; import { CreateProfileOther, ProfileOther, UpdateProfileOther } from "../entities/ProfileOther"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile/other") @Tags("ProfileOther") @Security("bearerAuth") @@ -56,7 +57,6 @@ export class ProfileOtherController extends Controller { public async otherAdminHistory(@Path() otherId: string, @Request() req: RequestWithUser) { const _record = await this.otherRepository.findOneBy({ id: otherId }); - console.log(">>>>", _record); if (_record) { await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", _record.profileId); } @@ -87,7 +87,7 @@ export class ProfileOtherController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profile.id); - + const before = null; const data = new ProfileOther(); const meta = { @@ -103,9 +103,10 @@ export class ProfileOtherController extends Controller { const history = new ProfileOtherHistory(); Object.assign(history, { ...data, id: undefined }); - await this.otherRepository.save(data); + await this.otherRepository.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileOtherId = data.id; - await this.otherHistoryRepository.save(history); + await this.otherHistoryRepository.save(history, { data: req }); return new HttpSuccess(); } @@ -119,7 +120,7 @@ export class ProfileOtherController extends Controller { const record = await this.otherRepository.findOneBy({ id: otherId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", record.profileId); - + const before = structuredClone(record); const history = new ProfileOtherHistory(); Object.assign(record, body); @@ -137,8 +138,9 @@ export class ProfileOtherController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.otherRepository.save(record), - this.otherHistoryRepository.save(history), + this.otherRepository.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.otherHistoryRepository.save(history, { data: req }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileOtherEmployeeController.ts b/src/controllers/ProfileOtherEmployeeController.ts index 4dfba612..93b62f2d 100644 --- a/src/controllers/ProfileOtherEmployeeController.ts +++ b/src/controllers/ProfileOtherEmployeeController.ts @@ -24,6 +24,7 @@ import { UpdateProfileOther, } from "../entities/ProfileOther"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-employee/other") @Tags("ProfileOther") @Security("bearerAuth") @@ -92,7 +93,7 @@ export class ProfileOtherEmployeeController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_EMP", profile.id); - + const before = null; const data = new ProfileOther(); const meta = { @@ -108,9 +109,10 @@ export class ProfileOtherEmployeeController extends Controller { const history = new ProfileOtherHistory(); Object.assign(history, { ...data, id: undefined }); - await this.otherRepository.save(data); + await this.otherRepository.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileOtherId = data.id; - await this.otherHistoryRepository.save(history); + await this.otherHistoryRepository.save(history, { data: req }); return new HttpSuccess(); } @@ -128,7 +130,7 @@ export class ProfileOtherEmployeeController extends Controller { "SYS_REGISTRY_EMP", record.profileEmployeeId, ); - + const before = structuredClone(record); const history = new ProfileOtherHistory(); Object.assign(record, body); @@ -146,8 +148,9 @@ export class ProfileOtherEmployeeController extends Controller { history.lastUpdatedAt = new Date(); await Promise.all([ - this.otherRepository.save(record), - this.otherHistoryRepository.save(history), + this.otherRepository.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.otherHistoryRepository.save(history, { data: req }), ]); return new HttpSuccess(); diff --git a/src/controllers/ProfileOtherEmployeeTempController.ts b/src/controllers/ProfileOtherEmployeeTempController.ts index 1c04bdbc..5911d17b 100644 --- a/src/controllers/ProfileOtherEmployeeTempController.ts +++ b/src/controllers/ProfileOtherEmployeeTempController.ts @@ -25,6 +25,7 @@ import { UpdateProfileOther, } from "../entities/ProfileOther"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-temp/other") @Tags("ProfileOther") @Security("bearerAuth") @@ -87,7 +88,7 @@ export class ProfileOtherEmployeeTempController extends Controller { if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } - + const before = null; const data = new ProfileOther(); const meta = { @@ -103,9 +104,10 @@ export class ProfileOtherEmployeeTempController extends Controller { const history = new ProfileOtherHistory(); Object.assign(history, { ...data, id: undefined }); - await this.otherRepository.save(data); + await this.otherRepository.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileOtherId = data.id; - await this.otherHistoryRepository.save(history); + await this.otherHistoryRepository.save(history, { data: req }); return new HttpSuccess(); } diff --git a/src/controllers/ProfileSalaryController.ts b/src/controllers/ProfileSalaryController.ts index 58611604..e35dc280 100644 --- a/src/controllers/ProfileSalaryController.ts +++ b/src/controllers/ProfileSalaryController.ts @@ -22,6 +22,7 @@ import { RequestWithUser } from "../middlewares/user"; import { Profile } from "../entities/Profile"; import { LessThan, MoreThan } from "typeorm"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile/salary") @Tags("ProfileSalary") @Security("bearerAuth") @@ -105,7 +106,7 @@ export class ProfileSalaryController extends Controller { where: { profileId: body.profileId }, order: { order: "DESC" }, }); - + const before = null; const data = new ProfileSalary(); const meta = { @@ -122,9 +123,10 @@ export class ProfileSalaryController extends Controller { const history = new ProfileSalaryHistory(); Object.assign(history, { ...data, id: undefined }); - await this.salaryRepo.save(data); + await this.salaryRepo.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileSalaryId = data.id; - await this.salaryHistoryRepo.save(history); + await this.salaryHistoryRepo.save(history, { data: req }); return new HttpSuccess(); } @@ -138,7 +140,7 @@ export class ProfileSalaryController extends Controller { const record = await this.salaryRepo.findOneBy({ id: salaryId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", record.profileId); - + const before = structuredClone(record); const history = new ProfileSalaryHistory(); Object.assign(record, body); @@ -155,7 +157,11 @@ export class ProfileSalaryController extends Controller { history.createdAt = new Date(); history.lastUpdatedAt = new Date(); - await Promise.all([this.salaryRepo.save(record), this.salaryHistoryRepo.save(history)]); + await Promise.all([ + this.salaryRepo.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.salaryHistoryRepo.save(history, { data: req }), + ]); return new HttpSuccess(); } diff --git a/src/controllers/ProfileSalaryEmployeeController.ts b/src/controllers/ProfileSalaryEmployeeController.ts index b3dc27e5..bdedc6e4 100644 --- a/src/controllers/ProfileSalaryEmployeeController.ts +++ b/src/controllers/ProfileSalaryEmployeeController.ts @@ -25,6 +25,7 @@ import { RequestWithUser } from "../middlewares/user"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import { LessThan, MoreThan } from "typeorm"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-employee/salary") @Tags("ProfileSalary") @Security("bearerAuth") @@ -115,7 +116,7 @@ export class ProfileSalaryEmployeeController extends Controller { where: { profileEmployeeId: body.profileEmployeeId }, order: { order: "DESC" }, }); - + const before = null; const data = new ProfileSalary(); const meta = { @@ -132,9 +133,10 @@ export class ProfileSalaryEmployeeController extends Controller { const history = new ProfileSalaryHistory(); Object.assign(history, { ...data, id: undefined }); - await this.salaryRepo.save(data); + await this.salaryRepo.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileSalaryId = data.id; - await this.salaryHistoryRepo.save(history); + await this.salaryHistoryRepo.save(history, { data: req }); return new HttpSuccess(); } @@ -152,7 +154,7 @@ export class ProfileSalaryEmployeeController extends Controller { "SYS_REGISTRY_EMP", record.profileEmployeeId, ); - + const before = structuredClone(record); const history = new ProfileSalaryHistory(); Object.assign(record, body); @@ -169,7 +171,11 @@ export class ProfileSalaryEmployeeController extends Controller { history.createdAt = new Date(); history.lastUpdatedAt = new Date(); - await Promise.all([this.salaryRepo.save(record), this.salaryHistoryRepo.save(history)]); + await Promise.all([ + this.salaryRepo.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.salaryHistoryRepo.save(history, { data: req }), + ]); return new HttpSuccess(); } diff --git a/src/controllers/ProfileSalaryEmployeeTempController.ts b/src/controllers/ProfileSalaryEmployeeTempController.ts index e7280f90..0e80775b 100644 --- a/src/controllers/ProfileSalaryEmployeeTempController.ts +++ b/src/controllers/ProfileSalaryEmployeeTempController.ts @@ -25,6 +25,7 @@ import { RequestWithUser } from "../middlewares/user"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import { LessThan, MoreThan } from "typeorm"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-temp/salary") @Tags("ProfileSalary") @Security("bearerAuth") @@ -109,7 +110,7 @@ export class ProfileSalaryEmployeeTempController extends Controller { where: { profileEmployeeId: body.profileEmployeeId }, order: { order: "DESC" }, }); - + const before = null; const data = new ProfileSalary(); const meta = { @@ -126,9 +127,10 @@ export class ProfileSalaryEmployeeTempController extends Controller { const history = new ProfileSalaryHistory(); Object.assign(history, { ...data, id: undefined }); - await this.salaryRepo.save(data); + await this.salaryRepo.save(data, { data: req }); + setLogDataDiff( req, { before, after: data }); history.profileSalaryId = data.id; - await this.salaryHistoryRepo.save(history); + await this.salaryHistoryRepo.save(history, { data: req }); return new HttpSuccess(); } @@ -143,7 +145,7 @@ export class ProfileSalaryEmployeeTempController extends Controller { const record = await this.salaryRepo.findOneBy({ id: salaryId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - + const before = structuredClone(record); const history = new ProfileSalaryHistory(); Object.assign(record, body); @@ -160,7 +162,11 @@ export class ProfileSalaryEmployeeTempController extends Controller { history.createdAt = new Date(); history.lastUpdatedAt = new Date(); - await Promise.all([this.salaryRepo.save(record), this.salaryHistoryRepo.save(history)]); + await Promise.all([ + this.salaryRepo.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.salaryHistoryRepo.save(history, { data: req }) + ]); return new HttpSuccess(); } diff --git a/src/controllers/ProfileTrainingController.ts b/src/controllers/ProfileTrainingController.ts index 04f6c937..3ac8da7d 100644 --- a/src/controllers/ProfileTrainingController.ts +++ b/src/controllers/ProfileTrainingController.ts @@ -24,6 +24,7 @@ import { ProfileTrainingHistory } from "../entities/ProfileTrainingHistory"; import { RequestWithUser } from "../middlewares/user"; import { Profile } from "../entities/Profile"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile/training") @Tags("ProfileTraining") @Security("bearerAuth") @@ -92,7 +93,7 @@ export class ProfileTrainingController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profile.id); - + const before = null; const data = new ProfileTraining(); const meta = { @@ -108,9 +109,10 @@ export class ProfileTrainingController extends Controller { const history = new ProfileTrainingHistory(); Object.assign(history, { ...data, id: undefined }); - await this.trainingRepo.save(data); + await this.trainingRepo.save(data, { data: req }); + setLogDataDiff( req, { before, after: data} ) history.profileTrainingId = data.id; - await this.trainingHistoryRepo.save(history); + await this.trainingHistoryRepo.save(history, { data: req }); return new HttpSuccess(); } @@ -124,7 +126,7 @@ export class ProfileTrainingController extends Controller { const record = await this.trainingRepo.findOneBy({ id: trainingId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", record.profileId); - + const before = structuredClone(record); const history = new ProfileTrainingHistory(); Object.assign(record, body); @@ -141,7 +143,11 @@ export class ProfileTrainingController extends Controller { history.createdAt = new Date(); history.lastUpdatedAt = new Date(); - await Promise.all([this.trainingRepo.save(record), this.trainingHistoryRepo.save(history)]); + await Promise.all([ + this.trainingRepo.save(record, { data: req }), + setLogDataDiff( req, { before, after: record} ), + this.trainingHistoryRepo.save(history, { data: req }), + ]); return new HttpSuccess(); } diff --git a/src/controllers/ProfileTrainingEmployeeController.ts b/src/controllers/ProfileTrainingEmployeeController.ts index 1f26f604..943b323b 100644 --- a/src/controllers/ProfileTrainingEmployeeController.ts +++ b/src/controllers/ProfileTrainingEmployeeController.ts @@ -25,6 +25,7 @@ import { ProfileTrainingHistory } from "../entities/ProfileTrainingHistory"; import { RequestWithUser } from "../middlewares/user"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-employee/training") @Tags("ProfileEmployeeTraining") @Security("bearerAuth") @@ -101,7 +102,7 @@ export class ProfileTrainingEmployeeController extends Controller { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_EMP", profile.id); - + const before = null; const data = new ProfileTraining(); const meta = { @@ -117,9 +118,10 @@ export class ProfileTrainingEmployeeController extends Controller { const history = new ProfileTrainingHistory(); Object.assign(history, { ...data, id: undefined }); - await this.trainingRepo.save(data); + await this.trainingRepo.save(data, { data: req }); + setLogDataDiff( req, { before, after: data} ) history.profileTrainingId = data.id; - await this.trainingHistoryRepo.save(history); + await this.trainingHistoryRepo.save(history, { data: req }); return new HttpSuccess(); } @@ -137,12 +139,12 @@ export class ProfileTrainingEmployeeController extends Controller { "SYS_REGISTRY_EMP", record.profileEmployeeId, ); - + const before = structuredClone(record); const history = new ProfileTrainingHistory(); Object.assign(record, body); Object.assign(history, { ...record, id: undefined }); - + history.profileTrainingId = trainingId; record.lastUpdateUserId = req.user.sub; record.lastUpdateFullName = req.user.name; @@ -154,7 +156,11 @@ export class ProfileTrainingEmployeeController extends Controller { history.createdAt = new Date(); history.lastUpdatedAt = new Date(); - await Promise.all([this.trainingRepo.save(record), this.trainingHistoryRepo.save(history)]); + await Promise.all([ + this.trainingRepo.save(record, { data: req }), + setLogDataDiff( req, { before, after: record} ), + this.trainingHistoryRepo.save(history, { data: req }), + ]); return new HttpSuccess(); } diff --git a/src/controllers/ProfileTrainingEmployeeTempController.ts b/src/controllers/ProfileTrainingEmployeeTempController.ts index d0d69115..8cbe6183 100644 --- a/src/controllers/ProfileTrainingEmployeeTempController.ts +++ b/src/controllers/ProfileTrainingEmployeeTempController.ts @@ -25,6 +25,7 @@ import { ProfileTrainingHistory } from "../entities/ProfileTrainingHistory"; import { RequestWithUser } from "../middlewares/user"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import permission from "../interfaces/permission"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/profile-temp/training") @Tags("ProfileEmployeeTraining") @Security("bearerAuth") @@ -94,7 +95,7 @@ export class ProfileTrainingEmployeeTempController extends Controller { if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } - + const before = null; const data = new ProfileTraining(); const meta = { @@ -110,9 +111,10 @@ export class ProfileTrainingEmployeeTempController extends Controller { const history = new ProfileTrainingHistory(); Object.assign(history, { ...data, id: undefined }); - await this.trainingRepo.save(data); + await this.trainingRepo.save(data, { data: req }); + setLogDataDiff(req, { before, after: data }); history.profileTrainingId = data.id; - await this.trainingHistoryRepo.save(history); + await this.trainingHistoryRepo.save(history, { data: req }); return new HttpSuccess(); } @@ -127,7 +129,7 @@ export class ProfileTrainingEmployeeTempController extends Controller { const record = await this.trainingRepo.findOneBy({ id: trainingId }); if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - + const before = structuredClone(record); const history = new ProfileTrainingHistory(); Object.assign(record, body); @@ -144,7 +146,11 @@ export class ProfileTrainingEmployeeTempController extends Controller { history.createdAt = new Date(); history.lastUpdatedAt = new Date(); - await Promise.all([this.trainingRepo.save(record), this.trainingHistoryRepo.save(history)]); + await Promise.all([ + this.trainingRepo.save(record, { data: req }), + setLogDataDiff(req, { before, after: record }), + this.trainingHistoryRepo.save(history, { data: req }), + ]); return new HttpSuccess(); } diff --git a/src/controllers/ProvinceController.ts b/src/controllers/ProvinceController.ts index b53b9e65..18dbd36a 100644 --- a/src/controllers/ProvinceController.ts +++ b/src/controllers/ProvinceController.ts @@ -19,6 +19,8 @@ import HttpStatusCode from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { Province, CreateProvince, UpdateProvince } from "../entities/Province"; import { Not } from "typeorm"; +import { setLogDataDiff } from "../interfaces/utils"; +import { RequestWithUser } from "../middlewares/user"; @Route("api/v1/org/metadata/province") @Tags("Province") @@ -77,7 +79,7 @@ export class ProvinceController extends Controller { async Post( @Body() requestBody: CreateProvince, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const _province = Object.assign(new Province(), requestBody); if (!_province) { @@ -91,14 +93,15 @@ export class ProvinceController extends Controller { if (checkName) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } - + const before = null; _province.createdUserId = request.user.sub; _province.createdFullName = request.user.name; _province.lastUpdateUserId = request.user.sub; _province.lastUpdateFullName = request.user.name; _province.createdAt = new Date(); _province.lastUpdatedAt = new Date(); - await this.provinceRepository.save(_province); + await this.provinceRepository.save(_province, { data: request }); + setLogDataDiff( request, { before, after: _province} ) return new HttpSuccess(); } @@ -114,7 +117,7 @@ export class ProvinceController extends Controller { @Path() id: string, @Body() requestBody: UpdateProvince, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const _province = await this.provinceRepository.findOne({ where: { id: id } }); if (!_province) { @@ -126,12 +129,13 @@ export class ProvinceController extends Controller { if (checkName) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } - + const before = structuredClone(_province); _province.lastUpdateUserId = request.user.sub; _province.lastUpdateFullName = request.user.name; _province.lastUpdatedAt = new Date(); this.provinceRepository.merge(_province, requestBody); - await this.provinceRepository.save(_province); + await this.provinceRepository.save(_province, { data: request }); + setLogDataDiff( request, { before, after: _province} ) return new HttpSuccess(); } diff --git a/src/controllers/RankController.ts b/src/controllers/RankController.ts index fb8529c4..fdf345ed 100644 --- a/src/controllers/RankController.ts +++ b/src/controllers/RankController.ts @@ -19,6 +19,8 @@ import HttpStatusCode from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { CreateRank, Rank } from "../entities/Rank"; import { Not } from "typeorm"; +import { setLogDataDiff } from "../interfaces/utils"; +import { RequestWithUser } from "../middlewares/user"; @Route("api/v1/org/metadata/rank") @Tags("Rank") @Security("bearerAuth") @@ -40,7 +42,7 @@ export class RankController extends Controller { async createRank( @Body() requestBody: CreateRank, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const checkName = await this.rankRepository.findOne({ where: { name: requestBody.name }, @@ -50,6 +52,7 @@ export class RankController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } + const before = null; const rank = Object.assign(new Rank(), requestBody); rank.createdUserId = request.user.sub; rank.createdFullName = request.user.name; @@ -57,7 +60,8 @@ export class RankController extends Controller { rank.lastUpdateFullName = request.user.name; rank.createdAt = new Date(); rank.lastUpdatedAt = new Date(); - await this.rankRepository.save(rank); + await this.rankRepository.save(rank, { data: request }); + setLogDataDiff( request, { before, after: rank} ) return new HttpSuccess(); } @@ -73,7 +77,7 @@ export class RankController extends Controller { @Path() id: string, @Body() requestBody: CreateRank, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const rank = await this.rankRepository.findOne({ where: { id: id } }); if (!rank) { @@ -87,12 +91,13 @@ export class RankController extends Controller { if (checkName) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } - + const before = structuredClone(rank); rank.lastUpdateUserId = request.user.sub; rank.lastUpdateFullName = request.user.name; rank.lastUpdatedAt = new Date(); this.rankRepository.merge(rank, requestBody); - await this.rankRepository.save(rank); + await this.rankRepository.save(rank, { data: request }); + setLogDataDiff(request, { before, after: rank }); return new HttpSuccess(); } @@ -104,14 +109,14 @@ export class RankController extends Controller { * @param {string} id Id ยศ */ @Delete("{id}") - async deleteRank(@Path() id: string) { + async deleteRank(@Path() id: string, @Request() req: RequestWithUser) { const delRank = await this.rankRepository.findOne({ where: { id }, }); if (!delRank) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); } - await this.rankRepository.delete({ id: id }); + await this.rankRepository.remove(delRank, { data: req }); return new HttpSuccess(); } diff --git a/src/controllers/RelationshipController.ts b/src/controllers/RelationshipController.ts index 8a46e818..e09b517f 100644 --- a/src/controllers/RelationshipController.ts +++ b/src/controllers/RelationshipController.ts @@ -19,6 +19,9 @@ import HttpStatusCode from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { CreateRelationship, Relationship } from "../entities/Relationship"; import { Not } from "typeorm"; +import { RequestWithUser } from "../middlewares/user"; +import { setLogDataDiff } from "../interfaces/utils"; +import { request } from "axios"; @Route("api/v1/org/metadata/relationship") @Tags("Relationship") @Security("bearerAuth") @@ -40,7 +43,7 @@ export class RelationshipController extends Controller { async createRelationship( @Body() requestBody: CreateRelationship, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const checkName = await this.relationshipRepository.findOne({ where: { name: requestBody.name }, @@ -49,7 +52,7 @@ export class RelationshipController extends Controller { if (checkName) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } - + const before = null; const relationship = Object.assign(new Relationship(), requestBody); relationship.createdUserId = request.user.sub; relationship.createdFullName = request.user.name; @@ -57,7 +60,8 @@ export class RelationshipController extends Controller { relationship.lastUpdateFullName = request.user.name; relationship.createdAt = new Date(); relationship.lastUpdatedAt = new Date(); - await this.relationshipRepository.save(relationship); + await this.relationshipRepository.save(relationship, { data: request }); + setLogDataDiff( request, { before, after: relationship }); return new HttpSuccess(); } @@ -73,7 +77,7 @@ export class RelationshipController extends Controller { @Path() id: string, @Body() requestBody: CreateRelationship, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const relationship = await this.relationshipRepository.findOne({ where: { id: id } }); if (!relationship) { @@ -88,11 +92,13 @@ export class RelationshipController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } + const before = structuredClone(relationship); relationship.lastUpdateUserId = request.user.sub; relationship.lastUpdateFullName = request.user.name; relationship.lastUpdatedAt = new Date(); this.relationshipRepository.merge(relationship, requestBody); - await this.relationshipRepository.save(relationship); + await this.relationshipRepository.save(relationship, { data: request }); + setLogDataDiff( request, { before, after: relationship } ) return new HttpSuccess(); } @@ -104,14 +110,14 @@ export class RelationshipController extends Controller { * @param {string} id Id สถานภาพ */ @Delete("{id}") - async deleteRelationship(@Path() id: string) { + async deleteRelationship(@Path() id: string, @Request() request: RequestWithUser) { const delRelationship = await this.relationshipRepository.findOne({ where: { id }, }); if (!delRelationship) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพนี้"); } - await this.relationshipRepository.delete({ id: id }); + await this.relationshipRepository.remove(delRelationship, { data: request }); return new HttpSuccess(); } diff --git a/src/controllers/ReligionController.ts b/src/controllers/ReligionController.ts index c883b851..dfd3b98d 100644 --- a/src/controllers/ReligionController.ts +++ b/src/controllers/ReligionController.ts @@ -19,6 +19,8 @@ import HttpStatusCode from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { CreateReligion, Religion } from "../entities/Religion"; import { Not } from "typeorm"; +import { setLogDataDiff } from "../interfaces/utils"; +import { RequestWithUser } from "../middlewares/user"; @Route("api/v1/org/metadata/religion") @Tags("Religion") @Security("bearerAuth") @@ -40,7 +42,7 @@ export class ReligionController extends Controller { async createReligion( @Body() requestBody: CreateReligion, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const checkName = await this.religionRepository.findOne({ where: { name: requestBody.name }, @@ -49,7 +51,7 @@ export class ReligionController extends Controller { if (checkName) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } - + const before = null; const religion = Object.assign(new Religion(), requestBody); religion.createdUserId = request.user.sub; religion.createdFullName = request.user.name; @@ -57,7 +59,8 @@ export class ReligionController extends Controller { religion.lastUpdateFullName = request.user.name; religion.createdAt = new Date(); religion.lastUpdatedAt = new Date(); - await this.religionRepository.save(religion); + await this.religionRepository.save(religion, { data: request }); + setLogDataDiff(request, { before, after: religion }); return new HttpSuccess(); } @@ -73,7 +76,7 @@ export class ReligionController extends Controller { @Path() id: string, @Body() requestBody: CreateReligion, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const religion = await this.religionRepository.findOne({ where: { id: id } }); if (!religion) { @@ -87,12 +90,13 @@ export class ReligionController extends Controller { if (checkName) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } - + const before = structuredClone(religion); religion.lastUpdateUserId = request.user.sub; religion.lastUpdateFullName = request.user.name; religion.lastUpdatedAt = new Date(); this.religionRepository.merge(religion, requestBody); - await this.religionRepository.save(religion); + await this.religionRepository.save(religion, { data: request }); + setLogDataDiff(request, { before, after: religion }); return new HttpSuccess(); } @@ -104,14 +108,14 @@ export class ReligionController extends Controller { * @param {string} id Id ศาสนา */ @Delete("{id}") - async deleteReligion(@Path() id: string) { + async deleteReligion(@Path() id: string, @Request() request: RequestWithUser) { const delReligion = await this.religionRepository.findOne({ where: { id }, }); if (!delReligion) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลศาสนานี้"); } - await this.religionRepository.delete({ id: id }); + await this.religionRepository.remove(delReligion, { data: request }); return new HttpSuccess(); } diff --git a/src/controllers/SubDistrictController.ts b/src/controllers/SubDistrictController.ts index 7d355f23..d503a695 100644 --- a/src/controllers/SubDistrictController.ts +++ b/src/controllers/SubDistrictController.ts @@ -20,6 +20,8 @@ import HttpError from "../interfaces/http-error"; import { SubDistrict, CreateSubDistrict, UpdateSubDistrict } from "../entities/SubDistrict"; import { District } from "../entities/District"; import { Not } from "typeorm"; +import { RequestWithUser } from "../middlewares/user"; +import { setLogDataDiff } from "../interfaces/utils"; @Route("api/v1/org/metadata/subDistrict") @Tags("SubDistrict") @@ -78,7 +80,7 @@ export class SubDistrictController extends Controller { async Post( @Body() requestBody: CreateSubDistrict, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const _subDistrict = Object.assign(new SubDistrict(), requestBody); if (!_subDistrict) { @@ -102,14 +104,15 @@ export class SubDistrictController extends Controller { if (checkName) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } - + const before = null; _subDistrict.createdUserId = request.user.sub; _subDistrict.createdFullName = request.user.name; _subDistrict.lastUpdateUserId = request.user.sub; _subDistrict.lastUpdateFullName = request.user.name; _subDistrict.createdAt = new Date(); _subDistrict.lastUpdatedAt = new Date(); - await this.subDistrictRepository.save(_subDistrict); + await this.subDistrictRepository.save(_subDistrict, { data: request }); + setLogDataDiff(request, { before, after: _subDistrict }); return new HttpSuccess(); } @@ -125,7 +128,7 @@ export class SubDistrictController extends Controller { @Path() id: string, @Body() requestBody: UpdateSubDistrict, - @Request() request: { user: Record }, + @Request() request: RequestWithUser, ) { const _subDistrict = await this.subDistrictRepository.findOne({ where: { id: id } }); if (!_subDistrict) { @@ -149,12 +152,13 @@ export class SubDistrictController extends Controller { if (checkName) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } - + const before = null; _subDistrict.lastUpdateUserId = request.user.sub; _subDistrict.lastUpdateFullName = request.user.name; _subDistrict.lastUpdatedAt = new Date(); this.subDistrictRepository.merge(_subDistrict, requestBody); - await this.subDistrictRepository.save(_subDistrict); + await this.subDistrictRepository.save(_subDistrict, { data: request }); + setLogDataDiff(request, { before, after: _subDistrict }); return new HttpSuccess(); } diff --git a/src/database/data-source.ts b/src/database/data-source.ts index efc31da6..aa285548 100644 --- a/src/database/data-source.ts +++ b/src/database/data-source.ts @@ -1,6 +1,36 @@ import "dotenv/config"; import "reflect-metadata"; -import { DataSource } from "typeorm"; +import { DataSource, Logger, QueryRunner } from "typeorm"; +import { RequestWithUser } from "../middlewares/user"; +import { addLogSequence } from "../interfaces/utils"; + +export class MyCustomLogger implements Logger { + log(level: "log" | "info" | "warn", message: any, queryRunner?: QueryRunner) { } + + logQuery(query: string, parameters?: any[], queryRunner?: QueryRunner): void { + const req = queryRunner?.data as RequestWithUser; + if (req?.app?.locals.logData?.sequence) { + addLogSequence(req, { + action: "database", + status: "success", + description: "Query Data.", + query: [ + "Query: " + query + (parameters ? " - Parameters:" + JSON.stringify(parameters) : ""), + ], + }); + } + } + + logMigration(message: string, queryRunner?: QueryRunner) {} + logQueryError( + error: string | Error, + query: string, + parameters?: any[], + queryRunner?: QueryRunner, + ) {} + logQuerySlow(time: number, query: string, parameters?: any[], queryRunner?: QueryRunner) {} + logSchemaBuild(message: string, queryRunner?: QueryRunner) {} +} export const AppDataSource = new DataSource({ type: "mysql", @@ -21,6 +51,7 @@ export const AppDataSource = new DataSource({ ? ["src/migration/**/*.ts"] : ["dist/migration/**/*{.ts,.js}"], subscribers: [], + logger: new MyCustomLogger(), }); // export default database; diff --git a/src/interfaces/call-api.ts b/src/interfaces/call-api.ts index 1989aeed..456c2f80 100644 --- a/src/interfaces/call-api.ts +++ b/src/interfaces/call-api.ts @@ -1,5 +1,6 @@ import { Path } from "tsoa"; import axios from "axios"; +import { addLogSequence } from "./utils"; class CallAPI { //Get @@ -14,8 +15,28 @@ class CallAPI { api_key: process.env.API_KEY, }, }); + addLogSequence(request, { + action: "request", + status: "success", + description: "connected", + request: { + method: "GET", + url: url, + response: JSON.stringify(response.data.result), + }, + }); return response.data.result; } catch (error) { + addLogSequence(request, { + action: "request", + status: "error", + description: "unconnected", + request: { + method: "GET", + url: url, + response: JSON.stringify(error), + }, + }); throw error; } } @@ -31,8 +52,30 @@ class CallAPI { api_key: process.env.API_KEY, }, }); + addLogSequence(request, { + action: "request", + status: "success", + description: "connected", + request: { + method: "POST", + url: url, + payload: JSON.stringify(sendData), + response: JSON.stringify(response.data.result), + }, + }); return response.data.result; } catch (error) { + addLogSequence(request, { + action: "request", + status: "error", + description: "unconnected", + request: { + method: "POST", + url: url, + payload: JSON.stringify(sendData), + response: JSON.stringify(error), + }, + }); console.log(error); throw error; } diff --git a/src/middlewares/auth.ts b/src/middlewares/auth.ts index e81aa156..ea60f6b4 100644 --- a/src/middlewares/auth.ts +++ b/src/middlewares/auth.ts @@ -57,6 +57,14 @@ export async function expressAuthentication( break; } + if (!request.app.locals.logData) { + request.app.locals.logData = {}; + } + + request.app.locals.logData.userId = payload.sub; + request.app.locals.logData.userName = payload.name; + request.app.locals.logData.user = payload.preferred_username; + return payload; } diff --git a/src/middlewares/error.ts b/src/middlewares/error.ts index b010f0a1..2bf192e9 100644 --- a/src/middlewares/error.ts +++ b/src/middlewares/error.ts @@ -4,6 +4,12 @@ import HttpStatus from "../interfaces/http-status"; import { ValidateError } from "tsoa"; function error(error: Error, _req: Request, res: Response, _next: NextFunction) { + const logData = _req.app.locals.logData.sequence?.at(-1); + if (logData) { + logData.status = "error"; + logData.description = error.message; + } + if (error instanceof HttpError) { return res.status(error.status).json({ status: error.status, diff --git a/src/middlewares/logs.ts b/src/middlewares/logs.ts index 5468949a..f203fecc 100644 --- a/src/middlewares/logs.ts +++ b/src/middlewares/logs.ts @@ -41,9 +41,9 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) { let system = "org"; if (req.url.startsWith("/api/v1/org/metadata/")) system="metadata"; if (req.url.startsWith("/api/v1/org/auth/authRoleAttr/")) system = "admin"; - if (req.url.startsWith("/api/v1/org/auth/authRoleAttr/")) system = "admin"; - if (req.url.startsWith("/api/v1/org/auth/authRoleAttr/")) system = "admin"; - if (req.url.startsWith("/api/v1/org/auth/authRoleAttr/")) system = "admin"; + if (req.url.startsWith("/api/v1/org/profile/")) system = "profile"; + // 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;