Skip to content

Commit

Permalink
added batch function
Browse files Browse the repository at this point in the history
  • Loading branch information
anyoussefinia committed Dec 12, 2024
1 parent b97f818 commit 2574960
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions services/admin/handlers/batchUpdateCognitoUsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import AWS from "aws-sdk";
const cognito = new AWS.CognitoIdentityServiceProvider();
import { getUser } from "../../app-api/getUser";

async function processCognitoUsers() {
const userPoolId = process.env.USER_POOL_ID;
console.log("user pool id: ", userPoolId)
let paginationToken = null;

do {
const params = {
UserPoolId: userPoolId,
AttributesToGet: ['email'],
PaginationToken: paginationToken
};

const listUsersResponse = await cognito.listUsers(params).promise();

for (const user of listUsersResponse.Users) {
const emailAttribute = user.Attributes.find(attr => attr.Name === 'email');
if (emailAttribute) {
const userEmail = emailAttribute.Value;

try {
const externalUser = await getUser(userEmail);
const roles = externalUser.roleList.map(role => role.role);

await updateUserAttribute(userPoolId, user.Username, roles);

Check failure on line 28 in services/admin/handlers/batchUpdateCognitoUsers.js

View workflow job for this annotation

GitHub Actions / Lint

'updateUserAttribute' was used before it was defined
} catch (error) {
console.error(`Error processing user ${userEmail}:`, error);
}
}
}

paginationToken = listUsersResponse.PaginationToken;
} while (paginationToken);
}

async function updateUserAttribute(userPoolId, username, roles) {
const params = {
UserPoolId: userPoolId,
Username: username,
UserAttributes: [
{
Name: 'custom:user_roles',
Value: JSON.stringify(roles)
}
]
};

await cognito.adminUpdateUserAttributes(params).promise();
}

export const main = async (event) => {

Check warning on line 54 in services/admin/handlers/batchUpdateCognitoUsers.js

View workflow job for this annotation

GitHub Actions / Lint

'event' is defined but never used
processCognitoUsers().catch(console.error);
}

0 comments on commit 2574960

Please sign in to comment.