Skip to content

Commit 8869b23

Browse files
committed
IOS-8524: Resolve issue where json structure from user attribute fails to fetch for new accounts
(cherry picked from commit 8479039)
1 parent 12685b8 commit 8869b23

File tree

5 files changed

+53
-8
lines changed

5 files changed

+53
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public enum UserAttributeErrorEntity: Error {
2+
case attributeNotFound
3+
}

Modules/Domain/MEGADomain/Sources/MEGADomain/RepositoryProtocol/User/UserAttributeRepositoryProtocol.swift

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ public protocol UserAttributeRepositoryProtocol: Sendable {
1313
/// - object: Encodable object to be stored as a JSON string.
1414
func mergeUserAttribute(_ attribute: UserAttributeEntity, key: String, object: Encodable) async throws
1515
func updateUserAttribute(_ attribute: UserAttributeEntity, key: String, value: String) async throws
16+
17+
/// Fetches dictionary structure under the given attribute. If the attribute does not exist or was never set before, it will throw an error. If a optional value has been set previously it will return the value stored under the attribute.
18+
/// - Parameter attribute: UserAttributeEntity location of where the value will be fetched from .
19+
/// - Returns: Optional [String: String] dictionary stored at the attribute, if attribute was never set previously it will throw.
20+
///
21+
/// - Throws: UserAttributeErrorEntity.attributeNotFound if the attribute has never set before.
1622
func userAttribute(for attribute: UserAttributeEntity) async throws -> [String: String]?
1723

1824
/// Retrieve the decodable object from the associated user attribute for the given key. If the object for the attribute and key does not exist it will throw an error.

Modules/Repository/MEGASDKRepo/Sources/MEGASDKRepo/Repository/User/UserAttributeRepository.swift

+9-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public struct UserAttributeRepository: UserAttributeRepositoryProtocol {
3333

3434
public func mergeUserAttribute(_ attribute: UserAttributeEntity, key: String, object: Encodable) async throws {
3535
let supportedModelDictionary = try object.convertToDictionary()
36-
let currentAppsPreference = try await userAttribute(for: attribute)
36+
let currentAppsPreference = try? await userAttribute(for: attribute)
3737

3838
let contentToSave: [String: Any] = if let existingEncodedString = currentAppsPreference?[key],
3939
existingEncodedString.isNotEmpty,
@@ -74,8 +74,14 @@ public struct UserAttributeRepository: UserAttributeRepositoryProtocol {
7474
switch result {
7575
case .success(let request):
7676
completion(.success(request.megaStringDictionary))
77-
case .failure:
78-
completion(.failure(GenericErrorEntity()))
77+
case .failure(let error):
78+
let mappedError: any Error = switch error.type {
79+
case .apiERange:
80+
UserAttributeErrorEntity.attributeNotFound
81+
default:
82+
GenericErrorEntity()
83+
}
84+
completion(.failure(mappedError))
7985
}
8086
})
8187
})

Modules/Repository/MEGASDKRepo/Sources/MEGASDKRepoMock/SDK/MockSdk.swift

+6-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public final class MockSdk: MEGASdk {
4848
private var _lastReadNotificationId: Int32
4949
private var _isNodeInheritingSensitivity: Bool
5050
private var _hasVersionsForNode: Bool
51-
51+
private let setUserAttributeTypeMegaSetError: (MEGAUserAttribute) -> MEGAErrorType
52+
5253
public private(set) var sendEvent_Calls = [(
5354
eventType: Int,
5455
message: String,
@@ -128,7 +129,8 @@ public final class MockSdk: MEGASdk {
128129
enabledNotificationIdList: MEGAIntegerList? = nil,
129130
lastReadNotificationId: Int32 = 0,
130131
isNodeInheritingSensitivity: Bool = false,
131-
hasVersionsForNode: Bool = false
132+
hasVersionsForNode: Bool = false,
133+
setUserAttributeTypeMegaSetError: @escaping (MEGAUserAttribute) -> MEGAErrorType = { _ in .apiOk }
132134
) {
133135
self.nodes = nodes
134136
self.rubbishNodes = rubbishNodes
@@ -175,6 +177,7 @@ public final class MockSdk: MEGASdk {
175177
_lastReadNotificationId = lastReadNotificationId
176178
_isNodeInheritingSensitivity = isNodeInheritingSensitivity
177179
_hasVersionsForNode = hasVersionsForNode
180+
self.setUserAttributeTypeMegaSetError = setUserAttributeTypeMegaSetError
178181
super.init()
179182
}
180183

@@ -574,7 +577,7 @@ public final class MockSdk: MEGASdk {
574577
return
575578
}
576579

577-
delegate.onRequestFinish?(self, request: MockRequest(handle: 1), error: MockError(errorType: megaSetError))
580+
delegate.onRequestFinish?(self, request: MockRequest(handle: 1), error: MockError(errorType: setUserAttributeTypeMegaSetError(type)))
578581
}
579582

580583
public override func deviceId() -> String? {

Modules/Repository/MEGASDKRepo/Tests/MEGASDKRepoTests/UserAttributeRepositoryTests.swift

+29-2
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,35 @@ final class UserAttributeRepositoryTests: XCTestCase {
101101
XCTAssertEqual(sdk.contentConsumptionPreferences[key]?.sorted(), try XCTUnwrap(targetJson).sorted())
102102
}
103103

104-
private func sut(contentConsumptionPreferences: [String: String] = [:]) -> (UserAttributeRepository, MockSdk) {
105-
let sdk = MockSdk(contentConsumptionPreferences: contentConsumptionPreferences)
104+
func testMergeUserAttribute_withErrorWhenRetrievingAttribute_shouldSave() async throws {
105+
let targetJson = """
106+
{"ios":{"timeline":{"mediaType":"videos","location":"cloudDrive","usePreference":false},"sensitives":{"showHiddenNodes":false}},"sensitives":{"onboarded":false}}
107+
""".trim
108+
let key = ContentConsumptionKeysEntity.key
109+
let (sut, sdk) = sut(megaSetError: .apiERange)
110+
let objectToSave = ContentConsumptionEntity(
111+
ios: .init(
112+
timeline: .init(mediaType: .videos, location: .cloudDrive, usePreference: false),
113+
sensitives: .init(showHiddenNodes: false)),
114+
sensitives: .init(onboarded: false))
115+
116+
try await sut.mergeUserAttribute(
117+
.contentConsumptionPreferences,
118+
key: ContentConsumptionKeysEntity.key,
119+
object: objectToSave)
120+
121+
XCTAssertEqual(sdk.contentConsumptionPreferences[key]?.sorted(), try XCTUnwrap(targetJson).sorted())
122+
}
123+
124+
private func sut(
125+
contentConsumptionPreferences: [String: String] = [:],
126+
megaSetError: MEGAErrorType = .apiOk,
127+
setUserAttributeTypeMegaSetError: @escaping (MEGAUserAttribute) -> MEGAErrorType = { _ in .apiOk }
128+
) -> (UserAttributeRepository, MockSdk) {
129+
let sdk = MockSdk(
130+
megaSetError: megaSetError,
131+
contentConsumptionPreferences: contentConsumptionPreferences,
132+
setUserAttributeTypeMegaSetError: setUserAttributeTypeMegaSetError)
106133
return (UserAttributeRepository(sdk: sdk), sdk)
107134
}
108135
}

0 commit comments

Comments
 (0)