Skip to content
Snippets Groups Projects
Commit b1666375 authored by Nayana Mogaveera's avatar Nayana Mogaveera
Browse files

Merge branch 'nayana' into 'development'

Nayana

See merge request !9
parents 7b8ce879 457572c8
No related branches found
No related tags found
1 merge request!9Nayana
...@@ -27,7 +27,9 @@ const createTemplate = async (name, desc,platform, widgets, hashId) => { ...@@ -27,7 +27,9 @@ const createTemplate = async (name, desc,platform, widgets, hashId) => {
const getParticularTemplate = async (page,platform) => { const getParticularTemplate = async (page,platform) => {
try { try {
const response = await templateService.getParticularTemplate(page,platform); 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 } return { status: '200', response }
} catch (error) { } catch (error) {
throw error throw error
......
...@@ -29,7 +29,8 @@ const createTemplate = async (req, res, next) => { ...@@ -29,7 +29,8 @@ const createTemplate = async (req, res, next) => {
const getParticularTemplate = async (req, res) => { const getParticularTemplate = async (req, res) => {
try { try {
console.log("GET template details of a particular page"); 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); const response = await templateBusiness.getParticularTemplate(page,platform);
return res.status(response.status).json({ data: response }); return res.status(response.status).json({ data: response });
} catch (error) { } catch (error) {
......
// const logger = require('utils/logger'); // const logger = require('utils/logger');
const Widget = require("../models/widget"); const Widget = require("../models/widget");
const Template = require("../models/template") const Template = require("../models/template");
const updateAndMapWidget = async (
const updateAndMapWidget = async (page,widgetId,position, startTime, endTime) => { page,
widgetId,
position,
startTime,
endTime
) => {
try { try {
const template = await Template.findOne({ name: page }); const template = await Template.findOne({ name: page });
if (!template) { if (!template) {
return { status: 404, error: 'template not found' }; return { status: 404, error: "template not found" };
} }
console.log(page,widgetId,position, startTime, endTime) console.log(page, widgetId, position, startTime, endTime);
const widgetIndex = template.widgets.findIndex(w => w.widgetId.toString() === widgetId); const widgetIndex = template.widgets.findIndex(
(w) => w.widgetId.toString() === widgetId
);
if (widgetIndex !== -1) { if (widgetIndex !== -1) {
template.widgets[widgetIndex] = { template.widgets[widgetIndex] = {
...template.widgets[widgetIndex], ...template.widgets[widgetIndex],
position, position,
startTime, startTime,
endTime endTime,
}; };
} else { } else {
template.widgets.push({ template.widgets.push({
widgetId: widgetId, widgetId: widgetId,
position, position,
startTime, startTime,
endTime endTime,
}); });
} }
await template.save(); await template.save();
return { status: 200, widgetId: widgetId, page: page }; return { status: 200, widgetId: widgetId, page: page };
} catch (error) { } catch (error) {
throw error throw error;
} }
}; };
const createTemplate = async (name, desc, platform, widgets, hashId) => { const createTemplate = async (name, desc, platform, widgets, hashId) => {
...@@ -44,34 +50,74 @@ const createTemplate = async (name, desc, platform, widgets, hashId) => { ...@@ -44,34 +50,74 @@ const createTemplate = async (name, desc, platform, widgets, hashId) => {
desc, desc,
platform, platform,
widgets, widgets,
hashId hashId,
}); });
// Save the template to the database // Save the template to the database
await newTemplate.save(); 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) { } catch (error) {
throw error throw error;
} }
}; };
const getParticularTemplate = async (page, platform) => { const getParticularTemplate = async (page, platform) => {
try { try {
const template = await Template.findOne({ name: page,platform}).populate('widgets.widgetId'); let pipeline = [];
if (!template) { pipeline.push({ $match: { name: page, platform: platform } });
return { status: 404, message: 'Template not found'};
} 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; return template;
} catch (error) { } catch (error) {
throw error throw error;
} }
}; };
module.exports = { module.exports = {
updateAndMapWidget, updateAndMapWidget,
createTemplate, createTemplate,
getParticularTemplate getParticularTemplate,
}; };
...@@ -46,9 +46,6 @@ paths: ...@@ -46,9 +46,6 @@ paths:
type: array type: array
items: items:
type: object type: object
properties:
widgetId:
type: string
position: position:
type: integer type: integer
startTime: startTime:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment