You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
993 B
JavaScript

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)
})