|
| 1 | +package shopify |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/tidepool-org/platform/log" |
| 9 | + "github.com/tidepool-org/platform/oura/customerio" |
| 10 | +) |
| 11 | + |
| 12 | +type OrdersCreateEvent struct { |
| 13 | + ID int64 `json:"id"` |
| 14 | + AdminGraphQLAPIID string `json:"admin_graphql_api_id"` |
| 15 | + ConfirmationNumber interface{} `json:"confirmation_number"` |
| 16 | + Confirmed bool `json:"confirmed"` |
| 17 | + ContactEmail string `json:"contact_email"` |
| 18 | + CreatedAt time.Time `json:"created_at"` |
| 19 | + DiscountCodes []DiscountCode `json:"discount_codes"` |
| 20 | + Email string `json:"email"` |
| 21 | + LineItems []LineItem `json:"line_items"` |
| 22 | + Name string `json:"name"` |
| 23 | + Note interface{} `json:"note"` |
| 24 | + NoteAttributes []interface{} `json:"note_attributes"` |
| 25 | + OrderNumber int `json:"order_number"` |
| 26 | + OrderStatusUrl string `json:"order_status_url"` |
| 27 | + ProcessedAt time.Time `json:"processed_at"` |
| 28 | + Reference interface{} `json:"reference"` |
| 29 | + UpdatedAt time.Time `json:"updated_at"` |
| 30 | + UserId interface{} `json:"user_id"` |
| 31 | + Returns []interface{} `json:"returns"` |
| 32 | +} |
| 33 | + |
| 34 | +type DiscountCode struct { |
| 35 | + Code string `json:"code"` |
| 36 | + Type string `json:"type"` |
| 37 | + Amount string `json:"amount"` |
| 38 | +} |
| 39 | + |
| 40 | +type LineItem struct { |
| 41 | + ID int64 `json:"id"` |
| 42 | + AdminGraphQLAPIID string `json:"admin_graphql_api_id"` |
| 43 | + ProductID int64 `json:"product_id"` |
| 44 | +} |
| 45 | + |
| 46 | +type OrdersCreateEventProcessor struct { |
| 47 | + logger log.Logger |
| 48 | + |
| 49 | + customerIOClient *customerio.Client |
| 50 | + shopifyClient Client |
| 51 | +} |
| 52 | + |
| 53 | +func NewOrdersCreateEventProcessor(logger log.Logger, customerIOClient *customerio.Client, shopifyClient Client) (*OrdersCreateEventProcessor, error) { |
| 54 | + return &OrdersCreateEventProcessor{ |
| 55 | + logger: logger, |
| 56 | + customerIOClient: customerIOClient, |
| 57 | + shopifyClient: shopifyClient, |
| 58 | + }, nil |
| 59 | +} |
| 60 | + |
| 61 | +func (f *OrdersCreateEventProcessor) Process(ctx context.Context, event OrdersCreateEvent) error { |
| 62 | + logger := f.logger.WithField("orderId", event.ID) |
| 63 | + |
| 64 | + var products []string |
| 65 | + for _, lineItem := range event.LineItems { |
| 66 | + products = append(products, fmt.Sprintf("%d", lineItem.ProductID)) |
| 67 | + } |
| 68 | + |
| 69 | + if len(products) == 0 { |
| 70 | + logger.Info("ignoring orders create event with no products") |
| 71 | + return nil |
| 72 | + } else if len(products) > 1 { |
| 73 | + logger.Warn("ignoring orders create event with multiple products") |
| 74 | + return nil |
| 75 | + } |
| 76 | + |
| 77 | + productID := products[0] |
| 78 | + logger = logger.WithField("productId", productID) |
| 79 | + |
| 80 | + attribute, ok := productIDToOuraDiscountAttribute[productID] |
| 81 | + if !ok { |
| 82 | + logger.Warn("unable to find discount attribute for product") |
| 83 | + return nil |
| 84 | + } |
| 85 | + |
| 86 | + var discountCodes []string |
| 87 | + for _, discountCode := range event.DiscountCodes { |
| 88 | + discountCodes = append(discountCodes, discountCode.Code) |
| 89 | + } |
| 90 | + |
| 91 | + if len(discountCodes) == 0 { |
| 92 | + logger.Warn("ignoring orders create event with no discount codes") |
| 93 | + return nil |
| 94 | + } else if len(discountCodes) > 1 { |
| 95 | + logger.Warn("ignoring orders create event with multiple discount codes") |
| 96 | + return nil |
| 97 | + } |
| 98 | + |
| 99 | + discountCode := discountCodes[0] |
| 100 | + customers, err := f.customerIOClient.FindCustomers(ctx, map[string]any{ |
| 101 | + "filter": map[string]any{ |
| 102 | + "and": []any{ |
| 103 | + map[string]any{ |
| 104 | + "field": attribute, |
| 105 | + "operator": "eq", |
| 106 | + "value": discountCode, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }) |
| 111 | + if err != nil { |
| 112 | + logger.WithError(err).Warn("unable to find customers") |
| 113 | + return nil |
| 114 | + } |
| 115 | + |
| 116 | + if len(customers.Identifiers) == 0 { |
| 117 | + logger.Warn("no customers found for delivered products") |
| 118 | + return nil |
| 119 | + } else if len(customers.Identifiers) > 1 { |
| 120 | + userIds := make([]string, 0, len(customers.Identifiers)) |
| 121 | + for _, id := range customers.Identifiers { |
| 122 | + userIds = append(userIds, id.ID) |
| 123 | + } |
| 124 | + logger.WithField("userIds", userIds).Warn("multiple customers found for delivered products") |
| 125 | + return nil |
| 126 | + } |
| 127 | + |
| 128 | + switch productID { |
| 129 | + case OuraSizingKitProductID: |
| 130 | + if err := f.onSizingKitOrdered(ctx, customers.Identifiers[0], discountCode); err != nil { |
| 131 | + logger.WithError(err).Warn("unable to send sizing kit ordered event") |
| 132 | + return err |
| 133 | + } |
| 134 | + case OuraRingProductID: |
| 135 | + if err := f.onRingOrdered(ctx, customers.Identifiers[0], discountCode); err != nil { |
| 136 | + logger.WithError(err).Warn("unable to send ring ordered event") |
| 137 | + return err |
| 138 | + } |
| 139 | + default: |
| 140 | + logger.Warn("ignoring orders create event for unknown product") |
| 141 | + } |
| 142 | + |
| 143 | + return nil |
| 144 | +} |
| 145 | + |
| 146 | +func (f *OrdersCreateEventProcessor) onSizingKitOrdered(ctx context.Context, identifiers customerio.Identifiers, discountCode string) error { |
| 147 | + sizingKitOrdered := customerio.Event{ |
| 148 | + Name: customerio.OuraSizingKitOrderedEventType, |
| 149 | + ID: discountCode, |
| 150 | + Data: customerio.OuraSizingKitOrderedData{ |
| 151 | + OuraSizingKitDiscountCode: discountCode, |
| 152 | + }, |
| 153 | + } |
| 154 | + |
| 155 | + return f.customerIOClient.SendEvent(ctx, identifiers.ID, sizingKitOrdered) |
| 156 | +} |
| 157 | + |
| 158 | +func (f *OrdersCreateEventProcessor) onRingOrdered(ctx context.Context, identifiers customerio.Identifiers, discountCode string) error { |
| 159 | + ringOrdered := customerio.Event{ |
| 160 | + Name: customerio.OuraRingOrderedEventType, |
| 161 | + ID: discountCode, |
| 162 | + Data: customerio.OuraRingOrderedData{ |
| 163 | + OuraRingDiscountCode: discountCode, |
| 164 | + }, |
| 165 | + } |
| 166 | + |
| 167 | + return f.customerIOClient.SendEvent(ctx, identifiers.ID, ringOrdered) |
| 168 | +} |
0 commit comments