139 lines
4.2 KiB
TypeScript
139 lines
4.2 KiB
TypeScript
import type { HttpContext } from '@adonisjs/core/http'
|
|
import axios from 'axios'
|
|
import fs from 'fs'
|
|
import path from 'path'
|
|
|
|
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 {
|
|
private saveErrorLog(error: any) {
|
|
try {
|
|
const logsDir = path.join(process.cwd(), 'storage', 'health-check')
|
|
|
|
// Tạo thư mục nếu không tồn tại
|
|
if (!fs.existsSync(logsDir)) {
|
|
fs.mkdirSync(logsDir, { recursive: true })
|
|
}
|
|
|
|
const timestamp = new Date().toISOString()
|
|
const fileName = `healthcheck-error-${new Date().toISOString().split('T')[0]}.log`
|
|
const filePath = path.join(logsDir, fileName)
|
|
|
|
const errorContent = `
|
|
=== ERROR LOG ===
|
|
Time: ${timestamp}
|
|
Message: ${error?.message || 'Unknown error'}
|
|
Status: ${error?.response?.status || 'N/A'}
|
|
URL: ${error?.response?.config?.url || 'N/A'}
|
|
Error Details: ${JSON.stringify(error?.response?.data || error, null, 2)}
|
|
---
|
|
`
|
|
|
|
fs.appendFileSync(filePath, errorContent)
|
|
console.log(`Error logged to: ${filePath}`)
|
|
} catch (logError) {
|
|
console.error('Failed to save error log:', logError)
|
|
}
|
|
}
|
|
// 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.get(
|
|
remoteUrl + '/api/stock-model-serial/get-list-for-test-log',
|
|
{
|
|
params: {
|
|
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/stock-model-serial/data-save-for-test-log',
|
|
{
|
|
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) {
|
|
// Lưu log lỗi
|
|
this.saveErrorLog(error)
|
|
|
|
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',
|
|
},
|
|
],
|
|
}
|
|
}
|
|
}
|
|
}
|