From f0f3f60a9052f410c2fa63e0a84edd7b8b7d9a2f Mon Sep 17 00:00:00 2001 From: Arvind Ramesh Date: Thu, 21 Nov 2024 14:08:04 -0800 Subject: [PATCH] feat: Adding `createReservations` function to the `EnvoyUserAPI` (#66) --- package-lock.json | 4 +-- package.json | 2 +- src/resources/ReservationResource.ts | 40 ++++++++++++++++++++++++++ src/sdk/EnvoyUserAPI.ts | 42 ++++++++++++++++++++++++++-- 4 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 src/resources/ReservationResource.ts diff --git a/package-lock.json b/package-lock.json index edc326e..c7e2d6b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@envoy/envoy-integrations-sdk", - "version": "2.2.1", + "version": "2.2.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@envoy/envoy-integrations-sdk", - "version": "2.2.1", + "version": "2.2.3", "license": "ISC", "dependencies": { "@types/dotenv": "^8.2.0", diff --git a/package.json b/package.json index fa2b239..c4b5d36 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@envoy/envoy-integrations-sdk", - "version": "2.2.1", + "version": "2.2.3", "description": "SDK for building Envoy integrations.", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/resources/ReservationResource.ts b/src/resources/ReservationResource.ts new file mode 100644 index 0000000..39f537b --- /dev/null +++ b/src/resources/ReservationResource.ts @@ -0,0 +1,40 @@ +import JSONAPIModel from "../util/json-api/JSONAPIModel"; + +export interface ReservationAttributes { + 'is-partial-day': boolean; + 'updated-at': number; + 'is-assignable': boolean; + 'name': string; + 'assigned-to': string | null; + 'neighborhood-id': number; + 'created-at': number; + 'neighborhood': number | null; + 'parent-desk-id': string | null; + 'availability': string | null; + 'enabled': boolean; + 'x-pos': number | null; + 'y-pos': number | null; +} + +export interface ReservationCreationAttributes { + // Required fields + userId: number; + + // Optional fields + deskId?: number | null; + locationId?: number | null; + inviteId?: number | null; + entryId?: number | null; + startTime?: number | null; // Unix timestamp + endTime?: number | null; // Unix timestamp + + meta?: { + autoAssignDesk?: boolean; + }; +} + +export type ReservationRelationships = 'location' | 'desk' | 'company' | 'floor' | 'employee' | 'entry' | 'invite' | 'user'; + +export type ReservationModel = JSONAPIModel + + diff --git a/src/sdk/EnvoyUserAPI.ts b/src/sdk/EnvoyUserAPI.ts index 095a099..c867520 100644 --- a/src/sdk/EnvoyUserAPI.ts +++ b/src/sdk/EnvoyUserAPI.ts @@ -17,6 +17,7 @@ import { UserModel } from '../resources/UserResource'; import { envoyBaseURL, envoyClientId, envoyClientSecret } from '../constants'; import { EnvoyMetaAuth } from './EnvoyMeta'; import { sanitizeAxiosError } from '../util/axiosConstructor'; +import { ReservationCreationAttributes, ReservationModel } from "../resources/ReservationResource"; export type EnvoyUserAPIScope = 'flows.read' | @@ -195,6 +196,43 @@ export default class EnvoyUserAPI extends EnvoyAPI { return data.data; } + async createReservation(reservationDetails: ReservationCreationAttributes): Promise { + let createReservationBody = { + data: { + relationships: { + user: { + data: { + type: 'users', + id: reservationDetails.userId + }}, + ...(reservationDetails.locationId && { + location: { + data: { + type: 'locations', + id: reservationDetails.locationId + } + } + } + ) + }, + attributes: { + 'start-time': reservationDetails.startTime, + ...(reservationDetails.endTime && { + 'end-time': reservationDetails.endTime + }), + 'booking-source': 'EXTERNAL_API', + 'booking-type': 'visitor' + } + } + } + const { data } = await this.axios({ + method: 'POST', + url: '/a/rms/reservations', + data: createReservationBody, + }); + return data.data; + } + /** * Requires `invites.write` scope. */ @@ -305,7 +343,7 @@ export default class EnvoyUserAPI extends EnvoyAPI { }); return data; } catch (error) { - throw sanitizeAxiosError(error); + throw sanitizeAxiosError(error); } } @@ -333,7 +371,7 @@ export default class EnvoyUserAPI extends EnvoyAPI { }); return data; } catch (error) { - throw sanitizeAxiosError(error); + throw sanitizeAxiosError(error); } } }