all the refactor
This commit is contained in:
+22
-20
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"git.k6n.net/mats/go-cart-actor/pkg/actor"
|
||||
messages "git.k6n.net/mats/go-cart-actor/proto/order"
|
||||
"git.k6n.net/mats/platform/money"
|
||||
)
|
||||
|
||||
// This file holds the mutation handlers. Each handler is the *only* place a
|
||||
@@ -37,8 +38,8 @@ func HandlePlaceOrder(o *OrderGrain, m *messages.PlaceOrder) error {
|
||||
o.Currency = m.GetCurrency()
|
||||
o.Locale = m.GetLocale()
|
||||
o.Country = m.GetCountry()
|
||||
o.TotalAmount = m.GetTotalAmount()
|
||||
o.TotalTax = m.GetTotalTax()
|
||||
o.TotalAmount = money.Cents(m.GetTotalAmount())
|
||||
o.TotalTax = money.Cents(m.GetTotalTax())
|
||||
o.CustomerEmail = m.GetCustomerEmail()
|
||||
o.CustomerName = m.GetCustomerName()
|
||||
if b := m.GetBillingAddress(); len(b) > 0 {
|
||||
@@ -54,10 +55,11 @@ func HandlePlaceOrder(o *OrderGrain, m *messages.PlaceOrder) error {
|
||||
Sku: l.GetSku(),
|
||||
Name: l.GetName(),
|
||||
Quantity: int(l.GetQuantity()),
|
||||
UnitPrice: l.GetUnitPrice(),
|
||||
UnitPrice: money.Cents(l.GetUnitPrice()),
|
||||
TaxRate: int(l.GetTaxRate()),
|
||||
TotalAmount: l.GetTotalAmount(),
|
||||
TotalTax: l.GetTotalTax(),
|
||||
TotalAmount: money.Cents(l.GetTotalAmount()),
|
||||
TotalTax: money.Cents(l.GetTotalTax()),
|
||||
Location: l.GetLocation(),
|
||||
})
|
||||
}
|
||||
o.Status = StatusPending
|
||||
@@ -74,7 +76,7 @@ func HandleAuthorizePayment(o *OrderGrain, m *messages.AuthorizePayment) error {
|
||||
at := msToString(m.GetAtMs())
|
||||
o.Payments = append(o.Payments, &Payment{
|
||||
Provider: m.GetProvider(),
|
||||
Authorized: m.GetAmount(),
|
||||
Authorized: money.Cents(m.GetAmount()),
|
||||
AuthRef: m.GetReference(),
|
||||
AuthorizedAt: at,
|
||||
})
|
||||
@@ -93,10 +95,10 @@ func HandleCapturePayment(o *OrderGrain, m *messages.CapturePayment) error {
|
||||
return fmt.Errorf("order: capture has no matching authorized payment")
|
||||
}
|
||||
at := msToString(m.GetAtMs())
|
||||
p.Captured += m.GetAmount()
|
||||
p.Captured += money.Cents(m.GetAmount())
|
||||
p.CaptureRef = m.GetReference()
|
||||
p.CapturedAt = at
|
||||
o.CapturedAmount += m.GetAmount()
|
||||
o.CapturedAmount += money.Cents(m.GetAmount())
|
||||
o.Status = StatusCaptured
|
||||
o.touch(at)
|
||||
return nil
|
||||
@@ -208,21 +210,21 @@ func HandleIssueRefund(o *OrderGrain, m *messages.IssueRefund) error {
|
||||
if m.GetAmount() <= 0 {
|
||||
return fmt.Errorf("order: refund amount must be positive")
|
||||
}
|
||||
if o.RefundedAmount+m.GetAmount() > o.CapturedAmount {
|
||||
if o.RefundedAmount+money.Cents(m.GetAmount()) > o.CapturedAmount {
|
||||
return fmt.Errorf("order: refund exceeds captured amount")
|
||||
}
|
||||
at := msToString(m.GetAtMs())
|
||||
o.Refunds = append(o.Refunds, Refund{
|
||||
Provider: m.GetProvider(),
|
||||
Amount: m.GetAmount(),
|
||||
Amount: money.Cents(m.GetAmount()),
|
||||
Reference: m.GetReference(),
|
||||
ReturnID: m.GetReturnId(),
|
||||
IssuedAt: at,
|
||||
})
|
||||
o.RefundedAmount += m.GetAmount()
|
||||
o.RefundedAmount += money.Cents(m.GetAmount())
|
||||
for _, p := range o.Payments {
|
||||
if p.Provider == m.GetProvider() {
|
||||
p.Refunded += m.GetAmount()
|
||||
p.Refunded += money.Cents(m.GetAmount())
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -264,10 +266,10 @@ func HandleRequestExchange(o *OrderGrain, m *messages.RequestExchange) error {
|
||||
Sku: nl.GetSku(),
|
||||
Name: nl.GetName(),
|
||||
Quantity: int(nl.GetQuantity()),
|
||||
UnitPrice: nl.GetUnitPrice(),
|
||||
UnitPrice: money.Cents(nl.GetUnitPrice()),
|
||||
TaxRate: int(nl.GetTaxRate()),
|
||||
TotalAmount: nl.GetTotalAmount(),
|
||||
TotalTax: nl.GetTotalTax(),
|
||||
TotalAmount: money.Cents(nl.GetTotalAmount()),
|
||||
TotalTax: money.Cents(nl.GetTotalTax()),
|
||||
}
|
||||
ex.NewLines = append(ex.NewLines, line)
|
||||
|
||||
@@ -296,14 +298,14 @@ func HandleEditOrderDetails(o *OrderGrain, m *messages.EditOrderDetails) error {
|
||||
o.BillingAddress = append([]byte(nil), b...)
|
||||
}
|
||||
|
||||
if sp := m.GetShippingPrice(); sp > 0 {
|
||||
if sp := money.Cents(m.GetShippingPrice()); sp > 0 {
|
||||
found := false
|
||||
var oldTotal int64
|
||||
var oldTotal money.Cents
|
||||
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].TotalAmount = sp.Mul(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
|
||||
@@ -317,9 +319,9 @@ func HandleEditOrderDetails(o *OrderGrain, m *messages.EditOrderDetails) error {
|
||||
Name: "Delivery",
|
||||
Quantity: 1,
|
||||
UnitPrice: sp,
|
||||
TaxRate: 25,
|
||||
TaxRate: 2500, // 25% in basis points
|
||||
TotalAmount: sp,
|
||||
TotalTax: ComputeTax(sp, 25),
|
||||
TotalTax: ComputeTax(sp, 2500),
|
||||
}
|
||||
o.Lines = append(o.Lines, line)
|
||||
o.TotalAmount += sp
|
||||
|
||||
Reference in New Issue
Block a user