diff --git a/.env b/.env index 2283672..bb0a273 100644 --- a/.env +++ b/.env @@ -1,2 +1,3 @@ PORT=4000 -MONGO_URI=mongodb://root:p62Z!ZatgY25@194.26.138.94:27017 \ No newline at end of file +MONGO_URI=mongodb://root:p62Z!ZatgY25@194.26.138.94:27017 +CRM_API_URL=https://crm.stream.graff.tech/api \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index b0bd900..a0a98fd 100644 --- a/dist/index.js +++ b/dist/index.js @@ -189,6 +189,52 @@ async function checkActiveSessions() { } } } +async function checkScheduledSessions() { + try { + const scheduledSessions = await got_cjs_1.default + .get(`${process.env.CRM_API_URL}/scheduled_sessions`) + .json(); + if (!scheduledSessions.length) + return; + for (const session of scheduledSessions) { + if ((0, date_fns_1.isAfter)(new Date(), (0, date_fns_1.parseISO)(session.startAt)) && + !session.activeSessionId) { + console.log("session.buildId", session.buildId); + const { build } = await got_cjs_1.default + .get(`${process.env.CRM_API_URL}/builds/${session.buildId}`) + .json(); + console.log("build", build); + const result = await got_cjs_1.default + .get(`https://coord.graff.tech/start?build=${build}&location=a1`) + .json(); + if (!result.stream) { + console.log("Not result"); + return; + } + const result2 = await got_cjs_1.default + .put(`${process.env.CRM_API_URL}/scheduled_sessions/${session.id}`, { + json: { + activeSessionId: result.stream, + }, + }) + .json(); + console.log("CRON Start session: ", result2); + } + else if ((0, date_fns_1.isAfter)(new Date(), (0, date_fns_1.parseISO)(session.endAt))) { + const result = await got_cjs_1.default + .post(`https://coord.graff.tech/active_sessions/${session.activeSessionId}/end`) + .json(); + console.log("CRON End active session: ", result); + } + } + } + catch (error) { + console.log("Error: ", error.message); + } +} node_cron_1.default.schedule("* * * * *", () => { checkActiveSessions(); }); +node_cron_1.default.schedule("* * * * *", () => { + checkScheduledSessions(); +}); diff --git a/src/index.ts b/src/index.ts index f71dd03..b4395cb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,7 +8,7 @@ import got from "got-cjs"; import Build from "./models/Build"; import { AccessToken } from "livekit-server-sdk"; import cron from "node-cron"; -import { differenceInMinutes } from "date-fns"; +import { differenceInMinutes, isAfter, parseISO } from "date-fns"; connectDB(); @@ -217,6 +217,65 @@ async function checkActiveSessions() { } } +async function checkScheduledSessions() { + try { + const scheduledSessions: any = await got + .get(`${process.env.CRM_API_URL}/scheduled_sessions`) + .json(); + + if (!scheduledSessions.length) return; + + for (const session of scheduledSessions) { + if ( + isAfter(new Date(), parseISO(session.startAt)) && + !session.activeSessionId + ) { + console.log("session.buildId", session.buildId); + const { build }: any = await got + .get(`${process.env.CRM_API_URL}/builds/${session.buildId}`) + .json(); + + console.log("build", build); + + const result: any = await got + .get(`https://coord.graff.tech/start?build=${build}&location=a1`) + .json(); + + if (!result.stream) { + console.log("Not result"); + return; + } + + const result2 = await got + .put(`${process.env.CRM_API_URL}/scheduled_sessions/${session.id}`, { + json: { + startAt: session.startAt, + endAt: session.endAt, + activeSessionId: result.stream, + }, + }) + .json(); + + console.log("CRON Start session: ", result2); + } else if (isAfter(new Date(), parseISO(session.endAt))) { + const result = await got + .post( + `https://coord.graff.tech/active_sessions/${session.activeSessionId}/end` + ) + .json(); + + console.log("CRON End active session: ", result); + } + } + } catch (error) { + console.log("Error: ", (error as Error).message); + } +} + cron.schedule("* * * * *", () => { checkActiveSessions(); }); + +cron.schedule("* * * * *", () => { + checkScheduledSessions(); +});