From f235672057542af7055e7206d428a2675df8f855 Mon Sep 17 00:00:00 2001 From: tkla Date: Tue, 31 May 2022 17:58:41 -0700 Subject: [PATCH 01/20] Enable workSchedule API for SDK --- lib/EnvoyAPI.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/EnvoyAPI.js b/lib/EnvoyAPI.js index a3e54d2..086b9cd 100644 --- a/lib/EnvoyAPI.js +++ b/lib/EnvoyAPI.js @@ -236,6 +236,20 @@ class EnvoyAPI { return EnvoyAPI.getDataFromBody(body); } + /** + * + * @param {{}} workSchedule - Work object with locationId, email, expectedArrivalAt (UTC date time format) + */ + async workSchedule(workSchedule) { + const body = await this.request({ + method: 'POST', + url: `/api/v1/work-schedules`, + body: workSchedule, + }); + + return EnvoyAPI.getDataFromBody(body); + } + /** * Creates an invite. * From 89c09564d37122c613e4c248b8f056233aaf7e41 Mon Sep 17 00:00:00 2001 From: tkla Date: Thu, 2 Jun 2022 23:38:28 -0700 Subject: [PATCH 02/20] Update workSchedule name and description --- lib/EnvoyAPI.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/EnvoyAPI.js b/lib/EnvoyAPI.js index 086b9cd..b665f91 100644 --- a/lib/EnvoyAPI.js +++ b/lib/EnvoyAPI.js @@ -237,16 +237,16 @@ class EnvoyAPI { } /** - * + * Create a new work schedule for an employee. * @param {{}} workSchedule - Work object with locationId, email, expectedArrivalAt (UTC date time format) */ - async workSchedule(workSchedule) { + async createWorkSchedule(workSchedule) { const body = await this.request({ method: 'POST', url: `/api/v1/work-schedules`, body: workSchedule, }); - + return EnvoyAPI.getDataFromBody(body); } From 1d68fa8511ade771614e1418a51e81a2e1d5d458 Mon Sep 17 00:00:00 2001 From: tkla Date: Fri, 3 Jun 2022 00:40:55 -0700 Subject: [PATCH 03/20] Add GET workSchedules request to EnvoyAPI --- lib/EnvoyAPI.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/EnvoyAPI.js b/lib/EnvoyAPI.js index b665f91..dc42990 100644 --- a/lib/EnvoyAPI.js +++ b/lib/EnvoyAPI.js @@ -236,6 +236,19 @@ class EnvoyAPI { return EnvoyAPI.getDataFromBody(body); } + /** + * Fetches a list of WorkSchedules. + * @param {*} workSchedule + */ + async workSchedules(workSchedule) { + const body = await this.request({ + method: 'GET', + url: `/api/v1/work-schedules`, + body: workSchedule, + }); + + return EnvoyAPI.getDataFromBody(body); + } /** * Create a new work schedule for an employee. * @param {{}} workSchedule - Work object with locationId, email, expectedArrivalAt (UTC date time format) From 5689d31ef780d43d2e88c3b51edd82fd6273a2dc Mon Sep 17 00:00:00 2001 From: tkla Date: Fri, 3 Jun 2022 02:40:50 -0700 Subject: [PATCH 04/20] Adjusted GET workSchedules to use its own request options rather than the default options --- lib/EnvoyAPI.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/EnvoyAPI.js b/lib/EnvoyAPI.js index dc42990..dcfd693 100644 --- a/lib/EnvoyAPI.js +++ b/lib/EnvoyAPI.js @@ -45,6 +45,7 @@ class EnvoyAPI { throw new Error('No token supplied.'); } + this.token = token; this.baseUrl = process.env.ENVOY_BASE_URL || 'https://app.envoy.com'; this.request = request.defaults({ headers: { @@ -109,7 +110,7 @@ class EnvoyAPI { async company() { const body = await this.request({ - url: '/api/v2/companies', + url: '/api/v1/companies', }); return EnvoyAPI.getDataFromBody(body); @@ -238,13 +239,21 @@ class EnvoyAPI { /** * Fetches a list of WorkSchedules. - * @param {*} workSchedule + * @param {{}} workSchedule object. */ async workSchedules(workSchedule) { - const body = await this.request({ - method: 'GET', - url: `/api/v1/work-schedules`, - body: workSchedule, + var options = { + 'method': 'GET', + 'url': 'https://api.envoy.com/v1/work-schedules', + 'headers': { + 'Authorization': 'Bearer ' + this.token + }, + json: true, + }; + + const body = await request(options, function (error, response) { + if (error) throw new Error(error); + response.body; }); return EnvoyAPI.getDataFromBody(body); From 00b4988ffd9c8fee3e7d4c7d83485f727505c9c8 Mon Sep 17 00:00:00 2001 From: tkla Date: Fri, 3 Jun 2022 13:32:38 -0700 Subject: [PATCH 05/20] Add companies api to EnvoyAPI --- lib/EnvoyAPI.js | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/EnvoyAPI.js b/lib/EnvoyAPI.js index dcfd693..e219e12 100644 --- a/lib/EnvoyAPI.js +++ b/lib/EnvoyAPI.js @@ -88,7 +88,7 @@ class EnvoyAPI { return EnvoyAPI.getDataFromBody(body); } - + async locations() { const body = await this.request({ @@ -97,7 +97,7 @@ class EnvoyAPI { return EnvoyAPI.getDataFromBody(body); } - + async location(locationId) { const body = await this.request({ @@ -106,7 +106,7 @@ class EnvoyAPI { return EnvoyAPI.getDataFromBody(body); } - + async company() { const body = await this.request({ @@ -116,6 +116,28 @@ class EnvoyAPI { return EnvoyAPI.getDataFromBody(body); } + /** + * Retrieve details about an organization or account. + * @returns {Promise} + */ + async companies() { + var options = { + 'method': 'GET', + 'url': 'https://api.envoy.com/v1/companies', + 'headers': { + 'Authorization': 'Bearer ' + this.token + }, + json: true, + }; + + const body = await request(options, function (error, response) { + if (error) throw new Error(error); + response.body; + }); + + return EnvoyAPI.getDataFromBody(body); + } + async me() { const body = await this.request({ @@ -246,7 +268,7 @@ class EnvoyAPI { 'method': 'GET', 'url': 'https://api.envoy.com/v1/work-schedules', 'headers': { - 'Authorization': 'Bearer ' + this.token + 'Authorization': 'Bearer ' + this.token }, json: true, }; @@ -511,4 +533,4 @@ class EnvoyAPI { } } -module.exports = EnvoyAPI; +module.exports = EnvoyAPI; \ No newline at end of file From 61c2150de8698acef6ac57a48f69793648cc4c07 Mon Sep 17 00:00:00 2001 From: tkla Date: Fri, 3 Jun 2022 15:14:14 -0700 Subject: [PATCH 06/20] Add GET /entry --- lib/EnvoyAPI.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/lib/EnvoyAPI.js b/lib/EnvoyAPI.js index e219e12..23aa1b6 100644 --- a/lib/EnvoyAPI.js +++ b/lib/EnvoyAPI.js @@ -165,6 +165,35 @@ class EnvoyAPI { return EnvoyAPI.getDataFromBody(body); } + + /** + * Import employee records to the Employee Directory. The file headers must be in the following order, Full Name, Email, Mobile Number, Assistants Email. + * If your csv does not contain email entries, it will override the previous employee directory in full. + * Possibly deprecated? This route doesn't seem to exist anymore. + * @param {file} file + * @param {string} api_key + * @returns + */ + async importEmployeeRecords(file, api_key) { + var options = { + 'method': 'GET', + 'url': `https://app.envoy.com/api/configuration/employee_list/${api_key}`, + 'headers': { + 'Authorization': 'Bearer ' + this.token + }, + body: file, + json: true, + }; + + const body = await request(options, function (error, response) { + if (error) throw new Error(error); + response.body; + }); + + return EnvoyAPI.getDataFromBody(body); + } + + /** * Fetches the employees for this location. * @@ -259,6 +288,28 @@ class EnvoyAPI { return EnvoyAPI.getDataFromBody(body); } + /** + * Fetches an entry by id. + * @param {string | number}} Entry Id + */ + async entry(entryId) { + var options = { + 'method': 'GET', + 'url': `https://app.envoy.com/a/visitors/api/v2/entries/${entryId}`, + 'headers': { + 'Authorization': 'Bearer ' + this.token + }, + json: true, + }; + + const body = await request(options, function (error, response) { + if (error) throw new Error(error); + response.body; + }); + + return EnvoyAPI.getDataFromBody(body); + } + /** * Fetches a list of WorkSchedules. * @param {{}} workSchedule object. @@ -270,6 +321,7 @@ class EnvoyAPI { 'headers': { 'Authorization': 'Bearer ' + this.token }, + body: workSchedule, json: true, }; @@ -280,6 +332,7 @@ class EnvoyAPI { return EnvoyAPI.getDataFromBody(body); } + /** * Create a new work schedule for an employee. * @param {{}} workSchedule - Work object with locationId, email, expectedArrivalAt (UTC date time format) @@ -533,4 +586,4 @@ class EnvoyAPI { } } -module.exports = EnvoyAPI; \ No newline at end of file +module.exports = EnvoyAPI; From 0a8ef1a4ac540f6607b55c1fe088fca1a12a7ebd Mon Sep 17 00:00:00 2001 From: tkla Date: Mon, 6 Jun 2022 16:16:41 -0700 Subject: [PATCH 07/20] Add GET, POST, and PATCH createEntry --- lib/EnvoyAPI.js | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/lib/EnvoyAPI.js b/lib/EnvoyAPI.js index 23aa1b6..9899cb9 100644 --- a/lib/EnvoyAPI.js +++ b/lib/EnvoyAPI.js @@ -293,23 +293,44 @@ class EnvoyAPI { * @param {string | number}} Entry Id */ async entry(entryId) { - var options = { - 'method': 'GET', - 'url': `https://app.envoy.com/a/visitors/api/v2/entries/${entryId}`, - 'headers': { - 'Authorization': 'Bearer ' + this.token - }, - json: true, - }; + const body = await this.request({ + method: 'GET', + url: `/a/visitors/api/v2/entries/${entryId}` + }); - const body = await request(options, function (error, response) { - if (error) throw new Error(error); - response.body; + return EnvoyAPI.getDataFromBody(body); + } + /** + * @param {string | number} entryId + * @param {{}} entry + * @returns {Promise} + */ + async patchEntry(entryId, entry) { + const body = await this.request({ + method: 'PATCH', + url: `/a/visitors/api/v2/entries/${entry['entry-id']}`, + body: entry }); return EnvoyAPI.getDataFromBody(body); } + /** + * @param {{}} entry + * @returns {Promise} + */ + async createEntry(entry) { + const body = await this.request({ + method: 'POST', + url: `/a/visitors/api/v2/entries`, + body: entry + }) + + // Currently the route returns only a status code 204 for success. For now just return object indicating success. + return { "message" : "Success" }; + } + + /** * Fetches a list of WorkSchedules. * @param {{}} workSchedule object. From 02964bcd8ceb7c273c7d04366e45420a57e823a3 Mon Sep 17 00:00:00 2001 From: tkla Date: Mon, 6 Jun 2022 16:42:09 -0700 Subject: [PATCH 08/20] Add GET entries by date --- lib/EnvoyAPI.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/EnvoyAPI.js b/lib/EnvoyAPI.js index 9899cb9..8dd0674 100644 --- a/lib/EnvoyAPI.js +++ b/lib/EnvoyAPI.js @@ -300,6 +300,31 @@ class EnvoyAPI { return EnvoyAPI.getDataFromBody(body); } + + /** + * + * @param {*} entry Query params in addition to dates to filter by. + * entry = { + * location: { number }, + * limit: { number }, + * offset: { number }, + * start_date: { string }, + * end_date: { string } + * } + * + * start_date: '1900-01-31' (Example date String, same format for end_date) + * @returns {Promise} + */ + async getEntriesByDate(entry) { + let params = `?filter[location]=${entry.location}&page[limit]=${entry.limit}&page[offset]=${entry.offset}&start_date=${entry.start_date}&end_date=${entry.end_date}` + const body = await this.request({ + method: 'GET', + url: `/a/visitors/api/v2/entries/${params}` + }); + + return EnvoyAPI.getDataFromBody(body); + } + /** * @param {string | number} entryId * @param {{}} entry From f768af5c0f3629cfd460d3934db70fcad0c445ca Mon Sep 17 00:00:00 2001 From: tkla Date: Mon, 6 Jun 2022 19:05:00 -0700 Subject: [PATCH 09/20] Updated Urls for workschedule APIs, these will possibly be deprecated soon when v3 APIs are release --- lib/EnvoyAPI.js | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/lib/EnvoyAPI.js b/lib/EnvoyAPI.js index 8dd0674..3495883 100644 --- a/lib/EnvoyAPI.js +++ b/lib/EnvoyAPI.js @@ -352,9 +352,30 @@ class EnvoyAPI { }) // Currently the route returns only a status code 204 for success. For now just return object indicating success. - return { "message" : "Success" }; + return { "message": "Success" }; } + /** + * Fetches a WorkSchedule. + * @param { number | string } workSchedule Id. + */ + async workSchedule(workScheduleId) { + var options = { + method: 'GET', + url: `https://api.envoy.com/v1/work-schedules/${workScheduleId}`, + headers: { + Authorization: 'Bearer ' + this.token + }, + json: true, + }; + + const body = await request(options, function (error, response) { + if (error) throw new Error(error); + response.body; + }); + + return EnvoyAPI.getDataFromBody(body); + } /** * Fetches a list of WorkSchedules. @@ -362,10 +383,10 @@ class EnvoyAPI { */ async workSchedules(workSchedule) { var options = { - 'method': 'GET', - 'url': 'https://api.envoy.com/v1/work-schedules', - 'headers': { - 'Authorization': 'Bearer ' + this.token + method: 'GET', + url: 'https://api.envoy.com/v1/work-schedules', + headers: { + Authorization: 'Bearer ' + this.token }, body: workSchedule, json: true, @@ -384,10 +405,19 @@ class EnvoyAPI { * @param {{}} workSchedule - Work object with locationId, email, expectedArrivalAt (UTC date time format) */ async createWorkSchedule(workSchedule) { - const body = await this.request({ + var options = { method: 'POST', - url: `/api/v1/work-schedules`, + url: `https://api.envoy.com/v1/work-schedules`, + headers: { + Authorization: 'Bearer ' + this.token + }, body: workSchedule, + json: true, + }; + + const body = await request(options, function (error, response) { + if (error) throw new Error(error); + response.body; }); return EnvoyAPI.getDataFromBody(body); From a21d4c42f6489eb0af3d19ba30f825d5d64cf34a Mon Sep 17 00:00:00 2001 From: tkla Date: Mon, 6 Jun 2022 19:50:07 -0700 Subject: [PATCH 10/20] Add DELETE work schedule --- lib/EnvoyAPI.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lib/EnvoyAPI.js b/lib/EnvoyAPI.js index 3495883..e2b21c1 100644 --- a/lib/EnvoyAPI.js +++ b/lib/EnvoyAPI.js @@ -358,6 +358,7 @@ class EnvoyAPI { /** * Fetches a WorkSchedule. * @param { number | string } workSchedule Id. + * @returns {Promise} */ async workSchedule(workScheduleId) { var options = { @@ -380,6 +381,7 @@ class EnvoyAPI { /** * Fetches a list of WorkSchedules. * @param {{}} workSchedule object. + * @returns {Promise} */ async workSchedules(workSchedule) { var options = { @@ -403,6 +405,7 @@ class EnvoyAPI { /** * Create a new work schedule for an employee. * @param {{}} workSchedule - Work object with locationId, email, expectedArrivalAt (UTC date time format) + * @returns {Promise} */ async createWorkSchedule(workSchedule) { var options = { @@ -423,6 +426,31 @@ class EnvoyAPI { return EnvoyAPI.getDataFromBody(body); } + /** + * Deletes a work schedule item. Returns prev item to be deleted on success. + * @param {{}} workSchedule object. + * @returns {Promise} + */ + deleteWorkSchedule = async (workScheduleId) => { + var options = { + method: 'DELETE', + url: `https://api.envoy.com/v1/work-schedules/${workScheduleId}`, + headers: { + Authorization: 'Bearer ' + this.token + }, + json: true + }; + + const body = await this.workSchedule(workScheduleId); + await request(options, function (error, response) { + if (error) throw new Error(error); + response.body; + }); + + body.message = "Work Schedule has been removed."; + return body; + } + /** * Creates an invite. * From af279c95dcd20d0bd096cadedbd9177b4e8998e1 Mon Sep 17 00:00:00 2001 From: tkla Date: Mon, 6 Jun 2022 20:32:35 -0700 Subject: [PATCH 11/20] Add check in and check out for work schedule --- lib/EnvoyAPI.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/lib/EnvoyAPI.js b/lib/EnvoyAPI.js index e2b21c1..9670efe 100644 --- a/lib/EnvoyAPI.js +++ b/lib/EnvoyAPI.js @@ -431,7 +431,7 @@ class EnvoyAPI { * @param {{}} workSchedule object. * @returns {Promise} */ - deleteWorkSchedule = async (workScheduleId) => { + deleteWorkSchedule = async (workScheduleId) => { var options = { method: 'DELETE', url: `https://api.envoy.com/v1/work-schedules/${workScheduleId}`, @@ -446,11 +446,59 @@ class EnvoyAPI { if (error) throw new Error(error); response.body; }); - + body.message = "Work Schedule has been removed."; return body; } + /** + * Check in a certain work schedule item. + * @param { number | string } workSchedule Id + * @returns {Promise} + */ + async checkInWork(workScheduleId) { + var options = { + method: 'POST', + url: `https://api.envoy.com/v1/work-schedules/${workScheduleId}/checkin`, + headers: { + Authorization: 'Bearer ' + this.token + }, + json: true + }; + + const body = await request(options, function (error, response) { + if (error) throw new Error(error); + response.body; + }); + + // Returns status 204 so body has no content. + return { message: 'Successfully checked in.' } + } + + /** + * Check out a certain work schedule item. + * @param { number | string } workSchedule Id + * @returns {Promise} + */ + async checkOutWork(workScheduleId) { + var options = { + method: 'POST', + url: `https://api.envoy.com/v1/work-schedules/${workScheduleId}/checkout`, + headers: { + Authorization: 'Bearer ' + this.token + }, + json: true + }; + + const body = await request(options, function (error, response) { + if (error) throw new Error(error); + response.body; + }); + + // Returns status 204 so body has no content. + return { message: 'Successfully checked out.' } + } + /** * Creates an invite. * @@ -690,4 +738,4 @@ class EnvoyAPI { } } -module.exports = EnvoyAPI; +module.exports = EnvoyAPI; \ No newline at end of file From 828546ad4c6676ec0767d0ee126f5f76ad637bfb Mon Sep 17 00:00:00 2001 From: tkla Date: Tue, 7 Jun 2022 11:30:05 -0700 Subject: [PATCH 12/20] Add GET invites for both singular and multiple invites. Updated new API url to be stored as this.newUrl in EnvoyAPI --- lib/EnvoyAPI.js | 63 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 8 deletions(-) diff --git a/lib/EnvoyAPI.js b/lib/EnvoyAPI.js index 9670efe..0cb9469 100644 --- a/lib/EnvoyAPI.js +++ b/lib/EnvoyAPI.js @@ -47,6 +47,7 @@ class EnvoyAPI { this.token = token; this.baseUrl = process.env.ENVOY_BASE_URL || 'https://app.envoy.com'; + this.newUrl = 'https://api.envoy.com/v1'; this.request = request.defaults({ headers: { Authorization: `Bearer ${token}`, @@ -123,7 +124,7 @@ class EnvoyAPI { async companies() { var options = { 'method': 'GET', - 'url': 'https://api.envoy.com/v1/companies', + 'url': this.newUrl + '/companies', 'headers': { 'Authorization': 'Bearer ' + this.token }, @@ -363,7 +364,7 @@ class EnvoyAPI { async workSchedule(workScheduleId) { var options = { method: 'GET', - url: `https://api.envoy.com/v1/work-schedules/${workScheduleId}`, + url: this.newUrl + `/work-schedules/${workScheduleId}`, headers: { Authorization: 'Bearer ' + this.token }, @@ -386,14 +387,14 @@ class EnvoyAPI { async workSchedules(workSchedule) { var options = { method: 'GET', - url: 'https://api.envoy.com/v1/work-schedules', + url: this.newUrl + '/work-schedules', headers: { Authorization: 'Bearer ' + this.token }, body: workSchedule, json: true, }; - + console.log(options.url); const body = await request(options, function (error, response) { if (error) throw new Error(error); response.body; @@ -410,7 +411,7 @@ class EnvoyAPI { async createWorkSchedule(workSchedule) { var options = { method: 'POST', - url: `https://api.envoy.com/v1/work-schedules`, + url: this.newUrl + `/work-schedules`, headers: { Authorization: 'Bearer ' + this.token }, @@ -434,7 +435,7 @@ class EnvoyAPI { deleteWorkSchedule = async (workScheduleId) => { var options = { method: 'DELETE', - url: `https://api.envoy.com/v1/work-schedules/${workScheduleId}`, + url: this.newUrl + `/work-schedules/${workScheduleId}`, headers: { Authorization: 'Bearer ' + this.token }, @@ -459,7 +460,7 @@ class EnvoyAPI { async checkInWork(workScheduleId) { var options = { method: 'POST', - url: `https://api.envoy.com/v1/work-schedules/${workScheduleId}/checkin`, + url: this.newUrl + `/work-schedules/${workScheduleId}/checkin`, headers: { Authorization: 'Bearer ' + this.token }, @@ -483,7 +484,7 @@ class EnvoyAPI { async checkOutWork(workScheduleId) { var options = { method: 'POST', - url: `https://api.envoy.com/v1/work-schedules/${workScheduleId}/checkout`, + url: this.newUrl + `/work-schedules/${workScheduleId}/checkout`, headers: { Authorization: 'Bearer ' + this.token }, @@ -499,6 +500,52 @@ class EnvoyAPI { return { message: 'Successfully checked out.' } } + /** + * @param { number|string } Invite ID. + * @returns {Promise} + */ + async getInvite(inviteId) { + var options = { + method: 'GET', + url: this.newUrl + `/invites/${inviteId}`, + headers: { + Authorization: 'Bearer ' + this.token + }, + json: true + }; + + const body = await request(options, function (error, response) { + if (error) throw new Error(error); + response.body; + }); + + return EnvoyAPI.getDataFromBody(body); + } + + + /** + * @param { {} } invite Object containing query params. + * @returns {Promise} + */ + async getInvites(invite){ + var options = { + method: 'GET', + url: this.newUrl + `/invites`, + headers: { + Authorization: 'Bearer ' + this.token + }, + qs: invite, + json: true + }; + + const body = await request(options, function (error, response) { + if (error) throw new Error(error); + response.body; + }); + + return EnvoyAPI.getDataFromBody(body); + } + /** * Creates an invite. * From 69702c3079e033529dd5a847dd2a05cd04a4d14d Mon Sep 17 00:00:00 2001 From: tkla Date: Tue, 7 Jun 2022 15:20:00 -0700 Subject: [PATCH 13/20] Add createInviteV1 to create invites using new route --- lib/EnvoyAPI.js | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/lib/EnvoyAPI.js b/lib/EnvoyAPI.js index 0cb9469..c192e83 100644 --- a/lib/EnvoyAPI.js +++ b/lib/EnvoyAPI.js @@ -48,6 +48,9 @@ class EnvoyAPI { this.token = token; this.baseUrl = process.env.ENVOY_BASE_URL || 'https://app.envoy.com'; this.newUrl = 'https://api.envoy.com/v1'; + /* + Request library is deprecated as of 2020, we should update to another REST library soon. + */ this.request = request.defaults({ headers: { Authorization: `Bearer ${token}`, @@ -58,6 +61,21 @@ class EnvoyAPI { json: true, baseUrl: this.baseUrl, }); + + /* + Setting ENVOY_BASE_URL in .env to point to the new API endpoint will break some methods below relying on the old route. + A temporary workaround for now is to set a new Request default with the updated endpoint. + */ + // this.requestNewRoute = request.defaults({ + // headers: { + // Authorization: `Bearer ${token}`, + // 'Content-Type': 'application/vnd.api+json', + // Accept: 'application/vnd.api+json', + // 'X-Envoy-Context': JSON.stringify(xEnvoyContext), + // }, + // json: true, + // baseUrl: this.newUrl, + // }) } /** @@ -123,10 +141,10 @@ class EnvoyAPI { */ async companies() { var options = { - 'method': 'GET', - 'url': this.newUrl + '/companies', - 'headers': { - 'Authorization': 'Bearer ' + this.token + method: 'GET', + url: this.newUrl + '/companies', + headers: { + Authorization: 'Bearer ' + this.token }, json: true, }; @@ -524,7 +542,7 @@ class EnvoyAPI { /** - * @param { {} } invite Object containing query params. + * @param { {} } invite Object containing query params. See invites API documentation for list of all possbile Query params. * @returns {Promise} */ async getInvites(invite){ @@ -562,7 +580,24 @@ class EnvoyAPI { return EnvoyAPI.getDataFromBody(body); } + + async createInviteV1(invite) { + var options = { + method: 'GET', + url: this.newUrl + `/invites`, + headers: { + Authorization: 'Bearer ' + this.token + }, + body: invite, + json: true + }; + const body = await request(options, function (error, response) { + if (error) throw new Error(error); + response.body; + }); + } + /** * Updates an invite. * @@ -785,4 +820,4 @@ class EnvoyAPI { } } -module.exports = EnvoyAPI; \ No newline at end of file +module.exports = EnvoyAPI; From 9295232e0d5b8df9570cae2d7730d631955e672d Mon Sep 17 00:00:00 2001 From: tkla Date: Wed, 8 Jun 2022 10:13:54 -0700 Subject: [PATCH 14/20] Update createInviteV1 to send back request body --- lib/EnvoyAPI.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/EnvoyAPI.js b/lib/EnvoyAPI.js index c192e83..92f974a 100644 --- a/lib/EnvoyAPI.js +++ b/lib/EnvoyAPI.js @@ -583,7 +583,7 @@ class EnvoyAPI { async createInviteV1(invite) { var options = { - method: 'GET', + method: 'POST', url: this.newUrl + `/invites`, headers: { Authorization: 'Bearer ' + this.token @@ -596,6 +596,8 @@ class EnvoyAPI { if (error) throw new Error(error); response.body; }); + + return EnvoyAPI.getDataFromBody(body); } /** @@ -820,4 +822,4 @@ class EnvoyAPI { } } -module.exports = EnvoyAPI; +module.exports = EnvoyAPI; \ No newline at end of file From 7fd24d343993c478fb1ed4c2a2bc79ceae13c864 Mon Sep 17 00:00:00 2001 From: tkla Date: Wed, 8 Jun 2022 10:35:06 -0700 Subject: [PATCH 15/20] Add updateInviteV1 --- lib/EnvoyAPI.js | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/EnvoyAPI.js b/lib/EnvoyAPI.js index 92f974a..1fae8d8 100644 --- a/lib/EnvoyAPI.js +++ b/lib/EnvoyAPI.js @@ -581,6 +581,12 @@ class EnvoyAPI { return EnvoyAPI.getDataFromBody(body); } + /** + * Create an invite using the V1 API endpoint. + * + * @param {{}} invite + * @returns {Promise} + */ async createInviteV1(invite) { var options = { method: 'POST', @@ -599,7 +605,33 @@ class EnvoyAPI { return EnvoyAPI.getDataFromBody(body); } - + + /** + * Updates an invite. + * + * @param { number|string } inviteId + * @param {{}} invite + * @returns {Promise} + */ + async updateInviteV1(inviteId, invite) { + var options = { + method: 'POST', + url: this.newUrl + `/invites/${inviteId}`, + headers: { + Authorization: 'Bearer ' + this.token + }, + body: invite, + json: true + }; + + const body = await request(options, function (error, response) { + if (error) throw new Error(error); + response.body; + }); + + return EnvoyAPI.getDataFromBody(body); + } + /** * Updates an invite. * From 3f580af32f730e18f565f6bbb07677f8b8cb0642 Mon Sep 17 00:00:00 2001 From: tkla Date: Wed, 8 Jun 2022 10:59:34 -0700 Subject: [PATCH 16/20] Add removeInviteV1 --- lib/EnvoyAPI.js | 67 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 22 deletions(-) diff --git a/lib/EnvoyAPI.js b/lib/EnvoyAPI.js index 1fae8d8..96f23d5 100644 --- a/lib/EnvoyAPI.js +++ b/lib/EnvoyAPI.js @@ -609,11 +609,30 @@ class EnvoyAPI { /** * Updates an invite. * + * @param inviteId + * @param {{}} invite + * @returns {Promise} + */ + async updateInvite(inviteId, invite) { + + // eslint-disable-next-line no-param-reassign + invite.data.id = inviteId; + const body = await this.request({ + method: 'PUT', + url: `/api/v3/invites/${inviteId}`, + body: invite, + }); + + return EnvoyAPI.getDataFromBody(body); + } + /** + * Updates an invite using the V1 API endpoint. + * * @param { number|string } inviteId * @param {{}} invite * @returns {Promise} */ - async updateInviteV1(inviteId, invite) { + async updateInviteV1(inviteId, invite) { var options = { method: 'POST', url: this.newUrl + `/invites/${inviteId}`, @@ -632,26 +651,6 @@ class EnvoyAPI { return EnvoyAPI.getDataFromBody(body); } - /** - * Updates an invite. - * - * @param inviteId - * @param {{}} invite - * @returns {Promise} - */ - async updateInvite(inviteId, invite) { - - // eslint-disable-next-line no-param-reassign - invite.data.id = inviteId; - const body = await this.request({ - method: 'PUT', - url: `/api/v3/invites/${inviteId}`, - body: invite, - }); - - return EnvoyAPI.getDataFromBody(body); - } - /** * Updates an invite. * @@ -673,7 +672,7 @@ class EnvoyAPI { } /** - * Removes an invite. + * Removes an invite * * @param inviteId * @returns {Promise} @@ -685,6 +684,30 @@ class EnvoyAPI { }); } + /** + * Removes an invite using the V1 API endpoint. + * + * @param { number|string } inviteId + * @returns {Promise} + */ + async removeInviteV1(inviteId) { + var options = { + method: 'DELETE', + url: this.newUrl + `/invites/${inviteId}`, + headers: { + Authorization: 'Bearer ' + this.token + }, + json: true + }; + + const body = await request(options, function (error, response) { + if (error) throw new Error(error); + response.body; + }); + + return EnvoyAPI.getDataFromBody(body); + } + /** * Updates the job. * From eb540766dcc5ad7d2c501083e0cadf57769a52ef Mon Sep 17 00:00:00 2001 From: tkla Date: Wed, 8 Jun 2022 11:22:00 -0700 Subject: [PATCH 17/20] Add axios package. --- lib/EnvoyAPI.js | 1 + package-lock.json | 26 ++++++++++++++++++++++++++ package.json | 1 + 3 files changed, 28 insertions(+) diff --git a/lib/EnvoyAPI.js b/lib/EnvoyAPI.js index 96f23d5..73abf7d 100644 --- a/lib/EnvoyAPI.js +++ b/lib/EnvoyAPI.js @@ -1,4 +1,5 @@ const request = require('request-promise-native'); +const axios = require('axios'); const EnvoyResponseError = require('./EnvoyResponseError'); /** diff --git a/package-lock.json b/package-lock.json index c98ae93..bec9535 100644 --- a/package-lock.json +++ b/package-lock.json @@ -199,6 +199,27 @@ "resolved": "https://registry.npmjs.org/aws4/-/aws4-1.8.0.tgz", "integrity": "sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ==" }, + "axios": { + "version": "0.27.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.27.2.tgz", + "integrity": "sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==", + "requires": { + "follow-redirects": "^1.14.9", + "form-data": "^4.0.0" + }, + "dependencies": { + "form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + } + } + } + }, "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", @@ -1101,6 +1122,11 @@ "integrity": "sha512-a1hQMktqW9Nmqr5aktAux3JMNqaucxGcjtjWnZLHX7yyPCmlSV3M54nGYbqT8K+0GhF3NBgmJCc3ma+WOgX8Jg==", "dev": true }, + "follow-redirects": { + "version": "1.15.1", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.1.tgz", + "integrity": "sha512-yLAMQs+k0b2m7cVxpS1VKJVvoz7SS9Td1zss3XRwXj+ZDH00RJgnuLx7E44wx02kQLrdM3aOOy+FpzS7+8OizA==" + }, "forever-agent": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/forever-agent/-/forever-agent-0.6.1.tgz", diff --git a/package.json b/package.json index 0514148..107ae85 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ }, "homepage": "https://github.com/envoy/envoy-integrations-sdk-nodejs#readme", "dependencies": { + "axios": "^0.27.2", "body-parser": "^1.19.0", "dotenv": "^8.1.0", "jsonwebtoken": "^8.5.1", From db82bd4f62de5fc68641e8662ac7ab71f9d6448e Mon Sep 17 00:00:00 2001 From: tkla Date: Wed, 8 Jun 2022 14:38:22 -0700 Subject: [PATCH 18/20] Add reservations APIs --- lib/EnvoyAPI.js | 182 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 160 insertions(+), 22 deletions(-) diff --git a/lib/EnvoyAPI.js b/lib/EnvoyAPI.js index 73abf7d..cd25ce3 100644 --- a/lib/EnvoyAPI.js +++ b/lib/EnvoyAPI.js @@ -49,9 +49,8 @@ class EnvoyAPI { this.token = token; this.baseUrl = process.env.ENVOY_BASE_URL || 'https://app.envoy.com'; this.newUrl = 'https://api.envoy.com/v1'; - /* - Request library is deprecated as of 2020, we should update to another REST library soon. - */ + + //Request library is deprecated as of 2020. Proposing that we use Axios for future requests. this.request = request.defaults({ headers: { Authorization: `Bearer ${token}`, @@ -65,17 +64,18 @@ class EnvoyAPI { /* Setting ENVOY_BASE_URL in .env to point to the new API endpoint will break some methods below relying on the old route. - A temporary workaround for now is to set a new Request default with the updated endpoint. + A temporary workaround for now is to set a new axios default with the updated endpoint. */ - // this.requestNewRoute = request.defaults({ + // this.axios = axios.create({ // headers: { - // Authorization: `Bearer ${token}`, - // 'Content-Type': 'application/vnd.api+json', - // Accept: 'application/vnd.api+json', - // 'X-Envoy-Context': JSON.stringify(xEnvoyContext), + // common: { + // Authorization: `Bearer ${token}`, + // 'Content-Type': 'application/vnd.api+json', + // Accept: 'application/vnd.api+json', + // 'X-Envoy-Context': JSON.stringify(xEnvoyContext) + // } // }, - // json: true, - // baseUrl: this.newUrl, + // baseURL: this.newUrl, // }) } @@ -143,11 +143,11 @@ class EnvoyAPI { async companies() { var options = { method: 'GET', - url: this.newUrl + '/companies', + url: this.newUrl + `/companies`, headers: { Authorization: 'Bearer ' + this.token }, - json: true, + json: true }; const body = await request(options, function (error, response) { @@ -500,7 +500,7 @@ class EnvoyAPI { * @param { number | string } workSchedule Id * @returns {Promise} */ - async checkOutWork(workScheduleId) { + async checkOutWork(workScheduleId) { var options = { method: 'POST', url: this.newUrl + `/work-schedules/${workScheduleId}/checkout`, @@ -523,7 +523,7 @@ class EnvoyAPI { * @param { number|string } Invite ID. * @returns {Promise} */ - async getInvite(inviteId) { + async getInvite(inviteId) { var options = { method: 'GET', url: this.newUrl + `/invites/${inviteId}`, @@ -537,7 +537,7 @@ class EnvoyAPI { if (error) throw new Error(error); response.body; }); - + return EnvoyAPI.getDataFromBody(body); } @@ -546,7 +546,7 @@ class EnvoyAPI { * @param { {} } invite Object containing query params. See invites API documentation for list of all possbile Query params. * @returns {Promise} */ - async getInvites(invite){ + async getInvites(invite) { var options = { method: 'GET', url: this.newUrl + `/invites`, @@ -561,7 +561,7 @@ class EnvoyAPI { if (error) throw new Error(error); response.body; }); - + return EnvoyAPI.getDataFromBody(body); } @@ -581,7 +581,7 @@ class EnvoyAPI { return EnvoyAPI.getDataFromBody(body); } - + /** * Create an invite using the V1 API endpoint. * @@ -633,7 +633,7 @@ class EnvoyAPI { * @param {{}} invite * @returns {Promise} */ - async updateInviteV1(inviteId, invite) { + async updateInviteV1(inviteId, invite) { var options = { method: 'POST', url: this.newUrl + `/invites/${inviteId}`, @@ -691,7 +691,7 @@ class EnvoyAPI { * @param { number|string } inviteId * @returns {Promise} */ - async removeInviteV1(inviteId) { + async removeInviteV1(inviteId) { var options = { method: 'DELETE', url: this.newUrl + `/invites/${inviteId}`, @@ -708,7 +708,145 @@ class EnvoyAPI { return EnvoyAPI.getDataFromBody(body); } - + + /** + * Get a reservation by Id. + * @params { number|string } + * @returns {Promise} + */ + async reservation(resId) { + var options = { + method: 'GET', + url: this.newUrl + `/reservations/${resId}`, + headers: { + Authorization: 'Bearer ' + this.token + }, + json: true + }; + + const body = await request(options, function (error, response) { + if (error) throw new Error(error); + response.body; + }); + + return EnvoyAPI.getDataFromBody(body); + } + + /** + * Get reservations. + * @returns {Promise} + */ + async reservations(resParams) { + var options = { + method: 'GET', + url: this.newUrl + `/reservations`, + headers: { + Authorization: 'Bearer ' + this.token + }, + json: true + }; + + const body = await request(options, function (error, response) { + if (error) throw new Error(error); + response.body; + }); + + return EnvoyAPI.getDataFromBody(body); + } + + /** + * Create a reservation + * @params { {} } A reservation object containing body params. + * @returns {Promise} + */ + async createReservation(reservation) { + var options = { + method: 'POST', + url: this.newUrl + `/reservations`, + headers: { + Authorization: 'Bearer ' + this.token + }, + body: reservation, + json: true + }; + + const body = await request(options, function (error, response) { + if (error) throw new Error(error); + response.body; + }); + + return EnvoyAPI.getDataFromBody(body); + } + + /** + * Check in a reservation + * @params { number|string } Reservation Id. + * @returns {Promise} + */ + async checkInReservation(resId) { + var options = { + method: 'POST', + url: this.newUrl + `/reservations/${resId}/checkin`, + headers: { + Authorization: 'Bearer ' + this.token + }, + json: true + }; + + const body = await request(options, function (error, response) { + if (error) throw new Error(error); + response.body; + }); + + return EnvoyAPI.getDataFromBody(body); + } + + /** + * Check out a reservation + * @params { number|string } Reservation Id. + * @returns {Promise} + */ + async checkOutReservation(resId) { + var options = { + method: 'POST', + url: this.newUrl + `/reservations/${resId}/checkout`, + headers: { + Authorization: 'Bearer ' + this.token + }, + json: true + }; + + const body = await request(options, function (error, response) { + if (error) throw new Error(error); + response.body; + }); + + return EnvoyAPI.getDataFromBody(body); + } + + /** + * Cancel a reservation + * @params { number|string } Reservation Id. + * @returns {Promise} + */ + async cancelReservation(resId) { + var options = { + method: 'POST', + url: this.newUrl + `/reservations/${resId}/cancel`, + headers: { + Authorization: 'Bearer ' + this.token + }, + json: true + }; + + const body = await request(options, function (error, response) { + if (error) throw new Error(error); + response.body; + }); + + return EnvoyAPI.getDataFromBody(body); + } + /** * Updates the job. * From 3e9c9df630f6c6eb6f6dd87388bb74b312acd8e5 Mon Sep 17 00:00:00 2001 From: tkla Date: Wed, 8 Jun 2022 15:10:38 -0700 Subject: [PATCH 19/20] Add space APIs --- lib/EnvoyAPI.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/lib/EnvoyAPI.js b/lib/EnvoyAPI.js index cd25ce3..af947c8 100644 --- a/lib/EnvoyAPI.js +++ b/lib/EnvoyAPI.js @@ -847,6 +847,53 @@ class EnvoyAPI { return EnvoyAPI.getDataFromBody(body); } + /** + * Get a space by Id. + * @params { number|string } + * @returns {Promise} + */ + async space(spaceId) { + var options = { + method: 'GET', + url: this.newUrl + `/spaces/${spaceId}`, + headers: { + Authorization: 'Bearer ' + this.token + }, + json: true + }; + + const body = await request(options, function (error, response) { + if (error) throw new Error(error); + response.body; + }); + + return EnvoyAPI.getDataFromBody(body); + } + + /** + * Get all spaces with option query params to further filter results. + * @params { {} } Object containing query params. + * @returns {Promise} + */ + async spaces(space) { + var options = { + method: 'GET', + url: this.newUrl + `/spaces`, + headers: { + Authorization: 'Bearer ' + this.token + }, + qs: space, + json: true + }; + + const body = await request(options, function (error, response) { + if (error) throw new Error(error); + response.body; + }); + + return EnvoyAPI.getDataFromBody(body); + } + /** * Updates the job. * From 259ad6878117ff07febf695e4253f53da134a719 Mon Sep 17 00:00:00 2001 From: tkla Date: Fri, 10 Jun 2022 10:37:15 -0700 Subject: [PATCH 20/20] Change deleteWorkSchedule back to async class method --- lib/EnvoyAPI.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/EnvoyAPI.js b/lib/EnvoyAPI.js index af947c8..5d41194 100644 --- a/lib/EnvoyAPI.js +++ b/lib/EnvoyAPI.js @@ -451,7 +451,7 @@ class EnvoyAPI { * @param {{}} workSchedule object. * @returns {Promise} */ - deleteWorkSchedule = async (workScheduleId) => { + async deleteWorkSchedule(workScheduleId) { var options = { method: 'DELETE', url: this.newUrl + `/work-schedules/${workScheduleId}`,