Skip to content

Commit c246d58

Browse files
committed
Another SCIM/lookup example, adds Circle API v2
1 parent d4b1406 commit c246d58

File tree

4 files changed

+155
-5
lines changed

4 files changed

+155
-5
lines changed

examples/nm/data/groups.json

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
[
2+
{
3+
"displayName": "AW Alpha",
4+
"id": "2ebb373f-3023-4e43-a81d-a62cb3292f06",
5+
"memberCount": 25
6+
},
27
{
38
"displayName": "AW Example Editors",
49
"id": "901fc64c-1167-4a43-8a3c-b906f08858a9",
@@ -12,7 +17,7 @@
1217
{
1318
"displayName": "Architecting Workspaces",
1419
"id": "0a5337ff-8343-4da6-a285-fe5c48b9660f",
15-
"memberCount": 0
20+
"memberCount": 1
1621
},
1722
{
1823
"displayName": "Content Editors",
@@ -32,26 +37,41 @@
3237
{
3338
"displayName": "Formula Fundamentals 2.0",
3439
"id": "70158620-4985-4b86-b08e-95657b6d2edf",
35-
"memberCount": 117
40+
"memberCount": 123
41+
},
42+
{
43+
"displayName": "Managers",
44+
"id": "8f01b40a-b3ec-41e4-8b27-f01ab29f77ae",
45+
"memberCount": 1
3646
},
3747
{
3848
"displayName": "Notion Mastery",
3949
"id": "7d3e5712-a873-43a8-a4b5-2ab138a9e2ea",
40-
"memberCount": 522
50+
"memberCount": 485
4151
},
4252
{
4353
"displayName": "Notion Mastery Alumni",
4454
"id": "922f01d5-b5e4-4f13-9be7-411242a2c68b",
45-
"memberCount": 1160
55+
"memberCount": 1155
4656
},
4757
{
4858
"displayName": "Notion Mastery Membership",
4959
"id": "9e7b05bc-e9e6-4b7a-8246-f8b1af875ea2",
50-
"memberCount": 289
60+
"memberCount": 256
5161
},
5262
{
5363
"displayName": "Team",
5464
"id": "6b4b5525-5248-4bee-9d4a-05d5114b58b9",
5565
"memberCount": 4
66+
},
67+
{
68+
"displayName": "Vendor Editors",
69+
"id": "6f01ed99-c859-4022-b852-0295b1ca217d",
70+
"memberCount": 0
71+
},
72+
{
73+
"displayName": "Vendor Management",
74+
"id": "ae45a99e-bbf9-4b99-bcab-1a644c854e23",
75+
"memberCount": 2
5676
}
5777
]

examples/nm/member-add-aw.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* Add a new or existing Member to the "AW Alpha" group in Notion
3+
* and the "Architecting Workspaces" access group in Circle
4+
*/
5+
6+
const { notion } = require('../shared');
7+
const { findCircleMember, addToAccessGroup } = require('../shared/circle');
8+
const { yargs, RED_COLOR } = require('../shared/scim');
9+
const { findMember, addMemberToGroup } = require('./shared');
10+
11+
const argv = yargs
12+
.option('groupId', {
13+
alias: 'g',
14+
describe: 'The ID of the group to add the User to',
15+
demand: true,
16+
default: '2ebb373f-3023-4e43-a81d-a62cb3292f06',
17+
})
18+
.option('email', {
19+
alias: 'e',
20+
describe: "User's email address",
21+
demand: true,
22+
}).argv;
23+
24+
const dataSourceId = '527dfb28-a457-4b45-99d3-8ee18497a725';
25+
26+
async function findStudentByEmail(email) {
27+
const {
28+
results: [student],
29+
} = await notion.dataSources.query({
30+
data_source_id: dataSourceId,
31+
filter: {
32+
property: 'Email',
33+
email: {
34+
equals: email,
35+
},
36+
},
37+
});
38+
39+
return student;
40+
}
41+
42+
async function tagStudent(student) {
43+
const params = {
44+
page_id: student.id,
45+
properties: {
46+
Tags: {
47+
multi_select: [{ name: 'Architecting Workspaces' }],
48+
},
49+
},
50+
};
51+
52+
await notion.pages.update(params);
53+
}
54+
55+
(async () => {
56+
console.info(`Finding student <${argv.email}>...`);
57+
58+
const student = await findStudentByEmail(argv.email);
59+
60+
if (!student) {
61+
return console.log(RED_COLOR, `No student record by email <${argv.email}> found`);
62+
}
63+
64+
console.info('Tagging student...');
65+
66+
await tagStudent(student);
67+
68+
let {
69+
Name: {
70+
title: [{ plain_text: name }],
71+
},
72+
Email: { email: email },
73+
NMID: {
74+
rich_text: [{ plain_text: NMID }],
75+
},
76+
'Circle Email': { email: circleEmail },
77+
} = student.properties;
78+
79+
circleEmail = circleEmail || email;
80+
81+
console.info(`Finding member with ID (${NMID})...`);
82+
83+
const member = await findMember(NMID);
84+
85+
if (!member) {
86+
return console.log(RED_COLOR, `Could not find ${name} <${email}> (${NMID})`);
87+
}
88+
89+
console.info(`Adding member to group (${argv.groupId})...`);
90+
91+
await addMemberToGroup(argv.groupId, NMID);
92+
93+
console.info(`Looking up Circle member <${circleEmail}>...`);
94+
95+
let circleMember = await findCircleMember(circleEmail);
96+
97+
if (!circleMember) {
98+
return console.log(RED_COLOR, `Could not find Circle member <${circleEmail}>`);
99+
}
100+
101+
console.info(`Adding to Circle access group...`);
102+
103+
await addToAccessGroup(circleMember.email);
104+
105+
console.log('Done!');
106+
})();

examples/nm/member-add.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const groupKeyToId = {
3333
nm: '7d3e5712-a873-43a8-a4b5-2ab138a9e2ea', // Notion Mastery
3434
membership: '9e7b05bc-e9e6-4b7a-8246-f8b1af875ea2', // Notion Mastery Membership
3535
ff: '70158620-4985-4b86-b08e-95657b6d2edf', // Formula Fundamentals 2.0
36+
aw: '2ebb373f-3023-4e43-a81d-a62cb3292f06', // Architecting Workspaces
3637
};
3738

3839
(async () => {

examples/shared/circle.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { default: axios } = require('axios');
33
const yargs = require('yargs/yargs')(process.argv.slice(2));
44

55
const CIRCLE_BASE_URI = 'https://app.circle.so/api/v1/';
6+
const CIRCLE_BASE_URI_V2 = 'https://app.circle.so/api/admin/v2/';
67
const CIRCLE_COMMUNITY_ID = 493;
78
const CIRCLE_SPACE_GROUP_IDS = [
89
18581, // Welcome
@@ -13,6 +14,7 @@ const CIRCLE_SPACE_GROUP_IDS = [
1314
dotenv.config();
1415

1516
const token = yargs.argv.circleToken || process.env.CIRCLE_API_TOKEN;
17+
const tokenV2 = yargs.argv.circleTokenV2 || process.env.CIRCLE_API_TOKEN_V2;
1618
const communityId =
1719
yargs.argv.communityId || process.env.CIRCLE_COMMUNITY_ID || CIRCLE_COMMUNITY_ID;
1820

@@ -23,6 +25,14 @@ const circle = axios.create({
2325
},
2426
});
2527

28+
const circleV2 = axios.create({
29+
baseURL: CIRCLE_BASE_URI_V2,
30+
headers: {
31+
Authorization: `Token ${tokenV2}`,
32+
'Content-Type': 'application/json',
33+
},
34+
});
35+
2636
async function findCircleMember(email) {
2737
// GET https://app.circle.so/api/v1/community_members/search
2838

@@ -83,11 +93,24 @@ async function findAndRemoveCircleMember(email) {
8393
}
8494
}
8595

96+
async function addToAccessGroup(email, access_group_id = '70021') {
97+
const { data } = await circleV2.post(`access_groups/${access_group_id}/community_members`, {
98+
email,
99+
});
100+
101+
if (data.success === false) {
102+
return false;
103+
}
104+
105+
return data;
106+
}
107+
86108
module.exports = {
87109
CIRCLE_BASE_URI,
88110
CIRCLE_COMMUNITY_ID,
89111
circle,
90112
addCircleMember,
113+
addToAccessGroup,
91114
findAndRemoveCircleMember,
92115
findCircleMember,
93116
removeCircleMember,

0 commit comments

Comments
 (0)