-
Notifications
You must be signed in to change notification settings - Fork 38
/
signed_certifications.go
112 lines (96 loc) · 4.45 KB
/
signed_certifications.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
package internalapi
import (
"time"
"github.com/go-openapi/runtime/middleware"
"github.com/gofrs/uuid"
"go.uber.org/zap"
"github.com/transcom/mymove/pkg/appcontext"
certop "github.com/transcom/mymove/pkg/gen/internalapi/internaloperations/certification"
"github.com/transcom/mymove/pkg/gen/internalmessages"
"github.com/transcom/mymove/pkg/handlers"
"github.com/transcom/mymove/pkg/models"
)
func payloadForSignedCertificationModel(cert models.SignedCertification) *internalmessages.SignedCertificationPayload {
var ptrCertificationType *internalmessages.NullableSignedCertificationType
if cert.CertificationType != nil {
certificationType := internalmessages.NullableSignedCertificationType(*cert.CertificationType)
ptrCertificationType = &certificationType
}
return &internalmessages.SignedCertificationPayload{
CertificationText: handlers.FmtString(cert.CertificationText),
CertificationType: ptrCertificationType,
CreatedAt: handlers.FmtDateTime(cert.CreatedAt),
Date: handlers.FmtDateTime(cert.Date),
ID: handlers.FmtUUID(cert.ID),
MoveID: handlers.FmtUUID(cert.MoveID),
Signature: handlers.FmtString(cert.Signature),
UpdatedAt: handlers.FmtDateTime(cert.UpdatedAt),
}
}
// CreateSignedCertificationHandler creates a new issue via POST /issue
type CreateSignedCertificationHandler struct {
handlers.HandlerConfig
}
// Handle creates a new SignedCertification from a request payload
func (h CreateSignedCertificationHandler) Handle(params certop.CreateSignedCertificationParams) middleware.Responder {
return h.AuditableAppContextFromRequestWithErrors(params.HTTPRequest,
func(appCtx appcontext.AppContext) (middleware.Responder, error) {
moveID, _ := uuid.FromString(params.MoveID.String())
payload := params.CreateSignedCertificationPayload
var ptrCertType *models.SignedCertificationType
if payload.CertificationType != nil {
certType := models.SignedCertificationType(*payload.CertificationType)
ptrCertType = &certType
}
move, err := models.FetchMove(appCtx.DB(), appCtx.Session(), moveID)
if err != nil {
return handlers.ResponseForError(appCtx.Logger(), err), err
}
newSignedCertification, verrs, err := move.CreateSignedCertification(appCtx.DB(),
appCtx.Session().UserID,
*payload.CertificationText,
*payload.Signature,
(time.Time)(*payload.Date),
ptrCertType)
if verrs.HasAny() || err != nil {
return handlers.ResponseForVErrors(appCtx.Logger(), verrs, err), err
}
signedCertificationPayload := payloadForSignedCertificationModel(*newSignedCertification)
stringCertType := ""
if signedCertificationPayload.CertificationType != nil {
stringCertType = string(*signedCertificationPayload.CertificationType)
}
appCtx.Logger().Info("signedCertification created",
zap.String("id", signedCertificationPayload.ID.String()),
zap.String("moveId", signedCertificationPayload.MoveID.String()),
zap.String("createdAt", signedCertificationPayload.CreatedAt.String()),
zap.String("certification_type", stringCertType),
zap.String("certification_text", *signedCertificationPayload.CertificationText),
)
return certop.NewCreateSignedCertificationCreated().WithPayload(signedCertificationPayload), nil
})
}
// IndexSignedCertificationsHandler gets all signed certifications associated with a move
type IndexSignedCertificationsHandler struct {
handlers.HandlerConfig
}
// Handle gets a list of SignedCertifications for a move
func (h IndexSignedCertificationsHandler) Handle(params certop.IndexSignedCertificationParams) middleware.Responder {
return h.AuditableAppContextFromRequestWithErrors(params.HTTPRequest,
func(appCtx appcontext.AppContext) (middleware.Responder, error) {
moveID, _ := uuid.FromString(params.MoveID.String())
_, err := models.FetchMove(appCtx.DB(), appCtx.Session(), moveID)
if err != nil {
return handlers.ResponseForError(appCtx.Logger(), err), err
}
signedCertifications, err := models.FetchSignedCertifications(appCtx.DB(), appCtx.Session(), moveID)
var signedCertificationsPayload internalmessages.SignedCertifications
for _, sc := range signedCertifications {
signedCertificationsPayload = append(signedCertificationsPayload, payloadForSignedCertificationModel(*sc))
}
if err != nil {
return handlers.ResponseForError(appCtx.Logger(), err), err
}
return certop.NewIndexSignedCertificationOK().WithPayload(signedCertificationsPayload), nil
})
}