Skip to content

General referrals #5667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ ARG GSHEETS_API_KEY
ARG SENDGRID_API_KEY
ARG GROWSURF_API_KEY
ARG GROWSURF_CAMPAIGN_ID
ARG GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY

# Optimizely
ARG OPTIMIZELY_SDK_KEY
Expand Down Expand Up @@ -137,6 +138,7 @@ ENV SENDGRID_API_KEY=$SENDGRID_API_KEY
ENV GROWSURF_API_KEY=$GROWSURF_API_KEY
ENV GROWSURF_CAMPAIGN_ID=$GROWSURF_CAMPAIGN_ID
ENV GSHEETS_API_KEY=$GSHEETS_API_KEY
ENV GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY=$GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY

# Optimizely
ENV OPTIMIZELY_SDK_KEY=$OPTIMIZELY_SDK_KEY
Expand Down
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ docker build -t $TAG \
--build-arg GSHEETS_API_KEY=$GSHEETS_API_KEY \
--build-arg OPTIMIZELY_SDK_KEY=$OPTIMIZELY_SDK_KEY \
--build-arg COMMUNITY_APP_URL=$COMMUNITY_APP_URL \
--build-arg GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY=$GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY \
--build-arg VALID_ISSUERS=$VALID_ISSUERS .

# Copies "node_modules" from the created image, if necessary for caching.
Expand Down
1 change: 1 addition & 0 deletions config/custom-environment-variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,5 @@ module.exports = {
OPTIMIZELY: {
SDK_KEY: 'OPTIMIZELY_SDK_KEY',
},
GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY: 'GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY',
};
5 changes: 4 additions & 1 deletion config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,13 @@ module.exports = {
GROWSURF_COOKIE_SETTINGS: {
secure: true,
domain: '',
expires: 7, // days
expires: 30, // days
},

GSHEETS_API_KEY: 'AIzaSyBRdKySN5JNCb2H6ZxJdTTvp3cWU51jiOQ',
GOOGLE_SERVICE_ACCOUNT_EMAIL: 'communityappserviceacc@tc-sheets-to-contentful.iam.gserviceaccount.com',
GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY: '',
GIG_REFERRALS_SHEET: '1xilx7NxDAvzAzOTbPpvb3lL3RWv1VD5W24OEMAoF9HU',

AUTH_CONFIG: {
AUTH0_URL: 'TC_M2M_AUTH0_URL',
Expand Down
3 changes: 1 addition & 2 deletions src/assets/images/icon-facebook.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions src/assets/images/icon-linkedIn.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions src/assets/images/icon-twitter.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion src/server/routes/gSheet.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable max-len */
/**
* The routes related to GSheets integration
*/
Expand All @@ -14,6 +15,7 @@ const routes = express.Router();
routes.use(cors());
routes.options('*', cors());

routes.get('/:id', (req, res) => new GSheetService().getSheet(req, res));
routes.get('/:id', (req, res) => new GSheetService().getSheetAPI(req, res));
// routes.post('/:id', (req, res) => new GSheetService().addToSheetAPI(req, res)); // Enable it for API access to gsheets editing when needed

export default routes;
87 changes: 85 additions & 2 deletions src/server/services/gSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ const getCircularReplacer = () => {
* APIs in the same uniform manner.
*/
export default class GSheetService {
constructor() {
this.getSheetAPI = this.getSheetAPI.bind(this);
this.addToSheetAPI = this.getSheetAPI.bind(this);
this.addToSheet = this.getSheetAPI.bind(this);
}

/**
* getSheet
* @param {Object} req the request
* @param {Object} res the response
*/
async getSheet(req, res) {
async getSheetAPI(req, res) {
const { index } = req.query;
const { id } = req.params;
const doc = new GoogleSpreadsheet(id);
Expand All @@ -45,8 +51,85 @@ export default class GSheetService {
rows: JSON.parse(rowsJson),
});
} catch (e) {
res.status((e.response && e.response.status) || 500);
const status = (e.response && e.response.status) || 500;
if (status === 429) {
// rate limit issue - wait 15sec and retry
await new Promise(resolve => setTimeout(resolve, 15000));
return this.getSheetAPI(req, res);
}
res.status(status);
return res.send((e.response && e.response.data) || { ...e, message: e.message });
}
}

/**
* Adds rows to gsheet by ID
* Needs to be shared with the service account to work
* This is the controler method with req/res objects
* @param {Object} req the request
* @param {Object} res the response
*/
async addToSheetAPI(req, res) {
const { index } = req.query;
const { id } = req.params;
const doc = new GoogleSpreadsheet(id);
try {
// set credentials for working
await doc.useServiceAccountAuth({
client_email: config.GOOGLE_SERVICE_ACCOUNT_EMAIL,
private_key: config.GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY.replace(/\\m/g, '\n'),
});
// load doc infos
await doc.loadInfo();
// get 1st sheet
const sheet = doc.sheetsByIndex[index || 0];
const moreRows = await sheet.addRows(req.body);
const rowsJson = JSON.stringify(moreRows, getCircularReplacer());
return res.send({
rows: JSON.parse(rowsJson),
});
} catch (e) {
const status = (e.response && e.response.status) || 500;
if (status === 429) {
// rate limit issue - wait 15sec and retry
await new Promise(resolve => setTimeout(resolve, 15000));
return this.addToSheetAPI(req, res);
}
res.status(status);
return res.send((e.response && e.response.data) || { ...e, message: e.message });
}
}

/**
* Adds rows to gsheet by ID
* Needs to be shared with the service account to work
* @param {string} id the doc id
* @param {Array} paylod the body to send
* @param {string} index sheet index in the doc. Defaults to 0
*/
async addToSheet(id, payload, index = 0) {
const doc = new GoogleSpreadsheet(id);
try {
// set credentials for working
await doc.useServiceAccountAuth({
client_email: config.GOOGLE_SERVICE_ACCOUNT_EMAIL,
private_key: config.GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY.replace(/\\m/g, '\n'),
});
// load doc infos
await doc.loadInfo();
// get 1st sheet
const sheet = doc.sheetsByIndex[index || 0];
const moreRows = await sheet.addRows(payload);
const rowsJson = JSON.stringify(moreRows, getCircularReplacer());
return rowsJson;
} catch (e) {
const status = (e.response && e.response.status) || 500;
if (status === 429) {
// rate limit issue - wait 15sec and retry
await new Promise(resolve => setTimeout(resolve, 15000));
return this.addToSheet(id, payload, index);
}
return e;
}
}
}
52 changes: 51 additions & 1 deletion src/server/services/growsurf.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@ export default class GrowsurfService {
};
}

/**
* Gets get participant.
* @return {Promise}
* @param {String} idOrEmail growsurf id or email
*/
async getParticipantByIdOREmail(idOrEmail) {
const response = await fetch(`${this.private.baseUrl}/campaign/${config.GROWSURF_CAMPAIGN_ID}/participant/${idOrEmail}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: this.private.authorization,
},
});
if (response.status >= 300) {
return {
error: await response.json(),
code: response.status,
url: `${this.private.baseUrl}/campaign/${config.GROWSURF_CAMPAIGN_ID}/participant/${idOrEmail}`,
};
}
const data = await response.json();
return data;
}

/**
* Gets get participant by email or id.
* @return {Promise}
Expand Down Expand Up @@ -67,7 +91,6 @@ export default class GrowsurfService {
code: response.status,
url: `${this.private.baseUrl}/campaign/${config.GROWSURF_CAMPAIGN_ID}/participant`,
body,
private: this.private, // to remove in final release
};
}
const data = await response.json();
Expand Down Expand Up @@ -96,4 +119,31 @@ export default class GrowsurfService {
}
return result;
}

/**
* Update participant in growSurf
* @param {string} idOrEmail id or email
* @param {string} body payload
* @returns {Promise}
*/
async updateParticipant(idOrEmail, body) {
const response = await fetch(`${this.private.baseUrl}/campaign/${config.GROWSURF_CAMPAIGN_ID}/participant/${idOrEmail}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: this.private.authorization,
},
body,
});
if (response.status >= 300) {
return {
error: await response.json(),
code: response.status,
url: `${this.private.baseUrl}/campaign/${config.GROWSURF_CAMPAIGN_ID}/participant/${idOrEmail}`,
body,
};
}
const data = await response.json();
return data;
}
}
Loading