Skip to content

Commit

Permalink
Merge pull request #121 from Prometheus7-creator/multitenant
Browse files Browse the repository at this point in the history
added dynamic section api
  • Loading branch information
abhinovpankaj authored Jul 14, 2024
2 parents a26c5b0 + c795f99 commit c5f04bc
Show file tree
Hide file tree
Showing 7 changed files with 772 additions and 1 deletion.
1 change: 1 addition & 0 deletions database/mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var Connect = async function () {
module.exports.SubProjects = client.db(dbName).collection('SubProject');
module.exports.Locations = client.db(dbName).collection('Location');
module.exports.Sections = client.db(dbName).collection('VisualSection');
module.exports.DynamicSections = client.db(dbName).collection('DynamicVisualSection');
module.exports.Users = client.db(dbName).collection('Users');
module.exports.ProjectDocuments = client.db(dbName).collection('ProjectDocuments');

Expand Down
31 changes: 31 additions & 0 deletions model/dynamicSectionDAO.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const ObjectId = require('mongodb').ObjectId;
const mongo = require('../database/mongo');

module.exports = {
addSection: async (dynamicSection) => {
return await mongo.DynamicSections.insertOne(dynamicSection);
},
getAllSections: async () => {
return await mongo.DynamicSections.find({}).limit(50).sort({"_id": -1}).toArray();
},
getSectionById: async (id) => {
return await mongo.DynamicSections.findOne({ _id: new ObjectId(id) });
},
editSection: async (id, newData) => {
return await mongo.DynamicSections.updateOne({ _id: new ObjectId(id) }, { $set: newData },{upsert:false});
},
deleteSection: async (id) => {
return await mongo.DynamicSections.deleteOne({ _id: new ObjectId(id) });
},
getSectionByParentId: async (parentId) => {
return await mongo.DynamicSections.find({ parentid: new ObjectId(parentId) }).toArray();
},
addImageInSection : async (sectionId, url) => {
await mongo.DynamicSections.updateOne({ _id: new ObjectId(sectionId) }, { $push: { images: url } });
},
removeImageInSection : async (sectionId, url) => {
await mongo.DynamicSections.updateOne({ _id: new ObjectId(sectionId) }, { $pull: { images: url } });
}
}


2 changes: 2 additions & 0 deletions routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var projectRouter = require("./routes/project-endpoint");
var subprojectRouter = require("./routes/subproject-endpoint");
var locationRouter = require("./routes/location-endpoint");
var sectionRouter = require("./routes/section-endpoint");
var dynamicSectionRouter = require("./routes/dynamic-section-endpoint");
var invasivesectionRouter = require("./routes/invasivesection-endpoint");
var conclusiveSectionRouter = require("./routes/conclusivesection-endpoint");
var tenantRouter = require("./routes/tenants-endpoint");
Expand All @@ -26,6 +27,7 @@ module.exports = function(app) {
app.use("/api/subproject", subprojectRouter);
app.use("/api/location", locationRouter);
app.use("/api/section", sectionRouter);
app.use("/api/dynamicsection", dynamicSectionRouter);
app.use("/api/invasivesection", invasivesectionRouter);
app.use("/api/conclusivesection", conclusiveSectionRouter);
app.use("/api/tenants", authenticateToken,tenantRouter);
Expand Down
258 changes: 258 additions & 0 deletions routes/dynamic-section-endpoint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
"use strict";
var express = require('express');
var router = express.Router();
const ErrorResponse = require('../model/error');
var ObjectId = require('mongodb').ObjectId;
const newErrorResponse = require('../model/newError');
const DynamicSectionService = require("../service/dynamicSectionService");
const dynamicSectionDAO = require('../model/dynamicSectionDAO');

require("dotenv").config();

router.route('/add')
.post(async function (req,res){
try{
var errResponse;
// Get user input

const { name,additionalconsiderations,
additionalconsiderationshtml,furtherinvasivereviewrequired,images,createdby,parentid,parenttype,unitUnavailable, companyIdentifier } = req.body;

// Validate user input
if (!(name&&parentid)) {
errResponse = new ErrorResponse(400,"Name and parentid is required","");
res.status(400).json(errResponse);
return;
}
var creationtime= (new Date(Date.now())).toISOString();
var newSection = {
"additionalconsiderations":additionalconsiderations,
"additionalconsiderationshtml":additionalconsiderationshtml? additionalconsiderationshtml: "",
"createdat":creationtime,
"createdby":createdby,
"editedat":creationtime,
"lasteditedby":createdby,
"furtherinvasivereviewrequired":furtherinvasivereviewrequired.toLowerCase()==='true',
"name":name,
"parentid": new ObjectId(parentid),
"parenttype":parenttype,
"images":images,
"unitUnavailable": unitUnavailable,
"companyIdentifier": companyIdentifier
}
var result = await DynamicSectionService.addSection(newSection);
if (result.reason) {
return res.status(result.code).json(result);
}
if (result) {
//console.debug(result);
return res.status(201).json(result);
}
}
catch (exception) {
errResponse = new newErrorResponse(500, false, exception);
return res.status(500).json(errResponse);
}
});


router.route('/:id')
.get(async function(req,res){
try{
var errResponse;
const sectionId = req.params.id;
var result = await DynamicSectionService.getSectionById(sectionId);
if (result.reason) {
return res.status(result.code).json(result);
}
if (result) {
//console.debug(result);
return res.status(201).json(result);
}
}
catch (exception) {
errResponse = new newErrorResponse(500, false, exception);
return res.status(500).json(errResponse);
}
})

router.route('/:id')
.put(async function(req,res){
try{
var errResponse;
const sectionId = req.params.id;
const newData = req.body;
if(newData.parentid){
newData.parentid = new ObjectId(newData.parentid);
}

if(newData.furtherinvasivereviewrequired){
newData.furtherinvasivereviewrequired = newData.furtherinvasivereviewrequired.toLowerCase()==='true'
}

var result = await DynamicSectionService.editSetion(sectionId,newData);

if (result.reason) {
return res.status(result.code).json(result);
}
if (result) {
//console.debug(result);
return res.status(201).json(result);
}
}
catch (exception) {
errResponse = new newErrorResponse(500, false, exception);
return res.status(500).json(errResponse);
}
})
.delete(async function(req,res){
try{
var errResponse;
const sectionId = req.params.id;
var result = await DynamicSectionService.deleteSectionPermanently(sectionId);
if (result.reason) {
return res.status(result.code).json(result);
}
if (result) {
//console.debug(result);
return res.status(201).json(result);
}
}
catch (exception) {
errResponse = new newErrorResponse(500, false, exception);
return res.status(500).json(errResponse);
}
});

router.route('/:id/addimage')
.post(async function(req,res){
try {
var errResponse;
const sectionId = req.params.id;
const {url} = req.body;
var result = await DynamicSectionService.addImageInSection(sectionId,url);
if (result.reason) {
return res.status(result.code).json(result);
}
if (result) {
//console.debug(result);
return res.status(201).json(result);
}
}
catch (exception) {
errResponse = new newErrorResponse(500, false, exception);
return res.status(500).json(errResponse);
}
});

router.route('/:id/removeimage')
.post(async function(req,res){
try {
var errResponse;
const sectionId = req.params.id;
const {url} = req.body
var result = await DynamicSectionService.removeImageFromSection(sectionId,url);
if (result.reason) {
return res.status(result.code).json(result);
}
if (result) {
//console.debug(result);
return res.status(201).json(result);
}
}
catch (exception) {
errResponse = new newErrorResponse(500, false, exception);
return res.status(500).json(errResponse);
}
});

router.route('/getSectionById')
.post(async function(req, res) {
try {
const sectionId = req.body.sectionid; // Use req.body instead of req.params
const userName = req.body.username; // Use req.body instead of req.params

const result = await DynamicSectionService.getSectionById(sectionId);

if (result.reason) {
return res.status(result.code).json(result);
}
if (result) {
//console.debug(result);
return res.status(201).json(result);
}
}
catch (exception) {
const errResponse = new newErrorResponse(500, false, exception);
return res.status(500).json(errResponse);
}
});

router.route('/getSectionsByParentId')
.post(async function(req,res){
try{
var errResponse;
const parentId = req.body.parentid;
const username = req.body.username;
var result = await DynamicSectionService.getSectionsByParentId(parentId);
if (result.reason) {
return res.status(result.code).json(result);
}
if (result) {
//console.debug(result);
return res.status(201).json(result);
}
}
catch (exception) {
errResponse = new newErrorResponse(500, false, exception);
return res.status(500).json(errResponse);
}
})

router.route('/moveSection')
.post(async function(req, res){
try{
const sectionId = req.body.sectionId;
const newParentId = req.body.newParentId;

if (!(sectionId && newParentId)) {
const errResponse = new ErrorResponse(400,"Invalid move operation","");
res.status(400).json(errResponse);
return;
}

//Get the section object by id
const result = await dynamicSectionDAO.getSectionById(sectionId);
if (!result) {
return res.status(result).json(result);
}

const section = result;
//Remove the section from the original parent
const isSectionRemoved = await DynamicSectionService.deleteSectionPermanently(sectionId);
if (isSectionRemoved.reason) {
return res.status(isSectionRemoved.code).json(isSectionRemoved);
}
if (isSectionRemoved) {
//update the section parent id with new parent id
section.parentid = new ObjectId(newParentId);
//add the section to the new parent
const isSectionAdded = await DynamicSectionService.addSection(section);

if (isSectionAdded.reason) {
return res.status(isSectionAdded.code).json(isSectionAdded);
}
if (isSectionAdded) {
return res.status(201).json(isSectionAdded);
}
}

}
catch (exception){
console.log(exception);
const errResponse = new ErrorResponse(500, false, exception);
return res.status(500).json(errResponse);
}
})

module.exports = router ;
Loading

0 comments on commit c5f04bc

Please sign in to comment.