ATC_SIMPLE/BACKEND/app/controllers/healcheck_controller.ts

108 lines
3.3 KiB
TypeScript

import type { HttpContext } from '@adonisjs/core/http'
import axios from 'axios'
const linkWiki =
process.env.LINK_WIKI || 'https://logs.danielvu.com/api/wiki/page/insert?title=Dev_test'
const remoteUrl = process.env.ERP_URL || 'https://stage.nswteam.net'
export default class HealCheckController {
// GET /health-check
async check({}: HttpContext) {
try {
const header = {
Authorization: 'Bearer ' + process.env.ERP_TOKEN,
}
const resWiki = await axios.post(linkWiki, {
data: 'Health checking',
healthChecking: true,
})
let dataCheckNote = {
name: 'update-note-sn',
status: true,
message: 'Checking api update note SN success',
}
const responseDataSN = await axios.post(
remoteUrl + '/api/transferGetData',
{
urlAPI: '/api/stock-model-serial/get-list-for-test-log',
filter: {
where: {
_q: 'FOC1425Z3RN',
},
},
orgId: ['5fadc798f070e4b64b53ac9c', '5fadc7b0f070e4b64b53ac9d'],
},
{
headers: header,
}
)
if (!responseDataSN?.data?.data || responseDataSN?.data?.data?.length === 0) {
dataCheckNote = {
name: 'update-note-sn',
status: false,
message: 'Checking api update note SN fail',
}
}
const dataSN = responseDataSN?.data?.data[0] || {}
// console.log(payload)
const resSN = await axios.post(
remoteUrl + '/api/transferPostData',
{
urlAPI: '/api/stock-model-serial/data-save-for-test-log',
data: {
id: dataSN?.id,
serialNumberA: dataSN?.serialNumberA,
productModelId: dataSN?.productModelId,
orgId: dataSN?.orgId,
condition: dataSN?.condition,
testNotes: dataSN?.testNotes,
healthCheck: true,
},
},
{
headers: header,
}
)
return {
code: 200,
data: [
{
name: 'wiki',
status: resWiki.status < 400 ? true : false,
message: resWiki.status < 400 ? 'Checking api wiki success' : 'Checking api wiki fail',
},
{
...dataCheckNote,
status: resSN?.data?.Status === 'ERROR' ? false : true,
message:
resSN?.data?.Status === 'ERROR'
? `Checking api update note SN false: '${resSN.data?.Msg}'`
: 'Checking api update note SN success',
},
],
}
} catch (error: any) {
return {
code: 200,
data: [
{
name: 'wiki',
status: error?.response?.config?.url?.includes(linkWiki) ? false : true,
message: error?.response?.config?.url?.includes(linkWiki)
? 'Checking api wiki fail'
: 'Checking api wiki success',
},
{
name: 'update-note-sn',
status: error?.response?.config?.url?.includes(remoteUrl) ? false : true,
message: error?.response?.config?.url?.includes(remoteUrl)
? 'Checking api update note SN fail'
: 'Checking api update note SN success',
},
],
}
}
}
}