-
Notifications
You must be signed in to change notification settings - Fork 0
[EK-964] Aktywacja rabatów w koszyku #27
Changes from all commits
d80159e
a7d6b41
dae543e
0d96df7
2586e0c
0320346
7fa24cf
c39a599
9a50ad0
e4111c5
1bf9c33
9780e9f
b09072e
cd52b34
6790893
064cb94
1528881
88f7d72
2a34be9
a7b7066
c767750
cc5720a
a48769d
69e3938
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module ErrorHandlingHelper | ||
| # Parses and translate occurred error, then adds it to user errors. | ||
| # Dedicated for storefront controllers. | ||
| # @param [StandardError] | ||
| def handle_spl_error(error) | ||
| payload = Spl::ErrorPayloadParser.parse(error.message) || error | ||
| msg = Spl::ErrorTranslator.translate(payload) | ||
|
|
||
| clear_errors | ||
| try_spree_current_user.errors.add(:base, msg) | ||
| end | ||
|
|
||
| def clear_errors | ||
| try_spree_current_user.errors.clear | ||
| end | ||
|
|
||
| def token_expired?(err_msg) | ||
| err_msg == 'TOKEN_EXPIRED' | ||
| end | ||
| end |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Najs 😍 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module SplServiceHelper | ||
| def send_request(url, body) | ||
| Spl::SendRequestService.new(url, body).call | ||
| end | ||
|
|
||
| # Refreshes user private token to keep possible | ||
| # using customer oriented endpoints | ||
| # @param [user: Spree::User] | ||
| def refresh_user_token(user, store) | ||
| return if user.private_metadata.nil? | ||
| return if user.private_metadata['spl_refresh_token'].nil? | ||
|
|
||
| response = Spl::OauthTokenService.new(DateTime.current, store).refresh_token(user.private_metadata['spl_refresh_token']) | ||
|
|
||
| user.update!(private_metadata: { spl_access_token: response['access_token'], | ||
| spl_refresh_token: response['refresh_token'] }) | ||
| end | ||
|
|
||
| def token_refresh_needed(response_body, retry_counter, user, store) | ||
| token_expired?(response_body['errorCode']) && retry_counter < 1 && refresh_user_token(user, store) | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'json' | ||
|
|
||
| module Spl | ||
| module Coupons | ||
| class ActivateCouponService | ||
| class ActivateCouponServiceError < StandardError; end | ||
| include SplServiceHelper | ||
| include ErrorHandlingHelper | ||
| include LoginCheckHelper | ||
|
|
||
| def initialize(user, store, coupon_code) | ||
| @store = store | ||
| @activate_coupons_url = URI.parse(Spl::UrlCreatorService.new(store.private_metadata['spl_url']).coupon_activate) | ||
| @user = user | ||
| @coupon_code = coupon_code | ||
| @retry_counter = 0 | ||
| end | ||
|
|
||
| def call | ||
| return unless @user.present? && @user.private_metadata.present? | ||
| return unless logged_user?(@user) | ||
|
|
||
| response = send_request(@activate_coupons_url, body) | ||
| response_body = JSON.parse(response.body) | ||
| Rails.logger.debug response_body | ||
| raise ActivateCouponServiceError, response_body['msg'] if response_body['errorCode'] != '0' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Jak tak to zwrócisz, to błąd zawsze będzie po angielsku. Więc do rozważenia, żeby to zrobić 'na modłę' z rejestracji i nadpisać wyświetlanie błędów w kontrolerze z użyciem parsera i translatora
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorki ale co znaczy na modłę? xD i wyświetlenie błędu masz na myśli jako flash message?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Na modłę w sensie w ten sam sposób, podobnie do 🙌 |
||
|
|
||
| response_body['response'] | ||
| rescue ActivateCouponServiceError => e | ||
| raise e unless token_refresh_needed(response_body, @retry_counter, @user, @store) | ||
|
|
||
| @retry_counter += 1 | ||
| retry | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def body | ||
| { | ||
| context: { | ||
| prgCode: @store.private_metadata['spl_prg_code'], | ||
| oauthToken: @user.private_metadata['spl_access_token'] | ||
| }, | ||
| couponCode: @coupon_code | ||
| } | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'json' | ||
|
|
||
| module Spl | ||
| module Coupons | ||
| class DeactivateCouponService | ||
| class DeactivateCouponServiceError < StandardError; end | ||
| include SplServiceHelper | ||
| include ErrorHandlingHelper | ||
| include LoginCheckHelper | ||
|
|
||
| def initialize(user, store, coupon_code) | ||
| @store = store | ||
| @deactivate_coupons_url = URI.parse(Spl::UrlCreatorService.new(store.private_metadata['spl_url']).coupon_deactivate) | ||
| @user = user | ||
| @coupon_code = coupon_code | ||
| @retry_counter = 0 | ||
| end | ||
|
|
||
| def call | ||
| return unless @user.present? && @user.private_metadata.present? | ||
| return unless logged_user?(@user) | ||
|
|
||
| response = send_request(@deactivate_coupons_url, body) | ||
| response_body = JSON.parse(response.body) | ||
| Rails.logger.debug response_body | ||
| raise DeactivateCouponServiceError, response_body['msg'] if response_body['errorCode'] != '0' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tu jak wyżej z parserem i translatorem 🙌
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nie ukrywam, że jestem trochę sceptycznie nastawiony do tłumaczenia kodów błędów. Dzisiaj zwracają coś takiego, jutro zwrócą jakiś inny i trzeba będzie to aktualizować :/ Do tego wiadomości błędu związane z kuponami akurat są o zerowej przydatności - CANNOT_BE_MODIFIED xd jakby dzięki za info, ale może byście powiedzieli dlaczego. Także tutaj nie jestem przekonany
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Z jednej strony masz rację, aczkolwiek też zabezpieczyłam się przed tym, że podeślą coś czego nie znamy (bo ofc dokumentacja nie mówi o wszystkim co nam dostawca może zwrócić) po prostu tym, że mamy błąd generic. Więc użytkownik na pewno nie dostanie translation missing, bo gdy nie znajdzie klucza, to wrzuci generic. |
||
|
|
||
| response_body['response'] | ||
| rescue DeactivateCouponServiceError => e | ||
| raise e unless token_refresh_needed(response_body, @retry_counter, @user, @store) | ||
|
|
||
| @retry_counter += 1 | ||
| retry | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def body | ||
| { | ||
| context: { | ||
| prgCode: @store.private_metadata['spl_prg_code'], | ||
| oauthToken: @user.private_metadata['spl_access_token'] | ||
| }, | ||
| couponCode: @coupon_code | ||
| } | ||
| end | ||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Z czystej ciekawości, skąd się wzięły podpicia w Gemfile.lock jak nie ma żadnych zmian w Gemfile?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@agnieszkajacek Z EK-685, które pewnie poszło razem z Veroną na wyższą wersję 🤔 (tam również nie ma zmian w gemspec/gemfile). Co ty na to, żeby tutaj podbić w gemspec na spree 5.2?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dla mnie spoko!