diff --git a/.env b/.env new file mode 100644 index 0000000000000000000000000000000000000000..be824930b9d0ec33c18cfa346720ff94e64aeec1 --- /dev/null +++ b/.env @@ -0,0 +1,4 @@ +NODE_ENV=development +SERVICE_BASE_URL=https://30dd-206-84-239-127.ngrok-free.app +SERVICE_PORT=5000 +LOG_LEVEL=debug diff --git a/api/models/template.js b/api/models/template.js index 98426ca9bfbcf2b0828eebb1b286412f91a933d5..d1de02750e8d9344a6224b288f915cab1e41b1ba 100644 --- a/api/models/template.js +++ b/api/models/template.js @@ -13,6 +13,11 @@ const WidgetRefSchema = new Schema({ const TemplateSchema = new Schema({ name: { type: String, required: true }, desc: { type: String }, + platform: { + type: String, + enum: ['web', 'mobile', 'both'], + required: true + }, widgets: [WidgetRefSchema], hashId: { type: String, unique: true, required: true }, createdAt: { type: Date, default: Date.now }, diff --git a/api/template/business.js b/api/template/business.js index 50653188ab408e84e0b415b34875de56afa93483..fce072c6b7948d15089b75e8bfcf814c71001fe0 100644 --- a/api/template/business.js +++ b/api/template/business.js @@ -14,9 +14,9 @@ const updateAndMapWidget = async (page,widgetId,position, startTime, endTime) => }; -const createTemplate = async (name, desc, widgets, hashId) => { +const createTemplate = async (name, desc,platform, widgets, hashId) => { try { - const response = await templateService.createTemplate(name, desc, widgets, hashId); + const response = await templateService.createTemplate(name, desc,platform, widgets, hashId); return { status: '200', response } } catch (error) { throw error @@ -24,10 +24,12 @@ const createTemplate = async (name, desc, widgets, hashId) => { }; -const getParticularTemplate = async (page) => { +const getParticularTemplate = async (page,platform) => { try { - const response = await templateService.getParticularTemplate(page); - console.log(page,"Pagessssssss"); + const response = await templateService.getParticularTemplate(page,platform); + 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 fb4786479a42493797b8269d8d96b94c3dbd09ee..96f6aa18409805001249119e116898cef1f9cd3b 100644 --- a/api/template/controller.js +++ b/api/template/controller.js @@ -18,8 +18,8 @@ const updateAndMapWidget = async (req, res, next) => { const createTemplate = async (req, res, next) => { try { - const { name, desc, widgets, hashId } = req.body; - const response = await templateBusiness.createTemplate(name, desc, widgets, hashId); + const { name, desc,platform, widgets, hashId } = req.body; + const response = await templateBusiness.createTemplate(name, desc,platform, widgets, hashId); return res.status(response.status).json({ data: response }); } catch (error) { throw error @@ -30,7 +30,8 @@ const getParticularTemplate = async (req, res) => { try { console.log("GET template details of a particular page"); const { page } = req.params; - const response = await templateBusiness.getParticularTemplate(page); + const {platform}=req.query + const response = await templateBusiness.getParticularTemplate(page,platform); return res.status(response.status).json({ data: response }); } catch (error) { throw error; diff --git a/api/template/service.js b/api/template/service.js index 2111a862a7a8aeaff03b5965653123e60ca3423e..19e4e406f6a0217eb2545f624eb27fe665611ebe 100644 --- a/api/template/service.js +++ b/api/template/service.js @@ -1,101 +1,123 @@ // 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, widgets, hashId) => { +const createTemplate = async (name, desc, platform, widgets, hashId) => { try { - console.log("in service") const newTemplate = new Template({ name, 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,res) => { +const getParticularTemplate = async (page, platform) => { try { - 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" } }); + 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' }, - - '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(); - } + 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/package.json b/package.json index 67e81ce2539b9773c56c009cc04aaebddef40e72..0e24b61f394f6fb0c6071a643e3d3ae672da8b19 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "license": "ISC", "dependencies": { "body-parser": "^1.20.2", + "dotenv": "^16.4.5", "express": "^4.19.2", "mongoose": "^8.6.0", "nodemon": "^3.1.4", diff --git a/server.js b/server.js index 524f6cb34b36594784eee390a1e75c406b604ff8..f8fe990819079e79b36a888f3c77ca03a1f6bd99 100644 --- a/server.js +++ b/server.js @@ -1,4 +1,4 @@ -// server.js +require('dotenv').config(); const express = require('express'); const bodyParser = require('body-parser'); const swaggerUi = require('swagger-ui-express'); @@ -19,10 +19,11 @@ app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); // API base URL app.use(apiBaseRouter); - // Start the server -const PORT = process.env.PORT || 5000; +const PORT = process.env.SERVICE_PORT || 5000; +const BASE_URL = process.env.SERVICE_BASE_URL || `http://localhost:${PORT}`; + app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`); - console.log(`Swagger UI available at http://localhost:${PORT}/api-docs`); + console.log(`Swagger UI available at ${BASE_URL}/api-docs`); }); diff --git a/swagger.yaml b/swagger.yaml index 539b83380074c791102965dcbbfdcfec0b2e5e34..e6ec0dd650f754052a03bb2c655ce3e22c4367ee 100644 --- a/swagger.yaml +++ b/swagger.yaml @@ -21,6 +21,13 @@ paths: schema: type: string description: The page for which template details are requested. + - in: query # Add platform as a query parameter + name: platform + required: true + schema: + type: string + enum: ['web', 'mobile', 'both'] + description: The platform for which template details are requested (web, mobile, or both). responses: '200': description: Template retrieved successfully @@ -310,6 +317,11 @@ paths: type: string description: A description of the template example: "Template for the homepage widget" + platform: + type: string + enum: ['web', 'mobile', 'both'] + description: The platform for which the template is intended + example: "web" widgets: type: array description: A list of widgets mapped to the template