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