Skip to content

Commit

Permalink
Set scan date to nil if profile is deprecated (#1746)
Browse files Browse the repository at this point in the history
Task/Issue URL:
https://app.asana.com/0/1204167627774280/1205574642847436/f

**Description**:
Set scan date to nil if profile is deprecated
  • Loading branch information
Bunn authored Oct 11, 2023
1 parent 0fc4478 commit 69dda22
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
Expand All @@ -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)
}
}

Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -696,3 +713,4 @@ final class OperationPreferredDateCalculatorTests: XCTestCase {
XCTAssertTrue(areDatesEqualIgnoringSeconds(date1: expectedOptOutDate, date2: actualOptOutDate))
}
}
// swiftlint:enable type_body_length

0 comments on commit 69dda22

Please sign in to comment.