Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
Consolidated middleware (Express) function to handle POST, PUT, and G…
Browse files Browse the repository at this point in the history
…ET requests from the front end as POST requests. The backend (Sequelize) will call the relevant SQL function depending on the data in the request body.
  • Loading branch information
phyninja committed Jul 23, 2021
1 parent 78017ac commit 3ec2591
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 29 deletions.
4 changes: 3 additions & 1 deletion backend/db/migrations/20210717101512-addPolicies.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ module.exports = {
autoIncrement: true,
primaryKey: true
},
shop_id: {
type: Sequelize.INTEGER
},
all_policies: {
type: Sequelize.JSON
// TODO: include 'defaultValue' key?
},
auth_token: {
type: Sequelize.STRING
Expand Down
58 changes: 33 additions & 25 deletions backend/routes/policies.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,49 @@ const log = getLogger('routes.policies')

module.exports = function (router) {
// Apply the callback 'authShop' to any POST request to the endpoint '/shop/policies'
// Use the result to create an instance of the model 'Policies' in the DB
router.post('/shop/policies', authShop, async (req, res) => {
log.info(`Request body: `, req.body)
res.send(
await Policies.create({
allPolicies: req.body,
authToken: req.shop.authToken
})
)
})

router.get('/shop/policies', authShop, async (req, res) => {
const data = await Policies.findOne({
// Use the result to find or create an instance of the model 'Policies' in the DB
const [policyEntry, created] = await Policies.findOrCreate({
where: {
authToken: req.shop.authToken
},
defaults: {
allPolicies: ['']
}
})
res.send(data.allPolicies)
})

// Similar logic to a POST request, difference being, an already-existing
// instance of the model 'Policies' is updated
router.put('/shop/policies', authShop, async (req) => {
return await Policies.update(
{
allPolicies: req.body
},
{
where: {
authToken: req.shop.authToken
}
}
)
log.info(`policyEntry: `, policyEntry, `created? `, created)

// If a new entry was created for the shop, check whether the front end supplied data for 'allPolicies'. Depending on the result,
// update the entry, or just return
if (policyEntry.allPolicies !== req.body) {
res.send(
await Policies.update(
{
allPolicies: req.body
},
{
where: {
authToken: req.shop.authToken
}
}
)
)
} else {
res.send(JSON.stringify('Nothing to post.'))
}
})

// router.get('/shop/policies', async (req, res) => {
// const authToken = decodeURIComponent(
// String(req.headers.authorization).split(' ')[1]
// )
// const data = await Policies.findOne()
// res.send(data)
// })

router.delete('/shop/policies', authShop, async (req) => {
return await Policies.destroy({
where: {
Expand Down
16 changes: 13 additions & 3 deletions shop/src/pages/admin/settings/legal/Edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ const LegalSettings = () => {
// ['Return Policy', 'Eget egestas purus viverra accumsan.', false]
// ]

const [policies, setPolicies] = useState([
const [policies, setPolicies] = useState(
/*[
[
'Terms and Conditions',
'Eget egestas purus viverra accumsan in nisl nisi scelerisque. Nibh praesent tristique magna sit amet purus gravida quis. In nibh mauris cursus mattis molestie. Eget dolor morbi non arcu risus quis. Quam id leo in vitae turpis massa sed elementum. Lectus sit amet est placerat in egestas. Aliquam eleifend mi in nulla posuere sollicitudin aliquam ultrices. Feugiat nibh sed pulvinar proin. Semper quis lectus nulla at volutpat diam. Mattis vulputate enim nulla aliquet. Gravida in fermentum et sollicitudin ac orci phasellus egestas.'
Expand All @@ -44,7 +45,10 @@ const LegalSettings = () => {
'Return Policy',
'Eget egestas purus viverra accumsan in nisl nisi scelerisque. Nibh praesent tristique magna sit amet purus gravida quis. In nibh mauris cursus mattis molestie. Eget dolor morbi non arcu risus quis. Quam id leo in vitae turpis massa sed elementum. Lectus sit amet est placerat in egestas. Aliquam eleifend mi in nulla posuere sollicitudin aliquam ultrices. Feugiat nibh sed pulvinar proin. Semper quis lectus nulla at volutpat diam. Mattis vulputate enim nulla aliquet. Gravida in fermentum et sollicitudin ac orci phasellus egestas.'
]
])
]*/ [
''
]
)

// useEffect(() => {
// let timeout
Expand Down Expand Up @@ -194,7 +198,7 @@ const LegalSettings = () => {
// module (i.e. backend). Discussion: https://github.com/expressjs/body-parser/issues/309
const shopPoliciesRes = await post('/shop/policies', {
// method: 'PUT',
body: JSON.stringify(['Test Policy']),
body: JSON.stringify(['Test Policy updated']),
suppressError: true
})
shopPoliciesRes
Expand All @@ -208,6 +212,12 @@ const LegalSettings = () => {
// return
// }
setSaving(false)
// const newShopPolicies = await post('/shop/policies', {
// method: 'GET',
// suppressError: true
// })
// console.log('newShopPolicies: ', newShopPolicies)

dispatch({
type: 'toast',
message: (
Expand Down

0 comments on commit 3ec2591

Please sign in to comment.