-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
close #2112 create job when add to cart
- Loading branch information
Showing
6 changed files
with
163 additions
and
1 deletion.
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
41 changes: 41 additions & 0 deletions
41
app/interactors/spree_cm_commissioner/cart/add_item_status_marker.rb
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,41 @@ | ||
module SpreeCmCommissioner | ||
module Cart | ||
class AddItemStatusMarker < BaseInteractor | ||
delegate :order_number, :job_id, :status, :queued_at, :complete_at, to: :context | ||
|
||
def call | ||
if order_number.nil? || job_id.nil? || status.nil? | ||
context.fail!(message: 'Missing required fields') | ||
return | ||
end | ||
|
||
update_cart_firestore_status | ||
end | ||
|
||
def firestore | ||
@firestore ||= Google::Cloud::Firestore.new(project_id: service_account[:project_id], credentials: service_account) | ||
end | ||
|
||
def service_account | ||
@service_account ||= Rails.application.credentials.cloud_firestore_service_account | ||
end | ||
|
||
def update_cart_firestore_status | ||
data_to_set = build_data_to_set | ||
|
||
firestore.col('queues') | ||
.doc('cart') | ||
.col(order_number) | ||
.doc(job_id) | ||
.set(data_to_set, merge: true) | ||
end | ||
|
||
def build_data_to_set | ||
{ | ||
status: status, | ||
queued_at: queued_at.presence | ||
}.compact | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require 'google/cloud/firestore' | ||
|
||
module SpreeCmCommissioner | ||
module Cart | ||
class AddItemJob < ApplicationJob | ||
def perform(order_id, variant_id, quantity, public_metadata, private_metadata, options) # rubocop:disable Metrics/ParameterLists | ||
order = Spree::Order.find(order_id) | ||
variant = Spree::Variant.find(variant_id) | ||
|
||
Spree::Cart::AddItem.call( | ||
order: order, | ||
variant: variant, | ||
quantity: quantity, | ||
public_metadata: public_metadata, | ||
private_metadata: private_metadata, | ||
options: options | ||
) | ||
|
||
SpreeCmCommissioner::Cart::AddItemStatusMarker.call( | ||
order_number: order.number, | ||
job_id: job_id, | ||
status: 'complete' | ||
) | ||
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
34 changes: 34 additions & 0 deletions
34
spec/interactors/spree_cm_commissioner/cart/add_item_status_marker_spec.rb
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,34 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe SpreeCmCommissioner::Cart::AddItemStatusMarker do | ||
let(:order_number) { 'R12345678' } | ||
let(:job_id) { 'job_456' } | ||
let(:status) { 'processing' } | ||
let(:queued_at) { Time.now } | ||
let(:complete_at) { nil } | ||
|
||
subject { described_class.new(order_number: order_number, job_id: job_id, status: status, queued_at: queued_at, complete_at: complete_at) } | ||
|
||
describe '#call' do | ||
|
||
it 'fails when order_number is missing' do | ||
allow(subject).to receive(:order_number).and_return(nil) | ||
expect { subject.call }.to raise_error(Interactor::Failure) | ||
end | ||
|
||
it 'fails when job_id is missing' do | ||
allow(subject).to receive(:job_id).and_return(nil) | ||
expect { subject.call }.to raise_error(Interactor::Failure) | ||
end | ||
|
||
it 'fails when status is missing' do | ||
allow(subject).to receive(:status).and_return(nil) | ||
expect { subject.call }.to raise_error(Interactor::Failure) | ||
end | ||
|
||
it 'calls update_cart_firestore_status' do | ||
expect(subject).to receive(:update_cart_firestore_status) | ||
subject.call | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe SpreeCmCommissioner::Cart::AddItemJob, type: :job do | ||
describe '#perform' do | ||
let(:order) { create(:order) } | ||
let(:variant) { create(:variant) } | ||
let(:quantity) { 2 } | ||
|
||
it 'calls Spree::Cart::AddItem with correct arguments' do | ||
add_item_service = instance_double(Spree::Cart::AddItem) | ||
allow(Spree::Cart::AddItem).to receive(:new).and_return(add_item_service) | ||
allow(add_item_service).to receive(:call).and_return(true) | ||
described_class.new.perform(order.id, variant.id, quantity, {}, {}, {}) | ||
|
||
expect(add_item_service).to have_received(:call).with( | ||
order: order, | ||
variant: variant, | ||
quantity: quantity, | ||
public_metadata: {}, | ||
private_metadata: {}, | ||
options: {} | ||
) | ||
end | ||
end | ||
end |