Skip to content

Commit

Permalink
feat: new rules for IT entry (under 18)
Browse files Browse the repository at this point in the history
Co-authored-by: Emilio Apuzzo <emilio.apuzzo@ibm.com>
  • Loading branch information
Fehniix and Emilio Apuzzo authored Apr 5, 2022
1 parent e4d8a87 commit 6da0836
Show file tree
Hide file tree
Showing 7 changed files with 502 additions and 506 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct VaccinationInfo {
let vaccineDate: Date
let countryCode: String
let patientAge: Int?
let patientBirthDate: Date?

var emaAllProducts: [String]?{
return LocalData.getSetting(from: "EMA_vaccines")?.split(separator: ";").map{ String($0) }
Expand All @@ -48,6 +49,13 @@ struct VaccinationInfo {
var isCurrentDoseComplete: Bool { self.currentDoses == self.totalDoses && !self.isJJBooster && !self.isNonJJBooster }
var isCurrentDoseBooster: Bool { (self.currentDoses > self.totalDoses) || (isJJBooster || self.isNonJJBooster) }

func isPatientUnder18(vaccineUnder18Offset: Int) -> Bool {
guard let patientBirthDate = self.patientBirthDate?.add(vaccineUnder18Offset, ofType: .day) else { return false }
let computedPatientAge = Calendar.current.dateComponents([.year, .month, .day], from: patientBirthDate, to: Date())
guard let computedPatientAgeYear = computedPatientAge.year else { return false }
return computedPatientAgeYear < 18
}

var isEMAProduct: Bool {
if (emaAllProducts?.contains(medicalProduct) ?? false) // (Sputnik-V solo se emesso da San marino ovvero co="SM")
|| (countryCode == Constants.sanMarinoCode && medicalProduct == Constants.SputnikVacineCode) {
Expand Down Expand Up @@ -125,11 +133,10 @@ class VaccineConcreteValidator: DGCValidator {
guard let medicalProduct = hcert.medicalProduct else { return nil }
guard let countryCode = hcert.countryCode else { return nil }

return VaccinationInfo(currentDoses: currentDoses, totalDoses: totalDoses, medicalProduct: medicalProduct, vaccineDate: vaccineDate, countryCode: countryCode, patientAge: hcert.age)
return VaccinationInfo(currentDoses: currentDoses, totalDoses: totalDoses, medicalProduct: medicalProduct, vaccineDate: vaccineDate, countryCode: countryCode, patientAge: hcert.age, patientBirthDate: hcert.birthDate)
}

func checkVaccinationInterval(_ vaccinationInfo: VaccinationInfo) -> Status {

guard let start = getStartDays(vaccinationInfo: vaccinationInfo) else { return .notValid }
guard let end = getEndDays(vaccinationInfo: vaccinationInfo) else { return .notValid }
guard let ext = getExtensionDays(vaccinationInfo: vaccinationInfo) else { return .notValid }
Expand Down Expand Up @@ -215,7 +222,6 @@ class VaccineConcreteValidator: DGCValidator {
return endDaysForCompleteDose(vaccinationInfo)
}


public func extDaysForBoosterDose(_ vaccinationInfo: VaccinationInfo) -> Int? {
return endDaysForBoosterDose(vaccinationInfo)
}
Expand Down Expand Up @@ -281,7 +287,7 @@ class VaccineBoosterValidator: VaccineConcreteValidator {
}

class VaccineItalyEntryValidator: VaccineConcreteValidator {

override func validate(hcert: HCert) -> Status {
guard let vaccinationInfo = getVaccinationData(hcert) else { return .notValid }

Expand All @@ -307,7 +313,12 @@ class VaccineItalyEntryValidator: VaccineConcreteValidator {
}

public override func endDaysForCompleteDose(_ vaccinationInfo: VaccinationInfo) -> Int? {
let setting = Constants.vaccineCompleteEndDays_NOT_IT
var setting = Constants.vaccineCompleteEndDays_NOT_IT
let vaccineUnder18Offset: Int = self.getValue(for: Constants.vaccineCompleteEndDays_under_18_offset)?.intValue ?? 0
if vaccinationInfo.isPatientUnder18(vaccineUnder18Offset: vaccineUnder18Offset) {
setting = Constants.vaccineCompleteEndDays_under_18
}

return self.getValue(for: setting)?.intValue
}

Expand Down
3 changes: 3 additions & 0 deletions DGCAVerifier/Models/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ struct Constants {
static let vaccineBoosterStartDays_NOT_IT = "vaccine_start_day_booster_NOT_IT"
static let vaccineBoosterEndDays_NOT_IT = "vaccine_end_day_booster_NOT_IT"

static let vaccineCompleteEndDays_under_18 = "vaccine_end_day_complete_under_18"
static let vaccineCompleteEndDays_under_18_offset = "vaccine_complete_under_18_offset"

// MARK: EMA settings
static let vaccineCompleteEndDays_EMA = "vaccine_end_day_complete_EMA"
static let vaccineCompleteExtendedDays_EMA = "vaccine_end_day_complete_extended_EMA"
Expand Down
4 changes: 2 additions & 2 deletions DGCAVerifier/Pages/Result/VerificationViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ class VerificationViewController: UIViewController {
guard status.showPersonalData else { return }
guard let cert = viewModel.hCert else { return }
guard !cert.name.isEmpty else { return }
guard !cert.birthDate.isEmpty else { return }
guard !cert.birthDateString.isEmpty else { return }
let name = getResult(cert.name, for: "result.name")
let birthDate = getResult(cert.birthDate, for: "result.birthdate")
let birthDate = getResult(cert.birthDateString, for: "result.birthdate")
personalDataStackView.addArrangedSubview(name)
personalDataStackView.addArrangedSubview(birthDate)
personalDataStackView.superview?.isHidden = false
Expand Down
29 changes: 24 additions & 5 deletions DGCAVerifier/Utils/HCert/HCert+PersonalData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extension HCert {

}

var birthDate: String {
var birthDateString: String {
// TODO: use date formats to be placed inside Constants
let dob: String = body["dob"].string ?? ""

Expand All @@ -66,7 +66,7 @@ extension HCert {
}

var birthYear: Int? {
guard let birthYear = Int(birthDate[4]) else { return nil }
guard let birthYear = Int(birthDateString[4]) else { return nil }
return birthYear
}

Expand All @@ -77,17 +77,36 @@ extension HCert {
dateFormatter.dateFormat = $0

if $0 == "yyyy" {
return dateFormatter.date(from: birthDate)?.endOfYear()
return dateFormatter.date(from: birthDateString)?.endOfYear()
}

if $0 == "MM/yyyy" {
return dateFormatter.date(from: birthDate)?.endOfMonth()
return dateFormatter.date(from: birthDateString)?.endOfMonth()
}

return dateFormatter.date(from: birthDate)
return dateFormatter.date(from: birthDateString)
}
guard let birthdayDate = dates.first else { return nil }
return Calendar.current.dateComponents([.year, .month, .day], from: birthdayDate, to: Date()).year
}

var birthDate: Date? {
let dateFormatter = DateFormatter.getDefault(utc: true)
let formats = ["yyyy", "MM/yyyy", "dd/MM/yyyy"]
let dates: [Date] = formats.compactMap {
dateFormatter.dateFormat = $0

if $0 == "yyyy" {
return dateFormatter.date(from: birthDateString)?.endOfYear()
}

if $0 == "MM/yyyy" {
return dateFormatter.date(from: birthDateString)?.endOfMonth()
}

return dateFormatter.date(from: birthDateString)
}
guard let birthdayDate = dates.first else { return nil }
return birthdayDate
}
}
6 changes: 3 additions & 3 deletions DGCAVerifierTests/AutomatedTests/AutomatedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class AutomatedTests: XCTestCase {
var testCases = [TestCase]()

var scanModeCols = ["Base", "Ingresso in italia", "Rafforzata", "Visitatori RSA", "Studenti", "Lavoro"]

private func loadTestArrayfromCSV(fromURL url: URL, rowSeparator: String = "\r", colSeparator: String = ";") -> [TestCase] {
guard let csv = try? String(contentsOf: url, encoding: .utf8) else { return [] }
let rows = csv.components(separatedBy: rowSeparator)
Expand Down Expand Up @@ -116,15 +116,15 @@ class AutomatedTests: XCTestCase {
func printTestsReport() {

var resultsCSVString: String = "TEST ID;Base;Ex. Base;Rafforzata;Ex. Rafforzata;Visitatori RSA;Ex. Visitatori RSA;Studenti;Ex. Studenti;Lavoro;Ex. Lavoro;Ingresso in Italia;Ex. Ingresso in Italia;\n"

if plainResults {
resultsCSVString = "TEST ID;Base;Rafforzata;Visitatori RSA;Studenti;Lavoro;Ingresso in Italia;\n"
}

testCases.map{ testCase in
// Re-order scanModeCols to generate the CSV with columns aligned with Android
self.scanModeCols = ["Base", "Rafforzata", "Visitatori RSA", "Studenti", "Lavoro", "Ingresso in italia"]

let results: String = scanModeCols.map{ scanMode in
let actualValidity: String = testCase.actualValidity?.filter{ $0.mode == scanMode }.first?.result ?? ""
var expectedValidity: String = testCase.expectedValidity.filter{ $0.mode == scanMode }.first?.result ?? "-"
Expand Down
Loading

0 comments on commit 6da0836

Please sign in to comment.