TF-3083 PublicAsset Implement partial update

This commit is contained in:
DatDang
2024-08-15 11:13:58 +07:00
committed by Dat H. Pham
parent 0580679fcd
commit e7566fcbe5
8 changed files with 309 additions and 33 deletions
@@ -12,6 +12,8 @@ import 'package:jmap_dart_client/jmap/mail/extensions/public_asset/public_asset.
import 'package:jmap_dart_client/jmap/mail/extensions/public_asset/set/set_public_asset_method.dart';
import 'package:jmap_dart_client/jmap/mail/extensions/public_asset/set/set_public_asset_response.dart';
import 'package:tmail_ui_user/features/public_asset/domain/exceptions/public_asset_exceptions.dart';
import 'package:tmail_ui_user/features/public_asset/domain/repository/public_asset_repository.dart';
import 'package:tmail_ui_user/features/public_asset/presentation/public_asset_controller.dart';
import 'package:uuid/uuid.dart';
class PublicAssetApi {
@@ -29,6 +31,20 @@ class PublicAssetApi {
return MapEntry(publicAsset.id!, patchObject);
}
MapEntry<PublicAssetId, PatchObject> _toPartialPatchObjectMapEntry(
PublicAssetId publicAssetId,
UpdatingIdentityIds updatingIdentityIds,
) {
assert(
updatingIdentityIds.values.every((value) => value != false),
'All updating identity id values must be true or null'
);
final patchObject = PatchObject(updatingIdentityIds.map(
(key, value) => MapEntry('${PatchObject.identityIdsProperty}/${key.id.value}', value)));
return MapEntry(publicAssetId, patchObject);
}
Future<List<PublicAsset>> getPublicAssets(
Session session,
AccountId accountId,
@@ -134,4 +150,29 @@ class PublicAssetApi {
throw const CannotUpdatePublicAssetException();
}
}
Future<void> partialUpdatePublicAssets(
Session session,
AccountId accountId,
{required Map<PublicAssetId, UpdatingIdentityIds> mapPublicAssetIdToUpdatingIdentityIds}
) async {
final requestBuilder = JmapRequestBuilder(_httpClient, ProcessingInvocation());
final method = SetPublicAssetMethod(accountId);
method.addUpdates(
mapPublicAssetIdToUpdatingIdentityIds.map(_toPartialPatchObjectMapEntry)
);
final invocation = requestBuilder.invocation(method);
final response = await (requestBuilder..usings(method.requiredCapabilities))
.build()
.execute();
final notUpdatedPublicAssetIds = response.parse<SetPublicAssetResponse>(
invocation.methodCallId,
SetPublicAssetResponse.deserialize
)?.notUpdated;
if (notUpdatedPublicAssetIds?.isNotEmpty == true) {
throw const CannotUpdatePublicAssetException();
}
}
}