-
Notifications
You must be signed in to change notification settings - Fork 2
/
data.go
113 lines (89 loc) · 3.34 KB
/
data.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
// Package page contains the core code and business logic of Make and Register a Lasting Power of Attorney (MRLPA)
//
// Useful links:
// - [actor.Lpa] - details about the LPA being drafted
// - [actor.Donor] - details about the donor, provided by the applicant
// - [actor.CertificateProvider] - details about the certificate provider, provided by the applicant
// - [actor.CertificateProviderProvidedDetails] - details about the certificate provider, provided by the certificate provider
// - [actor.Attorney] - details about an attorney or replacement attorney, provided by the applicant
// - [actor.AttorneyDecisions] - details about how an attorney or replacement attorney should act, provided by the applicant
// - [actor.AttorneyProvidedDetails] - details about an attorney or replacement attorney, provided by the attorney or replacement attorney
// - [actor.PersonToNotify] - details about a person to notify, provided by the applicant
package page
import (
"context"
"strings"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/form"
)
type SessionData struct {
SessionID string
LpaID string
}
type SessionMissingError struct{}
func (s SessionMissingError) Error() string {
return "Session data not set"
}
func SessionDataFromContext(ctx context.Context) (*SessionData, error) {
data, ok := ctx.Value((*SessionData)(nil)).(*SessionData)
if !ok {
return nil, SessionMissingError{}
}
return data, nil
}
func ContextWithSessionData(ctx context.Context, data *SessionData) context.Context {
return context.WithValue(ctx, (*SessionData)(nil), data)
}
func CanGoTo(donor *actor.DonorProvidedDetails, url string) bool {
path, _, _ := strings.Cut(url, "?")
if path == "" {
return false
}
if strings.HasPrefix(path, "/lpa/") {
_, lpaPath, _ := strings.Cut(strings.TrimPrefix(path, "/lpa/"), "/")
return canGoToLpaPath(donor, "/"+lpaPath)
}
return true
}
func ChooseAttorneysState(attorneys actor.Attorneys, decisions actor.AttorneyDecisions) actor.TaskState {
if attorneys.Len() == 0 {
return actor.TaskNotStarted
}
if !attorneys.Complete() {
return actor.TaskInProgress
}
if attorneys.Len() > 1 && !decisions.IsComplete() {
return actor.TaskInProgress
}
return actor.TaskCompleted
}
func ChooseReplacementAttorneysState(donor *actor.DonorProvidedDetails) actor.TaskState {
if donor.WantReplacementAttorneys == form.No {
return actor.TaskCompleted
}
if donor.ReplacementAttorneys.Len() == 0 {
if donor.WantReplacementAttorneys.IsUnknown() {
return actor.TaskNotStarted
}
return actor.TaskInProgress
}
if !donor.ReplacementAttorneys.Complete() {
return actor.TaskInProgress
}
if donor.ReplacementAttorneys.Len() > 1 &&
(donor.Attorneys.Len() == 1 || donor.AttorneyDecisions.How.IsJointly() || donor.AttorneyDecisions.How.IsJointlyForSomeSeverallyForOthers()) &&
!donor.ReplacementAttorneyDecisions.IsComplete() {
return actor.TaskInProgress
}
if donor.AttorneyDecisions.How.IsJointlyAndSeverally() {
if donor.HowShouldReplacementAttorneysStepIn.Empty() {
return actor.TaskInProgress
}
if donor.ReplacementAttorneys.Len() > 1 &&
donor.HowShouldReplacementAttorneysStepIn.IsWhenAllCanNoLongerAct() &&
!donor.ReplacementAttorneyDecisions.IsComplete() {
return actor.TaskInProgress
}
}
return actor.TaskCompleted
}