@@ -17,8 +17,13 @@ import {
17
17
EntraInvitationError ,
18
18
InternalServerError ,
19
19
NotFoundError ,
20
+ ValidationError ,
20
21
} from "../../common/errors/index.js" ;
21
- import { DynamoDBClient , PutItemCommand } from "@aws-sdk/client-dynamodb" ;
22
+ import {
23
+ DynamoDBClient ,
24
+ PutItemCommand ,
25
+ UpdateItemCommand ,
26
+ } from "@aws-sdk/client-dynamodb" ;
22
27
import {
23
28
GENERIC_CACHE_SECONDS ,
24
29
genericConfig ,
@@ -96,19 +101,59 @@ const iamRoutes: FastifyPluginAsync = async (fastify, _options) => {
96
101
message : "Could not find token payload and/or username." ,
97
102
} ) ;
98
103
}
104
+ const netId = request . username . replace ( "@illinois.edu" , "" ) ;
105
+ if ( netId . includes ( "@" ) ) {
106
+ request . log . error (
107
+ `Found username ${ request . username } which cannot be turned into NetID via simple replacement.` ,
108
+ ) ;
109
+ throw new ValidationError ( {
110
+ message : "Username could not be parsed." ,
111
+ } ) ;
112
+ }
99
113
const userOid = request . tokenPayload . oid ;
100
114
const entraIdToken = await getEntraIdToken ( {
101
115
clients : await getAuthorizedClients ( ) ,
102
116
clientId : fastify . environmentConfig . AadValidClientId ,
103
117
secretName : genericConfig . EntraSecretName ,
104
118
logger : request . log ,
105
119
} ) ;
106
- await patchUserProfile (
120
+ const { discordUsername } = request . body ;
121
+ const ddbUpdateCommand = fastify . dynamoClient . send (
122
+ new UpdateItemCommand ( {
123
+ TableName : genericConfig . UserInfoTable ,
124
+ Key : {
125
+ id : {
126
+ S : request . username ,
127
+ } ,
128
+ } ,
129
+ UpdateExpression : `SET #netId = :netId, #updatedAt = :updatedAt, #firstName = :firstName, #lastName = :lastName ${ discordUsername ? ", #discordUsername = :discordUsername" : "" } ` ,
130
+ ExpressionAttributeNames : {
131
+ "#netId" : "netId" ,
132
+ "#updatedAt" : "updatedAt" ,
133
+ "#firstName" : "firstName" ,
134
+ "#lastName" : "lastName" ,
135
+ ...( discordUsername
136
+ ? { "#discordUsername" : "discordUsername" }
137
+ : { } ) ,
138
+ } ,
139
+ ExpressionAttributeValues : {
140
+ ":netId" : { S : netId } ,
141
+ ":firstName" : { S : request . body . givenName } ,
142
+ ":lastName" : { S : request . body . surname } ,
143
+ ":updatedAt" : { S : new Date ( ) . toISOString ( ) } ,
144
+ ...( discordUsername
145
+ ? { ":discordUsername" : { S : discordUsername } }
146
+ : { } ) ,
147
+ } ,
148
+ } ) ,
149
+ ) ;
150
+ const entraUpdateCommand = patchUserProfile (
107
151
entraIdToken ,
108
152
request . username ,
109
153
userOid ,
110
154
request . body ,
111
155
) ;
156
+ await Promise . all ( [ ddbUpdateCommand , entraUpdateCommand ] ) ;
112
157
reply . status ( 201 ) . send ( ) ;
113
158
} ,
114
159
) ;
@@ -170,7 +215,7 @@ const iamRoutes: FastifyPluginAsync = async (fastify, _options) => {
170
215
} ) ;
171
216
const groupMembers = listGroupMembers ( entraIdToken , groupId ) ;
172
217
const command = new PutItemCommand ( {
173
- TableName : `${ genericConfig . IAMTablePrefix } - grouproles` ,
218
+ TableName : `${ genericConfig . IAMTablePrefix } - grouproles` ,
174
219
Item : marshall ( {
175
220
groupUuid : groupId ,
176
221
roles : request . body . roles ,
@@ -190,7 +235,7 @@ const iamRoutes: FastifyPluginAsync = async (fastify, _options) => {
190
235
await fastify . dynamoClient . send ( command ) ;
191
236
await logPromise ;
192
237
fastify . nodeCache . set (
193
- `grouproles- ${ groupId } ` ,
238
+ `grouproles - ${ groupId } ` ,
194
239
request . body . roles ,
195
240
GENERIC_CACHE_SECONDS ,
196
241
) ;
@@ -202,7 +247,7 @@ const iamRoutes: FastifyPluginAsync = async (fastify, _options) => {
202
247
} ) ;
203
248
reply . send ( { message : "OK" } ) ;
204
249
} catch ( e : unknown ) {
205
- fastify . nodeCache . del ( `grouproles- ${ groupId } ` ) ;
250
+ fastify . nodeCache . del ( `grouproles - ${ groupId } ` ) ;
206
251
if ( e instanceof BaseError ) {
207
252
throw e ;
208
253
}
@@ -462,7 +507,7 @@ const iamRoutes: FastifyPluginAsync = async (fastify, _options) => {
462
507
content : `
463
508
Hello,
464
509
465
- We're letting you know that you have been added to the "${ groupMetadata . displayName } " access group by ${ request . username } . Changes may take up to 2 hours to reflect in all systems.
510
+ We're letting you know that you have been added to the "${ groupMetadata . displayName } " access group by ${ request . username } . Changes may take up to 2 hours to reflect in all systems.
466
511
467
512
No action is required from you at this time.
468
513
` ,
@@ -484,7 +529,7 @@ No action is required from you at this time.
484
529
content : `
485
530
Hello,
486
531
487
- We're letting you know that you have been removed from the "${ groupMetadata . displayName } " access group by ${ request . username } .
532
+ We're letting you know that you have been removed from the "${ groupMetadata . displayName } " access group by ${ request . username } .
488
533
489
534
No action is required from you at this time.
490
535
` ,
0 commit comments