Coupons API
parent
208c938d94
commit
2fc76660a1
@ -0,0 +1,35 @@
|
||||
const router = module.exports = require('express-promise-router')()
|
||||
const parseJSON = require('body-parser').json()
|
||||
const db = require('../db')
|
||||
const ensureAdmin = require('./middleware/ensureAdmin')
|
||||
|
||||
function makeCode(length){
|
||||
var text = "";
|
||||
var possible = "ABCDEFGHJKMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789";
|
||||
for( var i=0; i < length; i++ )
|
||||
text += possible.charAt(Math.floor(Math.random() * possible.length));
|
||||
return text;
|
||||
}
|
||||
|
||||
router.post('/', ensureAdmin, parseJSON, async (req, res) => {
|
||||
const code = req.body.code || makeCode(10)
|
||||
|
||||
const coupon = await db.coupon.create(
|
||||
code,
|
||||
req.body.valid_until,
|
||||
req.body.free_shipping,
|
||||
req.body.number_allowed_uses,
|
||||
req.body.flat_discount_cents || 0,
|
||||
req.body.percent_discount || 0,
|
||||
req.body.per_sock_discount || 0,
|
||||
req.body.number_of_socks_free || 0
|
||||
)
|
||||
|
||||
res.json(coupon)
|
||||
})
|
||||
|
||||
router.get('/', ensureAdmin, async (req, res) => {
|
||||
const coupons = await db.coupon.findAll()
|
||||
|
||||
res.json(coupons)
|
||||
})
|
Loading…
Reference in New Issue