deleteOrgUser method

  1. @override
Future<OrgUser> deleteOrgUser({
  1. required String orgId,
  2. required String userId,
})
override

Remove a user from the org with the given org id.

Implementation

@override
Future<OrgUser> deleteOrgUser({
  required String orgId,
  required String userId,
}) async {
  final operation = _amplifyApi.query<String>(
    request: GraphQLRequest(
      document: '''
        mutation DeleteOrgUser(\$orgID: ID!, \$userID: ID!) {
          deleteOrgUser(orgID: \$orgID, userID: \$userID) {
            user {
              userID
              userName
              firstName
              middleName
              familyName
              emailAddress
            }
            org {
              orgID
              orgName
            }
            isOwner
            isAdmin
            status
          }
        }
      ''',
      variables: {
        'orgID': orgId,
        'userID': userId,
      },
    ),
  );
  final response = await operation.response.onError((e, stackTrace) {
    _logger.severe(
      'Exception when invoking service call to '
      'delete org user "$userId" to org "$orgId": $e',
    );

    throw OrgServiceError(
      message: 'Failed to delete org user "$userId" to org "$orgId"',
      innerException: e is Exception ? e : Exception(e),
      innerStackTrace: stackTrace,
    );
  });

  if (response.hasErrors) {
    final errorPayload = jsonEncode(response.errors);
    _logger.severe('Failed to delete org user: $errorPayload');

    if (response.errors.any(
      (error) =>
          error.message.contains(
            'not associated with the given org',
          ) ||
          error.message.contains(
            'can only delete org users from orgs he/she owns',
          ),
    )) {
      throw OrgDoesNotExistError(orgId: orgId);
    } else {
      throw OrgDataUpdateError(orgId: orgId);
    }
  }

  final data = response.data;
  _logger.finest('Org user delete: $data');

  return OrgUser.fromJson(jsonDecode(data!)['deleteOrgUser']);
}