diff --git a/server/migrations/20240301202952-remove_integration_columns.js b/server/migrations/20240301202952-remove_integration_columns.js new file mode 100644 index 000000000..166b53f8c --- /dev/null +++ b/server/migrations/20240301202952-remove_integration_columns.js @@ -0,0 +1,24 @@ +"use strict"; + +module.exports = { + up: async (queryInterface, Sequelize) => { + await queryInterface.removeColumn("integrations", "config"); + await queryInterface.removeColumn("integrations", "metadata"); + await queryInterface.removeColumn("integrations", "active"); + }, + + down: async (queryInterface, Sequelize) => { + await queryInterface.addColumn("integrations", "config", { + type: Sequelize.STRING, + allowNull: true, + }); + await queryInterface.addColumn("integrations", "metadata", { + type: Sequelize.STRING, + allowNull: true, + }); + await queryInterface.addColumn("integrations", "active", { + type: Sequelize.BOOLEAN, + defaultValue: true, + }); + }, +}; diff --git a/server/migrations/20240301203455-create_integration_application_mapping.js b/server/migrations/20240301203455-create_integration_application_mapping.js new file mode 100644 index 000000000..633e53d57 --- /dev/null +++ b/server/migrations/20240301203455-create_integration_application_mapping.js @@ -0,0 +1,49 @@ +"use strict"; +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.createTable("integration_mapping", { + id: { + allowNull: false, + primaryKey: true, + type: Sequelize.UUID, + defaultValue: Sequelize.UUIDV4, + }, + integration_id: { + type: Sequelize.UUID, + references: { + model: "integrations", + key: "id", + }, + onUpdate: "CASCADE", + onDelete: "CASCADE", + }, + application_id: { + type: Sequelize.UUID, + references: { + model: "application", + key: "id", + }, + onUpdate: "CASCADE", + onDelete: "CASCADE", + }, + metaData: { + type: Sequelize.JSON, + allowNull: true, + }, + createdAt: { + type: Sequelize.DATE, + allowNull: false, + defaultValue: Sequelize.NOW, + }, + updatedAt: { + type: Sequelize.DATE, + allowNull: false, + defaultValue: Sequelize.NOW, + }, + }); + }, + + async down(queryInterface, Sequelize) { + await queryInterface.dropTable("integration_mapping"); + }, +}; diff --git a/server/models/application.js b/server/models/application.js index 4b60b23c9..4b1c5cdc8 100644 --- a/server/models/application.js +++ b/server/models/application.js @@ -1,7 +1,7 @@ -'use strict'; +"use strict"; module.exports = (sequelize, DataTypes) => { const application = sequelize.define( - 'application', + "application", { id: { primaryKey: true, @@ -101,8 +101,11 @@ module.exports = (sequelize, DataTypes) => { foreignKey: "application_id", onDelete: "CASCADE", }); + application.hasMany(models.integration_mapping, { + foreignKey: "application_id", + onDelete: "CASCADE", + }); }; - return application; }; diff --git a/server/models/integration_mapping.js b/server/models/integration_mapping.js new file mode 100644 index 000000000..e69de29bb diff --git a/server/routes/integrations/read.js b/server/routes/integrations/read.js index 78a9044b0..46fbafab3 100644 --- a/server/routes/integrations/read.js +++ b/server/routes/integrations/read.js @@ -1,6 +1,6 @@ const models = require("../../models"); const integrations = models.integrations; -let application = models.application; +const integration_mapping = models.integration_mapping; const express = require("express"); const router = express.Router(); const path = require("path"); @@ -9,28 +9,35 @@ const rootENV = path.join(process.cwd(), "..", ".env"); const serverENV = path.join(process.cwd(), ".env"); const ENVPath = fs.existsSync(rootENV) ? rootENV : serverENV; const { param, validationResult } = require("express-validator"); -const validatorUtil = require("../../utils/validator"); require("dotenv").config({ path: ENVPath }); +//return all integrations +router.get("/getAll", async (res) => { + try { + const result = await integrations.findAll(); + res.status(200).send(result); + } catch (err) { + // ... error checks + console.log(err); + } +}); + +//get all integration_mappings with application_id router.get( - "/get/:application_id", - [param("application_id").isUUID(4).withMessage("Invalid application id")], + "/getAll/:application_id", + [param("application_id").isUUID()], async (req, res) => { - const errors = validationResult(req).formatWith( - validatorUtil.errorFormatter - ); + const errors = validationResult(req); + if (!errors.isEmpty()) { + return res.status(400).json({ errors: errors.array() }); + } try { - if (!errors.isEmpty()) - return res.status(422).json({ success: false, errors: errors.array() }); const { application_id } = req.params; - if (!application_id) throw Error("Invalid app ID"); - - const result = await integrations.findAll({ + const result = await integration_mapping.findAll({ where: { application_id, }, }); - res.status(200).send(result); } catch (err) { // ... error checks @@ -39,30 +46,24 @@ router.get( } ); -//activate or deactive integration -router.put( - "/toggle/:application_id/:name", - [param("application_id").isUUID(4).withMessage("Invalid application id")], +//return one integration_mapping entry with application_id and integration_id paramter +router.get( + "/getOne/:application_id/:integration_id", + [param("application_id").isUUID(), param("integration_id").isUUID()], async (req, res) => { - const errors = validationResult(req).formatWith( - validatorUtil.errorFormatter - ); + const errors = validationResult(req); + if (!errors.isEmpty()) { + return res.status(400).json({ errors: errors.array() }); + } try { - if (!errors.isEmpty()) - return res.status(422).json({ success: false, errors: errors.array() }); - const { application_id, name } = req.params; - - const integration = await integrations.findOne({ - where: { application_id, name }, - raw: true, + const { application_id, integration_id } = req.params; + const result = await integration_mapping.findOne({ + where: { + application_id, + integration_id, + }, }); - - const { active, id } = integration; - - // flipping Active - await integrations.update({ active: !active }, { where: { id } }); - - res.status(200).send("integration toggled succesfully"); + res.status(200).send(result); } catch (err) { // ... error checks console.log(err); @@ -70,36 +71,72 @@ router.put( } ); -//update integration notifications -router.put( - "/update/:application_id/:name", - [param("application_id").isUUID(4).withMessage("Invalid application id")], +//create entry into integration_mapping table with application_id paramter +router.post( + "/create", + [body("application_id").isUUID(), body("integration_id").isUUID()], async (req, res) => { - const errors = validationResult(req).formatWith( - validatorUtil.errorFormatter - ); + const errors = validationResult(req); + if (!errors.isEmpty()) { + return res.status(400).json({ errors: errors.array() }); + } try { - if (!errors.isEmpty()) - return res.status(422).json({ success: false, errors: errors.array() }); - const { application_id, name } = req.params; - - if (!application_id) throw Error("Invalid app ID"); - const oldintegration = await integrations.findOne({ - where: { application_id, name }, - raw: true, + const { application_id, integration_id, metaData } = req.body; + const result = await integration_mapping.create({ + application_id, + integration_id, + metaData, }); + res.status(200).send(result); + } catch (err) { + // ... error checks + console.log(err); + } + } +); - const { id } = oldintegration; - - console.log(req.body); +//delete integration_mapping entry with application_id and integration_id paramter +router.delete( + "/delete", + [body("application_id").isUUID(), body("integration_id").isUUID()], + async (req, res) => { + const errors = validationResult(req); + if (!errors.isEmpty()) { + return res.status(400).json({ errors: errors.array() }); + } + try { + const { application_id, integration_id } = req.body; + const result = await integration_mapping.destroy({ + where: { + application_id, + integration_id, + }, + }); + res.status(200).send(result); + } catch (err) { + // ... error checks + console.log(err); + } + } +); - // adjusting - await integrations.update( - { metaData: req.body.notifications, config: req.body.active }, - { where: { id } } +//update integration_mapping entry with application_id and integration_id paramter +router.put( + "/update", + [body("application_id").isUUID(), body("integration_id").isUUID()], + async (req, res) => { + try { + const { application_id, integration_id, metaData } = req.body; + const result = await integration_mapping.update( + { metaData }, + { + where: { + application_id, + integration_id, + }, + } ); - - res.status(200).send("integration updated succesfully"); + res.status(200).send(result); } catch (err) { // ... error checks console.log(err);