123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- package graphql
- import (
- "github.com/Nerzal/gocloak/v12"
- "github.com/gshopify/service-wrapper/model"
- "github.com/gshopify/service-wrapper/scalar"
- "gshopper.com/gshopify/customer/graphql/generated"
- "strings"
- )
- func User2Customer(user *gocloak.User) *generated.Customer {
- if user == nil {
- return nil
- }
- customer := &generated.Customer{
- ID: *user.ID,
- Email: user.Email,
- FirstName: user.FirstName,
- LastName: user.LastName,
- CreatedAt: scalar.DateTime(0),
- UpdatedAt: scalar.DateTime(0),
- }
- customer.DisplayName = func(u *gocloak.User) string {
- if u == nil {
- return ""
- }
- if u.FirstName != nil || u.LastName != nil {
- n := strings.Builder{}
- if u.FirstName != nil && *u.FirstName != "" {
- n.WriteString(*u.FirstName)
- }
- if u.LastName != nil || *u.LastName != "" {
- n.WriteString(" ")
- n.WriteString(*u.LastName)
- }
- return n.String()
- }
- return *u.Email
- }(user)
- if user.Attributes != nil {
- for s, values := range *user.Attributes {
- var meta *generated.Metafield
- if len(values) < 1 {
- continue
- }
- switch s {
- case "phone_number":
- meta = NewMetafield(s, values[0], model.MetaFieldTypeSingleLineTextField)
- customer.Phone = gocloak.StringP(values[0])
- case "phone_region":
- meta = NewMetafield(s, values[0], model.MetaFieldTypeSingleLineTextField)
- case "phone_verified":
- meta = NewMetafield(s, values[0], model.MetaFieldTypeBoolean)
- case "created_at":
- _ = customer.CreatedAt.UnmarshalGQL(values[0])
- meta = NewMetafield(s, values[0], model.MetaFieldTypeDateTime)
- case "updated_at":
- _ = customer.UpdatedAt.UnmarshalGQL(values[0])
- meta = NewMetafield(s, values[0], model.MetaFieldTypeDateTime)
- case "accepts_marketing":
- customer.AcceptsMarketing = values[0] == "true"
- meta = NewMetafield(s, values[0], model.MetaFieldTypeBoolean)
- default:
- continue
- }
- customer.Metafields = append(customer.Metafields, meta)
- }
- }
- return customer
- }
|