-
Notifications
You must be signed in to change notification settings - Fork 38
/
ppm_shipment_updater.go
205 lines (180 loc) · 7.73 KB
/
ppm_shipment_updater.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
package ppmshipment
import (
"github.com/gofrs/uuid"
"github.com/transcom/mymove/pkg/appcontext"
"github.com/transcom/mymove/pkg/apperror"
"github.com/transcom/mymove/pkg/etag"
"github.com/transcom/mymove/pkg/models"
"github.com/transcom/mymove/pkg/services"
)
type ppmShipmentUpdater struct {
checks []ppmShipmentValidator
estimator services.PPMEstimator
addressCreator services.AddressCreator
addressUpdater services.AddressUpdater
}
var PPMShipmentUpdaterChecks = []ppmShipmentValidator{
checkShipmentType(),
checkShipmentID(),
checkPPMShipmentID(),
checkRequiredFields(),
checkAdvanceAmountRequested(),
}
func NewPPMShipmentUpdater(ppmEstimator services.PPMEstimator, addressCreator services.AddressCreator, addressUpdater services.AddressUpdater) services.PPMShipmentUpdater {
return &ppmShipmentUpdater{
checks: PPMShipmentUpdaterChecks,
estimator: ppmEstimator,
addressCreator: addressCreator,
addressUpdater: addressUpdater,
}
}
func (f *ppmShipmentUpdater) UpdatePPMShipmentWithDefaultCheck(appCtx appcontext.AppContext, ppmShipment *models.PPMShipment, mtoShipmentID uuid.UUID) (*models.PPMShipment, error) {
return f.updatePPMShipment(appCtx, ppmShipment, mtoShipmentID, f.checks...)
}
func (f *ppmShipmentUpdater) updatePPMShipment(appCtx appcontext.AppContext, ppmShipment *models.PPMShipment, mtoShipmentID uuid.UUID, checks ...ppmShipmentValidator) (*models.PPMShipment, error) {
if ppmShipment == nil {
return nil, nil
}
oldPPMShipment, err := FindPPMShipmentByMTOID(appCtx, mtoShipmentID)
if err != nil {
return nil, err
}
isPrimeCounseled, err := IsPrimeCounseledPPM(appCtx, mtoShipmentID)
if err != nil {
return nil, err
}
updatedPPMShipment, err := mergePPMShipment(*ppmShipment, oldPPMShipment)
if err != nil {
return nil, err
}
err = validatePPMShipment(appCtx, *updatedPPMShipment, oldPPMShipment, &oldPPMShipment.Shipment, checks...)
if err != nil {
return nil, err
}
transactionError := appCtx.NewTransaction(func(txnAppCtx appcontext.AppContext) error {
// This potentially updates the MTOShipment.Distance field so include it in the transaction
estimatedIncentive, estimatedSITCost, err := f.estimator.EstimateIncentiveWithDefaultChecks(appCtx, *oldPPMShipment, updatedPPMShipment)
if err != nil {
return err
}
updatedPPMShipment.EstimatedIncentive = estimatedIncentive
updatedPPMShipment.SITEstimatedCost = estimatedSITCost
if appCtx.Session() != nil {
if appCtx.Session().IsOfficeUser() {
edited := models.PPMAdvanceStatusEdited
if oldPPMShipment.HasRequestedAdvance != nil && updatedPPMShipment.HasRequestedAdvance != nil {
if !*oldPPMShipment.HasRequestedAdvance && *updatedPPMShipment.HasRequestedAdvance {
updatedPPMShipment.AdvanceStatus = &edited
} else if *oldPPMShipment.HasRequestedAdvance && !*updatedPPMShipment.HasRequestedAdvance {
updatedPPMShipment.AdvanceStatus = &edited
}
}
if oldPPMShipment.AdvanceAmountRequested != nil && updatedPPMShipment.AdvanceAmountRequested != nil {
if *oldPPMShipment.AdvanceAmountRequested != *updatedPPMShipment.AdvanceAmountRequested {
updatedPPMShipment.AdvanceStatus = &edited
}
}
}
if appCtx.Session().IsMilApp() {
if isPrimeCounseled && updatedPPMShipment.HasRequestedAdvance != nil {
received := models.PPMAdvanceStatusReceived
notReceived := models.PPMAdvanceStatusNotReceived
if updatedPPMShipment.HasReceivedAdvance != nil && *updatedPPMShipment.HasRequestedAdvance {
if *updatedPPMShipment.HasReceivedAdvance {
updatedPPMShipment.AdvanceStatus = &received
}
if !*updatedPPMShipment.HasReceivedAdvance {
updatedPPMShipment.AdvanceStatus = ¬Received
}
}
}
}
}
finalIncentive, err := f.estimator.FinalIncentiveWithDefaultChecks(appCtx, *oldPPMShipment, updatedPPMShipment)
if err != nil {
return err
}
updatedPPMShipment.FinalIncentive = finalIncentive
if updatedPPMShipment.W2Address != nil {
var updatedAddress *models.Address
var createOrUpdateErr error
if updatedPPMShipment.W2Address.ID.IsNil() {
updatedAddress, createOrUpdateErr = f.addressCreator.CreateAddress(txnAppCtx, updatedPPMShipment.W2Address)
} else {
updatedAddress, createOrUpdateErr = f.addressUpdater.UpdateAddress(txnAppCtx, updatedPPMShipment.W2Address, etag.GenerateEtag(oldPPMShipment.W2Address.UpdatedAt))
}
if createOrUpdateErr != nil {
return createOrUpdateErr
}
updatedPPMShipment.W2AddressID = &updatedAddress.ID
updatedPPMShipment.W2Address = updatedAddress
}
if updatedPPMShipment.PickupAddress != nil {
var updatedAddress *models.Address
var createOrUpdateErr error
if updatedPPMShipment.PickupAddress.ID.IsNil() {
updatedAddress, createOrUpdateErr = f.addressCreator.CreateAddress(txnAppCtx, updatedPPMShipment.PickupAddress)
} else {
updatedAddress, createOrUpdateErr = f.addressUpdater.UpdateAddress(txnAppCtx, updatedPPMShipment.PickupAddress, etag.GenerateEtag(oldPPMShipment.PickupAddress.UpdatedAt))
}
if createOrUpdateErr != nil {
return createOrUpdateErr
}
updatedPPMShipment.PickupAddressID = &updatedAddress.ID
updatedPPMShipment.PickupAddress = updatedAddress
}
if updatedPPMShipment.SecondaryPickupAddress != nil {
var updatedAddress *models.Address
var createOrUpdateErr error
if updatedPPMShipment.SecondaryPickupAddress.ID.IsNil() {
updatedAddress, createOrUpdateErr = f.addressCreator.CreateAddress(txnAppCtx, updatedPPMShipment.SecondaryPickupAddress)
} else {
updatedAddress, createOrUpdateErr = f.addressUpdater.UpdateAddress(txnAppCtx, updatedPPMShipment.SecondaryPickupAddress, etag.GenerateEtag(oldPPMShipment.SecondaryPickupAddress.UpdatedAt))
}
if createOrUpdateErr != nil {
return createOrUpdateErr
}
updatedPPMShipment.SecondaryPickupAddressID = &updatedAddress.ID
updatedPPMShipment.SecondaryPickupAddress = updatedAddress
}
if updatedPPMShipment.DestinationAddress != nil {
var updatedAddress *models.Address
var createOrUpdateErr error
if updatedPPMShipment.DestinationAddress.ID.IsNil() {
updatedAddress, createOrUpdateErr = f.addressCreator.CreateAddress(txnAppCtx, updatedPPMShipment.DestinationAddress)
} else {
updatedAddress, createOrUpdateErr = f.addressUpdater.UpdateAddress(txnAppCtx, updatedPPMShipment.DestinationAddress, etag.GenerateEtag(oldPPMShipment.DestinationAddress.UpdatedAt))
}
if createOrUpdateErr != nil {
return createOrUpdateErr
}
updatedPPMShipment.DestinationAddressID = &updatedAddress.ID
updatedPPMShipment.DestinationAddress = updatedAddress
}
if updatedPPMShipment.SecondaryDestinationAddress != nil {
var updatedAddress *models.Address
var createOrUpdateErr error
if updatedPPMShipment.SecondaryDestinationAddress.ID.IsNil() {
updatedAddress, createOrUpdateErr = f.addressCreator.CreateAddress(txnAppCtx, updatedPPMShipment.SecondaryDestinationAddress)
} else {
updatedAddress, createOrUpdateErr = f.addressUpdater.UpdateAddress(txnAppCtx, updatedPPMShipment.SecondaryDestinationAddress, etag.GenerateEtag(oldPPMShipment.SecondaryDestinationAddress.UpdatedAt))
}
if createOrUpdateErr != nil {
return createOrUpdateErr
}
updatedPPMShipment.SecondaryDestinationAddressID = &updatedAddress.ID
updatedPPMShipment.SecondaryDestinationAddress = updatedAddress
}
verrs, err := appCtx.DB().ValidateAndUpdate(updatedPPMShipment)
if verrs != nil && verrs.HasAny() {
return apperror.NewInvalidInputError(updatedPPMShipment.ID, err, verrs, "Invalid input found while updating the PPMShipments.")
} else if err != nil {
return apperror.NewQueryError("PPMShipments", err, "")
}
return nil
})
if transactionError != nil {
return nil, transactionError
}
return updatedPPMShipment, nil
}