Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | 3x 39x 39x 39x 39x 39x 39x 39x 780x 3861x 780x 110x 769x 769x 769x 110x 1x 1x 109x 109x 1x 1x 108x 108x 110x | import { CognitoIdentityProviderClient, ListUsersCommand, ListUsersCommandInput, ListUsersCommandOutput, AttributeType, } from "@aws-sdk/client-cognito-identity-provider"; export type StateUser = { firstName: string; lastName: string; email: string; formattedEmailAddress: string; }; type CognitoUserAttributes = { email?: string; given_name?: string; family_name?: string; "custom:state"?: string; [key: string]: string | undefined; }; export const getAllStateUsers = async ({ userPoolId, state, }: { userPoolId: string; state: string; }): Promise<StateUser[]> => { try { const params: ListUsersCommandInput = { UserPoolId: userPoolId, Limit: 60, }; const command = new ListUsersCommand(params); const cognitoClient = new CognitoIdentityProviderClient({ region: process.env.region, }); const response: ListUsersCommandOutput = await cognitoClient.send(command); Iif (!response.Users || response.Users.length === 0) { return []; } const filteredStateUsers = response.Users.filter((user) => { const stateAttribute = user.Attributes?.find( (attr): attr is AttributeType => attr.Name === "custom:state" && attr.Value !== undefined, ); return stateAttribute?.Value?.split(",").includes(state); }).map((user) => { const attributes = user.Attributes?.reduce<CognitoUserAttributes>((acc, attr) => { Eif (attr.Name && attr.Value) { acc[attr.Name] = attr.Value; } return acc; }, {}); // Skip users without valid email components if (!attributes?.email) { console.error(`No email found for user: ${JSON.stringify(user, null, 2)}`); return null; } // Validate email format const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailRegex.test(attributes.email)) { console.error(`Invalid email format for user: ${attributes.email}`); return null; } const formattedEmailAddress = `${attributes.given_name} ${attributes.family_name} <${attributes.email}>`; return { firstName: attributes.given_name ?? "", lastName: attributes.family_name ?? "", email: attributes.email, formattedEmailAddress, }; }); return filteredStateUsers.filter((user): user is StateUser => user !== null); } catch (error) { console.error("Error fetching users:", error); throw new Error("Error fetching users"); } }; |