checkpoint

This commit is contained in:
AdisakKanthawilang 2024-11-20 11:40:36 +07:00
parent ef18fb08c5
commit 25323e8b67
2 changed files with 171 additions and 129 deletions

View file

@ -1,5 +1,9 @@
import { Body, Controller, Get, Post, Query, Route, Security, Tags } from "tsoa";
import { Body, Controller, Get, Post, Query, Route, Security, Tags, Response } from "tsoa";
import { Client as ElasticsearchClient } from "@elastic/elasticsearch";
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
function getEnvVar(name: string) {
const value = process.env[name];
@ -174,7 +178,9 @@ export class ReportController extends Controller {
output: string;
sequence: string[];
}
const lists = queryData.hits.hits.map((x) => {
const fs = require('node:fs');
const content = queryData.hits.hits.map((x) => {
const source = x._source as DocumentSource;
return {
id: x._id,
@ -200,13 +206,41 @@ export class ReportController extends Controller {
}],
};
});
const contentString = JSON.stringify(content, null, 2);
let downloadsPath = path.join(os.homedir(), 'Downloads');
if (!fs.existsSync(downloadsPath)) {
downloadsPath = path.join(os.homedir(), 'ดาวน์โหลด');
if (!fs.existsSync(downloadsPath)) {
throw new Error("ไม่พบโฟลเดอร์ Downloads หรือ ดาวน์โหลด ในระบบนี้");
}
}
const filePath = path.join(downloadsPath, `log_${id}.txt`);
const data = lists.length === 1 ? lists[0] : lists;
return {
template: "logs_detail",
reportName: "docx-report",
data: data,
};
try {
const writeStream = fs.createWriteStream(filePath);
writeStream.on('error', (error:any) => {
console.error("Error writing file:", error);
throw error;
});
writeStream.write(contentString);
writeStream.end();
return {
status: 200,
message: "Created successfully",
result: "success"
};
} catch (error:any) {
console.error("An error occurred:", error);
return {
status: 500,
message: "Failed to create file",
result: "error",
error: error.message
};
}
}
}