|
| 1 | +/** |
| 2 | + * This service provides operations of gamification. |
| 3 | + */ |
| 4 | + |
| 5 | +const _ = require('lodash') |
| 6 | +const Joi = require('joi') |
| 7 | +const config = require('config') |
| 8 | +const request = require('request') |
| 9 | +const helper = require('../common/helper') |
| 10 | +const logger = require('../common/logger') |
| 11 | +const errors = require('../common/errors') |
| 12 | + |
| 13 | +// The Mambo API uses specifically the OAuth 2.0 Client Credentials grant flow. |
| 14 | +// https://api.mambo.io/docs/#authentication |
| 15 | +// this is where the token is stored after it is exchanged from Mambo |
| 16 | +// it is used on all calls to Mambo API |
| 17 | +let MAMBO_ACCESS_TOKEN; |
| 18 | + |
| 19 | +/** |
| 20 | + * This function Request Access Token from Mambo by using Client Credentials flow. |
| 21 | + * When token is provided it is stored in MAMBO_ACCESS_TOKEN |
| 22 | + * @returns Promise |
| 23 | + */ |
| 24 | +async function getAccessToken() { |
| 25 | + const options = { |
| 26 | + method: 'POST', |
| 27 | + url: `${config.MAMBO_DOMAIN_URL}/oauth/token`, |
| 28 | + headers: { |
| 29 | + 'Authorization': `Basic ${Buffer.from(`${config.MAMBO_PUBLIC_KEY}:${config.MAMBO_PRIVATE_KEY}`).toString('base64')}`, |
| 30 | + 'Content-Type': 'application/x-www-form-urlencoded' |
| 31 | + }, |
| 32 | + form: { |
| 33 | + 'grant_type': 'client_credentials' |
| 34 | + } |
| 35 | + }; |
| 36 | + // wrap in Promise and return |
| 37 | + return new Promise(function (resolve, reject) { |
| 38 | + request(options, |
| 39 | + function (error, response, body) { |
| 40 | + if (response.statusCode === 200) { |
| 41 | + MAMBO_ACCESS_TOKEN = JSON.parse(body) |
| 42 | + resolve(MAMBO_ACCESS_TOKEN) |
| 43 | + } else { |
| 44 | + reject(error) |
| 45 | + } |
| 46 | + } |
| 47 | + ) |
| 48 | + }) |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +/** |
| 53 | + * Get a user's rewards and all available rewards |
| 54 | + * API details https://api.mambo.io/docs/#!/Users/getUserRewards |
| 55 | + * @param {String} handle the member handle |
| 56 | + * @param {Object} query the query parameters |
| 57 | + * @returns {Object} the rewards |
| 58 | + */ |
| 59 | +async function getMemberRewards(handle, query) { |
| 60 | + // if no API token, try get one first |
| 61 | + if (!MAMBO_ACCESS_TOKEN) await getAccessToken(); |
| 62 | + // prepare the API request |
| 63 | + let apiQuery = '?'; |
| 64 | + if (query.tags) { |
| 65 | + const tags = helper.parseCommaSeparatedString(query.tyags) |
| 66 | + apiQuery += tags.map(t => `tags=${t}`).join('&') |
| 67 | + } |
| 68 | + if (query.tagsJoin && query.tags) { |
| 69 | + apiQuery += `&tagsJoin=${query.tagsJoin}` |
| 70 | + } |
| 71 | + const options = { |
| 72 | + method: 'GET', |
| 73 | + url: `${config.MAMBO_DOMAIN_URL}/api/v1/${query.site || config.MAMBO_DEFAULT_SITE}/users/${encodeURIComponent(handle)}/rewards${apiQuery}`, |
| 74 | + headers: { |
| 75 | + 'Authorization': `Bearer ${MAMBO_ACCESS_TOKEN.access_token}` |
| 76 | + } |
| 77 | + }; |
| 78 | + // wrap in Promise and return |
| 79 | + return new Promise(function (resolve, reject) { |
| 80 | + request(options, |
| 81 | + function (error, response, body) { |
| 82 | + if (response.statusCode === 200) { |
| 83 | + resolve(JSON.parse(body)) |
| 84 | + } else { |
| 85 | + if (response.statusCode === 401) { |
| 86 | + // token expired |
| 87 | + // reset it and call the function to retry |
| 88 | + MAMBO_ACCESS_TOKEN = null; |
| 89 | + resolve(getMemberRewards(handle, query)) |
| 90 | + } else { |
| 91 | + reject(error) |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + ) |
| 96 | + }) |
| 97 | +} |
| 98 | + |
| 99 | +getMemberRewards.schema = { |
| 100 | + handle: Joi.string().required(), |
| 101 | + query: Joi.object().keys({ |
| 102 | + site: Joi.string(), |
| 103 | + tags: Joi.string(), |
| 104 | + tagsJoin: Joi.string().valid('hasAllOf', 'hasAnyOf') |
| 105 | + }) |
| 106 | +} |
| 107 | + |
| 108 | +module.exports = { |
| 109 | + getMemberRewards |
| 110 | +} |
| 111 | + |
| 112 | +logger.buildService(module.exports) |
0 commit comments