diff --git a/Sources/SpeziFHIR/Extensions/ConditionOnsetXR4+Date.swift b/Sources/SpeziFHIR/Extensions/ConditionOnsetXR4+Date.swift new file mode 100644 index 0000000..837113b --- /dev/null +++ b/Sources/SpeziFHIR/Extensions/ConditionOnsetXR4+Date.swift @@ -0,0 +1,28 @@ +// +// This source file is part of the Stanford Spezi open source project +// +// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md) +// +// SPDX-License-Identifier: MIT +// + +import Foundation +import ModelsR4 + + +extension Condition.OnsetX { + var date: Date? { + switch self { + case let .dateTime(date): + return try? date.value?.asNSDate() + case let .period(period): + return period.date + case .age: + return nil + case .range: + return nil + case .string: + return nil + } + } +} diff --git a/Sources/SpeziFHIR/Extensions/DiagnosticReportEffectiveXR4+Date.swift b/Sources/SpeziFHIR/Extensions/DiagnosticReportEffectiveXR4+Date.swift new file mode 100644 index 0000000..53585fc --- /dev/null +++ b/Sources/SpeziFHIR/Extensions/DiagnosticReportEffectiveXR4+Date.swift @@ -0,0 +1,22 @@ +// +// This source file is part of the Stanford Spezi open source project +// +// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md) +// +// SPDX-License-Identifier: MIT +// + +import Foundation +import ModelsR4 + + +extension DiagnosticReport.EffectiveX { + var date: Date? { + switch self { + case let .dateTime(date): + return try? date.value?.asNSDate() + case let .period(period): + return period.date + } + } +} diff --git a/Sources/SpeziFHIR/Extensions/ImmunizationOccurrenceXR4+Date.swift b/Sources/SpeziFHIR/Extensions/ImmunizationOccurrenceXR4+Date.swift new file mode 100644 index 0000000..1426168 --- /dev/null +++ b/Sources/SpeziFHIR/Extensions/ImmunizationOccurrenceXR4+Date.swift @@ -0,0 +1,22 @@ +// +// This source file is part of the Stanford Spezi open source project +// +// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md) +// +// SPDX-License-Identifier: MIT +// + +import Foundation +import ModelsR4 + + +extension Immunization.OccurrenceX { + var date: Date? { + switch self { + case let .dateTime(date): + return try? date.value?.asNSDate() + case .string: + return nil + } + } +} diff --git a/Sources/SpeziFHIR/Extensions/MedicationAdministrationEffectiveXR4+Date.swift b/Sources/SpeziFHIR/Extensions/MedicationAdministrationEffectiveXR4+Date.swift new file mode 100644 index 0000000..b68819e --- /dev/null +++ b/Sources/SpeziFHIR/Extensions/MedicationAdministrationEffectiveXR4+Date.swift @@ -0,0 +1,23 @@ +// +// This source file is part of the Stanford Spezi open source project +// +// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md) +// +// SPDX-License-Identifier: MIT +// + +import Foundation +import ModelsR4 + + +extension Period { + var date: Date? { + if let endDate = try? end?.value?.asNSDate() { + return endDate + } + if let startDate = try? start?.value?.asNSDate() { + return startDate + } + return nil + } +} diff --git a/Sources/SpeziFHIR/Extensions/PeriodR4+Date.swift b/Sources/SpeziFHIR/Extensions/PeriodR4+Date.swift new file mode 100644 index 0000000..340aca8 --- /dev/null +++ b/Sources/SpeziFHIR/Extensions/PeriodR4+Date.swift @@ -0,0 +1,22 @@ +// +// This source file is part of the Stanford Spezi open source project +// +// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md) +// +// SPDX-License-Identifier: MIT +// + +import Foundation +import ModelsR4 + + +extension MedicationAdministration.EffectiveX { + var date: Date? { + switch self { + case let .dateTime(date): + return try? date.value?.asNSDate() + case let .period(period): + return period.date + } + } +} diff --git a/Sources/SpeziFHIR/Extensions/ProcedurePerformedXR4+Date.swift b/Sources/SpeziFHIR/Extensions/ProcedurePerformedXR4+Date.swift new file mode 100644 index 0000000..ae46302 --- /dev/null +++ b/Sources/SpeziFHIR/Extensions/ProcedurePerformedXR4+Date.swift @@ -0,0 +1,28 @@ +// +// This source file is part of the Stanford Spezi open source project +// +// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md) +// +// SPDX-License-Identifier: MIT +// + +import Foundation +import ModelsR4 + + +extension Procedure.PerformedX { + var date: Date? { + switch self { + case let .dateTime(date): + return try? date.value?.asNSDate() + case let .period(period): + return period.date + case .age: + return nil + case .range: + return nil + case .string: + return nil + } + } +} diff --git a/Sources/SpeziFHIR/Extensions/SupplyDeliveryOccurrenceXR4+Date.swift b/Sources/SpeziFHIR/Extensions/SupplyDeliveryOccurrenceXR4+Date.swift new file mode 100644 index 0000000..d91ff5d --- /dev/null +++ b/Sources/SpeziFHIR/Extensions/SupplyDeliveryOccurrenceXR4+Date.swift @@ -0,0 +1,24 @@ +// +// This source file is part of the Stanford Spezi open source project +// +// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md) +// +// SPDX-License-Identifier: MIT +// + +import Foundation +import ModelsR4 + + +extension SupplyDelivery.OccurrenceX { + var date: Date? { + switch self { + case let .dateTime(date): + return try? date.value?.asNSDate() + case let .period(period): + return period.date + case .timing: + return nil + } + } +} diff --git a/Sources/SpeziFHIR/FHIRResource/FHIRResource.swift b/Sources/SpeziFHIR/FHIRResource/FHIRResource.swift index d0ab724..e191fa9 100644 --- a/Sources/SpeziFHIR/FHIRResource/FHIRResource.swift +++ b/Sources/SpeziFHIR/FHIRResource/FHIRResource.swift @@ -55,34 +55,40 @@ public struct FHIRResource: Sendable, Identifiable, Hashable { switch versionedResource { case let .r4(resource): switch resource { + case let carePlan as ModelsR4.CarePlan: + return carePlan.period?.date + case let careTeam as ModelsR4.CareTeam: + return careTeam.period?.date + case let claim as ModelsR4.Claim: + return try? claim.billablePeriod?.end?.value?.asNSDate() case let condition as ModelsR4.Condition: - guard case let .dateTime(date) = condition.onset else { - return nil - } - return try? date.value?.asNSDate() + return condition.onset?.date + case let device as ModelsR4.Device: + return try? device.manufactureDate?.value?.asNSDate() case let diagnosticReport as ModelsR4.DiagnosticReport: - guard case let .dateTime(date) = diagnosticReport.effective else { - return nil - } - return try? date.value?.asNSDate() + return diagnosticReport.effective?.date + case let documentReference as ModelsR4.DocumentReference: + return try? documentReference.date?.value?.asNSDate() case let encounter as ModelsR4.Encounter: return try? encounter.period?.end?.value?.asNSDate() + case let explanationOfBenefit as ModelsR4.ExplanationOfBenefit: + return try? explanationOfBenefit.billablePeriod?.end?.value?.asNSDate() case let immunization as ModelsR4.Immunization: - guard case let .dateTime(date) = immunization.occurrence else { - return nil - } - return try? date.value?.asNSDate() + return immunization.occurrence.date case let medicationRequest as ModelsR4.MedicationRequest: return try? medicationRequest.authoredOn?.value?.asNSDate() + case let medicationAdministration as ModelsR4.MedicationAdministration: + return medicationAdministration.effective.date case let observation as ModelsR4.Observation: return try? observation.issued?.value?.asNSDate() case let procedure as ModelsR4.Procedure: - guard case let .dateTime(date) = procedure.performed else { - return nil - } - return try? date.value?.asNSDate() - case is ModelsR4.Patient: - return .now + return procedure.performed?.date + case let patient as ModelsR4.Patient: + return try? patient.birthDate?.value?.asNSDate() + case let provenance as ModelsR4.Provenance: + return try? provenance.recorded.value?.asNSDate() + case let supplyDelivery as ModelsR4.SupplyDelivery: + return supplyDelivery.occurrence?.date default: return nil } @@ -92,16 +98,31 @@ public struct FHIRResource: Sendable, Identifiable, Hashable { return try? observation.issued?.value?.asNSDate() case let medicationOrder as ModelsDSTU2.MedicationOrder: return try? medicationOrder.dateWritten?.value?.asNSDate() + case let medicationStatement as ModelsDSTU2.MedicationStatement: + guard case let .dateTime(date) = medicationStatement.effective else { + return nil + } + return try? date.value?.asNSDate() case let condition as ModelsDSTU2.Condition: guard case let .dateTime(date) = condition.onset else { return nil } return try? date.value?.asNSDate() case let procedure as ModelsDSTU2.Procedure: - guard case let .dateTime(date) = procedure.performed else { - return nil + switch procedure.performed { + case let .dateTime(date): + if let date = try? date.value?.asNSDate() { + return date + } + case let .period(period): + if let date = try? period.end?.value?.asNSDate() { + return date + } + default: + break } - return try? date.value?.asNSDate() + + return nil default: return nil } diff --git a/Sources/SpeziFHIRInterpretation/Resources/Localizable.xcstrings b/Sources/SpeziFHIRInterpretation/Resources/Localizable.xcstrings index d3c975c..5e878e2 100644 --- a/Sources/SpeziFHIRInterpretation/Resources/Localizable.xcstrings +++ b/Sources/SpeziFHIRInterpretation/Resources/Localizable.xcstrings @@ -90,7 +90,7 @@ "en" : { "stringUnit" : { "state" : "translated", - "value" : "Your task is to private a title and compact summary for an FHIR resource from the user's clinical record. You should provide the title and summary in the following locale: {{LOCALE}}.\n\nYour response should contain two lines without headings, markdown formatting, or any other structure beyond two lines. Directly provide the content without any additional structure or an introduction. Another computer program will parse the output.\n\n1. Line: A 1-5 Word summary of the FHIR resource that immediately identifies the resource and provides the essential information at a glance. Do NOT use a complete sentence; instead, use a formatting typically used for titles in computer systems.\n\n2. Line: Provide a short one-sentence summary of the resource in less than 40 words. Ensure that the summary only focuses on the essential information that a patient would need and, e.g., excludes the person who prescribed a medication or similar metadata. Ensure that all clinically relevant data is included, which allows us to use the summary in additional prompts where using the complete JSON might not be feasible.\n\nThe following JSON representation defines the FHIR resource that you should provide a title and summary for:\n\n{{FHIR_RESOURCE}}" + "value" : "Your task is to private a title and compact summary for an FHIR resource from the user's clinical record. You should provide the title and summary in the following locale: {{LOCALE}}.\n\nYour response should contain two lines without headings, markdown formatting, or any other structure beyond two lines. Directly provide the content without any additional structure or an introduction. Another computer program will parse the output.\n\n1. Line: A 1-5 Word summary of the FHIR resource that immediately identifies the resource and provides the essential information at a glance. Do NOT use a complete sentence; instead, use a formatting typically used for titles in computer systems.\n\n2. Line: Provide a short one-sentence summary of the resource in less than 100 words. Ensure that the summary only focuses on the essential information that a patient would need and, e.g., excludes the person who prescribed a medication or similar metadata. Ensure that all clinically relevant data is included, which allows us to use the summary in additional prompts where using the complete JSON might not be feasible.\n\nThe following JSON representation defines the FHIR resource that you should provide a title and summary for:\n\n{{FHIR_RESOURCE}}" } } }