-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from equintana/reset
✨ Reset command
- Loading branch information
Showing
9 changed files
with
214 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ def name | |
end | ||
|
||
def value | ||
@value ||= data[2].to_f | ||
@value ||= data[2].to_i | ||
end | ||
|
||
def service_exists? | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# frozen_string_literal: true | ||
require 'lita/helpers/messages_helper' | ||
|
||
module Lita | ||
module Interactors | ||
# Set the customer quantity to zero | ||
class ResetQuantity < BaseInteractor | ||
include Lita::Helpers::MessagesHelper | ||
|
||
attr_reader :data | ||
|
||
def initialize(handler, data) | ||
@handler = handler | ||
@data = data | ||
end | ||
|
||
def perform | ||
if service_exists? | ||
update_costumer_if_exist | ||
else | ||
@error = msg_not_found(service_name: name) | ||
end | ||
self | ||
end | ||
|
||
private | ||
|
||
def name | ||
@name ||= data[1] | ||
end | ||
|
||
def customer_name | ||
@customer_name ||= data[2].delete('@') | ||
end | ||
|
||
def service | ||
@service ||= repository.find(name) | ||
end | ||
|
||
def customer | ||
service[:customers][customer_name.to_sym] | ||
end | ||
|
||
def service_exists? | ||
repository.exists?(name) | ||
end | ||
|
||
def customer_exists? | ||
service[:customers].keys.include?(customer_name.to_sym) | ||
end | ||
|
||
def update_costumer_if_exist | ||
if customer_exists? | ||
update_customer_quantity | ||
else | ||
@error = msg_customer_not_found(service_name: name, | ||
customer_name: customer_name) | ||
end | ||
end | ||
|
||
def update_customer_quantity | ||
reset_quantity | ||
repository.update(service) | ||
end | ||
|
||
def reset_quantity | ||
customer[:quantity] = 0 | ||
|
||
@message = I18n.t('lita.handlers.service.reset.success', | ||
customer_name: customer_name) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# frozen_string_literal: false | ||
require 'spec_helper' | ||
|
||
describe Lita::Interactors::ResetQuantity do | ||
let(:data) { ['service the-service reset @erlinis', name, 'erlinis'] } | ||
let(:interactor) { described_class.new(handler, data) } | ||
let(:handler) { double('handler') } | ||
let(:fake_repository) { double('redis-repository') } | ||
|
||
before do | ||
allow(interactor).to receive(:repository).and_return(fake_repository) | ||
end | ||
|
||
describe '#perform' do | ||
let(:name) { 'the-service' } | ||
|
||
describe 'when the service does not exist' do | ||
let(:error_message) do | ||
I18n.t('lita.handlers.service.errors.not_found', service_name: name) | ||
end | ||
|
||
before do | ||
allow(fake_repository).to receive(:exists?).with(name).and_return(false) | ||
end | ||
|
||
it 'shows an error message' do | ||
interactor.perform | ||
expect(interactor.success?).to eq false | ||
expect(interactor.error).to eq error_message | ||
end | ||
end | ||
|
||
describe 'when service exists' do | ||
describe 'when customer is not in the service' do | ||
let(:service) do | ||
{ name: name, | ||
value: 2000, | ||
state: 'active', | ||
customers: { khal: { quantity: 1, value: 2000 } } } | ||
end | ||
|
||
let(:error_message) do | ||
I18n.t('lita.handlers.service.customer.not_found', | ||
service_name: name, customer_name: 'erlinis') | ||
end | ||
|
||
before do | ||
allow(fake_repository).to receive(:exists?).with(name).and_return(true) | ||
allow(fake_repository).to receive(:find).with(name).and_return(service) | ||
end | ||
|
||
it 'shows an error message' do | ||
interactor.perform | ||
expect(interactor.success?).to eq false | ||
expect(interactor.error).to eq error_message | ||
end | ||
end | ||
|
||
describe 'when customer is in service' do | ||
let(:success_message) do | ||
I18n.t('lita.handlers.service.reset.success', customer_name: 'erlinis') | ||
end | ||
|
||
before do | ||
service = { | ||
name: name, | ||
value: 2000, | ||
state: 'active', | ||
customers: { | ||
erlinis: { quantity: 3, value: 2000 } | ||
} | ||
} | ||
|
||
service_customer_updated = { | ||
name: name, | ||
value: 2000, | ||
state: 'active', | ||
customers: { | ||
erlinis: { quantity: 0, value: 2000 } | ||
} | ||
} | ||
|
||
allow(fake_repository).to receive(:exists?).with(name).and_return(true) | ||
allow(fake_repository).to receive(:find).with(name).and_return(service) | ||
allow(fake_repository).to receive(:update).with(service_customer_updated) | ||
end | ||
|
||
it 'reset the customer quantity to zero' do | ||
interactor.perform | ||
expect(interactor.success?).to eq true | ||
expect(interactor.message).to eq success_message | ||
end | ||
end | ||
end | ||
end | ||
end |