Coupon can be added to order
parent
511ef3e3ee
commit
3639c1c804
@ -0,0 +1,41 @@
|
||||
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]
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
const pg = require('./pg')
|
||||
const mappings = require('./mappings')
|
||||
const joinjs = require('join-js').default;
|
||||
const debug = require('debug')('sos:db')
|
||||
|
||||
const util = module.exports = {};
|
||||
const validateFunctionName = name => /^[a-z_]+$/.test(name)
|
||||
const getParamString = (length) => Array.from({length}, (_,i)=>i+1).map(i => '$' + i).join(', ')
|
||||
|
||||
util.executeQuery = async function({query, returnType, single = false}){
|
||||
debug(query)
|
||||
|
||||
const {rows} = await pg.query(query)
|
||||
|
||||
const mappedObjs = joinjs.map(rows, mappings, returnType + 'Map', returnType + '_')
|
||||
|
||||
if(single)
|
||||
return mappedObjs[0]
|
||||
return mappedObjs
|
||||
}
|
||||
|
||||
util.executeFunction = async function({name, params, returnType, single}) {
|
||||
if(!validateFunctionName(name)) throw new Error("Invalid function name: " + name);
|
||||
|
||||
const query = {
|
||||
text: `select * from sos.${name}( ${getParamString(params.length)} )`,
|
||||
values: params
|
||||
}
|
||||
|
||||
return util.executeQuery({query, returnType, single})
|
||||
}
|
Loading…
Reference in New Issue