Skip to content

Commit 79c9f2e

Browse files
committed
refactor: rewrite c8 tutorial to use a full crud cycle of a self-contained entity (role_
1 parent 63c7d35 commit 79c9f2e

File tree

2 files changed

+61
-103
lines changed

2 files changed

+61
-103
lines changed

camunda-8.js

+18-28
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,33 @@
11
import axios from "axios";
2-
import { getAccessToken } from "../auth.js";
2+
import { getAccessToken } from "./auth.js";
33

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-
async function getUser() {
11-
console.log("User key:", userKey);
12-
}
13-
14-
async function createGroup([groupName]) {
15-
console.log(
16-
`Group added! Name: ${newGroup.name}. Key: ${newGroup.groupKey}.`
17-
);
4+
// An action that lists all roles.
5+
async function listRoles() {
6+
console.log("listing roles");
187
}
198

20-
async function assignUser([groupKey, userKey]) {
21-
console.log(`Group assigned to ${assignee}.`);
9+
// An action that creates a role.
10+
async function createRole([roleName]) {
11+
console.log(`adding role ${roleName}`);
2212
}
2313

24-
async function retrieveGroup([groupKey]) {
25-
results.forEach(x => console.log(`Name: ${x.name}; ID: ${x.assignedMemberKeys
26-
}`));
14+
// An action that retrieves a role.
15+
async function getRole([roleKey]) {
16+
console.log(`viewing role ${roleKey}`);
2717
}
2818

29-
async function deleteGroup([groupKey]) {
30-
console.log("Group deleted!");
19+
// An action that deletes a role.
20+
async function deleteRole([roleKey]) {
21+
console.log(`deleting role ${roleKey}`);
3122
}
3223

3324
// These functions are aliased to specific command names for terseness.
3425
// The name of each property translates to a method that can be called by the CLI.
35-
// e.g. if we export a function named `assign`, you can run `npm run cli camunda8 get`.
26+
// e.g. if we export a function named `list`, you can run `npm run cli zeebe get`.
3627

3728
export default {
38-
get: getUser,
39-
create: createGroup,
40-
assign: assignUser,
41-
retrieve: retrieveGroup,
42-
delete: deleteGroup
29+
list: listRoles,
30+
create: createRole,
31+
view: getRole,
32+
delete: deleteRole
4333
};

completed/camunda-8.js

+43-75
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,22 @@
11
import axios from "axios";
2-
import { getAccessToken } from "../auth.js";
2+
import { getAccessToken } from "./auth.js";
33

44
const authorizationConfiguration = {
55
clientId: process.env.ZEEBE_CLIENT_ID,
66
clientSecret: process.env.ZEEBE_CLIENT_SECRET,
77
audience: process.env.ZEEBE_AUDIENCE
88
};
99

10-
// Retrieves the current authenticated user key.
11-
async function getUser() {
12-
const accessToken = await getAccessToken(authorizationConfiguration);
13-
14-
const camundaApiUrl = process.env.ZEEBE_BASE_URL;
15-
// This is the API endpoint to retrieve the current authenticated user key.
16-
const url = `${camundaApiUrl}/authentication/me`;
17-
18-
// Configure the API call.
19-
const options = {
20-
method: "GET",
21-
url,
22-
headers: {
23-
Accept: "application/json",
24-
Authorization: `Bearer ${accessToken}`
25-
}
26-
};
27-
28-
try {
29-
// Call the endpoint.
30-
const response = await axios(options);
31-
32-
// Process the results from the API call.
33-
const userKey = response.data;
34-
35-
// Emit the user key to output.
36-
console.log("User key:", userKey);
37-
} catch (error) {
38-
// Emit an error from the server.
39-
console.error(error.message);
40-
}
41-
}
42-
43-
// An action that creates a group.
44-
async function createGroup([groupName]) {
10+
// An action that lists all roles.
11+
async function listRoles() {
4512
// Every request needs an access token.
4613
const accessToken = await getAccessToken(authorizationConfiguration);
4714

4815
// These settings come from your .env file.
4916
const camundaApiUrl = process.env.ZEEBE_BASE_URL;
5017

51-
// This is the API endpoint to add a new client to a cluster.
52-
const url = `${camundaApiUrl}/groups`;
18+
// This is the API endpoint to query roles.
19+
const url = `${camundaApiUrl}/roles/search`;
5320

5421
// Configure the API call.
5522
const options = {
@@ -59,38 +26,37 @@ async function createGroup([groupName]) {
5926
Accept: "application/json",
6027
Authorization: `Bearer ${accessToken}`
6128
},
62-
data: {
63-
// The body contains information about the new group.
64-
groupName : groupName
65-
}
29+
// No filtering/paging/sorting, we want all roles.
30+
data: {}
6631
};
6732

6833
try {
34+
// Call the endpoint.
6935
const response = await axios(options);
7036

7137
// Process the results from the API call.
72-
const newGroup = response.data;
38+
const results = response.data;
7339

74-
// Emit new group to output.
75-
console.log(
76-
`Group added! Name: ${newGroup.name}. Key: ${newGroup.groupKey}.`
40+
// Emit roles to output.
41+
results.items.forEach(x =>
42+
console.log(`Role Name: ${x.name}; key: ${x.key}`)
7743
);
7844
} catch (error) {
7945
// Emit an error from the server.
8046
console.error(error.message);
8147
}
8248
}
8349

84-
// An action that assigns a user to a group by a key.
85-
async function assignUser([groupKey, userKey]) {
50+
// An action that creates a role.
51+
async function createRole([roleName]) {
8652
// Every request needs an access token.
8753
const accessToken = await getAccessToken(authorizationConfiguration);
8854

8955
// These settings come from your .env file.
9056
const camundaApiUrl = process.env.ZEEBE_BASE_URL;
9157

92-
// This is the API endpoint to assign a user to a group.
93-
const url = `${camundaApiUrl}/groups/${groupKey}/users/${userKey}`;
58+
// This is the API endpoint to add a new client to a cluster.
59+
const url = `${camundaApiUrl}/roles`;
9460

9561
// Configure the API call.
9662
const options = {
@@ -100,35 +66,36 @@ async function assignUser([groupKey, userKey]) {
10066
Accept: "application/json",
10167
Authorization: `Bearer ${accessToken}`
10268
},
69+
data: {
70+
// The body contains information about the new role.
71+
name: roleName
72+
}
10373
};
10474

10575
try {
106-
// Call the add endpoint.
10776
const response = await axios(options);
10877

10978
// Process the results from the API call.
110-
if (response.status === 204) {
111-
console.log(`Group assigned to ${userKey}.`);
112-
} else {
113-
// Emit an unexpected error message.
114-
console.error("Unable to assign this user!");
115-
}
79+
const newRole = response.data;
80+
81+
// Emit new role to output.
82+
console.log(`Role added! Name: ${roleName}. Key: ${newRole.roleKey}.`);
11683
} catch (error) {
11784
// Emit an error from the server.
11885
console.error(error.message);
11986
}
12087
}
12188

122-
// An action that retrieves assigned member keys within a group.
123-
async function retrieveGroup([groupKey]) {
89+
// An action that retrieves a role.
90+
async function getRole([roleKey]) {
12491
// Every request needs an access token.
12592
const accessToken = await getAccessToken(authorizationConfiguration);
12693

12794
// These settings come from your .env file.
12895
const camundaApiUrl = process.env.ZEEBE_BASE_URL;
12996

130-
// This is the API endpoint to list all assigned member keys within a group.
131-
const url = `${camundaApiUrl}/groups/${groupKey}`;
97+
// This is the API endpoint to get a specific role.
98+
const url = `${camundaApiUrl}/roles/${roleKey}`;
13299

133100
// Configure the API call.
134101
const options = {
@@ -147,23 +114,25 @@ async function retrieveGroup([groupKey]) {
147114
// Process the results from the API call.
148115
const results = response.data;
149116

150-
// Emit clients to output.
151-
results.forEach(x => console.log(`Name: ${x.name}; ID: ${x.assignedMemberKeys
152-
}`));
153-
// Not sure how to format the above -- is forEach needed??
117+
// Emit role to output.
118+
console.log(
119+
`Role Name: ${results.name}; Key: ${
120+
results.key
121+
}; Members: ${JSON.stringify(results.assignedMemberKeys)}`
122+
);
154123
} catch (error) {
155124
// Emit an error from the server.
156125
console.error(error.message);
157126
}
158127
}
159128

160-
// An action to delete a group.
161-
async function deleteGroup([groupKey]) {
129+
// An action that deletes a role.
130+
async function deleteRole([roleKey]) {
162131
const accessToken = await getAccessToken(authorizationConfiguration);
163132

164133
const camundaApiUrl = process.env.ZEEBE_BASE_URL;
165134

166-
const url = `${camundaApiUrl}/groups/${groupKey}`;
135+
const url = `${camundaApiUrl}/roles/${roleKey}`;
167136

168137
// Configure the API call.
169138
const options = {
@@ -181,10 +150,10 @@ async function deleteGroup([groupKey]) {
181150

182151
// Process the results from the API call.
183152
if (response.status === 204) {
184-
console.log("Group deleted!");
153+
console.log("Role deleted!");
185154
} else {
186155
// Emit an unexpected error message.
187-
console.error("Unable to delete this group!");
156+
console.error("Unable to delete this role!");
188157
}
189158
} catch (error) {
190159
// Emit an error from the server.
@@ -197,9 +166,8 @@ async function deleteGroup([groupKey]) {
197166
// e.g. if we export a function named `list`, you can run `npm run cli zeebe get`.
198167

199168
export default {
200-
get: getUser,
201-
create: createGroup,
202-
assign: assignUser,
203-
retrieve: retrieveGroup,
204-
delete: deleteGroup
169+
list: listRoles,
170+
create: createRole,
171+
view: getRole,
172+
delete: deleteRole
205173
};

0 commit comments

Comments
 (0)