ผูก log เมนูทะเบียนประวัติ

This commit is contained in:
AdisakKanthawilang 2024-10-03 15:57:44 +07:00
parent 5643cc67c4
commit 6539804937
76 changed files with 909 additions and 431 deletions

View file

@ -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<string, any> },
@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<string, any> },
@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();
}