From 927499c172f340502c78af21e226af36758b924c Mon Sep 17 00:00:00 2001 From: "nayana.mogaveera" <nayana.mogaveera@niveussolutions.com> Date: Thu, 12 Sep 2024 15:10:12 +0530 Subject: [PATCH] template-changes --- api/template/business.js | 14 ++++++----- api/template/service.js | 53 ++++++++++++++++++++++++++++++++-------- 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/api/template/business.js b/api/template/business.js index fcebdae..60fd613 100644 --- a/api/template/business.js +++ b/api/template/business.js @@ -32,15 +32,17 @@ const createTemplate = async (name, desc, platform, widgets, hashId) => { }; -const getParticularTemplate = async (page, platform) => { +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 }; + const response = await templateService.getParticularTemplate(page,platform); + 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 } + }; diff --git a/api/template/service.js b/api/template/service.js index 195e074..1d26e2d 100644 --- a/api/template/service.js +++ b/api/template/service.js @@ -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'); - - 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}`); + 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) { - logger.error("Error in getParticularTemplate Service:", error.message); throw error; } }; -- GitLab