Skip to content

Commit 90ba9c4

Browse files
authored
Merge pull request #487 from Iterable/MOB-725-updatecart-part2
[MOB-725] updateCart (part 2)
2 parents 28fbbf9 + 7a8cf8f commit 90ba9c4

File tree

3 files changed

+158
-90
lines changed

3 files changed

+158
-90
lines changed

swift-sdk/Internal/RequestCreator.swift

Lines changed: 87 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,31 @@ struct RequestCreator {
1616
// MARK: - API REQUEST CALLS
1717

1818
func createUpdateEmailRequest(newEmail: String) -> Result<IterableRequest, IterableError> {
19-
var body: [String: Any] = [JsonKey.newEmail: newEmail]
19+
if case .none = auth.emailOrUserId {
20+
ITBError(Self.authMissingMessage)
21+
return .failure(IterableError.general(description: Self.authMissingMessage))
22+
}
23+
24+
var body = [String: Any]()
2025

2126
if let email = auth.email {
2227
body[JsonKey.currentEmail] = email
2328
} else if let userId = auth.userId {
2429
body[JsonKey.currentUserId] = userId
25-
} else {
26-
ITBError("Both email and userId are nil")
27-
return .failure(IterableError.general(description: "Both email and userId are nil"))
2830
}
2931

32+
body[JsonKey.newEmail] = newEmail
33+
3034
return .success(.post(createPostRequest(path: Const.Path.updateEmail, body: body)))
3135
}
3236

3337
func createRegisterTokenRequest(registerTokenInfo: RegisterTokenInfo,
3438
notificationsEnabled: Bool) -> Result<IterableRequest, IterableError> {
3539
if case .none = auth.emailOrUserId {
36-
ITBError("Both email and userId are nil")
37-
return .failure(IterableError.general(description: "Both email and userId are nil"))
40+
ITBError(Self.authMissingMessage)
41+
return .failure(IterableError.general(description: Self.authMissingMessage))
3842
}
39-
43+
4044
let dataFields = DataFieldsHelper.createDataFields(sdkVersion: registerTokenInfo.sdkVersion,
4145
deviceId: registerTokenInfo.deviceId,
4246
device: UIDevice.current,
@@ -47,7 +51,7 @@ struct RequestCreator {
4751
let deviceDictionary: [String: Any] = [
4852
JsonKey.token: registerTokenInfo.hexToken,
4953
JsonKey.platform: RequestCreator.pushServicePlatformToString(registerTokenInfo.pushServicePlatform,
50-
apnsType: registerTokenInfo.apnsType),
54+
apnsType: registerTokenInfo.apnsType),
5155
JsonKey.applicationName: registerTokenInfo.appName,
5256
JsonKey.dataFields: dataFields,
5357
]
@@ -67,27 +71,29 @@ struct RequestCreator {
6771

6872
func createUpdateUserRequest(dataFields: [AnyHashable: Any], mergeNestedObjects: Bool) -> Result<IterableRequest, IterableError> {
6973
if case .none = auth.emailOrUserId {
70-
ITBError("Both email and userId are nil")
71-
return .failure(IterableError.general(description: "Both email and userId are nil"))
74+
ITBError(Self.authMissingMessage)
75+
return .failure(IterableError.general(description: Self.authMissingMessage))
7276
}
73-
77+
7478
var body = [AnyHashable: Any]()
7579

76-
body[JsonKey.dataFields] = dataFields
77-
body[JsonKey.mergeNestedObjects] = NSNumber(value: mergeNestedObjects)
7880
setCurrentUser(inDict: &body)
7981

8082
if auth.email == nil, auth.userId != nil {
8183
body[JsonKey.preferUserId] = true
8284
}
8385

86+
body[JsonKey.mergeNestedObjects] = NSNumber(value: mergeNestedObjects)
87+
88+
body[JsonKey.dataFields] = dataFields
89+
8490
return .success(.post(createPostRequest(path: Const.Path.updateUser, body: body)))
8591
}
8692

8793
func createUpdateCartRequest(items: [CommerceItem], dataFields: [AnyHashable: Any]?) -> Result<IterableRequest, IterableError> {
8894
if case .none = auth.emailOrUserId {
89-
ITBError("Both email and userId are nil")
90-
return .failure(IterableError.general(description: "Both email and userId are nil"))
95+
ITBError(Self.authMissingMessage)
96+
return .failure(IterableError.general(description: Self.authMissingMessage))
9197
}
9298

9399
var apiUserDict = [AnyHashable: Any]()
@@ -108,16 +114,16 @@ struct RequestCreator {
108114

109115
func createTrackPurchaseRequest(_ total: NSNumber, items: [CommerceItem], dataFields: [AnyHashable: Any]?) -> Result<IterableRequest, IterableError> {
110116
if case .none = auth.emailOrUserId {
111-
ITBError("Both email and userId are nil")
112-
return .failure(IterableError.general(description: "Both email and userId are nil"))
117+
ITBError(Self.authMissingMessage)
118+
return .failure(IterableError.general(description: Self.authMissingMessage))
113119
}
114120

115-
let itemsToSerialize = items.map { $0.toDictionary() }
116-
117121
var apiUserDict = [AnyHashable: Any]()
118122

119123
setCurrentUser(inDict: &apiUserDict)
120124

125+
let itemsToSerialize = items.map { $0.toDictionary() }
126+
121127
var body: [String: Any] = [JsonKey.Commerce.user: apiUserDict,
122128
JsonKey.Commerce.items: itemsToSerialize,
123129
JsonKey.Commerce.total: total]
@@ -130,15 +136,12 @@ struct RequestCreator {
130136
}
131137

132138
func createTrackPushOpenRequest(_ campaignId: NSNumber, templateId: NSNumber?, messageId: String, appAlreadyRunning: Bool, dataFields: [AnyHashable: Any]?) -> Result<IterableRequest, IterableError> {
133-
var body = [AnyHashable: Any]()
134-
var reqDataFields = [AnyHashable: Any]()
135-
136-
if let dataFields = dataFields {
137-
reqDataFields = dataFields
139+
if case .none = auth.emailOrUserId {
140+
ITBError(Self.authMissingMessage)
141+
return .failure(IterableError.general(description: Self.authMissingMessage))
138142
}
139143

140-
reqDataFields[JsonKey.appAlreadyRunning] = appAlreadyRunning
141-
body[JsonKey.dataFields] = reqDataFields
144+
var body = [AnyHashable: Any]()
142145

143146
setCurrentUser(inDict: &body)
144147

@@ -150,19 +153,29 @@ struct RequestCreator {
150153

151154
body.setValue(for: JsonKey.messageId, value: messageId)
152155

156+
var compositeDataFields = [AnyHashable: Any]()
157+
158+
if let dataFields = dataFields {
159+
compositeDataFields = dataFields
160+
}
161+
162+
compositeDataFields[JsonKey.appAlreadyRunning] = appAlreadyRunning
163+
164+
body[JsonKey.dataFields] = compositeDataFields
165+
153166
return .success(.post(createPostRequest(path: Const.Path.trackPushOpen, body: body)))
154167
}
155168

156169
func createTrackEventRequest(_ eventName: String, dataFields: [AnyHashable: Any]?) -> Result<IterableRequest, IterableError> {
157170
if case .none = auth.emailOrUserId {
158-
ITBError("Both email and userId are nil")
159-
return .failure(IterableError.general(description: "Both email and userId are nil"))
171+
ITBError(Self.authMissingMessage)
172+
return .failure(IterableError.general(description: Self.authMissingMessage))
160173
}
161-
174+
162175
var body = [AnyHashable: Any]()
163176

164177
setCurrentUser(inDict: &body)
165-
178+
166179
body.setValue(for: JsonKey.eventName, value: eventName)
167180

168181
if let dataFields = dataFields {
@@ -179,10 +192,10 @@ struct RequestCreator {
179192
campaignId: NSNumber? = nil,
180193
templateId: NSNumber? = nil) -> Result<IterableRequest, IterableError> {
181194
if case .none = auth.emailOrUserId {
182-
ITBError("Both email and userId are nil")
183-
return .failure(IterableError.general(description: "Both email and userId are nil"))
195+
ITBError(Self.authMissingMessage)
196+
return .failure(IterableError.general(description: Self.authMissingMessage))
184197
}
185-
198+
186199
var body = [AnyHashable: Any]()
187200

188201
setCurrentUser(inDict: &body)
@@ -216,10 +229,10 @@ struct RequestCreator {
216229

217230
func createGetInAppMessagesRequest(_ count: NSNumber) -> Result<IterableRequest, IterableError> {
218231
if case .none = auth.emailOrUserId {
219-
ITBError("Both email and userId are nil")
220-
return .failure(IterableError.general(description: "Both email and userId are nil"))
232+
ITBError(Self.authMissingMessage)
233+
return .failure(IterableError.general(description: Self.authMissingMessage))
221234
}
222-
235+
223236
var args: [AnyHashable: Any] = [JsonKey.InApp.count: count.description,
224237
JsonKey.platform: JsonValue.iOS,
225238
JsonKey.systemVersion: UIDevice.current.systemVersion,
@@ -236,16 +249,15 @@ struct RequestCreator {
236249

237250
func createTrackInAppOpenRequest(inAppMessageContext: InAppMessageContext) -> Result<IterableRequest, IterableError> {
238251
if case .none = auth.emailOrUserId {
239-
ITBError("Both email and userId are nil")
240-
return .failure(IterableError.general(description: "Both email and userId are nil"))
252+
ITBError(Self.authMissingMessage)
253+
return .failure(IterableError.general(description: Self.authMissingMessage))
241254
}
242-
243-
var body = [AnyHashable: Any]()
244255

245-
body.setValue(for: JsonKey.messageId, value: inAppMessageContext.messageId)
256+
var body = [AnyHashable: Any]()
246257

247258
setCurrentUser(inDict: &body)
248259

260+
body.setValue(for: JsonKey.messageId, value: inAppMessageContext.messageId)
249261
body.setValue(for: JsonKey.inAppMessageContext, value: inAppMessageContext.toMessageContextDictionary())
250262
body.setValue(for: JsonKey.deviceInfo, value: deviceMetadata.asDictionary())
251263

@@ -258,18 +270,16 @@ struct RequestCreator {
258270

259271
func createTrackInAppClickRequest(inAppMessageContext: InAppMessageContext, clickedUrl: String) -> Result<IterableRequest, IterableError> {
260272
if case .none = auth.emailOrUserId {
261-
ITBError("Both email and userId are nil")
262-
return .failure(IterableError.general(description: "Both email and userId are nil"))
273+
ITBError(Self.authMissingMessage)
274+
return .failure(IterableError.general(description: Self.authMissingMessage))
263275
}
264-
265-
var body = [AnyHashable: Any]()
266276

267-
body.setValue(for: JsonKey.messageId, value: inAppMessageContext.messageId)
277+
var body = [AnyHashable: Any]()
268278

269279
setCurrentUser(inDict: &body)
270280

281+
body.setValue(for: JsonKey.messageId, value: inAppMessageContext.messageId)
271282
body.setValue(for: JsonKey.clickedUrl, value: clickedUrl)
272-
273283
body.setValue(for: JsonKey.inAppMessageContext, value: inAppMessageContext.toMessageContextDictionary())
274284
body.setValue(for: JsonKey.deviceInfo, value: deviceMetadata.asDictionary())
275285

@@ -282,13 +292,17 @@ struct RequestCreator {
282292

283293
func createTrackInAppCloseRequest(inAppMessageContext: InAppMessageContext, source: InAppCloseSource?, clickedUrl: String?) -> Result<IterableRequest, IterableError> {
284294
if case .none = auth.emailOrUserId {
285-
ITBError("Both email and userId are nil")
286-
return .failure(IterableError.general(description: "Both email and userId are nil"))
295+
ITBError(Self.authMissingMessage)
296+
return .failure(IterableError.general(description: Self.authMissingMessage))
287297
}
288-
298+
289299
var body = [AnyHashable: Any]()
290300

301+
setCurrentUser(inDict: &body)
302+
291303
body.setValue(for: JsonKey.messageId, value: inAppMessageContext.messageId)
304+
body.setValue(for: JsonKey.inAppMessageContext, value: inAppMessageContext.toMessageContextDictionary())
305+
body.setValue(for: JsonKey.deviceInfo, value: deviceMetadata.asDictionary())
292306

293307
if let source = source {
294308
body.setValue(for: JsonKey.closeAction, value: source)
@@ -298,30 +312,24 @@ struct RequestCreator {
298312
body.setValue(for: JsonKey.clickedUrl, value: clickedUrl)
299313
}
300314

301-
body.setValue(for: JsonKey.inAppMessageContext, value: inAppMessageContext.toMessageContextDictionary())
302-
body.setValue(for: JsonKey.deviceInfo, value: deviceMetadata.asDictionary())
303-
304315
if let inboxSessionId = inAppMessageContext.inboxSessionId {
305316
body.setValue(for: JsonKey.inboxSessionId, value: inboxSessionId)
306317
}
307318

308-
setCurrentUser(inDict: &body)
309-
310319
return .success(.post(createPostRequest(path: Const.Path.trackInAppClose, body: body)))
311320
}
312321

313322
func createTrackInAppDeliveryRequest(inAppMessageContext: InAppMessageContext) -> Result<IterableRequest, IterableError> {
314323
if case .none = auth.emailOrUserId {
315-
ITBError("Both email and userId are nil")
316-
return .failure(IterableError.general(description: "Both email and userId are nil"))
324+
ITBError(Self.authMissingMessage)
325+
return .failure(IterableError.general(description: Self.authMissingMessage))
317326
}
318-
319-
var body = [AnyHashable: Any]()
320327

321-
body.setValue(for: JsonKey.messageId, value: inAppMessageContext.messageId)
328+
var body = [AnyHashable: Any]()
322329

323330
setCurrentUser(inDict: &body)
324331

332+
body.setValue(for: JsonKey.messageId, value: inAppMessageContext.messageId)
325333
body.setValue(for: JsonKey.inAppMessageContext, value: inAppMessageContext.toMessageContextDictionary())
326334
body.setValue(for: JsonKey.deviceInfo, value: deviceMetadata.asDictionary())
327335

@@ -330,51 +338,50 @@ struct RequestCreator {
330338

331339
func createInAppConsumeRequest(_ messageId: String) -> Result<IterableRequest, IterableError> {
332340
if case .none = auth.emailOrUserId {
333-
ITBError("Both email and userId are nil")
334-
return .failure(IterableError.general(description: "Both email and userId are nil"))
341+
ITBError(Self.authMissingMessage)
342+
return .failure(IterableError.general(description: Self.authMissingMessage))
335343
}
336-
337-
var body = [AnyHashable: Any]()
338344

339-
body.setValue(for: JsonKey.messageId, value: messageId)
345+
var body = [AnyHashable: Any]()
340346

341347
setCurrentUser(inDict: &body)
342348

349+
body.setValue(for: JsonKey.messageId, value: messageId)
350+
343351
return .success(.post(createPostRequest(path: Const.Path.inAppConsume, body: body)))
344352
}
345353

346354
func createTrackInAppConsumeRequest(inAppMessageContext: InAppMessageContext, source: InAppDeleteSource?) -> Result<IterableRequest, IterableError> {
347355
if case .none = auth.emailOrUserId {
348-
ITBError("Both email and userId are nil")
349-
return .failure(IterableError.general(description: "Both email and userId are nil"))
356+
ITBError(Self.authMissingMessage)
357+
return .failure(IterableError.general(description: Self.authMissingMessage))
350358
}
351-
359+
352360
var body = [AnyHashable: Any]()
353361

362+
setCurrentUser(inDict: &body)
363+
354364
body.setValue(for: JsonKey.messageId, value: inAppMessageContext.messageId)
365+
body.setValue(for: JsonKey.inAppMessageContext, value: inAppMessageContext.toMessageContextDictionary())
366+
body.setValue(for: JsonKey.deviceInfo, value: deviceMetadata.asDictionary())
355367

356368
if let source = source {
357369
body.setValue(for: JsonKey.deleteAction, value: source)
358370
}
359371

360-
body.setValue(for: JsonKey.inAppMessageContext, value: inAppMessageContext.toMessageContextDictionary())
361-
body.setValue(for: JsonKey.deviceInfo, value: deviceMetadata.asDictionary())
362-
363372
if let inboxSessionId = inAppMessageContext.inboxSessionId {
364373
body.setValue(for: JsonKey.inboxSessionId, value: inboxSessionId)
365374
}
366375

367-
setCurrentUser(inDict: &body)
368-
369376
return .success(.post(createPostRequest(path: Const.Path.inAppConsume, body: body)))
370377
}
371378

372379
func createTrackInboxSessionRequest(inboxSession: IterableInboxSession) -> Result<IterableRequest, IterableError> {
373380
if case .none = auth.emailOrUserId {
374-
ITBError("Both email and userId are nil")
375-
return .failure(IterableError.general(description: "Both email and userId are nil"))
381+
ITBError(Self.authMissingMessage)
382+
return .failure(IterableError.general(description: Self.authMissingMessage))
376383
}
377-
384+
378385
guard let inboxSessionId = inboxSession.id else {
379386
return .failure(IterableError.general(description: "expecting session UUID"))
380387
}
@@ -388,7 +395,7 @@ struct RequestCreator {
388395
}
389396

390397
var body = [AnyHashable: Any]()
391-
398+
392399
setCurrentUser(inDict: &body)
393400

394401
body.setValue(for: JsonKey.inboxSessionId, value: inboxSessionId)
@@ -425,12 +432,14 @@ struct RequestCreator {
425432
if let packageName = Bundle.main.appPackageName {
426433
args[JsonKey.InApp.packageName] = packageName
427434
}
428-
435+
429436
return .success(.get(createGetRequest(forPath: Const.Path.getRemoteConfiguration, withArgs: args as! [String: String])))
430437
}
431438

432439
// MARK: - PRIVATE
433440

441+
private static let authMissingMessage = "Both email and userId are nil"
442+
434443
private func createPostRequest(path: String, body: [AnyHashable: Any]? = nil) -> PostRequest {
435444
PostRequest(path: path,
436445
args: [JsonKey.Header.apiKey: apiKey],
@@ -462,6 +471,5 @@ struct RequestCreator {
462471
case .none:
463472
ITBInfo("Current user is unavailable")
464473
}
465-
466474
}
467475
}

tests/unit-tests/InAppHelperTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ class InAppHelperTests: XCTestCase {
107107
fatalError()
108108
}
109109

110+
func updateCart(items: [CommerceItem], dataFields: [AnyHashable : Any]?) -> Future<SendRequestValue, SendRequestError> {
111+
fatalError()
112+
}
113+
110114
func track(purchase _: NSNumber, items _: [CommerceItem], dataFields _: [AnyHashable: Any]?) -> Future<SendRequestValue, SendRequestError> {
111115
fatalError()
112116
}

0 commit comments

Comments
 (0)