22 * Change access from one email to another.
33 */
44
5+ const { notion } = require ( '../shared' ) ;
56const { RED_COLOR , yargs } = require ( '../shared/scim' ) ;
7+
68const {
79 addMemberToGroup,
810 findMemberByEmail,
@@ -28,6 +30,8 @@ const argv = yargs
2830 demand : true ,
2931 } ) . argv ;
3032
33+ const studentsDbId = '9d29ced8e9ba467c84e74fabbbbacc01' ;
34+
3135( async ( ) => {
3236 const { groupId, old : oldEmail , new : newEmail } = argv ;
3337
@@ -43,6 +47,57 @@ const argv = yargs
4347 return console . log ( RED_COLOR , 'Could not find or provision user' ) ;
4448 }
4549
50+ // Add new user to group
4651 await addMemberToGroup ( argv . groupId , user . id ) ;
52+
53+ // Remove old user from workspace
4754 await removeMemberFromWorkspace ( oldMember . id ) ;
55+
56+ // Fetch the record in the student database by the previous email
57+ const {
58+ results : [ student ] ,
59+ } = await notion . databases . query ( {
60+ database_id : studentsDbId ,
61+ filter : {
62+ property : 'Email' ,
63+ email : {
64+ equals : oldEmail ,
65+ } ,
66+ } ,
67+ } ) ;
68+
69+ if ( student ) {
70+ return console . log ( RED_COLOR , `No student record by email <${ oldEmail } > found` ) ;
71+ }
72+
73+ // Update the emails and NMID in student database
74+ await notion . pages . update ( {
75+ page_id : student . id ,
76+ properties : {
77+ Email : {
78+ email : newEmail ,
79+ } ,
80+ 'Previous Email' : {
81+ email : oldEmail ,
82+ } ,
83+ NMID : {
84+ rich_text : [
85+ {
86+ text : {
87+ content : user . id ,
88+ } ,
89+ } ,
90+ ] ,
91+ } ,
92+ } ,
93+ } ) ;
94+
95+ // Comment on student record noting the change
96+ await notion . comments . create ( {
97+ parent : {
98+ type : 'page_id' ,
99+ page_id : student . id ,
100+ } ,
101+ rich_text : [ { text : { content : `${ oldEmail } - ${ oldMember . id } ` } } ] ,
102+ } ) ;
48103} ) ( ) ;
0 commit comments