const pg = require('../pg') const joinjs = require('join-js').default; const debug = require('debug')('sos:db:coupon') const mappings = require('../mappings') const coupon = module.exports = {} coupon.create = async function(code, valid_until, free_shipping, number_allowed_uses, flat_discount_cents, percent_discount, per_sock_discount_cents, number_of_socks_free){ const query = { text: 'select * from sos.create_coupon($1, $2, $3, $4, $5, $6, $7, $8)', values: [ code, valid_until, free_shipping, number_allowed_uses, flat_discount_cents, percent_discount, per_sock_discount_cents, number_of_socks_free ] } debug(query) const {rows} = await pg.query(query); return joinjs.map(rows, mappings, 'couponMap', 'coupon_')[0] } coupon.find = async function(code){ const query = { text: 'select * from sos.coupon where coupon_code = $1', values: [ code ] } debug(query) const {rows} = await pg.query(query); return joinjs.map(rows, mappings, 'couponMap', 'coupon_')[0] } coupon.findAll = async function() { const query = 'select * from sos.coupon' debug(query) const {rows} = await pg.query(query); return joinjs.map(rows, mappings, 'couponMap', 'coupon_') }