From c655e7f5954e00da15d173760a36357ef3dc8c04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=A0indel=C3=A1=C5=99?= Date: Thu, 12 Sep 2024 16:18:57 +0200 Subject: [PATCH] Add simplified productList API option --- requests.http | 6 +++++ routes/api/productList.js | 54 +++++++++++++++++++-------------------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/requests.http b/requests.http index 53d28cd..a64633b 100644 --- a/requests.http +++ b/requests.http @@ -49,3 +49,9 @@ sbf-API-secret: developmentsecret GET https://localhost/api/productList HTTP/1.1 Content-Type: application/json sbf-API-secret: developmentsecret + +### + +GET https://localhost/api/productList?voicebot=1 HTTP/1.1 +Content-Type: application/json +sbf-API-secret: developmentsecret diff --git a/routes/api/productList.js b/routes/api/productList.js index b2fdbbf..5595e8b 100644 --- a/routes/api/productList.js +++ b/routes/api/productList.js @@ -1,7 +1,6 @@ import { Router } from 'express' import { ensureAuthenticatedAPI } from '../../functions/ensureAuthenticatedAPI.js' import Product from '../../models/product.js' -import Category from '../../models/category.js' const router = Router() let responseJson @@ -61,33 +60,34 @@ router.get('/', ensureAuthenticatedAPI, function (req, res) { } } ]) - .then((docs) => { - Category.find({ disabled: { $in: [null, false] } }) - .sort([['name', 1]]) - .then((categories) => { - res.set('Content-Type', 'application/json') - if (!docs) { - res.status(404) - res.json('NOT_FOUND') - } else { - res.status(200) - responseJson = docs - console.log(categories) - res.json(responseJson) - } - }) - .catch(() => { - res.status(400) - res.set('Content-Type', 'application/problem+json') - const responseJson = { - type: 'https://github.com/houby-studio/small-business-fridge/wiki/API-documentation#productList', - title: 'Failed to retrieve list of categories.', - status: 400 - } - res.json(responseJson) - }) + .then((products) => { + res.set('Content-Type', 'application/json') + if (!products) { + res.status(404) + res.json('NOT_FOUND') + } else { + res.status(200) + if (req.query.voicebot) { + responseJson = products.map((product) => { + return { + keypadId: product.keypadId, + displayName: product.displayName, + description: product.description, + category: product.category.some((a) => typeof a == 'object') + ? product.category[0].name + : '', + stockSum: product.stockSum, + price: product.stock[0].price + } + }) + } else { + responseJson = products + } + res.json(responseJson) + } }) - .catch(() => { + .catch((e) => { + console.log(e) res.status(400) res.set('Content-Type', 'application/problem+json') const responseJson = {