-
Notifications
You must be signed in to change notification settings - Fork 38
/
ppm_document.go
229 lines (191 loc) · 8.81 KB
/
ppm_document.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package ghcapi
import (
"fmt"
"io"
"time"
"github.com/go-openapi/runtime/middleware"
"github.com/gofrs/uuid"
"go.uber.org/zap"
"github.com/transcom/mymove/pkg/appcontext"
"github.com/transcom/mymove/pkg/apperror"
ppmdocumentops "github.com/transcom/mymove/pkg/gen/ghcapi/ghcoperations/ppm"
"github.com/transcom/mymove/pkg/gen/ghcmessages"
"github.com/transcom/mymove/pkg/handlers"
"github.com/transcom/mymove/pkg/handlers/ghcapi/internal/payloads"
"github.com/transcom/mymove/pkg/models"
"github.com/transcom/mymove/pkg/notifications"
"github.com/transcom/mymove/pkg/services"
)
// GetPPMDocumentsHandler is the handler that fetches all of the documents for a PPM shipment for the office api
type GetPPMDocumentsHandler struct {
handlers.HandlerConfig
services.PPMDocumentFetcher
}
// Handle retrieves all documents for a PPM shipment
func (h GetPPMDocumentsHandler) Handle(params ppmdocumentops.GetPPMDocumentsParams) middleware.Responder {
return h.AuditableAppContextFromRequestWithErrors(params.HTTPRequest,
func(appCtx appcontext.AppContext) (middleware.Responder, error) {
errInstance := fmt.Sprintf("Instance: %s", h.GetTraceIDFromRequest(params.HTTPRequest))
errPayload := &ghcmessages.Error{Message: &errInstance}
if !appCtx.Session().IsOfficeApp() {
return ppmdocumentops.NewGetPPMDocumentsForbidden().WithPayload(errPayload), apperror.NewSessionError("Request should come from the office app.")
}
shipmentID := uuid.FromStringOrNil(params.ShipmentID.String())
ppmDocuments, err := h.PPMDocumentFetcher.GetPPMDocuments(appCtx, shipmentID)
if err != nil {
appCtx.Logger().Error("ghcapi.GetPPMDocumentsHandler error", zap.Error(err))
switch e := err.(type) {
case apperror.QueryError:
if e.Unwrap() != nil {
// If you can unwrap, log the error (usually a pq error) for better debugging
appCtx.Logger().Error(
"ghcapi.GetPPMDocumentsHandler error",
zap.Error(e.Unwrap()),
)
}
return ppmdocumentops.NewGetPPMDocumentsInternalServerError().WithPayload(errPayload), nil
default:
return ppmdocumentops.NewGetPPMDocumentsInternalServerError().WithPayload(errPayload), nil
}
}
returnPayload := payloads.PPMDocuments(h.FileStorer(), ppmDocuments)
return ppmdocumentops.NewGetPPMDocumentsOK().WithPayload(returnPayload), nil
})
}
// FinishDocumentReviewHandler is the handler that updates a PPM shipment for the office api when documents have been reviewed
type FinishDocumentReviewHandler struct {
handlers.HandlerConfig
services.PPMShipmentReviewDocuments
}
// Handle finishes a review for a PPM shipment's documents
func (h FinishDocumentReviewHandler) Handle(params ppmdocumentops.FinishDocumentReviewParams) middleware.Responder {
return h.AuditableAppContextFromRequestWithErrors(params.HTTPRequest,
func(appCtx appcontext.AppContext) (middleware.Responder, error) {
errInstance := fmt.Sprintf("Instance: %s", h.GetTraceIDFromRequest(params.HTTPRequest))
errPayload := &ghcmessages.Error{Message: &errInstance}
if appCtx.Session() == nil {
return ppmdocumentops.NewFinishDocumentReviewUnauthorized(), apperror.NewSessionError("No user session")
} else if !appCtx.Session().IsOfficeApp() {
return ppmdocumentops.NewFinishDocumentReviewForbidden(), apperror.NewSessionError("Request is not from the customer app")
} else if appCtx.Session().UserID.IsNil() {
return ppmdocumentops.NewFinishDocumentReviewForbidden(), apperror.NewSessionError("No user ID in session")
}
ppmShipmentID, err := uuid.FromString(params.PpmShipmentID.String())
if err != nil || ppmShipmentID.IsNil() {
appCtx.Logger().Error("error with PPM Shipment ID", zap.Error(err))
return ppmdocumentops.NewFinishDocumentReviewBadRequest().WithPayload(errPayload), err
}
ppmShipment, err := h.PPMShipmentReviewDocuments.SubmitReviewedDocuments(appCtx, ppmShipmentID)
if err != nil {
appCtx.Logger().Error("ghcapi.FinishDocumentReviewHandler", zap.Error(err))
switch e := err.(type) {
case apperror.NotFoundError:
return ppmdocumentops.NewFinishDocumentReviewNotFound(), err
case apperror.ConflictError:
return ppmdocumentops.NewFinishDocumentReviewConflict(), err
case apperror.InvalidInputError:
return ppmdocumentops.NewFinishDocumentReviewUnprocessableEntity().WithPayload(
payloadForValidationError(
handlers.ValidationErrMessage,
err.Error(),
h.GetTraceIDFromRequest(params.HTTPRequest),
e.ValidationErrors,
),
), err
case apperror.PreconditionFailedError:
msg := fmt.Sprintf("%v | Instance: %v", handlers.FmtString(err.Error()), h.GetTraceIDFromRequest(params.HTTPRequest))
return ppmdocumentops.NewFinishDocumentReviewPreconditionFailed().WithPayload(
&ghcmessages.Error{Message: &msg},
), err
case apperror.QueryError:
if e.Unwrap() != nil {
// If you can unwrap, log the error (usually a pq error) for better debugging
appCtx.Logger().Error(
"ghcapi.FinishDocumentReviewHandler error",
zap.Error(e.Unwrap()),
)
}
return ppmdocumentops.NewFinishDocumentReviewInternalServerError(), err
default:
return ppmdocumentops.NewFinishDocumentReviewInternalServerError(), err
}
}
returnPayload := payloads.PPMShipment(h.FileStorer(), ppmShipment)
/* Don't send emails to BLUEBARK moves */
move, err := models.FetchMoveByMoveIDWithOrders(appCtx.DB(), ppmShipment.Shipment.MoveTaskOrderID)
if err != nil {
return nil, err
}
if move.Orders.OrdersType != "BLUEBARK" {
err = h.NotificationSender().SendNotification(appCtx,
notifications.NewPpmPacketEmail(ppmShipment.ID),
)
if err != nil {
appCtx.Logger().Error("problem sending email to user", zap.Error(err))
}
}
return ppmdocumentops.NewFinishDocumentReviewOK().WithPayload(returnPayload), nil
})
}
// ShowAOAPacketHandler returns a Shipment Summary Worksheet PDF
type showAOAPacketHandler struct {
handlers.HandlerConfig
services.SSWPPMComputer
services.SSWPPMGenerator
services.AOAPacketCreator
}
// Handle returns a generated PDF
func (h showAOAPacketHandler) Handle(params ppmdocumentops.ShowAOAPacketParams) middleware.Responder {
return h.AuditableAppContextFromRequestWithErrors(params.HTTPRequest,
func(appCtx appcontext.AppContext) (middleware.Responder, error) {
logger := appCtx.Logger()
ppmShipmentID, err := uuid.FromString(params.PpmShipmentID)
if err != nil {
errInstance := fmt.Sprintf("Instance: %s", h.GetTraceIDFromRequest(params.HTTPRequest))
errPayload := &ghcmessages.Error{Message: &errInstance}
appCtx.Logger().Error(err.Error())
return ppmdocumentops.NewShowAOAPacketBadRequest().WithPayload(errPayload), err
}
AOAPacket, err := h.AOAPacketCreator.CreateAOAPacket(appCtx, ppmShipmentID)
if err != nil {
logger.Error("Error creating AOA", zap.Error(err))
errInstance := fmt.Sprintf("Instance: %s", h.GetTraceIDFromRequest(params.HTTPRequest))
errPayload := &ghcmessages.Error{Message: &errInstance}
return ppmdocumentops.NewShowAOAPacketInternalServerError().
WithPayload(errPayload), err
}
payload := io.NopCloser(AOAPacket)
filename := fmt.Sprintf("inline; filename=\"AOA-%s.pdf\"", time.Now().Format("01-02-2006_15-04-05"))
return ppmdocumentops.NewShowAOAPacketOK().WithContentDisposition(filename).WithPayload(payload), nil
})
}
// ShowPaymentPacketHandler returns a PPM Payment Packet PDF
type ShowPaymentPacketHandler struct {
handlers.HandlerConfig
services.PaymentPacketCreator
}
// Handle returns a generated PDF
func (h ShowPaymentPacketHandler) Handle(params ppmdocumentops.ShowPaymentPacketParams) middleware.Responder {
return h.AuditableAppContextFromRequestWithErrors(params.HTTPRequest,
func(appCtx appcontext.AppContext) (middleware.Responder, error) {
ppmShipmentID, err := uuid.FromString(params.PpmShipmentID.String())
if err != nil {
return handlers.ResponseForError(appCtx.Logger(), err), err
}
pdf, err := h.PaymentPacketCreator.GenerateDefault(appCtx, ppmShipmentID)
if err != nil {
switch err.(type) {
case apperror.NotFoundError:
// this indicates ppm was not found
appCtx.Logger().Warn(fmt.Sprintf("ghcapi.DownPaymentPacket NotFoundError ppmShipmentID:%s", ppmShipmentID.String()), zap.Error(err))
return ppmdocumentops.NewShowPaymentPacketNotFound(), err
default:
appCtx.Logger().Error(fmt.Sprintf("ghcapi.DownPaymentPacket InternalServerError ppmShipmentID:%s", ppmShipmentID.String()), zap.Error(err))
return ppmdocumentops.NewShowPaymentPacketInternalServerError(), err
}
}
payload := io.NopCloser(pdf)
filename := fmt.Sprintf("inline; filename=\"ppm_payment_packet-%s.pdf\"", time.Now().UTC().Format("2006-01-02T15:04:05.000Z"))
return ppmdocumentops.NewShowPaymentPacketOK().WithContentDisposition(filename).WithPayload(payload), nil
})
}