-
Notifications
You must be signed in to change notification settings - Fork 2
/
update.go
116 lines (96 loc) · 4.75 KB
/
update.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
package lpastore
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor/actoruid"
)
type updateRequest struct {
Type string `json:"type"`
Changes []updateRequestChange `json:"changes"`
}
type updateRequestChange struct {
Key string `json:"key"`
Old any `json:"old"`
New any `json:"new"`
}
func (c *Client) sendUpdate(ctx context.Context, lpaUID string, actorUID actoruid.UID, body updateRequest) error {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(body); err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+"/lpas/"+lpaUID+"/updates", &buf)
if err != nil {
return err
}
return c.do(ctx, actorUID, req, nil)
}
func (c *Client) SendCertificateProvider(ctx context.Context, lpaUID string, certificateProvider *actor.CertificateProviderProvidedDetails) error {
body := updateRequest{
Type: "CERTIFICATE_PROVIDER_SIGN",
Changes: []updateRequestChange{
{Key: "/certificateProvider/signedAt", New: certificateProvider.Certificate.Agreed},
{Key: "/certificateProvider/contactLanguagePreference", New: certificateProvider.ContactLanguagePreference.String()},
},
}
if certificateProvider.HomeAddress.Line1 != "" {
body.Changes = append(body.Changes, updateRequestChange{Key: "/certificateProvider/address/line1", New: certificateProvider.HomeAddress.Line1})
}
if certificateProvider.HomeAddress.Line2 != "" {
body.Changes = append(body.Changes, updateRequestChange{Key: "/certificateProvider/address/line2", New: certificateProvider.HomeAddress.Line2})
}
if certificateProvider.HomeAddress.Line3 != "" {
body.Changes = append(body.Changes, updateRequestChange{Key: "/certificateProvider/address/line3", New: certificateProvider.HomeAddress.Line3})
}
if certificateProvider.HomeAddress.TownOrCity != "" {
body.Changes = append(body.Changes, updateRequestChange{Key: "/certificateProvider/address/town", New: certificateProvider.HomeAddress.TownOrCity})
}
if certificateProvider.HomeAddress.Postcode != "" {
body.Changes = append(body.Changes, updateRequestChange{Key: "/certificateProvider/address/postcode", New: certificateProvider.HomeAddress.Postcode})
}
if certificateProvider.HomeAddress.Country != "" {
body.Changes = append(body.Changes, updateRequestChange{Key: "/certificateProvider/address/country", New: certificateProvider.HomeAddress.Country})
}
return c.sendUpdate(ctx, lpaUID, certificateProvider.UID, body)
}
func (c *Client) SendAttorney(ctx context.Context, donor *Lpa, attorney *actor.AttorneyProvidedDetails) error {
var attorneyKey string
if attorney.IsTrustCorporation && attorney.IsReplacement && donor.Attorneys.TrustCorporation.Name != "" {
attorneyKey = "/trustCorporations/1"
} else if attorney.IsTrustCorporation {
attorneyKey = "/trustCorporations/0"
} else if attorney.IsReplacement {
attorneyKey = fmt.Sprintf("/attorneys/%d", len(donor.Attorneys.Attorneys)+donor.ReplacementAttorneys.Index(attorney.UID))
} else {
attorneyKey = fmt.Sprintf("/attorneys/%d", donor.Attorneys.Index(attorney.UID))
}
body := updateRequest{
Type: "ATTORNEY_SIGN",
Changes: []updateRequestChange{
{Key: attorneyKey + "/mobile", New: attorney.Mobile},
{Key: attorneyKey + "/contactLanguagePreference", New: attorney.ContactLanguagePreference.String()},
},
}
if attorney.IsTrustCorporation {
body.Changes = append(body.Changes,
updateRequestChange{Key: attorneyKey + "/signatories/0/firstNames", New: attorney.AuthorisedSignatories[0].FirstNames},
updateRequestChange{Key: attorneyKey + "/signatories/0/lastName", New: attorney.AuthorisedSignatories[0].LastName},
updateRequestChange{Key: attorneyKey + "/signatories/0/professionalTitle", New: attorney.AuthorisedSignatories[0].ProfessionalTitle},
updateRequestChange{Key: attorneyKey + "/signatories/0/signedAt", New: attorney.AuthorisedSignatories[0].Confirmed},
)
if !attorney.AuthorisedSignatories[1].Confirmed.IsZero() {
body.Changes = append(body.Changes,
updateRequestChange{Key: attorneyKey + "/signatories/1/firstNames", New: attorney.AuthorisedSignatories[1].FirstNames},
updateRequestChange{Key: attorneyKey + "/signatories/1/lastName", New: attorney.AuthorisedSignatories[1].LastName},
updateRequestChange{Key: attorneyKey + "/signatories/1/professionalTitle", New: attorney.AuthorisedSignatories[1].ProfessionalTitle},
updateRequestChange{Key: attorneyKey + "/signatories/1/signedAt", New: attorney.AuthorisedSignatories[1].Confirmed},
)
}
} else {
body.Changes = append(body.Changes, updateRequestChange{Key: attorneyKey + "/signedAt", New: attorney.Confirmed})
}
return c.sendUpdate(ctx, donor.LpaUID, attorney.UID, body)
}