diff --git a/api/template/business.js b/api/template/business.js index c42fc3bd1f8632b90a90d05e47e16a8a8103b85c..fce072c6b7948d15089b75e8bfcf814c71001fe0 100644 --- a/api/template/business.js +++ b/api/template/business.js @@ -27,7 +27,9 @@ const createTemplate = async (name, desc,platform, widgets, hashId) => { const getParticularTemplate = async (page,platform) => { try { const response = await templateService.getParticularTemplate(page,platform); - console.log(page,"Pagessssssss"); + if (response.length == 0) { + return { status: 404, message: "Template not found" }; + } return { status: '200', response } } catch (error) { throw error diff --git a/api/template/controller.js b/api/template/controller.js index a8cc12034e16876014791771c1887ea03329d3bb..96f6aa18409805001249119e116898cef1f9cd3b 100644 --- a/api/template/controller.js +++ b/api/template/controller.js @@ -29,7 +29,8 @@ const createTemplate = async (req, res, next) => { const getParticularTemplate = async (req, res) => { try { console.log("GET template details of a particular page"); - const { page,platform } = req.params; + const { page } = req.params; + const {platform}=req.query const response = await templateBusiness.getParticularTemplate(page,platform); return res.status(response.status).json({ data: response }); } catch (error) { diff --git a/api/template/service.js b/api/template/service.js index e1ac2b12614ba8e4f156aeef1d3e24e35bf694ea..19e4e406f6a0217eb2545f624eb27fe665611ebe 100644 --- a/api/template/service.js +++ b/api/template/service.js @@ -1,40 +1,46 @@ // const logger = require('utils/logger'); const Widget = require("../models/widget"); -const Template = require("../models/template") +const Template = require("../models/template"); - -const updateAndMapWidget = async (page,widgetId,position, startTime, endTime) => { +const updateAndMapWidget = async ( + page, + widgetId, + position, + startTime, + endTime +) => { try { const template = await Template.findOne({ name: page }); if (!template) { - return { status: 404, error: 'template not found' }; + return { status: 404, error: "template not found" }; } - console.log(page,widgetId,position, startTime, endTime) - const widgetIndex = template.widgets.findIndex(w => w.widgetId.toString() === widgetId); - + console.log(page, widgetId, position, startTime, endTime); + const widgetIndex = template.widgets.findIndex( + (w) => w.widgetId.toString() === widgetId + ); + if (widgetIndex !== -1) { template.widgets[widgetIndex] = { - ...template.widgets[widgetIndex], - position, - startTime, - endTime + ...template.widgets[widgetIndex], + position, + startTime, + endTime, }; } else { template.widgets.push({ - widgetId: widgetId, - position, - startTime, - endTime + widgetId: widgetId, + position, + startTime, + endTime, }); } await template.save(); return { status: 200, widgetId: widgetId, page: page }; } catch (error) { - throw error + throw error; } - }; const createTemplate = async (name, desc, platform, widgets, hashId) => { @@ -44,34 +50,74 @@ const createTemplate = async (name, desc, platform, widgets, hashId) => { desc, platform, widgets, - hashId + hashId, }); // Save the template to the database await newTemplate.save(); - return { status: 201, message: 'Template created successfully', id: newTemplate._id }; + return { + status: 201, + message: "Template created successfully", + id: newTemplate._id, + }; } catch (error) { - throw error + throw error; } - }; -const getParticularTemplate = async (page,platform) => { +const getParticularTemplate = async (page, platform) => { try { - const template = await Template.findOne({ name: page,platform}).populate('widgets.widgetId'); - if (!template) { - return { status: 404, message: 'Template not found'}; - } + let pipeline = []; + pipeline.push({ $match: { name: page, platform: platform } }); + + pipeline.push({ $unwind: { path: "$widgets" } }); + pipeline.push({ + $lookup: { + localField: "widgets.widgetId", + foreignField: "_id", + from: "widgets", + as: "widget", + }, + }); + pipeline.push({ $unwind: { path: "$widget" } }); + + const group = { + $group: { + _id: "$_id", + name: { $first: "$name" }, + desc: { $first: "$desc" }, + hashId: { $first: "$hashId" }, + platform: { $first: "$platform" }, + + widgets: { + $addToSet: { + widgetId: "$widget._id", + title: "$widget.title", + widget: "$widget.widget", + enable: "$widget.enable", + sectionHeadingText: "$widget.sectionHeadingText", + apiUrl: "$widget.apiUrl", + buttonData: "$widget.buttonData", + list: "$widget.list", + position: "$widgets.position", + startTime: "$widgets.startTime", + endTime: "$widgets.endTime", + }, + }, + }, + }; + pipeline.push(group); + const template = await Template.aggregate(pipeline); + + return template; } catch (error) { - throw error + throw error; } - }; - module.exports = { updateAndMapWidget, createTemplate, - getParticularTemplate -}; \ No newline at end of file + getParticularTemplate, +}; diff --git a/swagger.yaml b/swagger.yaml index f85f05fa9cad3b146033c5297ad55637c0601931..e6ec0dd650f754052a03bb2c655ce3e22c4367ee 100644 --- a/swagger.yaml +++ b/swagger.yaml @@ -46,9 +46,6 @@ paths: type: array items: type: object - properties: - widgetId: - type: string position: type: integer startTime: