Add company specific promocodes

pull/4679/head
Berkeley Martinez 2015-11-23 15:38:05 -08:00
parent d6a7fa5562
commit 36067f37cb
4 changed files with 47 additions and 6 deletions

View File

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

View File

@ -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;
}

View File

@ -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": [],

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) {
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,28 @@ 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) {
return Job.updateAll({ id: id }, { promoCodeUsed: code })
.then(function({ count = 0 } = {}) {
log('job', count);
if (count) {
return promo;
}
return Promise.reject(new Error(
`Job ${id} not found`
));
});
});
};
Promo.remoteMethod(
@ -26,6 +53,11 @@ export default function promo(Promo) {
{
description: 'Get button id for promocode',
accepts: [
{
arg: 'id',
type: 'string',
required: true
},
{
arg: 'code',
type: 'string',