Skip to content

Commit

Permalink
feature: school scan mode
Browse files Browse the repository at this point in the history
Co-authored-by: eapuzzo <[email protected]>
Co-authored-by: Johnny Bueti <[email protected]>
Co-authored-by: Ludovico Girolimini <[email protected]>
  • Loading branch information
4 people authored Feb 4, 2022
1 parent 307ae3e commit b78fb8a
Show file tree
Hide file tree
Showing 12 changed files with 416 additions and 307 deletions.
131 changes: 74 additions & 57 deletions DGCAVerifier/BusinessRules/Internal/RecoveryValidityCheck.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,80 +29,97 @@ import SwiftDGC
struct RecoveryValidityCheck {

typealias Validator = MedicalRulesValidator

func isRecoveryValid(_ hcert: HCert) -> Status {
guard let validFrom = hcert.recoveryDateFrom else { return .notValid }
guard let validUntil = hcert.recoveryDateUntil else { return .notValid }

guard let recoveryValidFromDate = validFrom.toRecoveryDate else { return .notValid }
guard let recoveryValidUntilDate = validUntil.toRecoveryDate else { return .notValid }

var start: Int?
var end: Int?

let scanMode: String = Store.get(key: .scanMode) ?? ""
switch scanMode {
case Constants.scanMode2G:
start = getStartDays(from: hcert)
end = getEndDays(from: hcert)
case Constants.scanMode3G:
start = getStartDays3G(from: hcert)
end = getEndDays3G(from: hcert)
case Constants.scanModeBooster:
start = getStartDays(from: hcert)
end = getEndDays(from: hcert)
default:
return .notValid

private func validityEnd(_ hcert: HCert, dateFrom: Date, dateUntil: Date, additionalDays: Int) -> Date? {
if isSchoolScanMode() {
guard let recoveryDateFirstPositive = hcert.recoveryDateFirstPositive?.toRecoveryDate else { return nil }
guard let validityExtension = recoveryDateFirstPositive.add(additionalDays, ofType: .day) else { return nil }
return dateUntil < validityExtension ? dateUntil : validityExtension

} else {
guard let validityExtension = dateFrom.add(additionalDays, ofType: .day) else { return nil }
return dateUntil > validityExtension ? dateUntil : validityExtension
}
}

func isRecoveryValid(_ hcert: HCert) -> Status {

guard let validityFrom = hcert.recoveryDateFrom?.toRecoveryDate else { return .notValid }
guard let validityUntil = hcert.recoveryDateUntil?.toRecoveryDate else { return .notValid }

guard let recoveryStartDays = getStartDays(from: hcert) else { return .notValid }
guard let recoveryEndDays = getEndDays(from: hcert) else { return .notValid }

guard let recoveryStartDays = start else { return .notGreenPass }
guard let recoveryEndDays = end else { return .notGreenPass }
guard let validityStart = validityFrom.add(recoveryStartDays, ofType: .day) else { return .notValid }
guard let validityEnd = validityEnd(hcert, dateFrom: validityFrom, dateUntil: validityUntil, additionalDays: recoveryEndDays) else { return .notValid }

guard let validityStart = recoveryValidFromDate.add(recoveryStartDays, ofType: .day) else { return .notValid }
let validityEnd = recoveryValidUntilDate
guard let validityExtension = recoveryValidFromDate.add(recoveryEndDays, ofType: .day) else { return .notValid }

guard let currentDate = Date.startOfDay else { return .notValid }

let recoveryStatus = Validator.validate(currentDate, from: validityStart, to: validityEnd, extendedTo: validityExtension)
let recoveryStatus = Validator.validate(currentDate, from: validityStart, to: validityEnd)

guard scanMode != Constants.scanModeBooster else { return recoveryStatus == .valid ? .verificationIsNeeded : recoveryStatus }
guard !isBoosterScanMode() else { return recoveryStatus == .valid ? .verificationIsNeeded : recoveryStatus }

return recoveryStatus
}

private func getStartDays3G(from hcert: HCert) -> Int? {
let isITCode = hcert.countryCode == Constants.ItalyCountryCode
let startDaysConfig: String
if isSpecialRecovery(hcert: hcert){
startDaysConfig = Constants.recoverySpecialStartDays
}
else {
startDaysConfig = isITCode ? Constants.recoveryStartDays_IT : Constants.recoveryStartDays_NOT_IT

private func getEndDays(from hcert: HCert) -> Int? {
let scanMode: String = Store.get(key: .scanMode) ?? ""
let isSpecialRecovery = isSpecialRecovery(hcert: hcert)
switch scanMode {
case Constants.scanMode2G, Constants.scanModeBooster:
let endDaysConfig = isSpecialRecovery ? Constants.recoverySpecialEndDays : Constants.recoveryEndDays_IT
return getValue(for: endDaysConfig)?.intValue
case Constants.scanMode3G:
let isITCode = hcert.countryCode == Constants.ItalyCountryCode
let endDaysConfig: String
if isSpecialRecovery{
endDaysConfig = Constants.recoverySpecialEndDays
}
else {
endDaysConfig = isITCode ? Constants.recoveryEndDays_IT : Constants.recoveryEndDays_NOT_IT
}
return getValue(for: endDaysConfig)?.intValue
case Constants.scanModeSchool:
let endDaysConfig = Constants.recoverySchoolEndDays
return getValue(for: endDaysConfig)?.intValue
default:
return nil
}
return getValue(for: startDaysConfig)?.intValue
}

private func getEndDays3G(from hcert: HCert) -> Int? {
let isITCode = hcert.countryCode == Constants.ItalyCountryCode
let startDaysConfig: String
if isSpecialRecovery(hcert: hcert){
startDaysConfig = Constants.recoverySpecialEndDays
}
else {
startDaysConfig = isITCode ? Constants.recoveryEndDays_IT : Constants.recoveryEndDays_NOT_IT
private func getStartDays(from hcert: HCert) -> Int? {
let scanMode: String = Store.get(key: .scanMode) ?? ""
let isSpecialRecovery = isSpecialRecovery(hcert: hcert)
switch scanMode {
case Constants.scanMode2G, Constants.scanModeBooster:
let startDaysConfig = isSpecialRecovery ? Constants.recoverySpecialStartDays : Constants.recoveryStartDays_IT
return getValue(for: startDaysConfig)?.intValue
case Constants.scanMode3G:
let isITCode = hcert.countryCode == Constants.ItalyCountryCode
let startDaysConfig: String
if isSpecialRecovery{
startDaysConfig = Constants.recoverySpecialStartDays
}
else {
startDaysConfig = isITCode ? Constants.recoveryStartDays_IT : Constants.recoveryStartDays_NOT_IT
}
return getValue(for: startDaysConfig)?.intValue
case Constants.scanModeSchool:
let startDaysConfig = Constants.recoveryStartDays_IT
return getValue(for: startDaysConfig)?.intValue
default:
return nil
}
return getValue(for: startDaysConfig)?.intValue
}

private func getStartDays(from hcert: HCert) -> Int? {
let startDaysConfig = isSpecialRecovery(hcert: hcert) ? Constants.recoverySpecialStartDays : Constants.recoveryStartDays_IT
return getValue(for: startDaysConfig)?.intValue
private func isSchoolScanMode() -> Bool{
let scanMode: String = Store.get(key: .scanMode) ?? ""
return scanMode == Constants.scanModeSchool
}

private func getEndDays(from hcert: HCert) -> Int? {
let endDaysConfig = isSpecialRecovery(hcert: hcert) ? Constants.recoverySpecialEndDays : Constants.recoveryEndDays_IT
return getValue(for: endDaysConfig)?.intValue
private func isBoosterScanMode() -> Bool{
let scanMode: String = Store.get(key: .scanMode) ?? ""
return scanMode == Constants.scanModeBooster
}

private func isSpecialRecovery(hcert: HCert) -> Bool {
Expand Down
29 changes: 18 additions & 11 deletions DGCAVerifier/BusinessRules/Internal/TestValidityCheck.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct TestValidityCheck {

typealias Validator = MedicalRulesValidator

func isTestDateValid(_ hcert: HCert) -> Status {
private func isTestDateValid(_ hcert: HCert) -> Status {
guard hcert.isKnownTestType else { return .notValid }

let startHours = getStartHours(for: hcert)
Expand All @@ -47,37 +47,44 @@ struct TestValidityCheck {
return Validator.validate(Date(), from: validityStart, to: validityEnd)
}

func isTestNegative(_ hcert: HCert) -> Status {
private func isTestNegative(_ hcert: HCert) -> Status {
guard let isNegative = hcert.testNegative else { return .notValid }
return isNegative ? .valid : .notValid
}

func isTestValid(_ hcert: HCert) -> Status {
private func testStatusForScanMode(_ hcert: HCert) -> Status {
let scanMode: String = Store.get(key: .scanMode) ?? ""
guard scanMode != Constants.scanMode2G, scanMode != Constants.scanModeBooster else { return .notValid }
if (scanMode == Constants.scanMode2G || scanMode == Constants.scanModeBooster || scanMode == Constants.scanModeSchool) {
return .notValid
}
else { return .valid }
}

func isTestValid(_ hcert: HCert) -> Status {
guard testStatusForScanMode(hcert) == .valid else { return .notValid }
let testValidityResults = [isTestNegative(hcert), isTestDateValid(hcert)]
return testValidityResults.first(where: {$0 != .valid}) ?? .valid
}

func getStartHours(for hcert: HCert) -> String? {
private func getStartHours(for hcert: HCert) -> String? {
if (hcert.isMolecularTest) { return molecularStartHours }
if (hcert.isRapidTest) { return rapidStartHours }
return nil
}

func getEndHours(for hcert: HCert) -> String? {
private func getEndHours(for hcert: HCert) -> String? {
if (hcert.isMolecularTest) { return molecularEndHours }
if (hcert.isRapidTest) { return rapidEndHours }
return nil
}

func getValue(from key: String) -> String? {
private func getValue(from key: String) -> String? {
LocalData.getSetting(from: key)
}

var molecularStartHours: String? { getValue(from: Constants.molecularStartHoursKey) }
var molecularEndHours: String? { getValue(from: Constants.molecularEndHoursKey) }
var rapidStartHours: String? { getValue(from: Constants.rapidStartHoursKey) }
var rapidEndHours: String? { getValue(from: Constants.rapidEndHoursKey) }
private var molecularStartHours: String? { getValue(from: Constants.molecularStartHoursKey) }
private var molecularEndHours: String? { getValue(from: Constants.molecularEndHoursKey) }
private var rapidStartHours: String? { getValue(from: Constants.rapidStartHoursKey) }
private var rapidEndHours: String? { getValue(from: Constants.rapidEndHoursKey) }

}
Loading

0 comments on commit b78fb8a

Please sign in to comment.