From 0206bea1b1985d2678be85e01fdba4f2577fa33a Mon Sep 17 00:00:00 2001
From: "nayana.mogaveera" <nayana.mogaveera@niveussolutions.com>
Date: Fri, 6 Sep 2024 15:00:26 +0530
Subject: [PATCH] commit-sep-6-15

---
 api/template/service.js | 26 ++++++++++++++++++++++++--
 swagger.yaml            |  3 ---
 2 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/api/template/service.js b/api/template/service.js
index 2d172f8..2111a86 100644
--- a/api/template/service.js
+++ b/api/template/service.js
@@ -58,11 +58,33 @@ const createTemplate = async (name, desc, widgets, hashId) => {
 
 const getParticularTemplate = async (page,res) => {
   try {
-    const template = await Template.findOne({ name: page}).populate('widgets.widgetId');
+    let pipeline =[];
+    pipeline.push({ '$match': { 'name': page } });
+    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' },
+            
+             '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)
+    // const template = await Template.findOne({ name: page}).populate('widgets');
     if (!template) {
       // return res.status(404).json({ error: 'Template not found' });
       console.error('Template not found');
-      res.status(404).send();
+      // res.status(404).send();
     }
     return template;
   } catch (error) {
diff --git a/swagger.yaml b/swagger.yaml
index 812af00..539b833 100644
--- a/swagger.yaml
+++ b/swagger.yaml
@@ -39,9 +39,6 @@ paths:
                     type: array
                     items:
                       type: object
-                      properties:
-                        widgetId:
-                          type: string
                         position:
                           type: integer
                         startTime:
-- 
GitLab