diff --git a/common/models/promo.js b/common/models/promo.js new file mode 100644 index 00000000000..617635f0c61 --- /dev/null +++ b/common/models/promo.js @@ -0,0 +1,49 @@ +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' + } + ] + } + ); +} diff --git a/common/models/promo.json b/common/models/promo.json new file mode 100644 index 00000000000..8cab2c27c4b --- /dev/null +++ b/common/models/promo.json @@ -0,0 +1,59 @@ +{ + "name": "promo", + "base": "PersistedModel", + "strict": true, + "idInjection": true, + "trackChanges": false, + "properties": { + "code": { + "type": "string", + "required": true, + "description": "The code to unlock the promotional discount" + }, + "name": { + "type": "string", + "required": true, + "description": "The name of the discount" + }, + "buttonId": { + "type": "string", + "required": true, + "description": "The id of paypal button" + }, + "type": { + "type": "string", + "description": "A selector of different types of buttons for the same discount" + }, + "fullPrice": { + "type": "number", + "required": true, + "description": "The original amount" + }, + "discountAmount": { + "type": "number", + "description": "The amount of the discount if applicable" + }, + "discountPercent": { + "type": "number", + "description": "The amount of discount as a percentage if applicable" + } + }, + "validations": [], + "relations": {}, + "acls": [ + { + "accessType": "*", + "principalType": "ROLE", + "principalId": "$everyone", + "permission": "DENY" + }, + { + "accessType": "EXECUTE", + "principalType": "ROLE", + "principalId": "$everyone", + "permission": "ALLOW", + "property": "getButton" + } + ], + "methods": [] +} diff --git a/server/model-config.json b/server/model-config.json index c993d603dfe..1b9d552ab96 100644 --- a/server/model-config.json +++ b/server/model-config.json @@ -51,6 +51,10 @@ "dataSource": "db", "public": true }, + "promo": { + "dataSource": "db", + "public": true + }, "user": { "dataSource": "db", "public": true