-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Sync Client for sharing contact form (#11)
* Added issuSyncToken * Added transferCallResolve * Updated README
- Loading branch information
Showing
7 changed files
with
606 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import '@twilio-labs/serverless-runtime-types'; | ||
import { | ||
Context, | ||
ServerlessCallback, | ||
ServerlessFunctionSignature, | ||
} from '@twilio-labs/serverless-runtime-types/types'; | ||
import { responseWithCors, bindResolve, error500, success } from 'tech-matters-serverless-helpers'; | ||
|
||
const TokenValidator = require('twilio-flex-token-validator').functionValidator; | ||
|
||
type EnvVars = { | ||
ACCOUNT_SID: string; | ||
SYNC_SERVICE_API_KEY: string; | ||
SYNC_SERVICE_API_SECRET: string; | ||
SYNC_SERVICE_SID: string; | ||
}; | ||
|
||
// This is added to event by TokenValidator (if a valid Token was provided) https://www.npmjs.com/package/twilio-flex-token-validator#token-result | ||
export type AuthEvent = { | ||
TokenResult: { | ||
identity: string; | ||
}; | ||
}; | ||
|
||
export const handler: ServerlessFunctionSignature = TokenValidator( | ||
async (context: Context<EnvVars>, event: AuthEvent, callback: ServerlessCallback) => { | ||
const response = responseWithCors(); | ||
const resolve = bindResolve(callback)(response); | ||
|
||
try { | ||
const { identity } = event.TokenResult; | ||
|
||
const { | ||
ACCOUNT_SID, | ||
SYNC_SERVICE_API_KEY, | ||
SYNC_SERVICE_API_SECRET, | ||
SYNC_SERVICE_SID, | ||
} = context; | ||
|
||
if (!identity) { | ||
throw new Error('Identity is missing, something is wrong with the token provided'); | ||
} | ||
if (!(SYNC_SERVICE_API_KEY && SYNC_SERVICE_API_SECRET && SYNC_SERVICE_SID)) { | ||
throw new Error('Sync Service information missing, set your env vars'); | ||
} | ||
|
||
const { AccessToken } = Twilio.jwt; | ||
const { SyncGrant } = AccessToken; | ||
|
||
const syncGrant = new SyncGrant({ serviceSid: SYNC_SERVICE_SID }); | ||
|
||
const accessToken = new AccessToken( | ||
ACCOUNT_SID, | ||
SYNC_SERVICE_API_KEY, | ||
SYNC_SERVICE_API_SECRET, | ||
{ identity }, | ||
); | ||
|
||
accessToken.addGrant(syncGrant); | ||
|
||
resolve(success({ token: accessToken.toJwt() })); | ||
} catch (err) { | ||
resolve(error500(err)); | ||
} | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import '@twilio-labs/serverless-runtime-types'; | ||
import { | ||
Context, | ||
ServerlessCallback, | ||
ServerlessFunctionSignature, | ||
} from '@twilio-labs/serverless-runtime-types/types'; | ||
import { | ||
responseWithCors, | ||
bindResolve, | ||
error400, | ||
error500, | ||
success, | ||
} from 'tech-matters-serverless-helpers'; | ||
|
||
const TokenValidator = require('twilio-flex-token-validator').functionValidator; | ||
|
||
type EnvVars = { | ||
TWILIO_WORKSPACE_SID: string; | ||
}; | ||
|
||
export type Body = { | ||
taskSid?: string; | ||
reservationSid?: string; | ||
}; | ||
|
||
async function closeReservation(context: Context<EnvVars>, body: Required<Body>) { | ||
const client = context.getTwilioClient(); | ||
|
||
const closedReservation = await client.taskrouter | ||
.workspaces(context.TWILIO_WORKSPACE_SID) | ||
.tasks(body.taskSid) | ||
.reservations(body.reservationSid) | ||
.update({ | ||
reservationStatus: 'completed', | ||
}); | ||
|
||
return closedReservation; | ||
} | ||
|
||
export const handler: ServerlessFunctionSignature = TokenValidator( | ||
async (context: Context<EnvVars>, event: Body, callback: ServerlessCallback) => { | ||
const response = responseWithCors(); | ||
const resolve = bindResolve(callback)(response); | ||
|
||
const { taskSid, reservationSid } = event; | ||
|
||
try { | ||
if (taskSid === undefined) { | ||
resolve(error400('taskSid')); | ||
return; | ||
} | ||
if (reservationSid === undefined) { | ||
resolve(error400('reservationSid')); | ||
return; | ||
} | ||
|
||
const validBody = { taskSid, reservationSid }; | ||
|
||
const closedReservation = await closeReservation(context, validBody); | ||
|
||
resolve(success({ closed: closedReservation.sid })); | ||
} catch (err) { | ||
resolve(error500(err)); | ||
} | ||
}, | ||
); |
Oops, something went wrong.