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 HttpError from "../interfaces/http-error";
import HttpStatus from "../interfaces/http-status"; import HttpStatus from "../interfaces/http-status";
import HttpSuccess from "../interfaces/http-success"; import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status";
import { AuthRoleAttr, CreateAuthRoleAttr, UpdateAuthRoleAttr } from "../entities/AuthRoleAttr"; import { AuthRoleAttr, CreateAuthRoleAttr, UpdateAuthRoleAttr } from "../entities/AuthRoleAttr";
import { AuthRole } from "../entities/AuthRole"; import { AuthRole } from "../entities/AuthRole";
import { AuthSys } from "../entities/AuthSys"; import { AuthSys } from "../entities/AuthSys";
@ -48,7 +49,6 @@ export class AuthRoleAttrController extends Controller {
@Post() @Post()
public async newAuthRoleAttr(@Request() req: RequestWithUser, @Body() body: CreateAuthRoleAttr) { public async newAuthRoleAttr(@Request() req: RequestWithUser, @Body() body: CreateAuthRoleAttr) {
const chkAuthRole = await this.authRoleRepo.findOneBy({ id: body.authRoleId }); const chkAuthRole = await this.authRoleRepo.findOneBy({ id: body.authRoleId });
if (!chkAuthRole) { if (!chkAuthRole) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล roleId"); throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล roleId");
@ -59,6 +59,10 @@ export class AuthRoleAttrController extends Controller {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล systemId"); 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 data = new AuthRoleAttr();
const meta = { const meta = {
createdUserId: req.user.sub, createdUserId: req.user.sub,
@ -76,24 +80,28 @@ export class AuthRoleAttrController extends Controller {
@Patch("{roleAttrId}") @Patch("{roleAttrId}")
public async editAuthRoleAttr( public async editAuthRoleAttr(
@Body() requestBody: UpdateAuthRoleAttr, @Body() body: UpdateAuthRoleAttr,
@Request() req: RequestWithUser, @Request() req: RequestWithUser,
@Path() roleAttrId: string, @Path() roleAttrId: string,
) { ) {
const record = await this.authRoleAttrRepo.findOneBy({ id: roleAttrId }); const record = await this.authRoleAttrRepo.findOneBy({ id: roleAttrId });
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); 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) { if (!chkAuthRole) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล roleId"); 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) { if (!chkAuthSys) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล systemId"); 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; record.lastUpdateFullName = req.user.name;
await Promise.all([this.authRoleAttrRepo.save(record)]); await Promise.all([this.authRoleAttrRepo.save(record)]);
@ -103,8 +111,15 @@ export class AuthRoleAttrController extends Controller {
@Delete("{roleAttrId}") @Delete("{roleAttrId}")
public async deleteRole(@Path() roleAttrId: string) { 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) if (result.affected == undefined || result.affected <= 0)
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");

View file

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