-
Notifications
You must be signed in to change notification settings - Fork 12
/
shopify_data_mapper.go
84 lines (72 loc) · 2.15 KB
/
shopify_data_mapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package shopify
import (
"encoding/json"
"fmt"
"github.com/openline-ai/openline-customer-os/packages/runner/sync-customer-os-data/entity"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/utils"
)
func MapOrganization(inputJson string) (string, error) {
var input struct {
Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
ID int64 `json:"id"`
CreatedAtStr string `json:"created_at"`
Addresses []struct {
Company string `json:"company"`
}
}
err := json.Unmarshal([]byte(inputJson), &input)
if err != nil {
return "", fmt.Errorf("failed to parse input JSON: %v", err)
}
isCustomer := false
var name string
if input.Addresses != nil && len(input.Addresses) > 0 && input.Addresses[0].Company != "" {
name = input.Addresses[0].Company
isCustomer = true
}
output := entity.OrganizationData{
BaseData: entity.BaseData{
ExternalId: fmt.Sprintf("%d", input.ID),
ExternalSourceEntity: "customers",
ExternalSystem: "shopify",
},
Name: name,
IsCustomer: isCustomer,
Whitelisted: true,
}
output.CreatedAtStr = input.CreatedAtStr
return utils.ToJson(output)
}
func MapOrder(inputJson string) (string, error) {
var input struct {
Email string `json:"email,omitempty"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
ID int64 `json:"id,omitempty"`
CreatedAtStr string `json:"created_at,omitempty"`
}
err := json.Unmarshal([]byte(inputJson), &input)
if err != nil {
return "", fmt.Errorf("failed to parse input JSON: %v", err)
}
var name string
if input.FirstName != "" && input.LastName != "" {
name = input.FirstName + " " + input.LastName
} else {
name = input.Email
}
output := entity.OrganizationData{
BaseData: entity.BaseData{
ExternalId: fmt.Sprintf("%d", input.ID),
ExternalSourceEntity: "customers",
ExternalSystem: "shopify",
},
Name: name,
IsCustomer: true,
Whitelisted: true,
}
output.CreatedAtStr = input.CreatedAtStr
return utils.ToJson(output)
}