cart and checkout
This commit is contained in:
@@ -233,6 +233,103 @@ func HandleIssueRefund(o *OrderGrain, m *messages.IssueRefund) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// HandleRequestExchange records an exchange request.
|
||||
func HandleRequestExchange(o *OrderGrain, m *messages.RequestExchange) error {
|
||||
if o.Status != StatusFulfilled && o.Status != StatusCompleted && o.Status != StatusPartiallyFulfilled {
|
||||
return fmt.Errorf("order: cannot request an exchange in status %q", o.Status)
|
||||
}
|
||||
ex := Exchange{
|
||||
ID: m.GetId(),
|
||||
ReturnID: m.GetReturnId(),
|
||||
Reason: m.GetReason(),
|
||||
RequestedAt: msToString(m.GetAtMs()),
|
||||
}
|
||||
|
||||
// 1. Process return lines
|
||||
for _, rl := range m.GetReturnLines() {
|
||||
line := o.findLine(rl.GetReference())
|
||||
if line == nil {
|
||||
return fmt.Errorf("order: exchange return references unknown line %q", rl.GetReference())
|
||||
}
|
||||
ex.ReturnLines = append(ex.ReturnLines, FulfillmentEntry{
|
||||
Reference: rl.GetReference(),
|
||||
Quantity: int(rl.GetQuantity()),
|
||||
})
|
||||
}
|
||||
|
||||
// 2. Process new lines (replacement items)
|
||||
for _, nl := range m.GetNewLines() {
|
||||
line := Line{
|
||||
Reference: nl.GetReference(),
|
||||
Sku: nl.GetSku(),
|
||||
Name: nl.GetName(),
|
||||
Quantity: int(nl.GetQuantity()),
|
||||
UnitPrice: nl.GetUnitPrice(),
|
||||
TaxRate: int(nl.GetTaxRate()),
|
||||
TotalAmount: nl.GetTotalAmount(),
|
||||
TotalTax: nl.GetTotalTax(),
|
||||
}
|
||||
ex.NewLines = append(ex.NewLines, line)
|
||||
|
||||
// Add the replacement line to the order so it can be fulfilled
|
||||
o.Lines = append(o.Lines, line)
|
||||
}
|
||||
|
||||
// 3. Since there are new lines that are not yet fulfilled, transition order status back to partially fulfilled
|
||||
o.Status = StatusPartiallyFulfilled
|
||||
|
||||
o.Exchanges = append(o.Exchanges, ex)
|
||||
o.touch(ex.RequestedAt)
|
||||
return nil
|
||||
}
|
||||
|
||||
// HandleEditOrderDetails updates addresses or shipping price.
|
||||
func HandleEditOrderDetails(o *OrderGrain, m *messages.EditOrderDetails) error {
|
||||
if o.Status == StatusCompleted || o.Status == StatusCancelled || o.Status == StatusRefunded {
|
||||
return fmt.Errorf("order: cannot edit details in status %q", o.Status)
|
||||
}
|
||||
|
||||
if s := m.GetShippingAddress(); len(s) > 0 {
|
||||
o.ShippingAddress = append([]byte(nil), s...)
|
||||
}
|
||||
if b := m.GetBillingAddress(); len(b) > 0 {
|
||||
o.BillingAddress = append([]byte(nil), b...)
|
||||
}
|
||||
|
||||
if sp := m.GetShippingPrice(); sp > 0 {
|
||||
found := false
|
||||
var oldTotal int64
|
||||
for i := range o.Lines {
|
||||
if o.Lines[i].Name == "Delivery" {
|
||||
oldTotal = o.Lines[i].TotalAmount
|
||||
o.Lines[i].UnitPrice = sp
|
||||
o.Lines[i].TotalAmount = sp * int64(o.Lines[i].Quantity)
|
||||
o.Lines[i].TotalTax = ComputeTax(o.Lines[i].TotalAmount, o.Lines[i].TaxRate)
|
||||
o.TotalAmount = o.TotalAmount - oldTotal + o.Lines[i].TotalAmount
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
line := Line{
|
||||
Reference: "delivery",
|
||||
Sku: "delivery",
|
||||
Name: "Delivery",
|
||||
Quantity: 1,
|
||||
UnitPrice: sp,
|
||||
TaxRate: 25,
|
||||
TotalAmount: sp,
|
||||
TotalTax: ComputeTax(sp, 25),
|
||||
}
|
||||
o.Lines = append(o.Lines, line)
|
||||
o.TotalAmount += sp
|
||||
}
|
||||
}
|
||||
|
||||
o.touch(msToString(m.GetAtMs()))
|
||||
return nil
|
||||
}
|
||||
|
||||
// RegisterMutations registers every order mutation handler with the registry so
|
||||
// the grain pool can apply and replay them.
|
||||
func RegisterMutations(reg actor.MutationRegistry) {
|
||||
@@ -245,5 +342,9 @@ func RegisterMutations(reg actor.MutationRegistry) {
|
||||
actor.NewMutation(HandleCancelOrder),
|
||||
actor.NewMutation(HandleRequestReturn),
|
||||
actor.NewMutation(HandleIssueRefund),
|
||||
actor.NewMutation(HandleRequestExchange),
|
||||
actor.NewMutation(HandleEditOrderDetails),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user