-
Notifications
You must be signed in to change notification settings - Fork 38
/
mto_shipment.go
318 lines (279 loc) · 10.9 KB
/
mto_shipment.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package internalapi
import (
"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"
mtoshipmentops "github.com/transcom/mymove/pkg/gen/internalapi/internaloperations/mto_shipment"
"github.com/transcom/mymove/pkg/handlers"
"github.com/transcom/mymove/pkg/handlers/internalapi/internal/payloads"
"github.com/transcom/mymove/pkg/models"
"github.com/transcom/mymove/pkg/services"
)
//
// CREATE
//
// CreateMTOShipmentHandler is the handler to create MTO shipments
type CreateMTOShipmentHandler struct {
handlers.HandlerConfig
shipmentCreator services.ShipmentCreator
}
// Handle creates the mto shipment
func (h CreateMTOShipmentHandler) Handle(params mtoshipmentops.CreateMTOShipmentParams) middleware.Responder {
return h.AuditableAppContextFromRequestWithErrors(params.HTTPRequest,
func(appCtx appcontext.AppContext) (middleware.Responder, error) {
if appCtx.Session() == nil || (!appCtx.Session().IsMilApp() && appCtx.Session().ServiceMemberID == uuid.Nil) {
noSessionErr := apperror.NewSessionError("No service member ID")
return mtoshipmentops.NewCreateMTOShipmentUnauthorized(), noSessionErr
}
payload := params.Body
if payload == nil {
noBodyErr := apperror.NewBadDataError("Invalid mto shipment: params Body is nil")
appCtx.Logger().Error(noBodyErr.Error())
return mtoshipmentops.NewCreateMTOShipmentBadRequest().WithPayload(payloads.ClientError(handlers.BadRequestErrMessage,
"The MTO Shipment request body cannot be empty.", h.GetTraceIDFromRequest(params.HTTPRequest))), noBodyErr
}
mtoShipment := payloads.MTOShipmentModelFromCreate(payload)
var err error
mtoShipment, err = h.shipmentCreator.CreateShipment(appCtx, mtoShipment)
if err != nil {
appCtx.Logger().Error("internalapi.CreateMTOShipmentHandler", zap.Error(err))
switch e := err.(type) {
case apperror.NotFoundError:
return mtoshipmentops.
NewCreateMTOShipmentNotFound().
WithPayload(
payloads.ClientError(
handlers.NotFoundMessage,
err.Error(),
h.GetTraceIDFromRequest(params.HTTPRequest),
),
), err
case apperror.InvalidInputError:
return mtoshipmentops.
NewCreateMTOShipmentUnprocessableEntity().
WithPayload(
payloads.ValidationError(
handlers.ValidationErrMessage,
h.GetTraceIDFromRequest(params.HTTPRequest),
e.ValidationErrors,
),
), err
case apperror.QueryError:
if e.Unwrap() != nil {
// If you can unwrap, log the internal error (usually a pq error) for better debugging
appCtx.Logger().Error("internalapi.CreateMTOServiceItemHandler error", zap.Error(e.Unwrap()))
}
return mtoshipmentops.
NewCreateMTOShipmentInternalServerError().
WithPayload(
payloads.InternalServerError(
nil,
h.GetTraceIDFromRequest(params.HTTPRequest),
),
), err
default:
return mtoshipmentops.NewCreateMTOShipmentInternalServerError().
WithPayload(
payloads.InternalServerError(
nil,
h.GetTraceIDFromRequest(params.HTTPRequest),
),
), err
}
}
returnPayload := payloads.MTOShipment(h.FileStorer(), mtoShipment)
return mtoshipmentops.NewCreateMTOShipmentOK().WithPayload(returnPayload), nil
})
}
//
// UPDATE
//
// UpdateMTOShipmentHandler is the handler to update MTO shipments
type UpdateMTOShipmentHandler struct {
handlers.HandlerConfig
shipmentUpdater services.ShipmentUpdater
}
// Handle updates the mto shipment
func (h UpdateMTOShipmentHandler) Handle(params mtoshipmentops.UpdateMTOShipmentParams) middleware.Responder {
return h.AuditableAppContextFromRequestWithErrors(params.HTTPRequest,
func(appCtx appcontext.AppContext) (middleware.Responder, error) {
if appCtx.Session() == nil {
noSessionErr := apperror.NewSessionError("No user session")
return mtoshipmentops.NewUpdateMTOShipmentUnauthorized(), noSessionErr
}
if !appCtx.Session().IsMilApp() && appCtx.Session().ServiceMemberID == uuid.Nil {
noServiceMemberIDErr := apperror.NewSessionError("No service member ID")
return mtoshipmentops.NewUpdateMTOShipmentForbidden(), noServiceMemberIDErr
}
payload := params.Body
if payload == nil {
noBodyErr := apperror.NewBadDataError("Invalid mto shipment: params Body is nil")
appCtx.Logger().Error(noBodyErr.Error())
return mtoshipmentops.NewUpdateMTOShipmentBadRequest().WithPayload(payloads.ClientError(handlers.BadRequestErrMessage,
"The MTO Shipment request body cannot be empty.", h.GetTraceIDFromRequest(params.HTTPRequest))), noBodyErr
}
mtoShipment := payloads.MTOShipmentModelFromUpdate(payload)
mtoShipment.ID = uuid.FromStringOrNil(params.MtoShipmentID.String())
status := mtoShipment.Status
if status != "" && status != models.MTOShipmentStatusDraft && status != models.MTOShipmentStatusSubmitted {
invalidShipmentStatusErr := apperror.NewBadDataError("Invalid mto shipment status: shipment in service member app can only have draft or submitted status")
appCtx.Logger().Error(invalidShipmentStatusErr.Error())
return mtoshipmentops.NewUpdateMTOShipmentBadRequest().WithPayload(
payloads.ClientError(handlers.BadRequestErrMessage,
"When present, the MTO Shipment status must be one of: DRAFT or SUBMITTED.",
h.GetTraceIDFromRequest(params.HTTPRequest))), invalidShipmentStatusErr
}
updatedMTOShipment, err := h.shipmentUpdater.UpdateShipment(appCtx, mtoShipment, params.IfMatch, "internal")
if err != nil {
appCtx.Logger().Error("internalapi.UpdateMTOShipmentHandler", zap.Error(err))
switch e := err.(type) {
case apperror.NotFoundError:
return mtoshipmentops.
NewUpdateMTOShipmentNotFound().
WithPayload(
payloads.ClientError(
handlers.NotFoundMessage,
err.Error(),
h.GetTraceIDFromRequest(params.HTTPRequest),
),
), err
case apperror.UpdateError:
return mtoshipmentops.NewUpdateMTOShipmentBadRequest().WithPayload(
payloads.ClientError(handlers.BadRequestErrMessage,
err.Error(),
h.GetTraceIDFromRequest(params.HTTPRequest),
),
), err
case apperror.InvalidInputError:
return mtoshipmentops.
NewUpdateMTOShipmentUnprocessableEntity().
WithPayload(payloads.
ValidationError(
handlers.ValidationErrMessage,
h.GetTraceIDFromRequest(
params.HTTPRequest,
), e.ValidationErrors,
),
), err
case apperror.PreconditionFailedError:
return mtoshipmentops.
NewUpdateMTOShipmentPreconditionFailed().
WithPayload(
payloads.ClientError(
handlers.PreconditionErrMessage,
err.Error(),
h.GetTraceIDFromRequest(params.HTTPRequest),
),
), err
case apperror.QueryError:
if e.Unwrap() != nil {
// If you can unwrap, log the internal error (usually a pq error) for better debugging
appCtx.
Logger().
Error(
"internalapi.UpdateMTOShipmentHandler error",
zap.Error(e.Unwrap()),
)
}
return mtoshipmentops.
NewUpdateMTOShipmentInternalServerError().
WithPayload(
payloads.InternalServerError(
nil,
h.GetTraceIDFromRequest(params.HTTPRequest),
),
), err
default:
return mtoshipmentops.
NewUpdateMTOShipmentInternalServerError().
WithPayload(
payloads.InternalServerError(
nil,
h.GetTraceIDFromRequest(params.HTTPRequest),
),
), err
}
}
returnPayload := payloads.MTOShipment(h.FileStorer(), updatedMTOShipment)
return mtoshipmentops.NewUpdateMTOShipmentOK().WithPayload(returnPayload), nil
})
}
//
// GET ALL
//
// ListMTOShipmentsHandler returns a list of MTO Shipments
type ListMTOShipmentsHandler struct {
handlers.HandlerConfig
services.MTOShipmentFetcher
}
// Handle listing mto shipments for the move task order
func (h ListMTOShipmentsHandler) Handle(params mtoshipmentops.ListMTOShipmentsParams) middleware.Responder {
return h.AuditableAppContextFromRequestWithErrors(params.HTTPRequest,
func(appCtx appcontext.AppContext) (middleware.Responder, error) {
if appCtx.Session() == nil || (!appCtx.Session().IsMilApp() && appCtx.Session().ServiceMemberID == uuid.Nil) {
noSessionErr := apperror.NewSessionError("No session or service member ID")
return mtoshipmentops.NewListMTOShipmentsUnauthorized(), noSessionErr
}
moveID, err := uuid.FromString(params.MoveTaskOrderID.String())
// return any parsing error
if err != nil {
appCtx.Logger().Error("Invalid request: move ID not valid")
return mtoshipmentops.NewListMTOShipmentsBadRequest().WithPayload(payloads.ClientError(handlers.BadRequestErrMessage,
"The MTO Shipment request body cannot be empty.", h.GetTraceIDFromRequest(params.HTTPRequest))), err
}
// Search for shipments
shipments, err := h.MTOShipmentFetcher.ListMTOShipments(appCtx, moveID)
if err != nil {
appCtx.Logger().Error("internalapi.ListMTOShipmentsHandler", zap.Error(err))
switch err.(type) {
case apperror.NotFoundError:
return mtoshipmentops.NewListMTOShipmentsNotFound(), err
default:
return mtoshipmentops.NewListMTOShipmentsInternalServerError(), err
}
}
payload := payloads.MTOShipments(h.FileStorer(), (*models.MTOShipments)(&shipments))
return mtoshipmentops.NewListMTOShipmentsOK().WithPayload(*payload), nil
})
}
//
// DELETE
//
// DeleteShipmentHandler soft deletes a shipment
type DeleteShipmentHandler struct {
handlers.HandlerConfig
services.ShipmentDeleter
}
// Handle soft deletes a shipment
func (h DeleteShipmentHandler) Handle(params mtoshipmentops.DeleteShipmentParams) middleware.Responder {
return h.AuditableAppContextFromRequestWithErrors(params.HTTPRequest,
func(appCtx appcontext.AppContext) (middleware.Responder, error) {
shipmentID := uuid.FromStringOrNil(params.MtoShipmentID.String())
sm, err := models.GetCustomerFromShipment(appCtx.DB(), shipmentID)
if err != nil {
return mtoshipmentops.NewDeleteShipmentNotFound(), err
}
if appCtx.Session().ServiceMemberID != sm.ID {
return mtoshipmentops.NewDeleteShipmentForbidden(), err
}
_, err = h.DeleteShipment(appCtx, shipmentID)
if err != nil {
appCtx.Logger().Error("internalapi.DeleteShipmentHandler", zap.Error(err))
switch err.(type) {
case apperror.NotFoundError:
return mtoshipmentops.NewDeleteShipmentNotFound(), err
case apperror.ConflictError:
return mtoshipmentops.NewDeleteShipmentConflict(), err
case apperror.ForbiddenError:
return mtoshipmentops.NewDeleteShipmentForbidden(), err
case apperror.UnprocessableEntityError:
return mtoshipmentops.NewDeleteShipmentUnprocessableEntity(), err
default:
return mtoshipmentops.NewDeleteShipmentInternalServerError(), err
}
}
return mtoshipmentops.NewDeleteShipmentNoContent(), nil
})
}