Skip to content

Commit

Permalink
✨ Increase the quantity of a service used for a customer
Browse files Browse the repository at this point in the history
  • Loading branch information
Erlinis Quintana committed Nov 9, 2016
1 parent 1debba9 commit 9de1869
Show file tree
Hide file tree
Showing 10 changed files with 321 additions and 14 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ gem "lita-service"
## Usage

``` sh
lita service create <NAME> <*VALUE> # Value will be set to 0 if empty.
lita service create <NAME> <*VALUE> # Value will be set to 0 if empty.
lita service show <NAME>
lita service delete <NAME>

lita service <NAME> inscribe <CUSTOMER> <*VALUE> # Value will set to service's value if empty.
lita service <NAME> inscribe <CUSTOMER> <*VALUE> # Value will be set to service's value if empty.
lita service <NAME> add <CUSTOMER> <*QUANTITY> # Quantity will be set to 1 if empty.
```

## Example
Expand All @@ -31,4 +32,5 @@ gem "lita-service"
lita service delete awesome-service

lita service awesome-service inscribe erlinis
lita service awesome-service add erlinis 2
```
1 change: 1 addition & 0 deletions lib/lita-service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'lita/handlers/service'
require 'lita/interactors/base_interactor'
require 'lita/interactors/create_service'
require 'lita/interactors/add_quantity'
require 'lita/interactors/show_service'
require 'lita/interactors/delete_service'
require 'lita/interactors/inscribe_customer'
Expand Down
11 changes: 11 additions & 0 deletions lib/lita/handlers/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Service < Handler
route(/show ([\w-]+)/, :show)
route(/delete ([\w-]+)/, :delete)
route(/([\w-]+) inscribe ([\@\w-]+)( [0-9]*)?/, :inscribe)
route(/([\w-]+) add ([\@\w-]+)( [0-9-]*)?/, :add)

# Callbacks
def pong(response)
Expand Down Expand Up @@ -57,6 +58,16 @@ def inscribe(response)
reply(template, message, response, interactor)
end

def add(response)
interactor = Interactors::AddQuantity
.new(self, response.match_data)
.perform

template = :message
message = { message: interactor.message }
reply(template, message, response, interactor)
end

def reply(template, message, response, interactor)
unless interactor.success?
template = :error
Expand Down
9 changes: 7 additions & 2 deletions lib/lita/helpers/messages_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ def msg_not_found(service_name:)
service_name: service_name)
end

def msg_customer_duplicated(service_name:, customer:)
def msg_customer_not_found(service_name:, customer:)
I18n.t('lita.handlers.service.customer.not_found',
service_name: service_name, customer_name: customer)
end

def msg_customer_duplicated(service_name:, customer_name:)
I18n.t('lita.handlers.service.customer.duplicated',
service_name: service_name,
customer_name: customer)
customer_name: customer_name)
end
end
end
Expand Down
80 changes: 80 additions & 0 deletions lib/lita/interactors/add_quantity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# frozen_string_literal: true
require 'lita/helpers/messages_helper'

module Lita
module Interactors
# Inscribes a customer in a service
class AddQuantity < BaseInteractor
include Lita::Helpers::MessagesHelper

attr_reader :data
DEFAULT_QUANTITY = 1

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 customer_quantity
@customer_quantity ||= data[3].to_s
end

def service
@service ||= repository.find(name)
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: customer_name)
end
end

def update_customer_quantity
new_quantity = increment_quantity
repository.update(service)
@message = I18n.t('lita.handlers.service.add.success',
quantity: quantity_calculated,
customer_name: customer_name,
customer_quantity: new_quantity)
end

def increment_quantity
service[:customers][customer_name.to_sym][:quantity] += quantity_calculated
end

def quantity_calculated
return customer_quantity.to_i unless customer_quantity.empty?
DEFAULT_QUANTITY
end
end
end
end
15 changes: 10 additions & 5 deletions lib/lita/interactors/inscribe_customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ def initialize(handler, data)

def perform
if service_exists?
if new_customer?
add_customer
else
@error = msg_customer_duplicated(service_name: name, customer: customer_name)
end
inscribe_customer_if_new
else
@error = msg_not_found(service_name: name)
end
Expand Down Expand Up @@ -53,6 +49,15 @@ def new_customer?
!service[:customers].keys.include?(customer_name.to_sym)
end

def inscribe_customer_if_new
if new_customer?
add_customer
else
@error = msg_customer_duplicated(service_name: name,
customer_name: customer_name)
end
end

def add_customer
service[:customers][customer_name.to_sym] = customer
repository.update(service)
Expand Down
6 changes: 5 additions & 1 deletion locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ en:
delete:
success: "Service %{service_name} was deleted."
inscribe:
success: "%{customer_name} was added to %{service_name}"
success: "%{customer_name} was inscribed to %{service_name}."
tip: "Add customers with:\nlita service %{service_name} inscribe < CUSTOMER > < *VALUE >"
add:
success: "%{quantity} was added to %{customer_name}, new quantity: %{customer_quantity}"
errors:
duplicated: "A service called %{service_name} exist already"
not_found: "There isn't a service called %{service_name} or it was deleted."
customer:
duplicated: "%{customer_name} is already in %{service_name}."
not_found: "There is not %{customer_name} in %{service_name}"


70 changes: 67 additions & 3 deletions spec/lita/handlers/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
it { is_expected.to route('TheService inscribe erlinis').to(:inscribe) }
it { is_expected.to route('TheService inscribe @erlinis').to(:inscribe) }
it { is_expected.to route('TheService inscribe @erlinis 20').to(:inscribe) }
it { is_expected.to route('TheService add @erlinis 2').to(:add) }
it { is_expected.to route('TheService add @erlinis -2').to(:add) }
it { is_expected.to route('TheService add @erlinis').to(:add) }
end

describe 'callbacks' do
Expand Down Expand Up @@ -79,17 +82,19 @@
"-------------------------------------------------------------\n" \
" # | Name | Quantity | Value | Total \n" \
"-----+----------------------+----------+----------+----------\n" \
" 1 | erlinis | 0 | 2000 | 0 \n" \
" 2 | khal | 0 | 2000 | 0 \n" \
" 1 | erlinis | 1 | 2000 | 2000 \n" \
" 2 | khal | 2 | 2000 | 4000 \n" \
"-----+----------------------+----------+----------+----------\n" \
" | Total | 0 | *** | 0 \n" \
" | Total | 3 | *** | 6000 \n" \
"-------------------------------------------------------------\n"
end

before do
send_message('create TheService 2000')
send_message('TheService inscribe erlinis 2000')
send_message('TheService inscribe khal 2000')
send_message('TheService add erlinis 1')
send_message('TheService add khal 2')
end

it 'shows the service' do
Expand Down Expand Up @@ -139,6 +144,65 @@

describe '#inscribe' do
describe 'when service exists' do
before do
send_message('create TheService')
send_message('TheService inscribe @erlinis 2000')
end

describe 'customer not inscribed' do
let(:success_message) { 'erlinis was inscribed to TheService.' }

it 'inscribes the customer' do
expect(replies.last).to eq(success_message)
end
end

describe 'customer inscribed already' do
let(:error_message) { 'ERROR: erlinis is already in TheService.' }

it 'returns an error' do
send_message('TheService inscribe @erlinis 2000')
expect(replies.last).to eq(error_message)
end
end
end

describe 'when service does not exit' do
let(:error_message) do
"ERROR: There isn't a service called TheService " \
'or it was deleted.'
end

it 'replys with an error' do
send_message('TheService inscribe @erlinis')
expect(replies.last).to eq(error_message)
end
end
end

describe '#add' do
describe 'when service exists' do
before do
send_message('create TheService')
send_message('TheService inscribe @erlinis 2000')
end

describe 'with positive quantity' do
let(:success_message) { '2 was added to erlinis, new quantity: 2' }
it 'inscrease the customer quantity' do
send_message('TheService add @erlinis 2')
expect(replies.last).to eq(success_message)
end
end

describe 'with negative quantity' do
let(:success_message) { '-1 was added to erlinis, new quantity: 4' }
it 'decrease the customer quantity' do
send_message('TheService add @erlinis 5')
send_message('TheService add @erlinis -1')
expect(replies.last).to eq(success_message)
end
end
end

describe ' when service does not exit' do
Expand Down
8 changes: 7 additions & 1 deletion spec/lita/helpers/messages_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@
expect(subject.msg_not_found(service_name: service)).to eq msg
end

it '#customer_not_found' do
msg = I18n.t('lita.handlers.service.customer.not_found', service_name: service,
customer_name: 'erlinis')
expect(subject.msg_customer_not_found(service_name: service,
customer: 'erlinis')).to eq msg
end
it '#customer_duplicated' do
msg = I18n.t('lita.handlers.service.customer.duplicated', service_name: service,
customer_name: 'erlinis')
expect(subject.msg_customer_duplicated(service_name: service,
customer: 'erlinis')).to eq msg
customer_name: 'erlinis')).to eq msg
end
end

0 comments on commit 9de1869

Please sign in to comment.