Merge pull request #4679 from FreeCodeCamp/fix/change-promo-code-logic

Add company specific promocodes
This commit is contained in:
Michael D. Johnson
2015-11-23 19:06:59 -05:00
4 changed files with 49 additions and 6 deletions

View File

@ -100,6 +100,7 @@ export default contain(
renderPromo() { renderPromo() {
const { const {
id,
promoApplied, promoApplied,
promoCode, promoCode,
promoName, promoName,
@ -145,6 +146,7 @@ export default contain(
block={ true } block={ true }
onClick={ () => { onClick={ () => {
jobActions.applyCode({ jobActions.applyCode({
id,
code: promoCode, code: promoCode,
type: isHighlighted ? 'isHighlighted' : null type: isHighlighted ? 'isHighlighted' : null
}); });

View File

@ -170,8 +170,11 @@ export default Actions({
); );
}); });
jobActions.applyCode.subscribe(({ code = '', type = null}) => { jobActions.applyCode.subscribe(({ id, code = '', type = null}) => {
const body = { code: code.replace(/[^\d\w\s]/, '') }; const body = {
id,
code: code.replace(/[^\d\w\s]/, '')
};
if (type) { if (type) {
body.type = type; body.type = type;
} }

View File

@ -88,6 +88,10 @@
"type": "string", "type": "string",
"required": true, "required": true,
"description": "How do campers apply to job" "description": "How do campers apply to job"
},
"promoCodeUsed": {
"type": "string",
"description": "the promocode, if any, that the job uses"
} }
}, },
"validations": [], "validations": [],

View File

@ -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) { 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 ( if (
!isAlphanumeric(code) && !isAlphanumeric(code) &&
type && type &&
@ -14,11 +24,30 @@ export default function promo(Promo) {
const query = { const query = {
where: { 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( Promo.remoteMethod(
@ -26,6 +55,11 @@ export default function promo(Promo) {
{ {
description: 'Get button id for promocode', description: 'Get button id for promocode',
accepts: [ accepts: [
{
arg: 'id',
type: 'string',
required: true
},
{ {
arg: 'code', arg: 'code',
type: 'string', type: 'string',