Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow Alias When Start and End Time Not Set #275

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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