Skip to content

Commit

Permalink
feat(session): check sessionId and sessionStartTime before firing ses…
Browse files Browse the repository at this point in the history
…sion:start event

SUITEDEV-35464

Co-authored-by: Andras Sarro <[email protected]>
Co-authored-by: matusekma <[email protected]>
Co-authored-by: megamegax <[email protected]>
  • Loading branch information
4 people committed Apr 5, 2024
1 parent 0472bbe commit 0a44cf1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ final class MobileEngageSessionTests: EmarsysTestCase {
_ = try fakeEventClient.verify(\.fnSendEvents).times(times: .eq(0))
}

func testStart_shouldNotSendEvent_when_sessionWasAlreadyStarted() async throws {
self.sessionContext.contactToken = "testContactToken"
self.sessionContext.sessionId = testUuid
self.session.sessionStartTime = testDate

await self.session.start()

_ = try fakeEventClient.verify(\.fnSendEvents).times(times: .eq(0))
}

func testStart_shouldSet_sessionId_onSessionContext() async {
await self.session.start()

Expand Down
9 changes: 8 additions & 1 deletion Sources/EmarsysSDK/Session/MobileEngageSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class MobileEngageSession: SessionApi {
}

func start() async {
if ((self.sdkContext.config?.applicationCode != nil) && (self.sessionContext.contactToken != nil)) {
if (self.canStartSession()) {
self.sessionStartTime = self.timestampProvider.provide()
self.sessionContext.sessionId = self.uuidProvider.provide()
do {
Expand Down Expand Up @@ -84,4 +84,11 @@ class MobileEngageSession: SessionApi {
await self.stop()
}
}

private func canStartSession() -> Bool {
self.sdkContext.config?.applicationCode != nil &&
self.sessionContext.contactToken != nil &&
self.sessionContext.sessionId == nil &&
self.sessionStartTime == nil
}
}

0 comments on commit 0a44cf1

Please sign in to comment.