createOrg method

  1. @override
Future<OrgIi> createOrg({
  1. required String orgName,
  2. required Key orgKey,
  3. List<ContactInput>? contacts,
  4. List<AddressInput>? addresses,
  5. String? businessRef,
})
override

Create a new organization with the given information. Current logged in user will be added as the owner and admin of the organization.

Implementation

@override
Future<OrgIi> createOrg({
  required String orgName,
  required Key orgKey,
  List<ContactInput>? contacts,
  List<AddressInput>? addresses,
  String? businessRef,
}) async {
  final operation = _amplifyApi.query<String>(
    request: GraphQLRequest(
      document: '''
        mutation AddOrg(\$orgName: String!, \$orgKey: Key!, \$contacts: [ContactInput], \$addresses: [AddressInput], \$businessRef: String) {
          addOrg(orgName: \$orgName, orgKey: \$orgKey, contacts: \$contacts, addresses: \$addresses, businessRef: \$businessRef) {
            idKey
            orgUser {
              org {
                orgID
                orgName
                verified
                config
                quotas {
                  quotaName
                  description
                  period
                  softLimit
                  limit
                  used
                }
              }
              user {
                userID
                userName
              }
              isOwner
              isAdmin
              status
            }
          }
        }
      ''',
      variables: {
        'orgName': orgName,
        'orgKey': orgKey,
        'contacts': contacts,
        'addresses': addresses,
        'businessRef': businessRef,
      },
    ),
  );
  final response = await operation.response.onError((e, stackTrace) {
    _logger.severe(
      'Exception when invoking service call to '
      'create org with name "$orgName": $e',
    );

    throw OrgServiceError(
      message: 'Failed to create org with name: $orgName',
      innerException: e is Exception ? e : Exception(e),
      innerStackTrace: stackTrace,
    );
  });

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

    throw OrgServiceError(
      message: 'Failed to create org with name: $orgName',
    );
  }

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

  return OrgIi.fromJson(jsonDecode(data!)['addOrg']);
}