Skip to content

Commit

Permalink
Add simplified productList API option
Browse files Browse the repository at this point in the history
  • Loading branch information
megastary committed Sep 12, 2024
1 parent b462d61 commit c655e7f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
6 changes: 6 additions & 0 deletions requests.http
Original file line number Diff line number Diff line change
Expand Up @@ -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
54 changes: 27 additions & 27 deletions routes/api/productList.js
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit c655e7f

Please sign in to comment.