174 lines
No EOL
6.2 KiB
TypeScript
174 lines
No EOL
6.2 KiB
TypeScript
import express from "express";
|
|
import zlib from "zlib";
|
|
import { storageController } from "./logics/storage";
|
|
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 path from "path";
|
|
import fs from "fs";
|
|
import { fuzzyMatchController } from "./logics/fuzzyMatch";
|
|
|
|
|
|
const router = express.Router();
|
|
|
|
// Process Request From Miitel Webhook
|
|
router.post("/miitel", async (req, res) => {
|
|
try {
|
|
const body = req.body;
|
|
|
|
if('challenge' in body) return res.status(200).contentType('text/plain').send(body.challenge);
|
|
|
|
|
|
const parsedBody = MiiTelWebhookSchema.safeParse(body);
|
|
if (!parsedBody.success) throw createCustomError("ZOD_FAILED");
|
|
|
|
const videoInfo = parsedBody.data.video;
|
|
const gzipped = zlib.gzipSync(JSON.stringify(body));
|
|
await storageController.saveToGCS(CLOUD_STORAGE_LOG_FOLDER_NAME, `${videoInfo.id}.json.gz`, gzipped, 'application/gzip');
|
|
|
|
await processRequest(videoInfo);
|
|
// if(!result) throw
|
|
|
|
return res.status(200).send("ok");
|
|
} catch(err) {
|
|
return responseError(err, res);
|
|
}
|
|
});
|
|
|
|
// Refresh Master Data Everyday
|
|
router.post("/dailyBatch", async (req, res) => {
|
|
try {
|
|
console.log("Starting daily batch process...");
|
|
// export companies to GCS
|
|
const companies = await hubspotController.getCompanies();
|
|
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_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.");
|
|
}
|
|
});
|
|
|
|
// Check Log By Meeting ID
|
|
router.post("/getLog", async (req, res) => {
|
|
console.log(req.body);
|
|
const meetingId = req.body.meetingId;
|
|
const exist = await storageController.existsInGCS(CLOUD_STORAGE_LOG_FOLDER_NAME, `${meetingId}.json.gz`);
|
|
console.log("Log exists:", exist);
|
|
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);
|
|
});
|
|
|
|
|
|
// Check Log By Meeting ID
|
|
router.post("/reExecute", async (req, res) => {
|
|
try {
|
|
console.log(req.body);
|
|
const meetingId = req.body.meetingId;
|
|
const newTitle = req.body.newTitle;
|
|
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);
|
|
|
|
res.send(log);
|
|
} catch(error) {
|
|
console.log(error);
|
|
res.status(400).send("Failed");
|
|
}
|
|
});
|
|
|
|
|
|
// 過去のログを全てGoogle Driveへアップロード
|
|
// 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);
|
|
// const fileId = req.body.fileId;
|
|
// const googleAuth = await googleDriveController.getAuth();
|
|
// const driveClilent = googleDriveController.getDriveClient(googleAuth);
|
|
// await googleDriveController.deleteFile(driveClilent, fileId);
|
|
// res.send('ok');
|
|
// });
|
|
|
|
router.post("/test", async (req, res) => {
|
|
try {
|
|
await testProcess();
|
|
res.send("ok");
|
|
} catch (error) {
|
|
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; |