async function updatePermissions()

in scripts/updatePermissions/updatePermissions.ts [23:68]


async function updatePermissions(
  permissionName: string,
  permissionLevel: string,
  emailAddresses: string[]
): Promise<void> {
  for (const email of emailAddresses) {
    try {
      await dynamoDB.send(
        new UpdateCommand({
          TableName: TABLE_NAME,
          Key: { email },
          UpdateExpression: 'SET #permissions = list_append(if_not_exists(#permissions, :emptyList), :newPermission)',
          ExpressionAttributeNames: {
            '#permissions': 'permissions',
          },
          ExpressionAttributeValues: {
            ':newPermission': [{ name: permissionName, permission: permissionLevel }],
            ':emptyList': [],
          },
          ConditionExpression: 'attribute_exists(email)',
        })
      );
      console.log(`Successfully updated permissions for ${email}`);
    } catch (error: any) {
      if (error.name === 'ConditionalCheckFailedException') {
        // Item does not exist, create it
        try {
          await dynamoDB.send(
            new PutCommand({
              TableName: TABLE_NAME,
              Item: {
                email,
                permissions: [{ name: permissionName, permission: permissionLevel }],
              },
            })
          );
          console.log(`Created new item and added permissions for ${email}`);
        } catch (putError) {
          console.error(`Failed to create item for ${email}:`, putError);
        }
      } else {
        console.error(`Failed to update permissions for ${email}:`, error);
      }
    }
  }
}