Skip to content

Commit

Permalink
feat: Adding createReservations function to the EnvoyUserAPI (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArvindEnvoy authored Nov 21, 2024
1 parent c352580 commit f0f3f60
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
40 changes: 40 additions & 0 deletions src/resources/ReservationResource.ts
Original file line number Diff line number Diff line change
@@ -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<ReservationAttributes, ReservationRelationships, 'reservations'>


42 changes: 40 additions & 2 deletions src/sdk/EnvoyUserAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' |
Expand Down Expand Up @@ -195,6 +196,43 @@ export default class EnvoyUserAPI extends EnvoyAPI {
return data.data;
}

async createReservation(reservationDetails: ReservationCreationAttributes): Promise<ReservationModel> {
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.
*/
Expand Down Expand Up @@ -305,7 +343,7 @@ export default class EnvoyUserAPI extends EnvoyAPI {
});
return data;
} catch (error) {
throw sanitizeAxiosError(error);
throw sanitizeAxiosError(error);
}
}

Expand Down Expand Up @@ -333,7 +371,7 @@ export default class EnvoyUserAPI extends EnvoyAPI {
});
return data;
} catch (error) {
throw sanitizeAxiosError(error);
throw sanitizeAxiosError(error);
}
}
}

0 comments on commit f0f3f60

Please sign in to comment.