spree_gladly 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +45 -0
  5. data/.travis.yml +69 -0
  6. data/Appraisals +63 -0
  7. data/CODE_OF_CONDUCT.md +128 -0
  8. data/Gemfile +7 -0
  9. data/LICENSE +11 -0
  10. data/README.md +473 -0
  11. data/Rakefile +23 -0
  12. data/app/concerns/customer/database_adapter.rb +21 -0
  13. data/app/controllers/application_controller.rb +2 -0
  14. data/app/controllers/spree/admin/gladly_settings_controller.rb +38 -0
  15. data/app/controllers/spree/api/v1/customers_controller.rb +60 -0
  16. data/app/finders/customer/base_lookup.rb +37 -0
  17. data/app/finders/customer/basic_lookup.rb +26 -0
  18. data/app/finders/customer/detailed_lookup.rb +19 -0
  19. data/app/finders/customer/guest/basic_finder.rb +37 -0
  20. data/app/finders/customer/guest/detailed_finder.rb +42 -0
  21. data/app/finders/customer/registered/basic_finder.rb +68 -0
  22. data/app/finders/customer/registered/detailed_finder.rb +43 -0
  23. data/app/models/spree_gladly/configuration.rb +25 -0
  24. data/app/overrides/add_gladly_admin_menu_links.rb +10 -0
  25. data/app/presenters/customer/address_presenter.rb +27 -0
  26. data/app/presenters/customer/basic_lookup_presenter.rb +29 -0
  27. data/app/presenters/customer/detailed_lookup_presenter.rb +30 -0
  28. data/app/presenters/customer/guest/basic_presenter.rb +53 -0
  29. data/app/presenters/customer/guest/detailed_presenter.rb +117 -0
  30. data/app/presenters/customer/registered/basic_presenter.rb +60 -0
  31. data/app/presenters/customer/registered/detailed_presenter.rb +137 -0
  32. data/app/services/auth/authorization_header.rb +35 -0
  33. data/app/services/auth/error.rb +4 -0
  34. data/app/services/auth/header_parse_error.rb +4 -0
  35. data/app/services/auth/invalid_signature_error.rb +4 -0
  36. data/app/services/auth/missing_key_error.rb +4 -0
  37. data/app/services/auth/request_normalizer.rb +37 -0
  38. data/app/services/auth/signature_validator.rb +68 -0
  39. data/app/services/auth/time_header.rb +25 -0
  40. data/app/validators/lookup_validator.rb +89 -0
  41. data/app/validators/validation_result.rb +25 -0
  42. data/app/views/spree/admin/gladly_settings/edit.html.erb +25 -0
  43. data/bin/console +15 -0
  44. data/bin/setup +8 -0
  45. data/config/locales/en.yml +25 -0
  46. data/config/routes.rb +13 -0
  47. data/gemfiles/spree_3_0.gemfile +16 -0
  48. data/gemfiles/spree_3_1.gemfile +16 -0
  49. data/gemfiles/spree_3_7.gemfile +11 -0
  50. data/gemfiles/spree_4_0.gemfile +11 -0
  51. data/gemfiles/spree_4_1.gemfile +11 -0
  52. data/gemfiles/spree_4_2.gemfile +11 -0
  53. data/gemfiles/spree_master.gemfile +11 -0
  54. data/lib/generators/spree_gladly/install/install_generator.rb +17 -0
  55. data/lib/generators/spree_gladly/install/templates/config/initializers/spree_gladly.rb +18 -0
  56. data/lib/spree_gladly.rb +13 -0
  57. data/lib/spree_gladly/engine.rb +25 -0
  58. data/lib/spree_gladly/factories.rb +9 -0
  59. data/lib/spree_gladly/version.rb +5 -0
  60. data/spree.png +0 -0
  61. data/spree_gladly.gemspec +35 -0
  62. metadata +201 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 79f58571478fed7fa33637006c8b2157fc3b78816ba0c8607bfe1bfb940b390e
4
+ data.tar.gz: 4d8d50389dec9a281fc53b023e1d58fcec863116938d1b50671703012bd5fe85
5
+ SHA512:
6
+ metadata.gz: be1fdeefa5f51e74dd636703e72b385e8c40c10d0a6e1ce2d153e0c7bfc09869b93cdf579f2ff1d5fc2721bfd56b62c4d62703f24dcc7d745b2439921b6998a8
7
+ data.tar.gz: ece65399dc55fd6f0cde6c5554e93c683d45cabb9c749341137fc7fb7bca78cd06f618c6a80d86020dc292c92df277b3de35da0c1dcc2db6e119b7d302b08ac4
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ /.bundle/
2
+ /gemfiles/.bundle/
3
+ /gemfiles/*.lock
4
+ /gemfiles/vendor
5
+ /.yardoc
6
+ /_yardoc/
7
+ /coverage/
8
+ /doc/
9
+ /pkg/
10
+ /spec/reports/
11
+ /spec/dummy/
12
+ /tmp/
13
+ .idea/
14
+ Gemfile.lock
15
+ # rspec failure tracking
16
+ .rspec_status
17
+ .byebug_history
18
+ /vendor
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,45 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'bin/**/*'
4
+ - 'db/**/*'
5
+ - 'config/**/*'
6
+ - 'script/**/*'
7
+ - 'node_modules/**/*'
8
+ - 'Gemfile'
9
+ - 'spree_gladly.gemspec'
10
+ - 'Appraisals'
11
+ - 'gemfiles/**/*'
12
+ - 'Guardfile'
13
+ - 'lib/tasks/heroku_clear_tasks.rake'
14
+ - 'spec/rails_helper.rb'
15
+ - 'spec/spec_helper.rb'
16
+ - 'vendor/**/*'
17
+ - 'tmp/**/*'
18
+ - 'spec/dummy/**/*'
19
+ SuggestExtensions: false
20
+ NewCops: enable
21
+
22
+ Style/Documentation:
23
+ Enabled: false
24
+
25
+ Style/FrozenStringLiteralComment:
26
+ Enabled: false
27
+
28
+ Style/SpecialGlobalVars:
29
+ Enabled: false
30
+
31
+ Style/ClassVars:
32
+ Enabled: false
33
+
34
+ Layout/LineLength:
35
+ Enabled: true
36
+ Max: 120
37
+
38
+ Lint/RedundantCopEnableDirective:
39
+ Enabled: false
40
+
41
+ Metrics/MethodLength:
42
+ Max: 20
43
+
44
+ Metrics/AbcSize:
45
+ Max: 25
data/.travis.yml ADDED
@@ -0,0 +1,69 @@
1
+ os: linux
2
+ dist: bionic
3
+
4
+ branches:
5
+ only:
6
+ - master
7
+
8
+ cache: bundler
9
+
10
+ services:
11
+ - mysql
12
+ - postgresql
13
+
14
+ language: ruby
15
+
16
+ rvm:
17
+ - 2.3
18
+ - 2.4
19
+ - 2.5
20
+ - 2.7
21
+ - 3.0
22
+
23
+ env:
24
+ - DB=mysql
25
+ - DB=postgres
26
+
27
+ gemfile:
28
+ - gemfiles/spree_3_0.gemfile
29
+ - gemfiles/spree_3_1.gemfile
30
+ - gemfiles/spree_3_7.gemfile
31
+ - gemfiles/spree_master.gemfile
32
+
33
+ jobs:
34
+ fast_finish: true
35
+ allow_failures:
36
+ - gemfile: gemfiles/spree_master.gemfile
37
+ exclude:
38
+ - rvm: 2.3
39
+ gemfile: gemfiles/spree_3_7.gemfile
40
+ - rvm: 2.3
41
+ gemfile: gemfiles/spree_master.gemfile
42
+ - rvm: 2.4
43
+ gemfile: gemfiles/spree_3_7.gemfile
44
+ - rvm: 2.4
45
+ gemfile: gemfiles/spree_master.gemfile
46
+ - rvm: 2.5
47
+ gemfile: gemfiles/spree_3_0.gemfile
48
+ - rvm: 2.5
49
+ gemfile: gemfiles/spree_3_1.gemfile
50
+ - rvm: 2.5
51
+ gemfile: gemfiles/spree_master.gemfile
52
+ - rvm: 2.7
53
+ gemfile: gemfiles/spree_3_0.gemfile
54
+ - rvm: 2.7
55
+ gemfile: gemfiles/spree_3_1.gemfile
56
+ - rvm: 3.0
57
+ gemfile: gemfiles/spree_3_0.gemfile
58
+ - rvm: 3.0
59
+ gemfile: gemfiles/spree_3_1.gemfile
60
+ - rvm: 3.0
61
+ gemfile: gemfiles/spree_3_7.gemfile
62
+
63
+ script:
64
+ - bundle exec rake test_app
65
+ - bundle exec rake spec
66
+
67
+ before_install:
68
+ - sudo apt-get install libmysqlclient-dev
69
+ - mysql -u root -e "GRANT ALL ON *.* TO 'travis'@'%';"
data/Appraisals ADDED
@@ -0,0 +1,63 @@
1
+ appraise 'spree-3-0' do
2
+ gem 'sqlite3', '~> 1.3.6'
3
+ gem "sprockets", "~> 3.7.2"
4
+ gem 'spree_core', '~> 3.0.0'
5
+ gem 'spree_backend', '~> 3.0.0'
6
+ gem 'pg', '~> 0.18'
7
+ gem 'factory_girl'
8
+ gem 'rails_test_params_backport'
9
+ gem 'rubocop'
10
+ gem 'sass-rails'
11
+ end
12
+
13
+ appraise 'spree-3-1' do
14
+ gem 'sqlite3', '~> 1.3.6'
15
+ gem "sprockets", "~> 3.7.2"
16
+ gem 'spree_core', '~> 3.1.0'
17
+ gem 'spree_backend', '~> 3.1.0'
18
+ gem 'pg', '~> 0.18'
19
+ gem 'factory_girl'
20
+ gem 'rails_test_params_backport'
21
+ gem 'sass-rails'
22
+ gem 'rubocop'
23
+ end
24
+
25
+ appraise 'spree-3-7' do
26
+ gem 'spree_core', '~> 3.7.0'
27
+ gem 'spree_backend', '~> 3.7.0'
28
+ gem 'rails-controller-testing'
29
+ gem 'sass-rails'
30
+ gem 'rubocop'
31
+ end
32
+
33
+ appraise 'spree-4-0' do
34
+ gem 'spree_core', '~> 4.0.0.rc1'
35
+ gem 'spree_backend', '~> 4.0.0'
36
+ gem 'rails-controller-testing'
37
+ gem 'rspec-rails', '~> 4.0.0.beta2'
38
+ gem 'rubocop'
39
+ end
40
+
41
+ appraise 'spree-4-1' do
42
+ gem 'spree_core', '~> 4.1.0'
43
+ gem 'spree_backend', '~> 4.1.0'
44
+ gem 'rails-controller-testing'
45
+ gem 'rspec-rails', '~> 4.0.0.beta2'
46
+ gem 'rubocop'
47
+ end
48
+
49
+ appraise 'spree-4-2' do
50
+ gem 'spree_core', '~> 4.2.0.beta'
51
+ gem 'spree_backend', '~> 4.2.0.beta'
52
+ gem 'rails-controller-testing'
53
+ gem 'rspec-rails', '~> 4.0.0.beta2'
54
+ gem 'rubocop'
55
+ end
56
+
57
+ appraise 'spree-master' do
58
+ gem 'spree_core', github: 'spree/spree', branch: 'master'
59
+ gem 'spree_backend', github: 'spree/spree', branch: 'master'
60
+ gem 'rails-controller-testing'
61
+ gem 'rspec-rails', '~> 4.0.0.beta2'
62
+ gem 'rubocop'
63
+ end
@@ -0,0 +1,128 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, religion, or sexual identity
10
+ and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the
26
+ overall community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or
31
+ advances of any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email
35
+ address, without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official e-mail address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ hello@upsidelab.io.
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series
86
+ of actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or
93
+ permanent ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within
113
+ the community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.0, available at
119
+ https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
120
+
121
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct
122
+ enforcement ladder](https://github.com/mozilla/diversity).
123
+
124
+ [homepage]: https://www.contributor-covenant.org
125
+
126
+ For answers to common questions about this code of conduct, see the FAQ at
127
+ https://www.contributor-covenant.org/faq. Translations are available at
128
+ https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem 'rails-controller-testing'
6
+
7
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,11 @@
1
+ Copyright 2021 Upside Lab sp. z o.o.
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
+
11
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,473 @@
1
+ # SpreeGladly
2
+ [comment]: <> (add Travis status build badge when repo became public)
3
+
4
+ ## Overview
5
+
6
+ This exetension allows you to connect your [Spree](https://github.com/spree/spree) store with [Gladly](https://www.gladly.com/) service. It allows Gladly agents to see basic information about Spree customers and their orders.
7
+
8
+ It adheres to the specification of a Gladly Lookup adapter as described [here](https://developer.gladly.com/tutorials/lookup).
9
+
10
+ Supported Spree versions: `3.0`, `3.1`, `3.7`, `4.0`, `4.1`, `4.2`
11
+
12
+ <img src="./spree.png">
13
+
14
+
15
+ ## Table of contents
16
+ - [SpreeGladly](#spreegladly)
17
+ - [Overview](#overview)
18
+ - [Table of contents](#table-of-contents)
19
+ - [Installation](#installation)
20
+ - [Configuration](#configuration)
21
+ - [Spree Store side](#spree-store-side)
22
+ - [!!! Important !!!](#-important-)
23
+ - [Gladly Service side](#gladly-service-side)
24
+ - [Usage](#usage)
25
+ - [Basic Lookup](#basic-lookup)
26
+ - [Manual Search Request](#manual-search-request)
27
+ - [Automatic Search Request](#automatic-search-request)
28
+ - [Basic Lookup Response](#basic-lookup-response)
29
+ - [Detailed Lookup](#detailed-lookup)
30
+ - [How does the search work? What do the fields mean?](#how-does-the-search-work-what-do-the-fields-mean)
31
+ - [Basic search](#basic-search)
32
+ - [Detailed search](#detailed-search)
33
+ - [Customization](#customization)
34
+ - [Setup sandbox environment](#setup-sandbox-environment)
35
+ - [Testing](#testing)
36
+ - [Contributing](#contributing)
37
+ - [Code of Conduct](#code-of-conduct)
38
+
39
+ ## Installation
40
+
41
+ Add this line to your application's Gemfile:
42
+
43
+ ```ruby
44
+ gem 'spree_gladly'
45
+ ```
46
+
47
+ And then execute:
48
+
49
+ $ bundle install
50
+
51
+ Next, you should run the installer:
52
+
53
+ $ bundle exec rails generate spree_gladly:install
54
+
55
+
56
+ ## Configuration
57
+
58
+ ### Spree Store side
59
+
60
+ After installation, you will find in `config/initializers/spree_gladly.rb` directory the below file:
61
+
62
+ ```ruby
63
+ SpreeGladly.setup do |config|
64
+ # The key used to validate the Gladly lookup request signature.
65
+ # We recommend using a secure random string of length over 32.
66
+ config.signing_key = '<%= SecureRandom.base64(32) %>'
67
+
68
+ # You can change serializer on your own
69
+ config.basic_lookup_presenter = Customer::BasicLookupPresenter
70
+ config.detailed_lookup_presenter = Customer::DetailedLookupPresenter
71
+ config.order_limit = nil
72
+ config.order_includes = [:line_items]
73
+ config.order_sorting = { created_at: :desc }
74
+ config.order_states = ['complete']
75
+
76
+ # The request's timestamp is validated against `signing_threshold` to prevent replay attacks.
77
+ # Setting this value to `0` disables the threshold validation.
78
+ # Default is `0`.
79
+ # config.signing_threshold = 5.minutes
80
+ end
81
+
82
+ ```
83
+
84
+ where you are able to set the preferences:
85
+
86
+ - **signing_key:** *cryptographic key to sign every request to your lookup service*
87
+ - **signing_threshold:** *time value to prevent replay attacks ( default: 0 )*
88
+ - **basic_lookup_presenter:** *presenter which is responsible for basic lookup `results` payload ( default: Customer::BasicLookupPresenter )*
89
+ - **detailed_lookup_presenter:** *presenter which is responsible for detailed lookup `results` payload ( default: Customer::DetailedLookupPresenter )*
90
+ - **order_limit:** *you can set limit returned orders number in `detailed lookup` response, if `nil` than no limits `default: nil`*
91
+ - **order_includes:** *you can set what relation should be included in query, `default: :line_items`. This gets passed into .include() when fetching detailed lookup - if you want to display data from order's relationships, you may want to optimize the query*
92
+ - **order_sorting:** *you can set how returned orders should be sorted `default: { created_at: :desc }`
93
+ - **order_states:** *you can set order `state` which should be returned in response `default: ['complete']`. This defines states of `orders` that will be returned to Gladly (and that by default it will exclude `Spree::Orders` in `cart|address|delivery|payment` states*
94
+
95
+ You can also set `signing_key` and `signing_threshold` via the admin dashboard in your Spree instance. To do that, open `Gladly Settings` in the `Configurations` section.
96
+
97
+ <img width="1436" alt="gladly_settings_admin_dashboard" src="https://user-images.githubusercontent.com/1455599/123083627-83c99400-d420-11eb-87ca-c1c5e20583d9.png">
98
+
99
+ ### !!! Important !!!
100
+
101
+ Detailed lookups find customer's orders based on customer's profile, but will also include guest orders made with the same email address.
102
+ By default, Spree doesn't index the `email` field of `Spree::Orders` table. To ensure smooth operation of the lookup endpoint, add the following migration to your application.
103
+ ```ruby
104
+ class AddEmailIndexToSpreeOrders < ActiveRecord::Migration
105
+
106
+ def change
107
+ add_index :spree_orders, :email
108
+ end
109
+ end
110
+ ```
111
+ ***Note: please adjust migration to yours Rails version***
112
+
113
+
114
+ ### Gladly Service side
115
+
116
+ To understand how to set up the integration in Gladly please refer to the Gladly help docs
117
+
118
+ You will need the following information:
119
+ - lookup endpoint ( `https://example-spree-store.com/api/v1/customers/lookup` ), where `https://example-spree-store.com` is **your** Spree store URL.
120
+ - signing_key
121
+
122
+ ## Usage
123
+
124
+ To understand how the extension works from user's point of view please refer to the Gladly help docs.
125
+
126
+ The below description will assume that the reader has familiarized themselves with [Gladly Lookup Adapter tutorial](https://developer.gladly.com/tutorials/lookup).
127
+
128
+ ### Basic Lookup
129
+
130
+ The aim of basic search is to allow the agent to find potential customers that match the information that they have at hand, e.g. email, phone number, name.
131
+ As mentioned in the [Gladly tutorial](https://developer.gladly.com/tutorials/lookup) the initial search can be triggered automatically or by agent and because of that the Spree adapter accepts two different formats of requests.
132
+
133
+ #### Manual Search Request
134
+ OOTB the extension allows searching by email, phone number and name of the customer. Additional fields can be added.
135
+
136
+ ```json
137
+ {
138
+ "lookupLevel":"BASIC",
139
+ "uniqueMatchRequired":false,
140
+ "query":{
141
+ "emails":"customer@example.com",
142
+ "phones":"666-666-666",
143
+ "name": "Elka Melka",
144
+ }
145
+ }
146
+ ```
147
+
148
+ #### Automatic Search Request
149
+ The request is automatically populated with data available in customer's profile. OOTB the extension searches for customers based on the name and emails (ignoring the rest of the fields).
150
+
151
+ ```json
152
+ {
153
+ "lookupLevel":"BASIC",
154
+ "uniqueMatchRequired":false,
155
+ "query":
156
+ {
157
+ "emails":["customer@example.com", "another@email.com"],
158
+ "phones": ["666-666-666", "123-435-235"],
159
+ "name": "Elka Melka",
160
+ "lifetimeValue": "$500"
161
+ }
162
+ }
163
+ ```
164
+
165
+ #### Basic Lookup Response
166
+
167
+ By default the fields listed below are returned. Fields can be hidden via Gladly UI or the integration
168
+ can be extended to return more/different fields ([Customization](#customization)). The format of the response has to match the [Gladly Customer schema](https://developer.gladly.com/rest/#operation/createCustomer)
169
+
170
+ Note: we use the email address as the `externalCustomerId` and not the id used by Spree.
171
+
172
+ ```json
173
+ {
174
+ "results":[
175
+ {
176
+ "externalCustomerId": "customer@example.com",
177
+ "customAttributes": {
178
+ "spreeId": 1
179
+ },
180
+ "name": "James Bond",
181
+ "emails": [{"original": "customer@example.com"}],
182
+ "phones": [{"original": "666-666-666"}],
183
+ "address": "22 Apple Lane, 9999 San Francisco"
184
+ }
185
+ ]
186
+ }
187
+ ```
188
+
189
+ ### Detailed Lookup
190
+
191
+ Detailed lookup is used to update the linked customer with detailed data. This means that detailed lookup expects only one result returned. Gladly will send in the request all the customer's information except transactions. However, only the `externalCustomerId` is used to find the correct customer in Spree.
192
+
193
+ **request payload:**
194
+
195
+ ```json
196
+ {
197
+ "lookupLevel":"DETAILED",
198
+ "uniqueMatchRequired":true,
199
+ "query":{
200
+ "name": "Some name",
201
+ "totalOrderCount": "4",
202
+ "emails":[ "customer@example.com", "another@email.com"],
203
+ "phones":[
204
+ "666-666-666"
205
+ ],
206
+ "externalCustomerId":"customer@example.com",
207
+ "customAttributes": {
208
+ "spreeId": 1
209
+ }
210
+ }
211
+ }
212
+ ```
213
+
214
+ **response payload:**
215
+ The following payload shows all the fields that are by default returned from Spree. You can ask your Gladly representative to amend which fields are visible in the order card or the integration can be extended to return more/different fields ([Customization](#customization))
216
+
217
+ ```json
218
+ {
219
+ "results":[
220
+ {
221
+ "externalCustomerId": "customer@example.com",
222
+ "name":"James Bond",
223
+ "address":"Baker Street 007 London, AK 00021 United Kingdom",
224
+ "emails":[
225
+ {
226
+ "original":"customer@example.com"
227
+ }
228
+ ],
229
+ "phones":[
230
+ {
231
+ "original":"666-666-666"
232
+ }
233
+ ],
234
+ "customAttributes":{
235
+ "spreeId": "4",
236
+ "lifetimeValue":"$142.97",
237
+ "totalOrderCount":"2",
238
+ "guestOrderCount": "0",
239
+ "memberSince" : "May 17, 2021 10:18 AM UTC",
240
+ "customerLink": "https://example-spree-store.com/admin/users/4/edit"
241
+ },
242
+ "transactions":[
243
+ {
244
+ "type":"ORDER",
245
+ "orderStatus":"complete",
246
+ "orderNumber":"R185194841",
247
+ "orderLink":"https://example-spree-store.com/admin/orders/R185194841/edit",
248
+ "note":"",
249
+ "orderTotal":"$83.99",
250
+ "createdAt":"2021-06-21T10:29:43.881Z",
251
+ "products":[
252
+ {
253
+ "name":"Flared Midi Skirt",
254
+ "status":"fulfilled",
255
+ "sku": "SKU-1",
256
+ "quantity":"1",
257
+ "unitPrice":"$78.99",
258
+ "total": "$78.99",
259
+ "imageUrl":""
260
+ }
261
+ ]
262
+ },
263
+ {
264
+ "type":"ORDER",
265
+ "orderStatus":"complete",
266
+ "orderNumber":"R461455233",
267
+ "orderLink":"https://example-spree-store.com/admin/orders/R461455233/edit",
268
+ "note":"Some note about the order",
269
+ "orderTotal":"$58.98",
270
+ "createdAt":"2021-06-21T10:34:10.262Z",
271
+ "products":[
272
+ {
273
+ "name":"3 4 Sleeve T Shirt",
274
+ "status":"fulfilled",
275
+ "sku": "SKU-2",
276
+ "quantity":"2",
277
+ "unitPrice":"$26.99",
278
+ "total": "$53.98",
279
+ "imageUrl":"https://example-spree-store.com/images/sample_picture.jpg"
280
+ }
281
+ ]
282
+ }
283
+ ]
284
+ }
285
+ ]
286
+ }
287
+ ```
288
+
289
+ ### How does the search work? What do the fields mean?
290
+
291
+ It's worth noting that in Spree customers are able to create orders without being logged in. It is important to be able to link such customers with their Gladly profiles to be able to help them.
292
+ This slightly complicates the search and makes it worth an explanation. Since we want to be able to identify both registered customers and customers with only guest orders, we are using the email address instead of the `Spree::Account.id` as `externalCustomerId` in Gladly (the unique identifier of a customer in an external system).
293
+
294
+ Note details on the exact format of the jsons was shown in previous section. The below tables are for information of the meaning of the fields in Spree.
295
+
296
+ #### Basic search
297
+
298
+ As mentioned in [Basic Lookup](#basic-lookup) searching by name, emails and phone numbers is possible.
299
+ To simplify the search logic and improve performance, searching by name and phone number will only search customers with Spree profiles.
300
+ Searching by email searches both registered customers and guest orders. It happens in two phases (for each customer's email separately):
301
+
302
+ 1. Attempt to find any customers with a Spree profile with the given email. If found, search will return that customer.
303
+ 2. If no customer is found the search will attempt to find all orders associated with the given email. If multiple orders are found it will return the details of the latest (`completed_at`) order.
304
+
305
+ Below table explains the returned fields.
306
+
307
+ **For registered customers:**
308
+
309
+ | Gladly customer field | Spree field |
310
+ | ------------------------ | ------------------------------------------------------------- |
311
+ | name | Spree::User.bill_address.full_name |
312
+ | externalCustomerId | Spree::User.email |
313
+ | emails | Spree::User.email |
314
+ | customAttributes.spreeId | Spree::User.id |
315
+ | phones | Spree::User.bill_address.phone |
316
+ | address | Spree::User.bill_address (address1, address2, city, zipcode) |
317
+
318
+ **For guest customers**
319
+
320
+ In the below table Spree::Order means the latest (`Spree::Order.completed_at`) order that matched the email from the search.
321
+
322
+ | Gladly field | Spree |
323
+ | ------------------------ | ------------------------------------------------------------- |
324
+ | name | Spree::Order.bill_address.full_name |
325
+ | externalCustomerId | Spree::Order.email |
326
+ | emails | Spree::Order.email |
327
+ | customAttributes.spreeId | - |
328
+ | phones | Spree:Order.bill_address.phone |
329
+ | address | Spree::Order.bill_address.(address1, address2, city, zipcode) |
330
+
331
+ #### Detailed search
332
+
333
+ Detailed search assumes that we have found the right customer and we know their unique identifier. In Gladly this is the `externalCustomerId` and in Spree, it's equivalent to the email address.
334
+
335
+ The below tables list the fields returned from Spree.
336
+
337
+ **For registered customers:**
338
+
339
+ | Gladly field | Spree field |
340
+ | -------------------------------- | ----------------------------------------------------------------------------- |
341
+ | name | Spree::User.bill_address.full_name |
342
+ | externalCustomerId | Spree::User.email |
343
+ | emails | Spree::User.email |
344
+ | phones | Spree::User.bill_address.phone |
345
+ | address | Spree::User.bill_address.(address1 , address2, city, zipcode) |
346
+ | customAttributes.spreeId | Spree::User.id |
347
+ | customAttributes.totalOrderCount | total of Spree::Order(s) that match the `externalCustomerId` |
348
+ | customAttributes.guestOrderCount | customAttributes.totalOrderCount - Spree::Account.attributes.completed_orders |
349
+ | customAttributes.memberSince | Spree::User.created_at |
350
+ | customAttributes.customerLink | customer_profile_url(Spree::User) |
351
+ | customAttributes.lifetimeValue | sum `total` field of Spree::Order(s) that match the `externalCustomerId` |
352
+ | transactions | details of all Spree::Orders that match the `externalCustomerId` |
353
+
354
+ **For guest customers**
355
+
356
+ | Gladly field | Spree field |
357
+ | -------------------------------- | ------------------------------------------------------------------------ |
358
+ | name | - |
359
+ | externalCustomerId | Spree::User.email |
360
+ | emails | Spree::User.email |
361
+ | phones | - |
362
+ | address | - |
363
+ | customAttributes.spreeId | - |
364
+ | customAttributes.totalOrderCount | total of Spree::Orders that match the `externalCustomerId` |
365
+ | customAttributes.guestOrderCount | customAttributes.totalOrderCount |
366
+ | customAttributes.memberSince | - |
367
+ | customAttributes.customerLink | - |
368
+ | customAttributes.lifetimeValue | sum `total` field of Spree::Order(s) that match the `externalCustomerId` |
369
+ | transactions | details of all Spree::Orders that match the `externalCustomerId` |
370
+
371
+ ## Customization
372
+
373
+ Within `spree_gladly` gem we distinguish response for `guest` and `registerd` customer. For customize those, i.e [detailed lookup response](#detailed-lookup), to do that you have do following steps:
374
+
375
+ 1. replace `Customer::DetailedLookupPresenter` in `config/initializers/spree_gladly.rb` initializer file with your own.
376
+ 2. override methods `registerd_presenter` ( [default presenter](https://github.com/upsidelab/spree_gladly/blob/master/app/presenters/customer/registered/detailed_presenter.rb) ) or `guest_presenter` ( [default presenter](https://github.com/upsidelab/spree_gladly/blob/master/app/presenters/customer/guest/detailed_presenter.rb) ) with your own.
377
+
378
+ Please consider below example:
379
+
380
+ ```ruby
381
+ class GladlyCustomersPresenter
382
+ include Spree::Core::Engine.routes.url_helpers # this is important if you want to use Spree routes
383
+
384
+ def initialize(resource:)
385
+ @resource = resource
386
+ end
387
+
388
+ def to_h
389
+ return [] unless resource.customer.present?
390
+
391
+ resource.guest ? guest_presenter : registered_presenter
392
+ end
393
+
394
+ private
395
+
396
+ attr_reader :resource
397
+
398
+ def registered_presenter
399
+ YourOwn::DetailedPresenter.new(resource: resource).to_h
400
+ end
401
+
402
+ def guest_presenter
403
+ Customer::Guest::DetailedPresenter.new(resource: resource).to_h
404
+ end
405
+ ...
406
+ end
407
+ ```
408
+
409
+ `config/initializers/spree_gladly.rb`
410
+
411
+ ```ruby
412
+ SpreeGladly.setup do |config|
413
+ # ...
414
+
415
+ config.basic_lookup_presenter = Customer::BasicLookupPresenter
416
+ config.detailed_lookup_presenter = GladlyCustomersPresenter
417
+
418
+ # ...
419
+ end
420
+ ```
421
+
422
+ **!!! Important !!!**
423
+
424
+ If you would like to resign from `to_h` or change `initialize(resource:)` method, you have to override `Spree::Api::V1::CustomersController#serialize_collection` to do that, please follow by this [guide](https://guides.spreecommerce.org/developer/customization/logic.html#extending-controllers)
425
+
426
+ `app/controllers/spree/api/v1/customers_controller.rb`
427
+
428
+ ```ruby
429
+ def serialize_collection(type:, collection:)
430
+ presenter = {
431
+ detailed: SpreeGladly::Config.detailed_lookup_presenter.new(resource: collection),
432
+ basic: SpreeGladly::Config.basic_lookup_presenter.new(resource: collection)
433
+ }[type]
434
+
435
+ { results: presenter.to_h }
436
+ end
437
+ ```
438
+
439
+ ## Setup sandbox environment
440
+
441
+ 1. Deploy Spree store on hosting provider ( [heroku example](https://guides.spreecommerce.org/developer/deployment/heroku.html) )
442
+ 2. Install `spree_gladly` gem
443
+ 3. Setup `signing_key` and provide to **Gladly** agent
444
+
445
+ ## Testing
446
+
447
+ ````
448
+ bundle exec rake test_app
449
+ bundle exec rake spec
450
+ ````
451
+
452
+ To run specific test:
453
+ ````
454
+ bundle exec rspec spec/controllers/spree/api/v1/customers_controller_spec.rb
455
+ ````
456
+
457
+ Testing against specific Spree version:
458
+ ````
459
+ export BUNDLE_GEMFILE=gemfiles/spree_3_7.gemfile
460
+
461
+ bundle install
462
+
463
+ bundle exec rake test_app
464
+ bundle exec rake spec
465
+ ````
466
+
467
+ ## Contributing
468
+
469
+ Bug reports and pull requests are welcome on GitHub at https://github.com/upsidelab/spree_gladly.
470
+
471
+ ## Code of Conduct
472
+
473
+ Everyone interacting in the SpreeGladly project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/upsidelab/spree_gladly/blob/master/CODE_OF_CONDUCT.md).