-
Notifications
You must be signed in to change notification settings - Fork 0
/
order_create.go
179 lines (164 loc) · 5.5 KB
/
order_create.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
package orderutils
import (
"context"
"fmt"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"github.com/asia-loop-gmbh/asia-loop-utils-go/pkg/api"
"github.com/asia-loop-gmbh/asia-loop-utils-go/pkg/db"
"github.com/asia-loop-gmbh/asia-loop-utils-go/pkg/normalizer"
"github.com/asia-loop-gmbh/asia-loop-utils-go/pkg/random"
"github.com/asia-loop-gmbh/asia-loop-utils-go/pkg/servicegooglemaps"
"github.com/nam-truong-le/lambda-utils-go/pkg/logger"
)
func CreateOrder(ctx context.Context, orderOptions *api.CreateOrderOrderOptions,
addressOption *api.CreateOrderAddressOptions) (*db.Order, error) {
log := logger.FromContext(ctx)
log.Infof("create order: %s", orderOptions.OrderID)
firstName := normalizer.Name(ctx, addressOption.FirstName)
lastName := normalizer.Name(ctx, addressOption.LastName)
telephone := normalizer.PhoneNumber(ctx, addressOption.Telephone)
email := normalizer.Email(ctx, addressOption.Email)
var addressLine1 string
addressLine2 := addressOption.AddressLine2
var postcode string
var city string
var formattedAddress string
var validAddress bool
inputAddress := fmt.Sprintf(
"%s, %s %s",
addressOption.AddressLine1,
addressOption.Postcode,
addressOption.City,
)
resolveAddressResult, err := servicegooglemaps.ResolveAddress(ctx, inputAddress)
if err != nil {
log.Warnf("failed to resolve address: %s", inputAddress)
addressLine1 = addressOption.AddressLine1
postcode = addressOption.Postcode
city = addressOption.City
formattedAddress = inputAddress
validAddress = false
} else {
addressLine1 = fmt.Sprintf("%s %s", resolveAddressResult.Street, resolveAddressResult.StreetNumber)
postcode = resolveAddressResult.Postcode
city = resolveAddressResult.City
formattedAddress = resolveAddressResult.FormattedAddress
validAddress = true
}
collectionCustomer, err := db.CollectionCustomers(ctx)
if err != nil {
return nil, err
}
findCustomer := collectionCustomer.FindOne(ctx, bson.M{
"addressLine1": addressLine1,
"addressLine2": addressLine2,
"postcode": postcode,
"city": city,
"firstName": firstName,
"lastName": lastName,
})
customer := new(db.Customer)
err = findCustomer.Decode(customer)
if err != nil {
log.Infof("failed to find customer: %s %s (%s)", firstName, lastName, formattedAddress)
customerRef := fmt.Sprintf(
"%s%s",
random.String(2, false, true, false),
random.String(6, false, false, true),
)
customerCreatedAt := time.Now()
newCustomer := db.Customer{
ID: primitive.NewObjectID(),
FirstName: firstName,
LastName: lastName,
CustomerRef: customerRef,
Boxes: []int{},
AddressLine1: addressLine1,
AddressLine2: addressLine2,
Postcode: postcode,
City: city,
Telephone: telephone,
Email: email,
CreatedAt: customerCreatedAt,
UpdatedAt: customerCreatedAt,
}
if _, err := collectionCustomer.InsertOne(ctx, newCustomer); err != nil {
return nil, err
}
log.Infof("new customer created: %s %s (%s)", firstName, lastName, formattedAddress)
customer = &newCustomer
}
collectionOrder, err := db.CollectionOrders(ctx)
if err != nil {
return nil, err
}
secret := random.String(32, true, false, true)
orderCreatedAt := time.Now()
newOrder := db.Order{
ID: primitive.NewObjectID(),
Status: orderOptions.Status,
OrderID: orderOptions.OrderID,
OrderNumber: orderOptions.OrderNumber,
InvoiceNumber: orderOptions.InvoiceNumber,
ValidAddress: validAddress,
Address: formattedAddress,
AddressLine2: addressLine2,
Email: email,
Telephone: telephone,
CustomerNote: orderOptions.CustomerNote,
DeliveryDate: orderOptions.DeliveryDate,
DeliveryTime: orderOptions.DeliveryTime,
ShippingMethod: orderOptions.ShippingMethod,
Customer: customer.ID,
Items: orderOptions.Items,
Tip: orderOptions.Tip,
Total: orderOptions.Total,
Tax: orderOptions.Tax,
Net: orderOptions.Net,
CouponCode: orderOptions.CouponCode,
AppliedCouponTax: orderOptions.AppliedCouponTax,
AppliedCouponTotal: orderOptions.AppliedCouponTotal,
AppliedCouponNet: orderOptions.AppliedCouponNet,
Store: orderOptions.Store,
CompanyKey: orderOptions.CompanyKey,
Secret: secret,
Printed: false,
LocalPickupNotified: false,
PaymentEvents: make([]interface{}, 0),
CreatedAt: orderCreatedAt,
UpdatedAt: orderCreatedAt,
}
if _, err := collectionOrder.InsertOne(ctx, newOrder); err != nil {
return nil, err
}
log.Infof("order created: %s", orderOptions.OrderID)
customerUpdatedAt := time.Now()
if customer.Telephone == "" {
_, err := collectionCustomer.UpdateByID(ctx, customer.ID, bson.M{
"$set": bson.M{
"telephone": telephone,
"updatedAt": customerUpdatedAt,
},
})
if err != nil {
log.Errorf("failed to update customer telphone to %s: %s", telephone, err)
} else {
log.Infof("customer telphone updated: %s", telephone)
}
}
_, err = collectionCustomer.UpdateByID(ctx, customer.ID, bson.M{
"$set": bson.M{
"email": email,
"updatedAt": customerUpdatedAt,
},
})
if err != nil {
log.Errorf("failed to update customer email to %s: %s", email, err)
} else {
log.Infof("customer email updated: %s", email)
}
// TODO: post processing
return &newOrder, nil
}