addOrgUser method
Add a user to the org with the given org id. The user will be added as a member of the org.
Implementation
@override
Future<OrgUser> addOrgUser({
required String orgId,
required String userId,
bool isAdmin = false,
}) async {
final operation = _amplifyApi.query<String>(
request: GraphQLRequest(
document: '''
mutation AddOrgUser(\$orgID: ID!, \$userID: ID!, \$isAdmin: Boolean) {
addOrgUser(orgID: \$orgID, userID: \$userID, isAdmin: \$isAdmin) {
user {
userID
userName
firstName
middleName
familyName
emailAddress
}
org {
orgID
orgName
}
isOwner
isAdmin
status
}
}
''',
variables: {
'orgID': orgId,
'userID': userId,
'isAdmin': isAdmin,
},
),
);
final response = await operation.response.onError((e, stackTrace) {
_logger.severe(
'Exception when invoking service call to '
'add org user "$userId" to org "$orgId": $e',
);
throw OrgServiceError(
message: 'Failed to add 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 add org user: $errorPayload');
if (response.errors.any(
(error) =>
error.message.contains(
'not associated with the given org',
) ||
error.message.contains(
'does not own org',
),
)) {
throw OrgDoesNotExistError(orgId: orgId);
} else if (response.errors.any(
(error) => error.message.contains(
'already associated with org',
),
)) {
throw OrgUserExistsError(orgId: orgId, userId: userId);
} else {
throw OrgDataUpdateError(orgId: orgId);
}
}
final data = response.data;
_logger.finest('Org user added: $data');
return OrgUser.fromJson(jsonDecode(data!)['addOrgUser']);
}