validation

This commit is contained in:
AdisakKanthawilang 2024-06-11 16:09:12 +07:00
parent d78391e1ee
commit de27e1941b
3 changed files with 39 additions and 14 deletions

View file

@ -16,6 +16,7 @@ import { RequestWithUser } from "../middlewares/user";
import HttpError from "../interfaces/http-error";
import HttpStatus from "../interfaces/http-status";
import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status";
import { AuthRoleAttr, CreateAuthRoleAttr, UpdateAuthRoleAttr } from "../entities/AuthRoleAttr";
import { AuthRole } from "../entities/AuthRole";
import { AuthSys } from "../entities/AuthSys";
@ -48,17 +49,20 @@ export class AuthRoleAttrController extends Controller {
@Post()
public async newAuthRoleAttr(@Request() req: RequestWithUser, @Body() body: CreateAuthRoleAttr) {
const chkAuthRole = await this.authRoleRepo.findOneBy({ id: body.authRoleId });
if (!chkAuthRole) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล roleId");
}
const chkAuthSys = await this.authSysRepo.findOneBy({ id: body.authSysId });
if (!chkAuthSys) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล systemId");
}
body.attrOwnership = body.attrOwnership?.toUpperCase();
body.attrPrivilege = body.attrPrivilege?.toUpperCase();
body.authSysId = body.authSysId?.toUpperCase();
const data = new AuthRoleAttr();
const meta = {
createdUserId: req.user.sub,
@ -76,24 +80,28 @@ export class AuthRoleAttrController extends Controller {
@Patch("{roleAttrId}")
public async editAuthRoleAttr(
@Body() requestBody: UpdateAuthRoleAttr,
@Body() body: UpdateAuthRoleAttr,
@Request() req: RequestWithUser,
@Path() roleAttrId: string,
) {
const record = await this.authRoleAttrRepo.findOneBy({ id: roleAttrId });
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const chkAuthRole = await this.authRoleRepo.findOneBy({ id: requestBody.authRoleId });
const chkAuthRole = await this.authRoleRepo.findOneBy({ id: body.authRoleId });
if (!chkAuthRole) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล roleId");
}
const chkAuthSys = await this.authSysRepo.findOneBy({ id: requestBody.authSysId });
const chkAuthSys = await this.authSysRepo.findOneBy({ id: body.authSysId });
if (!chkAuthSys) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล systemId");
}
Object.assign(record, requestBody);
body.attrOwnership = body.attrOwnership?.toUpperCase();
body.attrPrivilege = body.attrPrivilege?.toUpperCase();
body.authSysId = body.authSysId?.toUpperCase();
Object.assign(record, body);
record.lastUpdateFullName = req.user.name;
await Promise.all([this.authRoleAttrRepo.save(record)]);
@ -103,8 +111,15 @@ export class AuthRoleAttrController extends Controller {
@Delete("{roleAttrId}")
public async deleteRole(@Path() roleAttrId: string) {
const result = await this.authRoleAttrRepo.delete({ id: roleAttrId });
let result: any;
try {
result = await this.authRoleAttrRepo.delete({ id: roleAttrId });
} catch {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่สามารถลบข้อมูลได้",
);
}
if (result.affected == undefined || result.affected <= 0)
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");

View file

@ -62,13 +62,13 @@ export class AuthRoleController extends Controller {
@Patch("{roleId}")
public async editAuthRole(
@Body() requestBody: UpdateAuthRole,
@Body() body: UpdateAuthRole,
@Request() req: RequestWithUser,
@Path() roleId: string,
) {
const record = await this.authRoleRepo.findOneBy({ id: roleId });
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
Object.assign(record, requestBody);
Object.assign(record, body);
record.lastUpdateFullName = req.user.name;
await Promise.all([this.authRoleRepo.save(record)]);

View file

@ -16,6 +16,7 @@ import { RequestWithUser } from "../middlewares/user";
import HttpError from "../interfaces/http-error";
import HttpStatus from "../interfaces/http-status";
import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status";
import { AuthSys, CreateAuthSys, UpdateAuthSys } from "../entities/AuthSys";
@Route("api/v1/org/auth/authSys")
@ -48,6 +49,8 @@ export class AuthSysController extends Controller {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบค่าไอดีที่ส่งมา");
}
body.id = body.id?.toUpperCase();
const data = new AuthSys();
const meta = {
createdUserId: req.user.sub,
@ -65,13 +68,16 @@ export class AuthSysController extends Controller {
@Patch("{systemId}")
public async editAuthSys(
@Body() requestBody: UpdateAuthSys,
@Body() body: UpdateAuthSys,
@Request() req: RequestWithUser,
@Path() systemId: string,
) {
const record = await this.authSysRepo.findOneBy({ id: systemId });
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
Object.assign(record, requestBody);
body.id = body.id?.toUpperCase();
Object.assign(record, body);
record.lastUpdateFullName = req.user.name;
await Promise.all([this.authSysRepo.save(record)]);
@ -81,8 +87,12 @@ export class AuthSysController extends Controller {
@Delete("{systemId}")
public async deleteAuthSys(@Path() systemId: string) {
const result = await this.authSysRepo.delete({ id: systemId });
let result: any;
try {
result = await this.authSysRepo.delete({ id: systemId });
} catch {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถลบข้อมูลได้");
}
if (result.affected == undefined || result.affected <= 0)
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");