mirror of
https://github.com/hanzoai/commerce.git
synced 2026-10-30 15:08:42 +00:00
Multi-tenant commerce and billing: orders, subscriptions, invoices, plans, usage metering and a credit ledger. One Go binary. MIT OR Apache-2.0. Claude-Session: https://claude.ai/code/session_01SAtaDFm8VDiWrhYHNNtxMw
58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
package sendgrid
|
|
|
|
import (
|
|
"github.com/hanzoai/commerce/log"
|
|
"github.com/hanzoai/commerce/types/email"
|
|
"github.com/hanzoai/commerce/util/json"
|
|
)
|
|
|
|
type Error struct {
|
|
Message string `json:"message"`
|
|
ErrorIndices []int `json:"error_indices"`
|
|
}
|
|
|
|
type ContactResponse struct {
|
|
NewCount int `json:"new_count"`
|
|
UpdatedCounti int `json:"unmodified_count"`
|
|
ErrorCount int `json:"error_count"`
|
|
ErrorIndices []int `json:"error_indices"`
|
|
Errors []Error `json:"errors"`
|
|
PersistedRecipients []string `json:"persisted_recipients"`
|
|
}
|
|
|
|
type Contact struct {
|
|
Id string `json:"id"`
|
|
}
|
|
|
|
func newContact(s *email.Subscriber) []byte {
|
|
m := []map[string]interface{}{s.Metadata}
|
|
if m[0] == nil {
|
|
m[0] = map[string]interface{}{}
|
|
}
|
|
m[0]["email"] = s.Email.Address
|
|
|
|
return json.EncodeBytes(m)
|
|
}
|
|
|
|
// Update or create contact if it doesn't exist
|
|
func (api API) UpdateContact(sub *email.Subscriber) (*Contact, error) {
|
|
c := api.Context
|
|
|
|
cont := newContact(sub)
|
|
log.Info("New Contact %s", cont, c)
|
|
|
|
res, err := api.Request("PATCH", "/v3/contactdb/recipients", nil, cont)
|
|
if err != nil {
|
|
return nil, log.Error("Failed to create contact: %v", err, c)
|
|
}
|
|
log.Info(res.StatusCode, c)
|
|
log.Info(res.Body, c)
|
|
log.Info(res.Headers, c)
|
|
|
|
// Decode response and get contact details
|
|
contactRes := new(ContactResponse)
|
|
json.DecodeBytes([]byte(res.Body), contactRes)
|
|
|
|
return &Contact{contactRes.PersistedRecipients[0]}, nil
|
|
}
|