mutationCustomerUpdate.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package graphql
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/Nerzal/gocloak/v12"
  6. "github.com/gshopify/service-wrapper/scalar"
  7. "github.com/gshopify/service-wrapper/server/middleware"
  8. "gshopper.com/gshopify/customer/graphql/generated"
  9. m "gshopper.com/gshopify/customer/model"
  10. )
  11. func (r *mutationResolver) CustomerUpdate(
  12. ctx context.Context, t string, udata generated.CustomerUpdateInput) (*generated.CustomerUpdatePayload, error) {
  13. var (
  14. inContext, err = middleware.InContext(ctx)
  15. response = &generated.CustomerUpdatePayload{}
  16. claims *m.Claims
  17. customer *generated.Customer
  18. phone *m.Phone
  19. )
  20. if err != nil {
  21. return nil, err
  22. }
  23. if _, claims, err = r.decodeAccessToken(ctx, t); err != nil {
  24. response.CustomerUserErrors = append(response.CustomerUserErrors,
  25. CustomerError(generated.CustomerErrorCodeTokenInvalid, err, "customerAccessToken"))
  26. return response, nil
  27. }
  28. customer, err = r.customer(ctx, claims.Sub)
  29. if err != nil {
  30. response.CustomerUserErrors = append(response.CustomerUserErrors,
  31. CustomerError(generated.CustomerErrorCodeTokenInvalid, err, "customerAccessToken"))
  32. return response, nil
  33. }
  34. // upsert FirstName
  35. if udata.FirstName != nil {
  36. customer.FirstName = udata.FirstName
  37. }
  38. // upsert LastName
  39. if udata.LastName != nil {
  40. customer.LastName = udata.LastName
  41. }
  42. // upsert AcceptsMarketing
  43. if udata.AcceptsMarketing != nil {
  44. customer.AcceptsMarketing = *udata.AcceptsMarketing
  45. }
  46. if udata.Phone != nil {
  47. phone, err = m.NewPhoneNumber(*udata.Phone, inContext.Country.String(), false)
  48. if err != nil {
  49. response.CustomerUserErrors = append(response.CustomerUserErrors,
  50. CustomerError(generated.CustomerErrorCodeInvalid, err, "phone"))
  51. return response, nil
  52. }
  53. customer.Phone = gocloak.StringP(phone.String())
  54. }
  55. user := gocloak.User{
  56. ID: gocloak.StringP(customer.ID),
  57. FirstName: customer.FirstName,
  58. LastName: customer.LastName,
  59. Email: customer.Email,
  60. Attributes: &map[string][]string{
  61. "accepts_marketing": {fmt.Sprintf("%v", customer.AcceptsMarketing)},
  62. "phone_number": {*customer.Phone},
  63. },
  64. }
  65. for _, metafield := range customer.Metafields {
  66. (*user.Attributes)[metafield.Key] = []string{metafield.Value}
  67. }
  68. (*user.Attributes)["updated_at"] = []string{fmt.Sprintf("%d", scalar.Now())}
  69. // update user properties & attributes
  70. admin, err := r.conf.Admin.Token(r.client, ctx)
  71. if err != nil {
  72. response.CustomerUserErrors = append(response.CustomerUserErrors,
  73. CustomerError(generated.CustomerErrorCodeTokenInvalid, err, "adminAccessToken"))
  74. return response, nil
  75. }
  76. if err = r.client.UpdateUser(ctx, admin.AccessToken, r.conf.Cli.Realm, user); err != nil {
  77. response.CustomerUserErrors = append(response.CustomerUserErrors,
  78. CustomerError(generated.CustomerErrorCodeInvalid, err))
  79. return response, nil
  80. }
  81. // update user password
  82. if udata.Password != nil {
  83. var payload *generated.CustomerAccessTokenCreatePayload
  84. if err = r.setPassword(ctx, customer.ID, *udata.Password, true); err != nil {
  85. response.CustomerUserErrors = append(response.CustomerUserErrors,
  86. CustomerError(generated.CustomerErrorCodeInvalid, err, "password"))
  87. return response, nil
  88. }
  89. _, _ = r.CustomerAccessTokenDelete(ctx, t)
  90. payload, err = r.CustomerAccessTokenCreate(ctx, generated.CustomerAccessTokenCreateInput{
  91. Email: *customer.Email,
  92. Password: *udata.Password,
  93. })
  94. if err != nil {
  95. response.CustomerUserErrors = append(response.CustomerUserErrors,
  96. CustomerError(generated.CustomerErrorCodeInvalid, err, "password"))
  97. return response, nil
  98. }
  99. response.CustomerAccessToken = payload.CustomerAccessToken
  100. }
  101. response.Customer = customer
  102. return response, nil
  103. }