From 7e807dd1e4ca7ad0c2e98abca7ae24fb70043ea9 Mon Sep 17 00:00:00 2001 From: Brandon Stalnaker Date: Tue, 30 Apr 2024 10:04:41 -0400 Subject: [PATCH] fix: Allow Alias When Start and End Time Not Set --- UnitTests/MPIdentityTests.m | 37 ++++++++++++++++++-- mParticle-Apple-SDK/Identity/MPIdentityApi.m | 21 +++++------ 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/UnitTests/MPIdentityTests.m b/UnitTests/MPIdentityTests.m index c7ee0baf7..653dde739 100644 --- a/UnitTests/MPIdentityTests.m +++ b/UnitTests/MPIdentityTests.m @@ -1023,8 +1023,17 @@ - (void)testAliasNilDates { NSDate *startTime = nil; NSDate *endTime = nil; MPAliasRequest *request = [MPAliasRequest requestWithSourceMPID:mpid1 destinationMPID:mpid2 startTime:startTime endTime:endTime]; + XCTAssertEqual(request.sourceMPID, mpid1); + XCTAssertEqual(request.destinationMPID, mpid2); + XCTAssertFalse(request.startTime); + XCTAssertFalse(request.endTime); + BOOL result = [MParticle.sharedInstance.identity aliasUsers:request]; - XCTAssertFalse(result); + + XCTAssertTrue(request.startTime); + XCTAssertTrue(request.endTime); + XCTAssertTrue([request.startTime compare:request.endTime] == NSOrderedAscending); + XCTAssertTrue(result); } - (void)testAliasDatesReversed { @@ -1033,8 +1042,20 @@ - (void)testAliasDatesReversed { NSDate *startTime = [NSDate dateWithTimeIntervalSince1970:200]; NSDate *endTime = [NSDate dateWithTimeIntervalSince1970:100]; MPAliasRequest *request = [MPAliasRequest requestWithSourceMPID:mpid1 destinationMPID:mpid2 startTime:startTime endTime:endTime]; + XCTAssertEqual(request.sourceMPID, mpid1); + XCTAssertEqual(request.destinationMPID, mpid2); + XCTAssertEqual(request.startTime, startTime); + XCTAssertEqual(request.endTime, endTime); + XCTAssertTrue([request.startTime compare:request.endTime] != NSOrderedAscending); + BOOL result = [MParticle.sharedInstance.identity aliasUsers:request]; - XCTAssertFalse(result); + + XCTAssertEqual(request.sourceMPID, mpid1); + XCTAssertEqual(request.destinationMPID, mpid2); + XCTAssertEqual(request.startTime, startTime); + XCTAssertEqual(request.endTime, endTime); + XCTAssertTrue([request.startTime compare:request.endTime] != NSOrderedAscending); + XCTAssertTrue(result); } - (void)testAliasValidData { @@ -1043,7 +1064,19 @@ - (void)testAliasValidData { NSDate *startTime = [NSDate dateWithTimeIntervalSince1970:100]; NSDate *endTime = [NSDate dateWithTimeIntervalSince1970:200]; MPAliasRequest *request = [MPAliasRequest requestWithSourceMPID:mpid1 destinationMPID:mpid2 startTime:startTime endTime:endTime]; + XCTAssertEqual(request.sourceMPID, mpid1); + XCTAssertEqual(request.destinationMPID, mpid2); + XCTAssertEqual(request.startTime, startTime); + XCTAssertEqual(request.endTime, endTime); + XCTAssertTrue([request.startTime compare:request.endTime] == NSOrderedAscending); + BOOL result = [MParticle.sharedInstance.identity aliasUsers:request]; + + XCTAssertEqual(request.sourceMPID, mpid1); + XCTAssertEqual(request.destinationMPID, mpid2); + XCTAssertEqual(request.startTime, startTime); + XCTAssertEqual(request.endTime, endTime); + XCTAssertTrue([request.startTime compare:request.endTime] == NSOrderedAscending); XCTAssertTrue(result); } diff --git a/mParticle-Apple-SDK/Identity/MPIdentityApi.m b/mParticle-Apple-SDK/Identity/MPIdentityApi.m index cabde23cc..14ab10394 100644 --- a/mParticle-Apple-SDK/Identity/MPIdentityApi.m +++ b/mParticle-Apple-SDK/Identity/MPIdentityApi.m @@ -450,22 +450,23 @@ - (BOOL)aliasUsers:(MPAliasRequest *)aliasRequest { return NO; } + double maxDaysAgo = MParticle.sharedInstance.stateMachine.aliasMaxWindow != nil ? MParticle.sharedInstance.stateMachine.aliasMaxWindow.doubleValue : 90; + double secondsPerDay = 60*60*24; + NSDate *oldestAllowableDate = [NSDate dateWithTimeIntervalSinceNow:-1*secondsPerDay*maxDaysAgo]; + if (aliasRequest.usedFirstLastSeen) { - double maxDaysAgo = MParticle.sharedInstance.stateMachine.aliasMaxWindow.doubleValue; - double secondsPerDay = 60*60*24; - NSDate *oldestAllowableDate = [NSDate dateWithTimeIntervalSinceNow:-1*secondsPerDay*maxDaysAgo]; if ([aliasRequest.startTime compare:oldestAllowableDate] == NSOrderedAscending) { aliasRequest.startTime = oldestAllowableDate; } } - if (aliasRequest.startTime == nil || aliasRequest.endTime == nil || [aliasRequest.startTime compare:aliasRequest.endTime] != NSOrderedAscending) { - if (!aliasRequest.usedFirstLastSeen) { - MPILogError(@"Invalid alias request - both start and end dates must exist and start date must occur before end date."); - } else { - MPILogError(@"Invalid alias request - Source User has not been seen in the last %@ days", MParticle.sharedInstance.stateMachine.aliasMaxWindow); - } - return NO; + if (aliasRequest.startTime == nil || aliasRequest.endTime == nil) { + aliasRequest.startTime = oldestAllowableDate; + aliasRequest.endTime = [NSDate dateWithTimeIntervalSinceNow:0]; + } + + if ([aliasRequest.startTime compare:aliasRequest.endTime] != NSOrderedAscending) { + MPILogWarning(@"Invalid alias request - Start date must occur before end date. Alias Request will likely fail"); } dispatch_async([MParticle messageQueue], ^{