|
| 1 | +import axios from "axios"; |
| 2 | +import { getAccessToken } from "../auth.js"; |
| 3 | + |
| 4 | +const authorizationConfiguration = { |
| 5 | + clientId: process.env.ZEEBE_CLIENT_ID, |
| 6 | + clientSecret: process.env.ZEEBE_CLIENT_SECRET, |
| 7 | + audience: process.env.ZEEBE_AUDIENCE |
| 8 | +}; |
| 9 | + |
| 10 | +// An action that assigns a Zeebe user task. |
| 11 | +async function assignUser([userTaskKey, assignee]) { |
| 12 | + // Every request needs an access token. |
| 13 | + const accessToken = await getAccessToken(authorizationConfiguration); |
| 14 | + |
| 15 | + // These settings come from your .env file. |
| 16 | + const zeebeApiUrl = process.env.ZEEBE_BASE_URL; |
| 17 | + |
| 18 | + // This is the API endpoint to assign a user task. |
| 19 | + const url = `${zeebeApiUrl}/user-tasks/${userTaskKey}/assignment`; |
| 20 | + |
| 21 | + // Configure the API call. |
| 22 | + const options = { |
| 23 | + method: "POST", |
| 24 | + url, |
| 25 | + headers: { |
| 26 | + Accept: "application/json", |
| 27 | + Authorization: `Bearer ${accessToken}` |
| 28 | + }, |
| 29 | + data: { |
| 30 | + // The body contains information about the new assignment. |
| 31 | + assignee: assignee |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + try { |
| 36 | + // Call the add endpoint. |
| 37 | + const response = await axios(options); |
| 38 | + |
| 39 | + // Process the results from the API call. |
| 40 | + if (response.status === 204) { |
| 41 | + console.log(`User task assigned to ${assignee}.`); |
| 42 | + } else { |
| 43 | + // Emit an unexpected error message. |
| 44 | + console.error("Unable to assign this user!"); |
| 45 | + } |
| 46 | + } catch (error) { |
| 47 | + // Emit an error from the server. |
| 48 | + console.error(error.message); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | +async function unassignUser([userTaskKey]) { |
| 53 | + const accessToken = await getAccessToken(authorizationConfiguration); |
| 54 | + |
| 55 | + const zeebeApiUrl = process.env.ZEEBE_BASE_URL; |
| 56 | + |
| 57 | + const url = `${zeebeApiUrl}/user-tasks/${userTaskKey}/assignee`; |
| 58 | + |
| 59 | + // Configure the API call. |
| 60 | + const options = { |
| 61 | + method: "DELETE", |
| 62 | + url, |
| 63 | + headers: { |
| 64 | + Accept: "application/json", |
| 65 | + Authorization: `Bearer ${accessToken}` |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + try { |
| 70 | + // Call the delete endpoint. |
| 71 | + const response = await axios(options); |
| 72 | + |
| 73 | + // Process the results from the API call. |
| 74 | + if (response.status === 204) { |
| 75 | + console.log("User task has been unassigned!"); |
| 76 | + } else { |
| 77 | + // Emit an unexpected error message. |
| 78 | + console.error("Unable to unassign this user task!"); |
| 79 | + } |
| 80 | + } catch (error) { |
| 81 | + // Emit an error from the server. |
| 82 | + console.error(error.message); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// These functions are aliased to specific command names for terseness. |
| 87 | +// The name of each property translates to a method that can be called by the CLI. |
| 88 | +// e.g. if we export a function named `assign`, you can run `npm run cli zeebe assign`. |
| 89 | + |
| 90 | +export default { |
| 91 | + assign: assignUser, |
| 92 | + unassign: unassignUser |
| 93 | +}; |
0 commit comments