-
Notifications
You must be signed in to change notification settings - Fork 38
/
customer_updater.go
157 lines (130 loc) · 4.96 KB
/
customer_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
package customer
import (
"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"
"github.com/transcom/mymove/pkg/services/query"
)
type customerUpdater struct {
fetchCustomer
}
// NewCustomerUpdater creates a new struct with the service dependencies
func NewCustomerUpdater() services.CustomerUpdater {
return &customerUpdater{fetchCustomer{}}
}
// UpdateCustomer updates the Customer model
func (s *customerUpdater) UpdateCustomer(appCtx appcontext.AppContext, eTag string, customer models.ServiceMember) (*models.ServiceMember, error) {
existingCustomer, err := s.fetchCustomer.FetchCustomer(appCtx, customer.ID)
if err != nil {
return nil, err
}
existingETag := etag.GenerateEtag(existingCustomer.UpdatedAt)
if existingETag != eTag {
return nil, apperror.NewPreconditionFailedError(customer.ID, query.StaleIdentifierError{StaleIdentifier: eTag})
}
transactionError := appCtx.NewTransaction(func(txnAppCtx appcontext.AppContext) error {
if residentialAddress := customer.ResidentialAddress; residentialAddress != nil {
existingCustomer.ResidentialAddress.StreetAddress1 = residentialAddress.StreetAddress1
existingCustomer.ResidentialAddress.City = residentialAddress.City
existingCustomer.ResidentialAddress.State = residentialAddress.State
existingCustomer.ResidentialAddress.PostalCode = residentialAddress.PostalCode
if residentialAddress.StreetAddress2 != nil {
existingCustomer.ResidentialAddress.StreetAddress2 = residentialAddress.StreetAddress2
}
if residentialAddress.StreetAddress3 != nil {
existingCustomer.ResidentialAddress.StreetAddress3 = residentialAddress.StreetAddress3
}
verrs, dbErr := txnAppCtx.DB().ValidateAndSave(existingCustomer.ResidentialAddress)
if verrs != nil && verrs.HasAny() {
return apperror.NewInvalidInputError(customer.ID, dbErr, verrs, "")
}
if dbErr != nil {
return dbErr
}
}
if backupAddress := customer.BackupMailingAddress; backupAddress != nil {
existingCustomer.BackupMailingAddress.StreetAddress1 = backupAddress.StreetAddress1
existingCustomer.BackupMailingAddress.City = backupAddress.City
existingCustomer.BackupMailingAddress.State = backupAddress.State
existingCustomer.BackupMailingAddress.PostalCode = backupAddress.PostalCode
if backupAddress.StreetAddress2 != nil {
existingCustomer.BackupMailingAddress.StreetAddress2 = backupAddress.StreetAddress2
}
if backupAddress.StreetAddress3 != nil {
existingCustomer.BackupMailingAddress.StreetAddress3 = backupAddress.StreetAddress3
}
verrs, dbErr := txnAppCtx.DB().ValidateAndSave(existingCustomer.BackupMailingAddress)
if verrs != nil && verrs.HasAny() {
return apperror.NewInvalidInputError(customer.ID, dbErr, verrs, "")
}
if dbErr != nil {
return dbErr
}
}
if backupContacts := customer.BackupContacts; len(backupContacts) > 0 {
existingCustomer.BackupContacts[0].Name = backupContacts[0].Name
existingCustomer.BackupContacts[0].Email = backupContacts[0].Email
existingCustomer.BackupContacts[0].Phone = backupContacts[0].Phone
verrs, dbErr := txnAppCtx.DB().ValidateAndSave(existingCustomer.BackupContacts)
if verrs != nil && verrs.HasAny() {
return apperror.NewInvalidInputError(customer.ID, dbErr, verrs, "")
}
if dbErr != nil {
return dbErr
}
}
if customer.FirstName != nil {
existingCustomer.FirstName = customer.FirstName
}
if customer.LastName != nil {
existingCustomer.LastName = customer.LastName
}
if customer.PersonalEmail != nil {
existingCustomer.PersonalEmail = customer.PersonalEmail
}
if customer.Telephone != nil {
existingCustomer.Telephone = customer.Telephone
}
if customer.SecondaryTelephone != nil {
existingCustomer.SecondaryTelephone = customer.SecondaryTelephone
}
if customer.PhoneIsPreferred != nil {
existingCustomer.PhoneIsPreferred = customer.PhoneIsPreferred
}
if customer.EmailIsPreferred != nil {
existingCustomer.EmailIsPreferred = customer.EmailIsPreferred
}
if customer.Suffix != nil {
if len(*customer.Suffix) == 0 {
existingCustomer.Suffix = nil
} else {
existingCustomer.Suffix = customer.Suffix
}
}
if customer.MiddleName != nil {
if len(*customer.MiddleName) == 0 {
existingCustomer.MiddleName = nil
} else {
existingCustomer.MiddleName = customer.MiddleName
}
}
if customer.CacValidated != existingCustomer.CacValidated {
existingCustomer.CacValidated = customer.CacValidated
}
// optimistic locking handled before transaction block
verrs, updateErr := txnAppCtx.DB().ValidateAndUpdate(existingCustomer)
if verrs != nil && verrs.HasAny() {
return apperror.NewInvalidInputError(customer.ID, err, verrs, "")
}
if updateErr != nil {
return updateErr
}
return nil
})
if transactionError != nil {
return nil, transactionError
}
return existingCustomer, err
}