Skip to content

Commit

Permalink
fix: Allow Alias When Start and End Time Not Set
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonStalnaker committed Apr 30, 2024
1 parent 5ea7f68 commit 7e807dd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
37 changes: 35 additions & 2 deletions UnitTests/MPIdentityTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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);
}

Expand Down
21 changes: 11 additions & 10 deletions mParticle-Apple-SDK/Identity/MPIdentityApi.m
Original file line number Diff line number Diff line change
Expand Up @@ -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], ^{
Expand Down

0 comments on commit 7e807dd

Please sign in to comment.