|
| 1 | +/** |
| 2 | + * Server-side functions necessary for effective integration with growsurf |
| 3 | + */ |
| 4 | +import fetch from 'isomorphic-fetch'; |
| 5 | +import config from 'config'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Auxiliary class that handles communication with growsurf |
| 9 | + */ |
| 10 | +export default class GrowsurfService { |
| 11 | + /** |
| 12 | + * Creates a new service instance. |
| 13 | + * @param {String} baseUrl The base API endpoint. |
| 14 | + */ |
| 15 | + constructor(baseUrl = 'https://api.growsurf.com/v2') { |
| 16 | + this.private = { |
| 17 | + baseUrl, |
| 18 | + apiKey: config.SECRET.GROWSURF_API_KEY, |
| 19 | + authorization: `Bearer ${config.SECRET.GROWSURF_API_KEY}`, |
| 20 | + }; |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Gets get participant by email or id. |
| 25 | + * @return {Promise} |
| 26 | + * @param {Object} req the request. |
| 27 | + * @param {Object} res the response. |
| 28 | + */ |
| 29 | + async getParticipant(req, res) { |
| 30 | + const { participantId } = req.query; |
| 31 | + const response = await fetch(`${this.private.baseUrl}/campaign/${config.GROWSURF_CAMPAIGN_ID}/participant/${participantId}`, { |
| 32 | + method: 'GET', |
| 33 | + headers: { |
| 34 | + 'Content-Type': 'application/json', |
| 35 | + Authorization: this.private.authorization, |
| 36 | + }, |
| 37 | + }); |
| 38 | + if (response.status >= 300) { |
| 39 | + res.status(response.status); |
| 40 | + return { |
| 41 | + error: await response.json(), |
| 42 | + code: response.status, |
| 43 | + url: `${this.private.baseUrl}/campaign/${config.GROWSURF_CAMPAIGN_ID}/participant/${participantId}`, |
| 44 | + }; |
| 45 | + } |
| 46 | + const data = await response.json(); |
| 47 | + return data; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Add participant |
| 52 | + * @return {Promise} |
| 53 | + * @param {Object} body the request payload. |
| 54 | + */ |
| 55 | + async addParticipant(body) { |
| 56 | + const response = await fetch(`${this.private.baseUrl}/campaign/${config.GROWSURF_CAMPAIGN_ID}/participant`, { |
| 57 | + method: 'POST', |
| 58 | + headers: { |
| 59 | + 'Content-Type': 'application/json', |
| 60 | + Authorization: this.private.authorization, |
| 61 | + }, |
| 62 | + body, |
| 63 | + }); |
| 64 | + if (response.status >= 300) { |
| 65 | + return { |
| 66 | + error: await response.json(), |
| 67 | + code: response.status, |
| 68 | + url: `${this.private.baseUrl}/campaign/${config.GROWSURF_CAMPAIGN_ID}/participant`, |
| 69 | + body, |
| 70 | + private: this.private, // to remove in final release |
| 71 | + }; |
| 72 | + } |
| 73 | + const data = await response.json(); |
| 74 | + return data; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Gets get participant by email or id |
| 79 | + * if not exxists create it |
| 80 | + * @return {Promise} |
| 81 | + * @param {Object} req the request. |
| 82 | + * @param {Object} res the response. |
| 83 | + */ |
| 84 | + async getOrCreateParticipant(req, res) { |
| 85 | + const { body } = req; |
| 86 | + const result = await this.addParticipant(JSON.stringify({ |
| 87 | + email: body.email, |
| 88 | + firstName: body.firstName, |
| 89 | + lastName: body.lastName, |
| 90 | + })); |
| 91 | + if (result.error) { |
| 92 | + res.status(result.code); |
| 93 | + } |
| 94 | + return result; |
| 95 | + } |
| 96 | +} |
0 commit comments