test redis

This commit is contained in:
Kittapath 2024-07-24 09:20:54 +07:00
parent df0aa633db
commit 81c4533f86
4 changed files with 132 additions and 54 deletions

View file

@ -22,14 +22,17 @@ import { AuthRoleAttr } from "../entities/AuthRoleAttr";
import { PosMaster } from "../entities/PosMaster";
import { EmployeePosMaster } from "../entities/EmployeePosMaster";
const REDIS_HOST = process.env.REDIS_HOST;
const REDIS_PORT = process.env.REDIS_PORT;
@Route("api/v1/org/auth/authRole")
@Tags("AuthRole")
@Security("bearerAuth")
export class AuthRoleController extends Controller {
private authRoleRepo = AppDataSource.getRepository(AuthRole);
private authRoleAttrRepo = AppDataSource.getRepository(AuthRoleAttr);
private authRoleAttrRepo = AppDataSource.getRepository(AuthRoleAttr);
private posMasterRepository = AppDataSource.getRepository(PosMaster);
private employeePosMasterRepository = AppDataSource.getRepository(EmployeePosMaster)
private employeePosMasterRepository = AppDataSource.getRepository(EmployeePosMaster);
private redis = require("redis");
@Get("list")
public async listAuthRole() {
@ -77,50 +80,51 @@ export class AuthRoleController extends Controller {
}
@Post("govoment")
public async AddAuthRoleGovoment(@Request() req: RequestWithUser, @Body() body: CreateAddAuthRole) {
public async AddAuthRoleGovoment(
@Request() req: RequestWithUser,
@Body() body: CreateAddAuthRole,
) {
let NULL_: any = null;
let getDetail;
let NULL_ : any = null;
let getDetail
if(body.authRoleId == "") {
body.authRoleId = NULL_
}
else {
if (body.authRoleId == "") {
body.authRoleId = NULL_;
} else {
getDetail = await this.authRoleRepo.findOneBy({ id: body.authRoleId });
if (!getDetail) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์");
}
const posMaster = await this.posMasterRepository.findOneBy({ id : body.posMasterId })
const posMaster = await this.posMasterRepository.findOneBy({ id: body.posMasterId });
if (!posMaster) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลตำแหน่ง");
posMaster.lastUpdateUserId = req.user.sub;
posMaster.lastUpdateFullName = req.user.name;
posMaster.authRoleId = body.authRoleId
posMaster.authRoleId = body.authRoleId;
await this.posMasterRepository.save(posMaster);
return new HttpSuccess();
}
@Post("employee")
public async AddAuthRoleEmployee(@Request() req: RequestWithUser, @Body() body: CreateAddAuthRole) {
public async AddAuthRoleEmployee(
@Request() req: RequestWithUser,
@Body() body: CreateAddAuthRole,
) {
let NULL_: any = null;
let getDetail;
let NULL_ : any = null;
let getDetail
if(body.authRoleId == "") {
body.authRoleId = NULL_
}
else {
if (body.authRoleId == "") {
body.authRoleId = NULL_;
} else {
getDetail = await this.authRoleRepo.findOneBy({ id: body.authRoleId });
if (!getDetail) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์");
}
const posMaster = await this.employeePosMasterRepository.findOneBy({ id : body.posMasterId })
const posMaster = await this.employeePosMasterRepository.findOneBy({ id: body.posMasterId });
if (!posMaster) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลตำแหน่ง");
posMaster.lastUpdateUserId = req.user.sub;
posMaster.lastUpdateFullName = req.user.name;
posMaster.authRoleId = body.authRoleId
posMaster.authRoleId = body.authRoleId;
await this.employeePosMasterRepository.save(posMaster);
return new HttpSuccess();
}
@ -231,4 +235,40 @@ export class AuthRoleController extends Controller {
return new HttpSuccess();
}
@Get("test/asdasd/{roleId}")
public async userRoleRedis(@Path() roleId: string) {
const redisClient = await this.redis.createClient({
host: REDIS_HOST,
port: REDIS_PORT,
});
console.log("xxxxxxxxxxxxxxxxx7");
let result = null;
console.log("xxxxxxxxxxxxxxxxx8");
await Promise.all([
await redisClient.get(roleId, async (err: any, reply: any) => {
console.log("xxxxxxxxxxxxxxxxx1");
console.log(reply);
if (reply != null) {
console.log("xxxxxxxxxxxxxxxxx2");
// console.log(reply);
// console.log(JSON.parse(reply));
result = JSON.parse(reply);
// return;
} else {
console.log("xxxxxxxxxxxxxxxxx3");
result = await this.authRoleRepo.find();
console.log("xxxxxxxxxxxxxxxxx4");
redisClient.setex(roleId, 20, JSON.stringify(result));
// return new HttpSuccess(result);
console.log("xxxxxxxxxxxxxxxxx5");
}
return new HttpSuccess(result);
}),
]);
console.log("xxxxxxxxxxxxxxxxx6");
console.log(result);
return new HttpSuccess("result");
}
}

View file

@ -52,26 +52,20 @@ export class ProfileEditEmployeeController extends Controller {
if (status != "" && status != null) {
qb.andWhere("ProfileEdit.status = :status", { status: status });
}
qb.andWhere("ProfileEdit.profileEmployeeId = :profileEmployeeId", { profileEmployeeId: profile.id });
qb.andWhere("ProfileEdit.profileEmployeeId = :profileEmployeeId", {
profileEmployeeId: profile.id,
});
})
.andWhere(
new Brackets((qb) => {
qb.where(
keyword != "" && keyword != null
? "ProfileEdit.topic LIKE :keyword"
: "1=1",
qb.where(keyword != "" && keyword != null ? "ProfileEdit.topic LIKE :keyword" : "1=1", {
keyword: `%${keyword}%`,
}).orWhere(
keyword != "" && keyword != null ? "ProfileEdit.detail LIKE :keyword" : "1=1",
{
keyword: `%${keyword}%`,
},
)
.orWhere(
keyword != "" && keyword != null
? "ProfileEdit.detail LIKE :keyword"
: "1=1",
{
keyword: `%${keyword}%`,
},
)
);
}),
)
.orderBy("ProfileEdit.createdAt", "ASC")
@ -118,22 +112,14 @@ export class ProfileEditEmployeeController extends Controller {
})
.andWhere(
new Brackets((qb) => {
qb.where(
keyword != "" && keyword != null
? "ProfileEdit.topic LIKE :keyword"
: "1=1",
qb.where(keyword != "" && keyword != null ? "ProfileEdit.topic LIKE :keyword" : "1=1", {
keyword: `%${keyword}%`,
}).orWhere(
keyword != "" && keyword != null ? "ProfileEdit.detail LIKE :keyword" : "1=1",
{
keyword: `%${keyword}%`,
},
)
.orWhere(
keyword != "" && keyword != null
? "ProfileEdit.detail LIKE :keyword"
: "1=1",
{
keyword: `%${keyword}%`,
},
)
);
}),
)
.orderBy("ProfileEdit.createdAt", "ASC")
@ -162,7 +148,7 @@ export class ProfileEditEmployeeController extends Controller {
}
@Get("{Id}")
public async detailProfileByIdEdit(@Path() Id: string) {
public async detailProfileEmployeeByIdEdit(@Path() Id: string) {
const getProfileEdit = await this.profileEditRepository.findOne({
where: { id: Id },
});
@ -233,7 +219,7 @@ export class ProfileEditEmployeeController extends Controller {
) {
// const record = await this.profileEditRepository.findOneBy({ id: editId });
const record = await this.profileEditRepository.findOne({
where: { id: editId }
where: { id: editId },
});
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");