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

51
package-lock.json generated
View file

@ -20,6 +20,7 @@
"node-cron": "^3.0.3",
"node-xlsx": "^0.24.0",
"promise.any": "^2.0.6",
"redis": "^3.1.2",
"reflect-metadata": "^0.2.1",
"swagger-ui-express": "^5.0.0",
"tsoa": "^6.0.1",
@ -3469,6 +3470,56 @@
"node": ">=8.10.0"
}
},
"node_modules/redis": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/redis/-/redis-3.1.2.tgz",
"integrity": "sha512-grn5KoZLr/qrRQVwoSkmzdbw6pwF+/rwODtrOr6vuBRiR/f3rjSTGupbF90Zpqm2oenix8Do6RV7pYEkGwlKkw==",
"dependencies": {
"denque": "^1.5.0",
"redis-commands": "^1.7.0",
"redis-errors": "^1.2.0",
"redis-parser": "^3.0.0"
},
"engines": {
"node": ">=10"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/node-redis"
}
},
"node_modules/redis-commands": {
"version": "1.7.0",
"resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.7.0.tgz",
"integrity": "sha512-nJWqw3bTFy21hX/CPKHth6sfhZbdiHP6bTawSgQBlKOVRG7EZkfHbbHwQJnrE4vsQf0CMNE+3gJ4Fmm16vdVlQ=="
},
"node_modules/redis-errors": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz",
"integrity": "sha512-1qny3OExCf0UvUV/5wpYKf2YwPcOqXzkwKKSmKHiE6ZMQs5heeE/c8eXK+PNllPvmjgAbfnsbpkGZWy8cBpn9w==",
"engines": {
"node": ">=4"
}
},
"node_modules/redis-parser": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz",
"integrity": "sha512-DJnGAeenTdpMEH6uAJRK/uiyEIH9WVsUmoLwzudwGJUwZPp80PDBWPHXSAGNPwNvIXAbe7MSUB1zQFugFml66A==",
"dependencies": {
"redis-errors": "^1.0.0"
},
"engines": {
"node": ">=4"
}
},
"node_modules/redis/node_modules/denque": {
"version": "1.5.1",
"resolved": "https://registry.npmjs.org/denque/-/denque-1.5.1.tgz",
"integrity": "sha512-XwE+iZ4D6ZUB7mfYRMb5wByE8L74HCn30FBN7sWnXksWc1LO1bPDl67pBR9o/kC4z/xSNAwkMYcGgqDV3BE3Hw==",
"engines": {
"node": ">=0.10"
}
},
"node_modules/reflect-metadata": {
"version": "0.2.1",
"resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.2.1.tgz",

View file

@ -38,6 +38,7 @@
"node-cron": "^3.0.3",
"node-xlsx": "^0.24.0",
"promise.any": "^2.0.6",
"redis": "^3.1.2",
"reflect-metadata": "^0.2.1",
"swagger-ui-express": "^5.0.0",
"tsoa": "^6.0.1",

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, "ไม่พบข้อมูล");