2024-06-11 13:19:38 +07:00
|
|
|
import {
|
|
|
|
|
Body,
|
|
|
|
|
Controller,
|
|
|
|
|
Delete,
|
|
|
|
|
Get,
|
|
|
|
|
Patch,
|
|
|
|
|
Path,
|
|
|
|
|
Post,
|
|
|
|
|
Request,
|
|
|
|
|
Route,
|
|
|
|
|
Security,
|
|
|
|
|
Tags,
|
|
|
|
|
} from "tsoa";
|
|
|
|
|
import { AppDataSource } from "../database/data-source";
|
|
|
|
|
import { RequestWithUser } from "../middlewares/user";
|
|
|
|
|
import HttpError from "../interfaces/http-error";
|
|
|
|
|
import HttpStatus from "../interfaces/http-status";
|
|
|
|
|
import HttpSuccess from "../interfaces/http-success";
|
2024-06-11 16:09:12 +07:00
|
|
|
import HttpStatusCode from "../interfaces/http-status";
|
2024-06-11 13:19:38 +07:00
|
|
|
import { AuthSys, CreateAuthSys, UpdateAuthSys } from "../entities/AuthSys";
|
|
|
|
|
|
|
|
|
|
@Route("api/v1/org/auth/authSys")
|
|
|
|
|
@Tags("AuthSystem")
|
|
|
|
|
@Security("bearerAuth")
|
|
|
|
|
export class AuthSysController extends Controller {
|
|
|
|
|
private authSysRepo = AppDataSource.getRepository(AuthSys);
|
|
|
|
|
|
|
|
|
|
@Get("list")
|
2024-06-11 14:20:19 +07:00
|
|
|
public async listAuthSys() {
|
2024-06-11 13:19:38 +07:00
|
|
|
const getList = await this.authSysRepo.find();
|
2024-06-12 13:23:02 +07:00
|
|
|
if (!getList || getList.length === 0) {
|
|
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
|
|
|
}
|
2024-06-11 13:19:38 +07:00
|
|
|
return new HttpSuccess(getList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Get("{systemId}")
|
|
|
|
|
public async detailAuthSys(@Path() systemId: string) {
|
|
|
|
|
const getDetail = await this.authSysRepo.findBy({ id: systemId });
|
|
|
|
|
if (!getDetail) {
|
|
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
|
|
|
}
|
|
|
|
|
return new HttpSuccess(getDetail);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post()
|
|
|
|
|
public async newAuthSys(@Request() req: RequestWithUser, @Body() body: CreateAuthSys) {
|
|
|
|
|
if (!body.id) {
|
|
|
|
|
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบค่าไอดีที่ส่งมา");
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-11 16:09:12 +07:00
|
|
|
body.id = body.id?.toUpperCase();
|
|
|
|
|
|
2024-06-11 13:19:38 +07:00
|
|
|
const data = new AuthSys();
|
|
|
|
|
const meta = {
|
|
|
|
|
createdUserId: req.user.sub,
|
|
|
|
|
createdFullName: req.user.name,
|
|
|
|
|
lastUpdateUserId: req.user.sub,
|
|
|
|
|
lastUpdateFullName: req.user.name,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Object.assign(data, { ...body, ...meta });
|
|
|
|
|
|
|
|
|
|
await this.authSysRepo.save(data);
|
|
|
|
|
|
2024-06-11 18:27:54 +07:00
|
|
|
return new HttpSuccess(data.id);
|
2024-06-11 13:19:38 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Patch("{systemId}")
|
|
|
|
|
public async editAuthSys(
|
2024-06-11 16:09:12 +07:00
|
|
|
@Body() body: UpdateAuthSys,
|
2024-06-11 13:19:38 +07:00
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Path() systemId: string,
|
|
|
|
|
) {
|
|
|
|
|
const record = await this.authSysRepo.findOneBy({ id: systemId });
|
|
|
|
|
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
2024-06-11 16:09:12 +07:00
|
|
|
|
|
|
|
|
body.id = body.id?.toUpperCase();
|
|
|
|
|
|
|
|
|
|
Object.assign(record, body);
|
2024-06-11 13:19:38 +07:00
|
|
|
record.lastUpdateFullName = req.user.name;
|
|
|
|
|
|
|
|
|
|
await Promise.all([this.authSysRepo.save(record)]);
|
|
|
|
|
|
|
|
|
|
return new HttpSuccess();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Delete("{systemId}")
|
2024-06-11 14:20:19 +07:00
|
|
|
public async deleteAuthSys(@Path() systemId: string) {
|
2024-06-11 16:09:12 +07:00
|
|
|
let result: any;
|
|
|
|
|
try {
|
|
|
|
|
result = await this.authSysRepo.delete({ id: systemId });
|
|
|
|
|
} catch {
|
|
|
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถลบข้อมูลได้");
|
|
|
|
|
}
|
2024-06-11 13:19:38 +07:00
|
|
|
if (result.affected == undefined || result.affected <= 0)
|
|
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
|
|
|
|
|
|
|
|
return new HttpSuccess();
|
|
|
|
|
}
|
|
|
|
|
}
|