cronjob admin token

This commit is contained in:
AdisakKanthawilang 2024-12-18 10:59:57 +07:00
parent 4077900079
commit 42c9c8a8f5
4 changed files with 36 additions and 28 deletions

18
package-lock.json generated
View file

@ -13,7 +13,7 @@
"@nestjs/platform-express": "^10.3.9",
"@tsoa/runtime": "^6.0.0",
"amqplib": "^0.10.4",
"axios": "^1.7.2",
"axios": "^1.7.9",
"cors": "^2.8.5",
"csv-parser": "^3.0.0",
"dotenv": "^16.3.1",
@ -25,6 +25,7 @@
"node-cron": "^3.0.3",
"node-xlsx": "^0.24.0",
"promise.any": "^2.0.6",
"querystring": "^0.2.1",
"redis": "^3.1.2",
"reflect-metadata": "^0.2.1",
"swagger-ui-express": "^5.0.0",
@ -819,9 +820,9 @@
}
},
"node_modules/axios": {
"version": "1.7.2",
"resolved": "https://registry.npmjs.org/axios/-/axios-1.7.2.tgz",
"integrity": "sha512-2A8QhOMrbomlDuiLeK9XibIBzuHeRcqqNOHp0Cyp5EoJ1IFDh+XZH3A6BkXtv0K4gFGCI0Y4BM7B1wOEi0Rmgw==",
"version": "1.7.9",
"resolved": "https://registry.npmjs.org/axios/-/axios-1.7.9.tgz",
"integrity": "sha512-LhLcE7Hbiryz8oMDdDptSrWowmB4Bl6RCt6sIJKpRB4XtVf0iEgewX3au/pJqm+Py1kCASkb/FFKjxQaLtxJvw==",
"dependencies": {
"follow-redirects": "^1.15.6",
"form-data": "^4.0.0",
@ -3684,6 +3685,15 @@
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/querystring": {
"version": "0.2.1",
"resolved": "https://registry.npmjs.org/querystring/-/querystring-0.2.1.tgz",
"integrity": "sha512-wkvS7mL/JMugcup3/rMitHmd9ecIGd2lhFhK9N3UUQ450h66d1r3Y9nvXzQAW1Lq+wyx61k/1pfKS5KuKiyEbg==",
"deprecated": "The querystring API is considered Legacy. new code should use the URLSearchParams API instead.",
"engines": {
"node": ">=0.4.x"
}
},
"node_modules/querystringify": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/querystringify/-/querystringify-2.2.0.tgz",

View file

@ -32,7 +32,7 @@
"@nestjs/platform-express": "^10.3.9",
"@tsoa/runtime": "^6.0.0",
"amqplib": "^0.10.4",
"axios": "^1.7.2",
"axios": "^1.7.9",
"cors": "^2.8.5",
"csv-parser": "^3.0.0",
"dotenv": "^16.3.1",
@ -44,6 +44,7 @@
"node-cron": "^3.0.3",
"node-xlsx": "^0.24.0",
"promise.any": "^2.0.6",
"querystring": "^0.2.1",
"redis": "^3.1.2",
"reflect-metadata": "^0.2.1",
"swagger-ui-express": "^5.0.0",

View file

@ -61,6 +61,8 @@ import { ProfileCertificateHistory } from "../entities/ProfileCertificateHistory
import permission from "../interfaces/permission";
import { CommandSign } from "../entities/CommandSign";
import { RoleKeycloak } from "../entities/RoleKeycloak";
import axios from "axios";
import querystring from "querystring";
@Route("api/v1/org/command")
@Tags("Command")
@ -1244,29 +1246,23 @@ export class CommandController extends Controller {
},
});
const data = {
let body = {
client_id: "gettoken",
client_secret: process.env.AUTH_ACCOUNT_SECRET,
grant_type: "password",
requested_token_type: "urn:ietf:params:oauth:token-type:refresh_token",
username: process.env.USERNAME_,
password: process.env.PASSWORD_,
grant_type: "client_credentials",
};
let _data: any = null;
await Promise.all([
await new CallAPI()
.PostDataKeycloak(`/realms/${process.env.KC_REALM}/protocol/openid-connect/token`, data)
.then(async (x) => {
_data = x;
})
.catch(async (x) => {
throw new HttpError(HttpStatus.UNAUTHORIZED, "ชื่อผู้ใช้งานหรือรหัสผ่านไม่ถูกต้อง");
}),
]);
if (_data == null) {
return new HttpError(HttpStatus.UNAUTHORIZED, "ชื่อผู้ใช้งานหรือรหัสผ่านไม่ถูกต้อง");
}
const postData = querystring.stringify(body);
// get admin token
const response = await axios.post(`${process.env.KC_URL}/realms/${process.env.KC_REALM}/protocol/openid-connect/token`, postData, {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
api_key: process.env.API_KEY,
},
});
const adminToken = response.data.access_token;
command.forEach(async (x) => {
const path = commandTypePath(x.commandType.code);
if (path == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบประเภทคำสั่งนี้ในระบบ");
@ -1280,8 +1276,8 @@ export class CommandController extends Controller {
// lastUpdateFullName: _data.user.name,
lastUpdatedAt: new Date(),
},
user: _data.user,
token: _data.access_token,
// user: _data.user,
token: adminToken,
};
sendToQueue(msg);
});

View file

@ -62,7 +62,7 @@ function createConsumer( //----> consumer
console.log("[AMQ] Process Consumer success");
return channel.ack(msg);
}
console.log("[AMQ] Process Consumer fail");
console.log("[AMQ] Process Consumer failed");
return await new Promise((resolve) => setTimeout(() => resolve(channel.nack(msg)), 3000));
},
{ noAck: false },
@ -82,9 +82,10 @@ async function handler(msg: amqp.ConsumeMessage): Promise<boolean> {
const path = commandTypePath(command.commandType.code);
if (path == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบประเภทคำสั่งนี้ในระบบ");
return await new CallAPI()
.PostData(
{
headers: { authorization: token }, //time bomb
headers: { authorization: token },
},
path + "/excecute",
{