This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addons.go
129 lines (108 loc) · 3.45 KB
/
addons.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Nuts consent store
* Copyright (C) 2019. Nuts community
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package api
import (
"time"
"github.com/nuts-foundation/nuts-consent-store/pkg"
)
// ToPatientConsent converts the api PatientConsent struct to an internal PatientConsent
func (sc PatientConsent) ToPatientConsent() (pkg.PatientConsent, error) {
var records []pkg.ConsentRecord
for _, r := range sc.Records {
cr, err := r.ToConsentRecord()
if err != nil {
return pkg.PatientConsent{}, err
}
records = append(records, cr)
}
return pkg.PatientConsent{
ID: sc.Id,
Subject: string(sc.Subject),
Custodian: string(sc.Custodian),
Actor: string(sc.Actor),
Records: records,
}, nil
}
// ToConsentRecord converts the API consent record object to the internal DB object
func (cr ConsentRecord) ToConsentRecord() (pkg.ConsentRecord, error) {
var resources []pkg.DataClass
for _, a := range cr.DataClasses {
resources = append(resources, pkg.DataClass{Code: a})
}
validFrom, err := time.Parse(time.RFC3339, string(cr.ValidFrom))
if err != nil {
return pkg.ConsentRecord{}, err
}
var validTo time.Time
if cr.ValidTo != nil {
validTo, err = time.Parse(time.RFC3339, string(*cr.ValidTo))
if err != nil {
return pkg.ConsentRecord{}, err
}
}
return pkg.ConsentRecord{
ValidFrom: validFrom,
ValidTo: &validTo,
Hash: cr.RecordHash,
PreviousHash: cr.PreviousRecordHash,
DataClasses: resources,
}, nil
}
// FromPatientConsent converts a slice of pkg.PatientConsent to a slice of api.PatientConsent
func FromPatientConsents(pc []pkg.PatientConsent) []PatientConsent {
var consents []PatientConsent
for _, c := range pc {
consents = append(consents, FromPatientConsent(c))
}
return consents
}
// FromPatientConsent converts a pkg.PatientConsent to a PatientConsent
func FromPatientConsent(pc pkg.PatientConsent) PatientConsent {
var records []ConsentRecord
for _, r := range pc.Records {
cr := FromConsentRecord(r)
records = append(records, cr)
}
return PatientConsent{
Id: pc.ID,
Subject: Identifier(pc.Subject),
Custodian: Identifier(pc.Custodian),
Actor: Identifier(pc.Actor),
Records: records,
}
}
// FromConsentRecord converts the DB type to api type
func FromConsentRecord(consentRecord pkg.ConsentRecord) ConsentRecord {
var resources []string
for _, r2 := range consentRecord.DataClasses {
resources = append(resources, r2.Code)
}
version := int(consentRecord.Version)
cr := ConsentRecord{
PreviousRecordHash: consentRecord.PreviousHash,
RecordHash: consentRecord.Hash,
DataClasses: resources,
ValidFrom: ValidFrom(consentRecord.ValidFrom.Format(time.RFC3339)),
Version: &version,
}
if consentRecord.ValidTo != nil {
validTo := ValidTo(consentRecord.ValidTo.Format(time.RFC3339))
cr.ValidTo = &validTo
}
return cr
}