summaryrefslogtreecommitdiff
path: root/app/services
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2023-04-01 12:00:33 -0600
committerHombreLaser <sebastian-440@live.com>2023-04-01 12:00:33 -0600
commit753bf8499ba5d8815db8d08a1dcf69caa20660cf (patch)
treec758e18e3009f9c6da002164686e88956430847b /app/services
parent64214e370778dc3efdaca85acd0cd7a569a79b81 (diff)
Mejora estructura del proyecto
Diffstat (limited to 'app/services')
-rw-r--r--app/services/addresses/address_services_base.rb22
-rw-r--r--app/services/addresses/create_address_service.rb19
-rw-r--r--app/services/addresses/destroy_address_service.rb18
-rw-r--r--app/services/addresses/update_address_service.rb28
-rw-r--r--app/services/token_generation_service.rb25
5 files changed, 112 insertions, 0 deletions
diff --git a/app/services/addresses/address_services_base.rb b/app/services/addresses/address_services_base.rb
new file mode 100644
index 0000000..06ffde7
--- /dev/null
+++ b/app/services/addresses/address_services_base.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+module Addresses
+ # The base class for all addresses' services.
+ class AddressServicesBase
+ def initialize(user_account, params)
+ @user_account = user_account
+ @params = params
+ end
+
+ private
+
+ def already_existing_address
+ @already_existing_address ||= Address.find_by(@params)
+ end
+
+ def add_address_to_user_addresses
+ already_existing_address.user_accounts << @user_account
+ already_existing_address.save
+ end
+ end
+end
diff --git a/app/services/addresses/create_address_service.rb b/app/services/addresses/create_address_service.rb
new file mode 100644
index 0000000..ef593a4
--- /dev/null
+++ b/app/services/addresses/create_address_service.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+module Addresses
+ # The service for address creation.
+ class CreateAddressService < AddressServicesBase
+ attr_reader :address
+
+ def call
+ if already_existing_address.nil?
+ @address = Address.new(@params)
+ @address.user_accounts << @user_account
+ @address.save
+ else
+ add_address_to_user_addresses
+ @address = already_existing_address
+ end
+ end
+ end
+end
diff --git a/app/services/addresses/destroy_address_service.rb b/app/services/addresses/destroy_address_service.rb
new file mode 100644
index 0000000..e217d11
--- /dev/null
+++ b/app/services/addresses/destroy_address_service.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+module Addresses
+ # The service for address deletion.
+ class DestroyAddressService < AddressServicesBase
+ def initialize(user_account, params, address)
+ super(user_account, params)
+ @address = address
+ end
+
+ def call
+ UserAccountAddress.find_by(address_id: @address.id, user_account_id: @user_account.id).destroy
+ return unless @address.user_accounts.count.zero?
+
+ @address.destroy
+ end
+ end
+end
diff --git a/app/services/addresses/update_address_service.rb b/app/services/addresses/update_address_service.rb
new file mode 100644
index 0000000..fe26b05
--- /dev/null
+++ b/app/services/addresses/update_address_service.rb
@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+
+module Addresses
+ # The service for address creation.
+ class UpdateAddressService < AddressServicesBase
+ attr_reader :address
+
+ def initialize(user_account, address_id, params)
+ super(user_account, params)
+ @address_id = address_id
+ end
+
+ def call
+ @address = @user_account.addresses.find_by(id: @address_id)
+
+ return :not_found if @address.nil?
+
+ if already_existing_address.nil?
+ return :unprocessable_entity unless @address.update(@params)
+ else
+ add_address_to_user_addresses
+ @address.user_account_addresses.find_by(user_account_id: @user_account.id).destroy
+ @address.destroy if @address.user_accounts.count.zero?
+ @address = already_existing_address
+ end
+ end
+ end
+end
diff --git a/app/services/token_generation_service.rb b/app/services/token_generation_service.rb
new file mode 100644
index 0000000..3af1e3f
--- /dev/null
+++ b/app/services/token_generation_service.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+# The service responsible for JWT generation.
+class TokenGenerationService
+ def initialize(params)
+ @email = params[:email]
+ @role = params[:role]
+ @user_account = UserAccount.find_by(email: @email)
+ end
+
+ def call(refresh_token_expiration)
+ @user_account.update_attribute(:session_key, SecureRandom.hex(16))
+ { token: generate_token(DateTime.current + 30.minutes), refresh: generate_token(refresh_token_expiration.to_i) }
+ end
+
+ private
+
+ def generate_token(expiration)
+ JWT.encode(payload.merge({ exp: expiration.to_i }), ENV['HMAC_SECRET_KEY'], 'HS512')
+ end
+
+ def payload
+ { data: @email, aud: @role, jti: @user_account.session_key }
+ end
+end