Skip to content

Commit

Permalink
Handling the conflict case where email has been already taken in AWS (#…
Browse files Browse the repository at this point in the history
…2271)

* Fix: change env variables from obj to string

This resolves the error faced in staging for the discord command developed by me /grant-aws-access, we were facing this issue when running this in stgaging, this is becuase in the node-config the values were being set as obj with name field as the actual value

* lint-fix

* removing console log statement

* removing the extra log

* handling conflict case, where user with email already exists in AWS
  • Loading branch information
vikhyat187 authored Dec 3, 2024
1 parent aaa2de4 commit fccab02
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 6 additions & 0 deletions controllers/awsAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ export const addUserToAWSGroup = async (req, res) => {
if (awsUserId === null){
// We need to create the user in AWS before and then fetch its Id
userCreationResponse = await createUser(userInfoData.user.username, userInfoData.user.email);

if (userCreationResponse.conflict){
return res.status(400).json({
error: `Username or Email is already being used, please use another email / username for creating account in AWS`
})
}
awsUserId = userCreationResponse.UserId;
}

Expand Down
6 changes: 4 additions & 2 deletions utils/awsFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ import {
const command = new CreateUserCommand(params);
return (await client.send(command));
} catch (error) {
console.error(`The error from create user ${error}`);
if (error.__type === 'ConflictException'){
return { conflict: true };
}

throw new Error(`Failed to create user: ${error instanceof Error ? error.message : String(error)}`);
}
};
Expand All @@ -93,7 +96,6 @@ export const addUserToGroup = async (groupId: string, awsUserId: string): Promis
const command = new CreateGroupMembershipCommand(params);
return (await client.send(command));
} catch (error) {
console.error("Error adding user to group:", error);
if (error.__type === 'ConflictException'){
return { conflict: true };
}
Expand Down

0 comments on commit fccab02

Please sign in to comment.