Skip to content

Commit a21c271

Browse files
kitallisclaude
andcommitted
Extract customer and total from nested order object in entries
The entries API returns order details (total, customer, etc.) nested inside an 'order' object, not at the top level of the entry. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 8603fa3 commit a21c271

File tree

1 file changed

+66
-6
lines changed

1 file changed

+66
-6
lines changed

main.go

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,60 @@ func getStringField(m map[string]any, key string) string {
731731
return ""
732732
}
733733

734+
// Extract total/currency from entry's nested order object
735+
func getEntryTotal(e SubscriptionEntry) string {
736+
if order, ok := e.Order.(map[string]any); ok {
737+
if totalDisplay, ok := order["totalDisplay"].(string); ok && totalDisplay != "" {
738+
return totalDisplay
739+
}
740+
if total, ok := order["total"].(float64); ok {
741+
currency := ""
742+
if c, ok := order["currency"].(string); ok {
743+
currency = c
744+
}
745+
return formatAnyAmount(total, currency)
746+
}
747+
}
748+
// Fallback to entry-level fields
749+
if e.TotalDisplay != "" {
750+
return e.TotalDisplay
751+
}
752+
return formatAnyAmount(e.Total, e.Currency)
753+
}
754+
755+
func getEntryCompleted(e SubscriptionEntry) bool {
756+
if order, ok := e.Order.(map[string]any); ok {
757+
if completed, ok := order["completed"].(bool); ok {
758+
return completed
759+
}
760+
}
761+
return e.Completed
762+
}
763+
764+
func getEntryCustomer(e SubscriptionEntry) AccountContact {
765+
if order, ok := e.Order.(map[string]any); ok {
766+
if customer, ok := order["customer"].(map[string]any); ok {
767+
return AccountContact{
768+
First: getStringField(customer, "first"),
769+
Last: getStringField(customer, "last"),
770+
Email: getStringField(customer, "email"),
771+
Company: getStringField(customer, "company"),
772+
Phone: getStringField(customer, "phone"),
773+
}
774+
}
775+
}
776+
return AccountContact{}
777+
}
778+
779+
func getEntryAccountID(e SubscriptionEntry) string {
780+
if order, ok := e.Order.(map[string]any); ok {
781+
if account, ok := order["account"].(string); ok {
782+
return account
783+
}
784+
}
785+
return ""
786+
}
787+
734788
func digestHandler(w http.ResponseWriter, r *http.Request) {
735789
if r.Method != http.MethodGet && r.Method != http.MethodPost {
736790
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
@@ -786,16 +840,25 @@ func digestHandler(w http.ResponseWriter, r *http.Request) {
786840
customerMap := make(map[string]*CustomerDigest)
787841
var customerOrder []string
788842
for _, s := range allSubs {
843+
// Try to get account ID from subscription, then from first entry's order
789844
accountID := getAccountID(s.Detail.Account)
845+
if accountID == "" && len(s.Entries) > 0 {
846+
accountID = getEntryAccountID(s.Entries[0])
847+
}
790848
if accountID == "" {
791849
accountID = s.Detail.ID // fallback to subscription ID
792850
}
793851

794852
cd, exists := customerMap[accountID]
795853
if !exists {
854+
// Try to get contact from subscription, then from first entry's order
855+
contact := getAccountContact(s.Detail.Account)
856+
if contact.Email == "" && len(s.Entries) > 0 {
857+
contact = getEntryCustomer(s.Entries[0])
858+
}
796859
cd = &CustomerDigest{
797860
AccountID: accountID,
798-
Contact: getAccountContact(s.Detail.Account),
861+
Contact: contact,
799862
}
800863
customerMap[accountID] = cd
801864
customerOrder = append(customerOrder, accountID)
@@ -895,14 +958,11 @@ func formatCustomerSection(cd *CustomerDigest) string {
895958

896959
for _, e := range sub.Entries {
897960
status := ":white_check_mark:"
898-
if !e.Completed {
961+
if !getEntryCompleted(e) {
899962
status = ":hourglass_flowing_sand:"
900963
}
901964

902-
amount := e.TotalDisplay
903-
if amount == "" {
904-
amount = formatAnyAmount(e.Total, e.Currency)
905-
}
965+
amount := getEntryTotal(e)
906966

907967
date := e.ChangedDisplay
908968
if date == "" {

0 commit comments

Comments
 (0)