Files
freeCodeCamp/common/models/promo.js
2015-10-29 17:10:29 -07:00

48 lines
857 B
JavaScript

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