Skip to content

Commit

Permalink
Add getTask endpoint to support fixed 'Save and End' task functionality
Browse files Browse the repository at this point in the history
(cherry picked from commit d71ccf8)
  • Loading branch information
stephenhand committed Nov 28, 2024
1 parent 24c003e commit d15b0c0
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 2 deletions.
5 changes: 3 additions & 2 deletions functions/completeTaskAssignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
error500,
success,
functionValidator as TokenValidator,
error403,
} from '@tech-matters/serverless-helpers';

type EnvVars = {
Expand Down Expand Up @@ -105,7 +106,7 @@ export const handler = TokenValidator(
const authToken = context.AUTH_TOKEN;
const token = event.Token;

if (!token || token === undefined) {
if (!token) {
resolve(error400('token'));
return;
}
Expand All @@ -121,7 +122,7 @@ export const handler = TokenValidator(

if (!isSupervisorToken) {
resolve(
error400(`Unauthorized: endpoint not open to non supervisors. ${isSupervisorToken}`),
error403(`Unauthorized: endpoint not open to non supervisors. ${isSupervisorToken}`),
);
return;
}
Expand Down
107 changes: 107 additions & 0 deletions functions/getTask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Get task as a supervisor
/**
* Copyright (C) 2021-2023 Technology Matters
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
import '@twilio-labs/serverless-runtime-types';
import { validator } from 'twilio-flex-token-validator';
import { Context, ServerlessCallback } from '@twilio-labs/serverless-runtime-types/types';
import {
responseWithCors,
bindResolve,
error400,
error500,
success,
functionValidator as TokenValidator,
error403,
} from '@tech-matters/serverless-helpers';

type EnvVars = {
TWILIO_WORKSPACE_SID: string;
ACCOUNT_SID: string;
AUTH_TOKEN: string;
};

type TaskInstance = Awaited<
ReturnType<
ReturnType<
ReturnType<ReturnType<Context['getTwilioClient']>['taskrouter']['workspaces']>['tasks']
>['fetch']
>
>;

type ContactComplete = {
action: 'complete';
taskSid: string;
targetSid: string;
finalTaskAttributes: TaskInstance['attributes'];
};

export type Body = {
request: { cookies: {}; headers: {} };
Token?: string;
} & ContactComplete;

export type TokenValidatorResponse = { worker_sid?: string; roles?: string[] };

export const handler = TokenValidator(
async (context: Context<EnvVars>, event: Body, callback: ServerlessCallback) => {
const response = responseWithCors();
const resolve = bindResolve(callback)(response);

const accountSid = context.ACCOUNT_SID;
const authToken = context.AUTH_TOKEN;
const token = event.Token;

if (!token) {
resolve(error400('token'));
return;
}

try {
const tokenResult: TokenValidatorResponse = await validator(
token as string,
accountSid,
authToken,
);

const isSupervisorToken =
Array.isArray(tokenResult.roles) && tokenResult.roles.includes('supervisor');

if (!isSupervisorToken) {
resolve(
error403(`Unauthorized: endpoint not open to non supervisors. ${isSupervisorToken}`),
);
return;
}

const { taskSid } = event;

if (taskSid === undefined) {
resolve(error400('taskSid is undefined'));
return;
}

const result = await context
.getTwilioClient()
.taskrouter.workspaces(context.TWILIO_WORKSPACE_SID)
.tasks(event.taskSid)
.fetch();

resolve(success(result));
} catch (err: any) {
resolve(error500(err));
}
},
);

0 comments on commit d15b0c0

Please sign in to comment.