2015-10-25 14:26:37 -07:00
|
|
|
import { isAlphanumeric } from 'validator';
|
|
|
|
|
|
|
|
export default function promo(Promo) {
|
2015-10-26 17:39:38 -07:00
|
|
|
Promo.getButton = function getButton(code, type = 'isNot') {
|
2015-10-25 14:26:37 -07:00
|
|
|
if (
|
|
|
|
!isAlphanumeric(code) &&
|
|
|
|
type &&
|
|
|
|
!isAlphanumeric(type)
|
|
|
|
) {
|
|
|
|
return Promise.reject(new Error(
|
|
|
|
'Code or Type should be an alphanumeric'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
const query = {
|
|
|
|
where: {
|
2015-10-26 17:39:38 -07:00
|
|
|
and: [{ code }, { type }]
|
2015-10-25 14:26:37 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return Promo.findOne(query);
|
|
|
|
};
|
|
|
|
|
|
|
|
Promo.remoteMethod(
|
|
|
|
'getButton',
|
|
|
|
{
|
|
|
|
description: 'Get button id for promocode',
|
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'code',
|
|
|
|
type: 'string',
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'type',
|
|
|
|
type: 'string'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
returns: [
|
|
|
|
{
|
|
|
|
arg: 'promo',
|
|
|
|
type: 'object'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|