Skip to content

Commit

Permalink
backend routes, models and and migration files for integration_mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
FancMa01 committed Mar 1, 2024
1 parent db9bbf2 commit e6d1f1e
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 61 deletions.
24 changes: 24 additions & 0 deletions server/migrations/20240301202952-remove_integration_columns.js
Original file line number Diff line number Diff line change
@@ -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,
});
},
};
Original file line number Diff line number Diff line change
@@ -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");
},
};
9 changes: 6 additions & 3 deletions server/models/application.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
"use strict";
module.exports = (sequelize, DataTypes) => {
const application = sequelize.define(
'application',
"application",
{
id: {
primaryKey: true,
Expand Down Expand Up @@ -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;
};
Empty file.
153 changes: 95 additions & 58 deletions server/routes/integrations/read.js
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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
Expand All @@ -39,67 +46,97 @@ 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);
}
}
);

//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);
Expand Down

0 comments on commit e6d1f1e

Please sign in to comment.