@@ -129,19 +129,19 @@ type SubscriptionListResponse struct {
129129}
130130
131131type SubscriptionDetail struct {
132- ID string `json:"id"`
133- Subscription string `json:"subscription"`
134- Active bool `json:"active"`
135- State string `json:"state"`
136- Product string `json:"product"`
137- Display string `json:"display"`
138- Currency string `json:"currency"`
139- Price any `json:"price"`
140- Quantity any `json:"quantity"`
141- Account SubscriptionAccount `json:"account"`
142- Begin any `json:"begin"`
143- BeginDisplay string `json:"beginDisplay"`
144- Live bool `json:"live"`
132+ ID string `json:"id"`
133+ Subscription string `json:"subscription"`
134+ Active bool `json:"active"`
135+ State string `json:"state"`
136+ Product string `json:"product"`
137+ Display string `json:"display"`
138+ Currency string `json:"currency"`
139+ Price any `json:"price"`
140+ Quantity any `json:"quantity"`
141+ Account any `json:"account"` // Can be string (ID) or object
142+ Begin any `json:"begin"`
143+ BeginDisplay string `json:"beginDisplay"`
144+ Live bool `json:"live"`
145145}
146146
147147type SubscriptionAccount struct {
@@ -686,6 +686,43 @@ func fetchSubscriptionEntries(subscriptionID string) ([]SubscriptionEntry, error
686686
687687// Weekly digest
688688
689+ func getAccountID (account any ) string {
690+ switch v := account .(type ) {
691+ case string :
692+ return v
693+ case map [string ]any :
694+ if id , ok := v ["id" ].(string ); ok && id != "" {
695+ return id
696+ }
697+ if acc , ok := v ["account" ].(string ); ok {
698+ return acc
699+ }
700+ }
701+ return ""
702+ }
703+
704+ func getAccountContact (account any ) AccountContact {
705+ if m , ok := account .(map [string ]any ); ok {
706+ if contact , ok := m ["contact" ].(map [string ]any ); ok {
707+ return AccountContact {
708+ First : getStringField (contact , "first" ),
709+ Last : getStringField (contact , "last" ),
710+ Email : getStringField (contact , "email" ),
711+ Company : getStringField (contact , "company" ),
712+ Phone : getStringField (contact , "phone" ),
713+ }
714+ }
715+ }
716+ return AccountContact {}
717+ }
718+
719+ func getStringField (m map [string ]any , key string ) string {
720+ if v , ok := m [key ].(string ); ok {
721+ return v
722+ }
723+ return ""
724+ }
725+
689726func digestHandler (w http.ResponseWriter , r * http.Request ) {
690727 if r .Method != http .MethodGet && r .Method != http .MethodPost {
691728 http .Error (w , "Method not allowed" , http .StatusMethodNotAllowed )
@@ -737,20 +774,20 @@ func digestHandler(w http.ResponseWriter, r *http.Request) {
737774 time .Sleep (300 * time .Millisecond )
738775 }
739776
740- // 4 . Group by account/customer
777+ // 3 . Group by account/customer
741778 customerMap := make (map [string ]* CustomerDigest )
742779 var customerOrder []string
743780 for _ , s := range allSubs {
744- accountID := s .Detail .Account . ID
781+ accountID := getAccountID ( s .Detail .Account )
745782 if accountID == "" {
746- accountID = s .Detail .Account . Account
783+ accountID = s .Detail .ID // fallback to subscription ID
747784 }
748785
749786 cd , exists := customerMap [accountID ]
750787 if ! exists {
751788 cd = & CustomerDigest {
752789 AccountID : accountID ,
753- Contact : s .Detail .Account . Contact ,
790+ Contact : getAccountContact ( s .Detail .Account ) ,
754791 }
755792 customerMap [accountID ] = cd
756793 customerOrder = append (customerOrder , accountID )
0 commit comments