diff --git a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Operations/DataBrokerProfileQueryOperationManager.swift b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Operations/DataBrokerProfileQueryOperationManager.swift index a352b68aad..5dd14e563b 100644 --- a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Operations/DataBrokerProfileQueryOperationManager.swift +++ b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Operations/DataBrokerProfileQueryOperationManager.swift @@ -303,10 +303,10 @@ struct DataBrokerProfileQueryOperationManager: OperationsManager { let dateUpdater = OperationPreferredDateUpdaterUseCase(database: database) try dateUpdater.updateOperationDataDates(brokerId: brokerId, - profileQueryId: profileQueryId, - extractedProfileId: extractedProfileId, - schedulingConfig: schedulingConfig) - } + profileQueryId: profileQueryId, + extractedProfileId: extractedProfileId, + schedulingConfig: schedulingConfig) + } private func handleOperationError(brokerId: Int64, profileQueryId: Int64, extractedProfileId: Int64?, error: Error, database: DataBrokerProtectionRepository) { let event: HistoryEvent diff --git a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Operations/OperationPreferredDateCalculator.swift b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Operations/OperationPreferredDateCalculator.swift index 48e0289d30..8b936fb351 100644 --- a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Operations/OperationPreferredDateCalculator.swift +++ b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Operations/OperationPreferredDateCalculator.swift @@ -23,7 +23,8 @@ struct OperationPreferredDateCalculator { func dateForScanOperation(currentPreferredRunDate: Date?, historyEvents: [HistoryEvent], extractedProfileID: Int64?, - schedulingConfig: DataBrokerScheduleConfig) throws -> Date? { + schedulingConfig: DataBrokerScheduleConfig, + isDeprecated: Bool = false) throws -> Date? { var newDate: Date? guard let lastEvent = historyEvents.last else { @@ -32,7 +33,13 @@ struct OperationPreferredDateCalculator { switch lastEvent.type { - case .noMatchFound, .matchesFound, .optOutConfirmed: + case .optOutConfirmed: + if isDeprecated { + return nil + } else { + newDate = Date().addingTimeInterval(schedulingConfig.maintenanceScan.hoursToSeconds) + } + case .noMatchFound, .matchesFound: newDate = Date().addingTimeInterval(schedulingConfig.maintenanceScan.hoursToSeconds) case .error: newDate = Date().addingTimeInterval(schedulingConfig.retryError.hoursToSeconds) diff --git a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Operations/OperationPreferredDateUpdater.swift b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Operations/OperationPreferredDateUpdater.swift index 6e33075e59..f87f696928 100644 --- a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Operations/OperationPreferredDateUpdater.swift +++ b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/Operations/OperationPreferredDateUpdater.swift @@ -34,7 +34,6 @@ struct OperationPreferredDateUpdaterUseCase: OperationPreferredDateUpdater { let database: DataBrokerProtectionRepository private let calculator = OperationPreferredDateCalculator() - // It would probably be good to break this into 2 methods, for scan and opt-out func updateOperationDataDates(brokerId: Int64, profileQueryId: Int64, extractedProfileId: Int64?, @@ -43,36 +42,19 @@ struct OperationPreferredDateUpdaterUseCase: OperationPreferredDateUpdater { guard let brokerProfileQuery = database.brokerProfileQueryData(for: brokerId, and: profileQueryId) else { return } - let currentScanPreferredRunDate = brokerProfileQuery.scanOperationData.preferredRunDate - - let newScanPreferredRunDate = try calculator.dateForScanOperation(currentPreferredRunDate: currentScanPreferredRunDate, - historyEvents: brokerProfileQuery.events, - extractedProfileID: extractedProfileId, - schedulingConfig: schedulingConfig) - - if newScanPreferredRunDate != currentScanPreferredRunDate { - updatePreferredRunDate(newScanPreferredRunDate, - brokerId: brokerId, - profileQueryId: profileQueryId, - extractedProfileId: nil) - } + try updateScanOperationDataDates(brokerId: brokerId, + profileQueryId: profileQueryId, + extractedProfileId: extractedProfileId, + schedulingConfig: schedulingConfig, + brokerProfileQuery: brokerProfileQuery) // We only need to update the optOut date if we have an extracted profile ID if let extractedProfileId = extractedProfileId { - let optOutOperation = brokerProfileQuery.optOutOperationsData.filter { $0.extractedProfile.id == extractedProfileId }.first - let currentOptOutPreferredRunDate = optOutOperation?.preferredRunDate - - let newOptOutPreferredDate = try calculator.dateForOptOutOperation(currentPreferredRunDate: currentOptOutPreferredRunDate, - historyEvents: brokerProfileQuery.events, - extractedProfileID: extractedProfileId, - schedulingConfig: schedulingConfig) - - if newOptOutPreferredDate != currentOptOutPreferredRunDate { - updatePreferredRunDate(newOptOutPreferredDate, - brokerId: brokerId, - profileQueryId: profileQueryId, - extractedProfileId: extractedProfileId) - } + try updateOptOutOperationDataDates(brokerId: brokerId, + profileQueryId: profileQueryId, + extractedProfileId: extractedProfileId, + schedulingConfig: schedulingConfig, + brokerProfileQuery: brokerProfileQuery) } } @@ -91,6 +73,50 @@ struct OperationPreferredDateUpdaterUseCase: OperationPreferredDateUpdater { } } + private func updateScanOperationDataDates(brokerId: Int64, + profileQueryId: Int64, + extractedProfileId: Int64?, + schedulingConfig: DataBrokerScheduleConfig, + brokerProfileQuery: BrokerProfileQueryData) throws { + + let currentScanPreferredRunDate = brokerProfileQuery.scanOperationData.preferredRunDate + + let newScanPreferredRunDate = try calculator.dateForScanOperation(currentPreferredRunDate: currentScanPreferredRunDate, + historyEvents: brokerProfileQuery.events, + extractedProfileID: extractedProfileId, + schedulingConfig: schedulingConfig, + isDeprecated: brokerProfileQuery.profileQuery.deprecated) + + if newScanPreferredRunDate != currentScanPreferredRunDate { + updatePreferredRunDate(newScanPreferredRunDate, + brokerId: brokerId, + profileQueryId: profileQueryId, + extractedProfileId: nil) + } + } + + private func updateOptOutOperationDataDates(brokerId: Int64, + profileQueryId: Int64, + extractedProfileId: Int64?, + schedulingConfig: DataBrokerScheduleConfig, + brokerProfileQuery: BrokerProfileQueryData) throws { + + let optOutOperation = brokerProfileQuery.optOutOperationsData.filter { $0.extractedProfile.id == extractedProfileId }.first + let currentOptOutPreferredRunDate = optOutOperation?.preferredRunDate + + let newOptOutPreferredDate = try calculator.dateForOptOutOperation(currentPreferredRunDate: currentOptOutPreferredRunDate, + historyEvents: brokerProfileQuery.events, + extractedProfileID: extractedProfileId, + schedulingConfig: schedulingConfig) + + if newOptOutPreferredDate != currentOptOutPreferredRunDate { + updatePreferredRunDate(newOptOutPreferredDate, + brokerId: brokerId, + profileQueryId: profileQueryId, + extractedProfileId: extractedProfileId) + } + } + private func updatePreferredRunDate( _ date: Date?, brokerId: Int64, profileQueryId: Int64, diff --git a/LocalPackages/DataBrokerProtection/Tests/DataBrokerProtectionTests/OperationPreferredDateCalculatorTests.swift b/LocalPackages/DataBrokerProtection/Tests/DataBrokerProtectionTests/OperationPreferredDateCalculatorTests.swift index f058d07560..ca980b2caf 100644 --- a/LocalPackages/DataBrokerProtection/Tests/DataBrokerProtectionTests/OperationPreferredDateCalculatorTests.swift +++ b/LocalPackages/DataBrokerProtection/Tests/DataBrokerProtectionTests/OperationPreferredDateCalculatorTests.swift @@ -18,7 +18,7 @@ import XCTest @testable import DataBrokerProtection - +// swiftlint:disable type_body_length // https://app.asana.com/0/1204586965688315/1204834439855281/f final class OperationPreferredDateCalculatorTests: XCTestCase { @@ -50,6 +50,23 @@ final class OperationPreferredDateCalculatorTests: XCTestCase { XCTAssertTrue(areDatesEqualIgnoringSeconds(date1: expectedScanDate, date2: actualScanDate)) } + func testOptOutConfirmedOnDeprecatedProfile_thenScanDateIsNil() throws { + let historyEvents = [ + HistoryEvent(extractedProfileId: 1, + brokerId: 1, + profileQueryId: 1, + type: .optOutConfirmed)] + + let calculator = OperationPreferredDateCalculator() + + let actualScanDate = try calculator.dateForScanOperation(currentPreferredRunDate: nil, + historyEvents: historyEvents, + extractedProfileID: nil, + schedulingConfig: schedulingConfig, + isDeprecated: true) + + XCTAssertNil(actualScanDate) + } /* If the time elapsed since the last profile removal exceeds the current date plus maintenance period (expired), we should proceed with scheduling a new opt-out request as the broker has failed to honor the previous one. */ @@ -696,3 +713,4 @@ final class OperationPreferredDateCalculatorTests: XCTestCase { XCTAssertTrue(areDatesEqualIgnoringSeconds(date1: expectedOptOutDate, date2: actualOptOutDate)) } } +// swiftlint:enable type_body_length