-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from Prometheus7-creator/multitenant
added dynamic section api
- Loading branch information
Showing
7 changed files
with
772 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } }); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ; |
Oops, something went wrong.