This commit is contained in:
kosukesuenaga 2025-12-24 11:36:34 +09:00
parent 1259ba76c9
commit 6454e1b46b
19 changed files with 667 additions and 611 deletions

View file

@ -1,12 +1,15 @@
import express from "express";
import zlib from "zlib";
import { storageController } from "./logics/storage";
import { MiiTelWebhookSchema, processRequest } from "./logics/process";
import { logUploadProcess, MiiTelWebhookSchema, processRequest, testProcess } from "./logics/process";
import { hubspotController } from "./logics/hubspot";
import { createCustomError, responseError } from "./logics/error";
import { CLOUD_STORAGE_LOG_FOLDER_NAME, CLOUD_STORAGE_MASTER_FOLDER_NAME, COMPANIES_FILE_NAME, OWNERS_FILE_NAME } from "../serverConfig";
import { Delay } from "cerceis-lib";
import { googleDriveController } from "./logics/googleDrive";
import path from "path";
import fs from "fs";
import { fuzzyMatchController } from "./logics/fuzzyMatch";
const router = express.Router();
@ -30,8 +33,7 @@ router.post("/miitel", async (req, res) => {
return res.status(200).send("ok");
} catch(err) {
responseError(err, res);
return;
return responseError(err, res);
}
});
@ -41,18 +43,19 @@ router.post("/dailyBatch", async (req, res) => {
console.log("Starting daily batch process...");
// export companies to GCS
const companies = await hubspotController.getCompanies();
if(!companies) throw createCustomError("GET_OWNERS_FAILED");
if(!companies) throw createCustomError("GET_COMPANIES_FAILED");
await storageController.saveToGCS(CLOUD_STORAGE_MASTER_FOLDER_NAME, COMPANIES_FILE_NAME, JSON.stringify(companies), 'application/json');
// export owners to GCS
const owners = await hubspotController.getOwners();
if(!owners) throw createCustomError("GET_COMPANIES_FAILED");
if(!owners) throw createCustomError("GET_OWNERS_FAILED");
await storageController.saveToGCS(CLOUD_STORAGE_MASTER_FOLDER_NAME, OWNERS_FILE_NAME, JSON.stringify(owners), 'application/json');
res.status(200).send("Daily batch executed.");
} catch (error) {
console.error("Error in daily batch:", error);
return res.status(400).send("Error executing daily batch.");
}
});
@ -60,11 +63,13 @@ router.post("/dailyBatch", async (req, res) => {
router.post("/getLog", async (req, res) => {
console.log(req.body);
const meetingId = req.body.meetingId;
const exist = await storageController.existsInGCS("request_log", "test.json.gz");
const exist = await storageController.existsInGCS(CLOUD_STORAGE_LOG_FOLDER_NAME, `${meetingId}.json.gz`);
console.log("Log exists:", exist);
const log = await storageController.loadFromGCS("request_log", meetingId + ".json.gz");
console.log(log)
res.send(log);
const log = await storageController.loadFromGCS(CLOUD_STORAGE_LOG_FOLDER_NAME, meetingId + ".json.gz");
if(!log) throw Error();
const params = MiiTelWebhookSchema.parse(JSON.parse(log));
// console.log(params)
res.send(params);
});
@ -77,11 +82,12 @@ router.post("/reExecute", async (req, res) => {
const log = await storageController.loadFromGCS(CLOUD_STORAGE_LOG_FOLDER_NAME, `${meetingId}.json.gz`);
if(!log) throw Error();
const params = MiiTelWebhookSchema.safeParse(JSON.parse(log));
console.log(params);
if(!params.success) throw createCustomError("ZOD_FAILED");
params.data.video.title = newTitle;
// console.log(params.data.video)
await processRequest(params.data.video);
// await processRequest(params.data.video);
res.send(log);
} catch(error) {
@ -92,28 +98,39 @@ router.post("/reExecute", async (req, res) => {
// 過去のログを全てGoogle Driveへアップロード
router.post("/logUpload", async (req, res) => {
try {
const list = await storageController.getFileList();
if(!list) throw createCustomError("GET_FILES_FAILED");
for(const l of list){
console.log(l);
const fileName = l.split('/')[1]
const log = await storageController.loadFromGCS('request_log', fileName);
if(!log) throw createCustomError("GET_FILES_FAILED");
// console.log(log);
const parsedLog = MiiTelWebhookSchema.safeParse(JSON.parse(log));
if(!parsedLog.success) throw createCustomError("ZOD_FAILED");
console.log(parsedLog.data.video.title);
await Delay(500);
}
res.send('ok');
} catch(error) {
console.log(error);
res.status(400).send("Failed");
}
});
// router.post("/logUpload", async (req, res) => {
// try {
// const list = await storageController.getFileList();
// if(!list) throw createCustomError("GET_FILES_FAILED");
// console.log("Total files to process:", list.length);
// const failedFiles: string[] = [];
// let count = 0;
// const tmplist = list.slice(1600,1800);
// for(const l of tmplist){
// console.log(l);
// count++;
// console.log(`Processing file ${count} of ${tmplist.length}`);
// const fileName = l.split('/')[1]
// const log = await storageController.loadFromGCS('request_log', fileName);
// if(!log) {
// failedFiles.push(fileName);
// continue;
// };
// const parsedLog = MiiTelWebhookSchema.safeParse(JSON.parse(log));
// if(!parsedLog.success) throw createCustomError("ZOD_FAILED");
// console.log(parsedLog.data.video.title);
// const result = await logUploadProcess(parsedLog.data.video);
// if(!result) failedFiles.push(fileName);
// await Delay(500);
// }
// const outputPath = path.join(__dirname, "../log/", 'failedFiles.json');
// fs.writeFileSync(outputPath, JSON.stringify(failedFiles, null, 2));
// res.send('ok');
// } catch(error) {
// console.log(error);
// res.status(400).send("Failed");
// }
// });
// router.post("/deleteFile", async (req, res) => {
// console.log(req.body);
@ -126,20 +143,32 @@ router.post("/logUpload", async (req, res) => {
router.post("/test", async (req, res) => {
try {
// const googleAuth = await googleDriveController.getAuth();
// const driveClilent = googleDriveController.getDriveClient(googleAuth);
// const sheetsClient = googleDriveController.getSheetsClient(googleAuth);
// const folderId = await googleDriveController.searchFileIdByFileName(driveClilent, MINUTES_CREATION_HISTORY_FOLDER_ID, '2025');
// if(!folderId) throw new Error()
// console.log(fileId);
// const sheetId = await googleDriveController.getLogSheetId(driveClilent, sheetsClient, folderId, 'test1');
// console.log('sheet id : ', sheetId);
await testProcess();
res.send("ok");
} catch (error) {
console.error("Error in /test endpoint:", error);
res.status(500).send("Error in /test endpoint");
console.error(error);
res.status(400).send("Error in /test endpoint");
}
});
router.post("/alertTest", async (_req, res) => {
res.status(500).send("Error");
});
// router.post("/debug", async (req, res) => {
// try {
// const a = await fuzzyMatchController.searchMatchedCompany("Aコープ九");
// console.log(a);
// res.send("ok");
// } catch (error) {
// console.error(error);
// res.status(400).send("Error in /test endpoint");
// }
// });
export default router;