python -> node.js

This commit is contained in:
kosukesuenaga 2025-12-05 14:12:11 +09:00
parent 092f2ec0f3
commit 395fba645d
62 changed files with 726 additions and 1702 deletions

View file

@ -1,7 +1,15 @@
import z from "zod";
import { aiController } from "./ai";
import { dateController } from "./date";
import { googleDriveController } from "./googleDrive";
import { googleDriveController, LogRowData, LogRowDataSchema } from "./googleDrive";
import { fileController } from "./file";
import path, { join } from "path";
import fs from "fs";
import { createCustomError, responseError } from "./error";
import { storageController } from "./storage";
import { CLOUD_STORAGE_MASTER_FOLDER_NAME, DATE_FORMAT, DATETIME_FORMAT, DOCUMENT_MIMETYPE, OWNERS_FILE_NAME, Y_FORMAT, YM_FORMAT } from "../../serverConfig";
import { hubspotController, OwnerSchema } from "./hubspot";
import { fuzzyMatchController } from "./fuzzyMatch";
const VideoInfoSchema = z.looseObject({
id: z.string(),
@ -26,32 +34,101 @@ export const MiiTelWebhookSchema = z.object({
// export type MiiTelWebhook = z.infer<typeof MiiTelWebhookSchema>;
export const processRequest = async(videoInfo: VideoInfo) => {
const videoId = videoInfo.id;
const title = videoInfo.title;
const startsAt = videoInfo.starts_at;
const endsAt = videoInfo.ends_at;
const accessPermission = videoInfo.access_permission;
const host_id = videoInfo.host.login_id;
const host_name = videoInfo.host.user_name;
const speechRecognition = videoInfo.speech_recognition.raw;
console.log(startsAt);
const jstStartsAt = dateController.convertToJst(startsAt);
const jstEndsAt = dateController.convertToJst(endsAt);
googleDriveController.checkConnection();
// console.log(dateController.getFormattedDate(startsAtJst, "yyyy/MM/dd hh:mm:ss"));
// console.log(endsAt);
// console.log("Processing video:", host_id, host_name, title);
if(accessPermission !== "EVERYONE" || !title.includes("様") || title.includes("社内")) return;
const GOOGLE_DRIVE_FOLDER_ID = process.env.GOOGLE_DRIVE_FOLDER_ID || '';
const MIITEL_REQUEST_LOG_FOLDER_ID = process.env.MIITEL_REQUEST_LOG_FOLDER_ID || '';
const MINUTES_CREATION_HISTORY_FOLDER_ID = process.env.MINUTES_CREATION_HISTORY_FOLDER_ID || '';
const MIITEL_URL = process.env.MIITEL_URL || '';
const HUBSPOT_COMPANY_URL = process.env.HUBSPOT_COMPANY_URL || '';
// Save Request Log to Google Drive
// const minute = await aiController.generateMinutes(speechRecognition);
// console.log(minute);
const FILE_PATH = join(__dirname, "../files/");
};
export const processRequest = async (videoInfo: VideoInfo) => {
try {
const videoId = videoInfo.id;
const title = videoInfo.title;
const startsAt = videoInfo.starts_at;
const endsAt = videoInfo.ends_at;
const accessPermission = videoInfo.access_permission;
const hostId = videoInfo.host.login_id;
const hostName = videoInfo.host.user_name;
const speechRecognition = videoInfo.speech_recognition.raw;
const jstStartsAt = dateController.convertToJst(startsAt);
const jstEndsAt = dateController.convertToJst(endsAt);
const fileName = fileController.createMinutesFileName(title, hostName, jstStartsAt);
const videoUrl = `${MIITEL_URL}app/video/${videoId}`;
if (accessPermission !== "EVERYONE" || !title.includes("様") || title.includes("社内")) return;
//
const googleAuth = await googleDriveController.getAuth();
const driveClient = googleDriveController.getDriveClient(googleAuth);
const docsClient = googleDriveController.getDocsClient(googleAuth);
const sheetsClient = googleDriveController.getSheetsClient(googleAuth);
// ===== Save Request Log to Google Drive =====
if (!fs.existsSync(FILE_PATH)) fs.mkdirSync(FILE_PATH, { recursive: true });
const outputPath = path.join(FILE_PATH, fileName + '.zip');
await fileController.createZip(videoInfo, outputPath, fileName);
const logFileId = await googleDriveController.uploadFile(driveClient, outputPath, MIITEL_REQUEST_LOG_FOLDER_ID, fileName + '.zip');
if(!logFileId) throw createCustomError("UPLOAD_LOG_FAILED");
// ===== Generate Minutes =====
const minutes = await aiController.generateMinutes(speechRecognition);
console.log(minutes);
if (!minutes) throw createCustomError("AI_GENERATION_FAILED");
let content = `会議履歴URL${videoUrl}\n`;
content += `担当者:${hostName}\n\n`;
content += minutes;
// ===== Upload To Google Drive =====
const documentId = await googleDriveController.createNewFile(driveClient, GOOGLE_DRIVE_FOLDER_ID, title, DOCUMENT_MIMETYPE);
if (!documentId) throw createCustomError("UPLOAD_MINUTES_FAILED");
const result = await googleDriveController.addContentToDocs(docsClient, documentId, minutes);
if(!result) throw createCustomError("UPLOAD_MINUTES_FAILED");
// ===== Create Meeting Log at Hubspot =====
const ownersJson = await storageController.loadJsonFromGCS(CLOUD_STORAGE_MASTER_FOLDER_NAME, OWNERS_FILE_NAME);
if(!ownersJson) throw createCustomError("GET_OWNERS_FAILED");
const parsedOwners = z.array(OwnerSchema).safeParse(JSON.parse(ownersJson));
if(!parsedOwners.success) throw createCustomError("ZOD_FAILED");
const ownerId = hubspotController.searchOwnerIdByEmail(hostId, parsedOwners.data);
const companyName = fileController.extractCompanyNameFromTitle(title);
const matchedCompany = await fuzzyMatchController.searchMatchedCompany(companyName);
if(matchedCompany) await hubspotController.createMeetingLog(matchedCompany.id, title, ownerId, minutes, startsAt, endsAt);
// ===== Apeend Log To SpreadSheet =====
const currentYear = dateController.getCurrentJstTime(Y_FORMAT);
const yearFileId = await googleDriveController.getFolderId(driveClient, MINUTES_CREATION_HISTORY_FOLDER_ID, currentYear);
if(!yearFileId) throw createCustomError("GET_FOLDER_ID_FAILED");
const currentYearMonth = dateController.getCurrentJstTime(YM_FORMAT);
const sheetId = await googleDriveController.getLogSheetId(driveClient, sheetsClient, yearFileId, currentYearMonth);
if(!sheetId) throw createCustomError("GET_SHEET_ID_FAILED");
const currentJstDateTimeStr = dateController.getCurrentJstTime(DATETIME_FORMAT);
const currentJstDateStr = dateController.getCurrentJstTime(DATE_FORMAT);
const rowData: LogRowData = LogRowDataSchema.parse({
timestamp: currentJstDateTimeStr,
meetingDate: currentJstDateStr,
title: title,
matchedCompanyName: matchedCompany?.name ?? '',
ownerName: hostName,
meetingUrl: videoUrl,
documentUrl: `https://docs.google.com/document/d/${documentId}/edit`,
hubspotUrl: matchedCompany ? `${HUBSPOT_COMPANY_URL}/${matchedCompany.id}` : '',
});
await googleDriveController.insertRowToSheet(sheetsClient, sheetId, Object.values(rowData));
return;
} catch (error) {
responseError(error);
return;
}
};