findOrCreateAppStoreVersion method

  1. @override
Future<ApiResourceDto<AppStoreVersionAttributesDto>> findOrCreateAppStoreVersion({
  1. required String appId,
  2. required String version,
  3. required bool shouldReleaseAutomatically,
})
override

Finds or creates the editable App Store version for a marketing version.

Implementation

@override
Future<ApiResourceDto<AppStoreVersionAttributesDto>> findOrCreateAppStoreVersion({
  required String appId,
  required String version,
  required bool shouldReleaseAutomatically,
}) async {
  final desiredReleaseType = shouldReleaseAutomatically
      ? AppStoreReleaseType.afterApproval
      : AppStoreReleaseType.manual;
  final query = Uri(
    queryParameters: <String, String>{
      'filter[platform]': 'IOS',
      'filter[versionString]': version,
      'limit': '10',
    },
  ).query;
  final existing = <ApiResourceDto<AppStoreVersionAttributesDto>>[
    for (final item in await _collection(
      '/v1/apps/$appId/appStoreVersions?$query',
    ))
      _appStoreVersionResource(item),
  ];
  final match = existing
      .where(
        (item) => item.attributes.versionString == version,
      )
      .firstOrNull;
  if (match != null) {
    if (match.attributes.releaseType != desiredReleaseType) {
      SmfError.check(
        match.attributes.appVersionState.isEditable,
        'App Store version $version is already '
        '${match.attributes.appVersionState.name}; its release policy '
        'can no '
        'longer be changed.',
        SmfErrorCode.appStoreReleasePolicyLocked,
      );
      final updated = _map(
        await _request(
          'PATCH',
          '/v1/appStoreVersions/${match.id}',
          body: <String, Object?>{
            'data': <String, Object?>{
              'type': 'appStoreVersions',
              'id': match.id,
              'attributes': <String, Object?>{
                'releaseType': desiredReleaseType.value,
              },
            },
          },
        ),
        'appStoreVersion',
      );
      return _appStoreVersionResource(updated['data']);
    }
    return match;
  }
  final created = _map(
    await _request(
      'POST',
      '/v1/appStoreVersions',
      body: <String, Object?>{
        'data': <String, Object?>{
          'type': 'appStoreVersions',
          'attributes': <String, Object?>{
            'platform': 'IOS',
            'versionString': version,
            'releaseType': desiredReleaseType.value,
          },
          'relationships': <String, Object?>{
            'app': <String, Object?>{
              'data': <String, Object?>{'type': 'apps', 'id': appId},
            },
          },
        },
      },
    ),
    'appStoreVersion',
  );
  return _appStoreVersionResource(created['data']);
}