UNPKG

synt_backend

Version:

Synt light-weight node backend service

86 lines (79 loc) 2.9 kB
const express = require("express"); const router = express.Router(); import "dotenv/config"; import db from "../mysql/models"; var keypoint = require("./../helpers/keypoint"); const notifier = require("./../helpers/notifier"); // import { attachUploadedFiles } from "./../helpers/db-storage"; // import { uploadFileToBucket } from "./../helpers/upload"; // routes router.post("/keypoint", handleKeypoint); module.exports = router; function handleKeypoint(req, res) { const { Trigger, TriggerDescription, CaseId } = req.body; console.log(`[keypoint] Trigger: ${Trigger} - ${TriggerDescription}`); keypoint .createClient("demo") .then(async (client) => { console.log("Receiving update: ", CaseId); let Case = await client.getCase(CaseId); let Incident = await db.Incident.findOne({ where: { case_id: CaseId }, include: { model: db.VME, include: { model: db.User, as: "Users" } }, }); if (Trigger === "CaseStatusChanged") { // update incident Incident.case = Case; Incident.save(); // create notification for (const User of Incident?.VME?.Users) { notifier.notify(User, "keypoint_case_status_changed", { User, Incident, VME: Incident.VME, }); } return res.json({ success: true, Incident }); } else if (Trigger === "CaseInvoiceCreated") { let PaymentFiles = req.body; // save files for (const File of PaymentFiles) { console.log("Receiving File: ", File); console.log(File.FileId); let Case = Incident.case; //FIXME: paymentLink to seperate attr. (temp) Case.paymentLink = "https://demo-api.keypoint.be/api/v3/file/" + File.FileId + "/content"; Incident.case = Case; Incident.save(); // const Content = await client.getFileContent(File.FileId); // structure file: https://demo-api.keypoint.be/api/v3/file/[id]/content // console.log(Content); //TODO: save in the cloud by link //TODO: create purchase automatically (keypoint will add data in webhook) // attach to specific case // attachUploadedFiles("Incident", Incident, Content, "Image"); } // create notification let Users = Incident.VME.Users.filter( (U) => U.UserVME.type === "synt_authoriser" || U.UserVME.type === "synt_viewer" ); // notify for (const User of Users) { notifier.notify(User, "keypoint_case_invoice_created", { User, Incident, VME: Incident.VME, }); } return res.json({ success: true, Incident }); } }) .catch((error) => { res.json({ success: false, error }); }); }