2
0

customer.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package graphql
  2. import (
  3. "github.com/Nerzal/gocloak/v12"
  4. "github.com/gshopify/service-wrapper/model"
  5. "github.com/gshopify/service-wrapper/scalar"
  6. "gshopper.com/gshopify/customer/graphql/generated"
  7. "strings"
  8. )
  9. func User2Customer(user *gocloak.User) *generated.Customer {
  10. if user == nil {
  11. return nil
  12. }
  13. customer := &generated.Customer{
  14. ID: *user.ID,
  15. Email: user.Email,
  16. FirstName: user.FirstName,
  17. LastName: user.LastName,
  18. CreatedAt: scalar.DateTime(0),
  19. UpdatedAt: scalar.DateTime(0),
  20. }
  21. customer.DisplayName = func(u *gocloak.User) string {
  22. if u == nil {
  23. return ""
  24. }
  25. if u.FirstName != nil || u.LastName != nil {
  26. n := strings.Builder{}
  27. if u.FirstName != nil && *u.FirstName != "" {
  28. n.WriteString(*u.FirstName)
  29. }
  30. if u.LastName != nil || *u.LastName != "" {
  31. n.WriteString(" ")
  32. n.WriteString(*u.LastName)
  33. }
  34. return n.String()
  35. }
  36. return *u.Email
  37. }(user)
  38. if user.Attributes != nil {
  39. for s, values := range *user.Attributes {
  40. var meta *generated.Metafield
  41. if len(values) < 1 {
  42. continue
  43. }
  44. switch s {
  45. case "phone_number":
  46. meta = NewMetafield(s, values[0], model.MetaFieldTypeSingleLineTextField)
  47. customer.Phone = gocloak.StringP(values[0])
  48. case "phone_region":
  49. meta = NewMetafield(s, values[0], model.MetaFieldTypeSingleLineTextField)
  50. case "phone_verified":
  51. meta = NewMetafield(s, values[0], model.MetaFieldTypeBoolean)
  52. case "created_at":
  53. _ = customer.CreatedAt.UnmarshalGQL(values[0])
  54. meta = NewMetafield(s, values[0], model.MetaFieldTypeDateTime)
  55. case "updated_at":
  56. _ = customer.UpdatedAt.UnmarshalGQL(values[0])
  57. meta = NewMetafield(s, values[0], model.MetaFieldTypeDateTime)
  58. case "accepts_marketing":
  59. customer.AcceptsMarketing = values[0] == "true"
  60. meta = NewMetafield(s, values[0], model.MetaFieldTypeBoolean)
  61. default:
  62. continue
  63. }
  64. customer.Metafields = append(customer.Metafields, meta)
  65. }
  66. }
  67. return customer
  68. }