diff --git a/common/app/routes/Jobs/components/GoToPayPal.jsx b/common/app/routes/Jobs/components/GoToPayPal.jsx index c5bc9ce451..77a554f14a 100644 --- a/common/app/routes/Jobs/components/GoToPayPal.jsx +++ b/common/app/routes/Jobs/components/GoToPayPal.jsx @@ -100,6 +100,7 @@ export default contain( renderPromo() { const { + id, promoApplied, promoCode, promoName, @@ -145,6 +146,7 @@ export default contain( block={ true } onClick={ () => { jobActions.applyCode({ + id, code: promoCode, type: isHighlighted ? 'isHighlighted' : null }); diff --git a/common/app/routes/Jobs/flux/Actions.js b/common/app/routes/Jobs/flux/Actions.js index ced8f5d723..08675048b7 100644 --- a/common/app/routes/Jobs/flux/Actions.js +++ b/common/app/routes/Jobs/flux/Actions.js @@ -170,8 +170,11 @@ export default Actions({ ); }); - jobActions.applyCode.subscribe(({ code = '', type = null}) => { - const body = { code: code.replace(/[^\d\w\s]/, '') }; + jobActions.applyCode.subscribe(({ id, code = '', type = null}) => { + const body = { + id, + code: code.replace(/[^\d\w\s]/, '') + }; if (type) { body.type = type; } diff --git a/common/models/job.json b/common/models/job.json index 6b687a1c5c..04776dc5d0 100644 --- a/common/models/job.json +++ b/common/models/job.json @@ -88,6 +88,10 @@ "type": "string", "required": true, "description": "How do campers apply to job" + }, + "promoCodeUsed": { + "type": "string", + "description": "the promocode, if any, that the job uses" } }, "validations": [], diff --git a/common/models/promo.js b/common/models/promo.js index 1901e47d2a..9a423bdd6b 100644 --- a/common/models/promo.js +++ b/common/models/promo.js @@ -1,7 +1,17 @@ -import { isAlphanumeric } from 'validator'; +import { isAlphanumeric, isHexadecimal } from 'validator'; +import debug from 'debug'; + +const log = debug('freecc:models:promo'); export default function promo(Promo) { - Promo.getButton = function getButton(code, type = 'isNot') { + Promo.getButton = function getButton(id, code, type = 'isNot') { + const Job = Promo.app.models.Job; + if (!id || !isHexadecimal(id)) { + return Promise.reject(new Error( + 'Must include job id' + )); + } + if ( !isAlphanumeric(code) && type && @@ -14,11 +24,30 @@ export default function promo(Promo) { const query = { where: { - and: [{ code }, { type }] + and: [{ + code: type === 'isNot' ? type : 'isHighlighted' + }, + { + type: type.replace(/^\$/g, '') + }] } }; - return Promo.findOne(query); + return Promo.findOne(query) + .then(function(promo) { + // turn promo model to plain js object; + promo = promo.toJSON(); + return Job.updateAll({ id: id }, { promoCodeUsed: code }) + .then(function({ count = 0 } = {}) { + log('job', count); + if (count) { + return Object.assign({}, promo, { name: `${code} Discount` }); + } + return Promise.reject(new Error( + `Job ${id} not found` + )); + }); + }); }; Promo.remoteMethod( @@ -26,6 +55,11 @@ export default function promo(Promo) { { description: 'Get button id for promocode', accepts: [ + { + arg: 'id', + type: 'string', + required: true + }, { arg: 'code', type: 'string',