-
Notifications
You must be signed in to change notification settings - Fork 2
/
path.go
86 lines (70 loc) · 2.61 KB
/
path.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
package attorney
import (
"net/http"
"net/url"
"strings"
"github.com/ministryofjustice/opg-modernising-lpa/internal/appcontext"
"github.com/ministryofjustice/opg-modernising-lpa/internal/attorney/attorneydata"
)
const (
PathCodeOfConduct = Path("/code-of-conduct")
PathConfirmDontWantToBeAttorney = Path("/confirm-you-do-not-want-to-be-an-attorney")
PathConfirmYourDetails = Path("/confirm-your-details")
PathPhoneNumber = Path("/phone-number")
PathProgress = Path("/progress")
PathReadTheLpa = Path("/read-the-lpa")
PathRightsAndResponsibilities = Path("/legal-rights-and-responsibilities")
PathSign = Path("/sign")
PathTaskList = Path("/task-list")
PathWhatHappensNext = Path("/what-happens-next")
PathWhatHappensWhenYouSign = Path("/what-happens-when-you-sign-the-lpa")
PathWouldLikeSecondSignatory = Path("/would-like-second-signatory")
PathYourPreferredLanguage = Path("/your-preferred-language")
)
type Path string
func (p Path) String() string {
return "/attorney/{id}" + string(p)
}
func (p Path) Format(id string) string {
return "/attorney/" + id + string(p)
}
func (p Path) Redirect(w http.ResponseWriter, r *http.Request, appData appcontext.Data, lpaID string) error {
rurl := p.Format(lpaID)
if fromURL := r.FormValue("from"); fromURL != "" {
rurl = fromURL
}
http.Redirect(w, r, appData.Lang.URL(rurl), http.StatusFound)
return nil
}
func (p Path) RedirectQuery(w http.ResponseWriter, r *http.Request, appData appcontext.Data, lpaID string, query url.Values) error {
rurl := p.Format(lpaID)
if fromURL := r.FormValue("from"); fromURL != "" {
rurl = fromURL
}
http.Redirect(w, r, appData.Lang.URL(rurl)+"?"+query.Encode(), http.StatusFound)
return nil
}
func (p Path) canVisit(attorney *attorneydata.Provided) bool {
switch p {
case PathRightsAndResponsibilities,
PathWhatHappensWhenYouSign,
PathSign,
PathWhatHappensNext:
return attorney.Tasks.ConfirmYourDetails.IsCompleted() && attorney.Tasks.ReadTheLpa.IsCompleted()
case PathWouldLikeSecondSignatory:
return attorney.Tasks.ConfirmYourDetails.IsCompleted() && attorney.Tasks.ReadTheLpa.IsCompleted() && attorney.IsTrustCorporation
default:
return true
}
}
func CanGoTo(attorney *attorneydata.Provided, url string) bool {
path, _, _ := strings.Cut(url, "?")
if path == "" {
return false
}
if strings.HasPrefix(path, "/attorney/") {
_, attorneyPath, _ := strings.Cut(strings.TrimPrefix(path, "/attorney/"), "/")
return Path("/" + attorneyPath).canVisit(attorney)
}
return true
}