Files
freeCodeCamp/common/models/promo.js
Berkeley Martinez 568d1ef1d2 Add promo model
2015-10-29 17:10:29 -07:00

50 lines
848 B
JavaScript

import { isAlphanumeric } from 'validator';
export default function promo(Promo) {
Promo.getButton = function getButton(code, type = null) {
if (
!isAlphanumeric(code) &&
type &&
!isAlphanumeric(type)
) {
return Promise.reject(new Error(
'Code or Type should be an alphanumeric'
));
}
const query = {
where: {
code,
type
}
};
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'
}
]
}
);
}