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

template-changes

parent cdb53b1f
No related branches found
No related tags found
1 merge request!15template-changes
......@@ -34,13 +34,15 @@ const createTemplate = async (name, desc, platform, widgets, hashId) => {
const getParticularTemplate = async (page,platform) => {
try {
logger.info(`Business logic: Fetching template for page: ${page}, platform: ${platform}`);
const response = await templateService.getParticularTemplate(page,platform);
return { status: 200, response };
if (response.length == 0) {
return { status: 404, message: "Template not found" };
}
return { status: '200', response }
} catch (error) {
logger.error("Error in getParticularTemplate Business Logic:", error.message);
throw error;
throw error
}
};
......
......@@ -87,18 +87,51 @@ const createTemplate = async (name, desc, platform, widgets, hashId) => {
const getParticularTemplate = async (page, platform) => {
try {
logger.info(`Service: Querying database for page: ${page}, platform: ${platform}`);
const template = await Template.find({ name: page, platform }).populate('widgets.widgetId');
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);
if (!template) {
logger.warn(`Template not found for page: ${page}, platform: ${platform}`);
return { status: 404, message: 'Template not found' };
}
logger.info(`Template found for page: ${page}, platform: ${platform}`);
return template;
} catch (error) {
logger.error("Error in getParticularTemplate Service:", error.message);
throw error;
}
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment