Skip to content

Commit

Permalink
Swap members (email) SCIM example
Browse files Browse the repository at this point in the history
  • Loading branch information
typeoneerror committed Dec 4, 2023
1 parent 228adb5 commit 1bbcabb
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 11 deletions.
48 changes: 48 additions & 0 deletions examples/nm/change-member-emails.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Change access from one email to another.
*/

const { RED_COLOR, yargs } = require('../shared/scim');
const {
addMemberToGroup,
findMemberByEmail,
findOrProvisionUser,
removeMemberFromWorkspace,
} = require('./shared');

const argv = yargs
.option('groupId', {
alias: 'g',
describe: 'The ID of the group to add the User to',
demand: true,
default: '7d3e5712-a873-43a8-a4b5-2ab138a9e2ea',
})
.option('old', {
alias: 'o',
describe: "User's current email address",
demand: true,
})
.option('new', {
alias: 'n',
describe: "User's new email address",
demand: true,
}).argv;

(async () => {
const { groupId, old: oldEmail, new: newEmail } = argv;

// Find the old member
const oldMember = await findMemberByEmail(oldEmail);
if (!oldMember) {
return console.log(RED_COLOR, `No member by email <${oldEmail}> found`);
}

// Provision the new member
const user = await findOrProvisionUser(newEmail);
if (!user.id) {
return console.log(RED_COLOR, 'Could not find or provision user');
}

await addMemberToGroup(argv.groupId, user.id);
await removeMemberFromWorkspace(oldMember.id);
})();
2 changes: 1 addition & 1 deletion examples/nm/data/groups.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@
"memberCount": 4,
"index": 3
}
]
]
12 changes: 12 additions & 0 deletions examples/nm/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ async function findOrProvisionUser(email) {
return user;
}

async function removeMemberFromWorkspace(userId) {
try {
// DELETE https://api.notion.com/scim/v2/Users/{id}
const { status, statusText } = await scim.delete(`Users/${userId}`);

console.log(`Removed member (${status}): ${statusText} - ${userId}`);
} catch ({ response: { status, statusText } }) {
console.log(`Failed to remove member (${status}): ${statusText}`);
}
}

async function getCache(fileName) {
const tmpDir = path.join(__dirname, 'data');
const cachePath = path.join(tmpDir, `${fileName}.json`);
Expand Down Expand Up @@ -128,6 +139,7 @@ module.exports = {
findMemberByEmail,
findOrProvisionUser,
removeMemberFromGroup,
removeMemberFromWorkspace,
getCache,
setCache,
};
12 changes: 2 additions & 10 deletions examples/nm/user-remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

const { scim, yargs } = require('../shared/scim');
const { findMemberByEmail } = require('./shared');
const { findMemberByEmail, removeMemberFromWorkspace } = require('./shared');

const argv = yargs
.option('email', {
Expand All @@ -31,13 +31,5 @@ const argv = yargs
return console.log('Could not find user');
}

// DELETE https://api.notion.com/scim/v2/Users/{id}

try {
const { status, statusText } = await scim.delete(`Users/${userId}`);

console.log(`${status}: ${statusText} - ${userId}`);
} catch ({ response: { status, statusText } }) {
console.log(`${status}: ${statusText}`);
}
await removeMemberFromWorkspace(userId);
})();

0 comments on commit 1bbcabb

Please sign in to comment.