Skip to content

Commit

Permalink
🥔✨ Marketplace: Sprout model for OrderNotificationMethod (#1561)
Browse files Browse the repository at this point in the history
- #1511

OK this is awkwardly named, but it is the first tiny step towards
allowing us to manage multiple order notification emails without the
silly comma separated value.
  • Loading branch information
zspencer authored Jun 15, 2023
1 parent 4c536ea commit 52941ca
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 3 deletions.
2 changes: 2 additions & 0 deletions app/furniture/marketplace/marketplace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Marketplace < Furniture

has_many :delivery_areas, inverse_of: :marketplace, dependent: :destroy

has_many :order_notification_methods, inverse_of: :marketplace, dependent: :destroy

setting :notify_emails
setting :stripe_account
alias_method :vendor_stripe_account, :stripe_account
Expand Down
2 changes: 1 addition & 1 deletion app/furniture/marketplace/order/received_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Marketplace
class Order
class ReceivedMailer < Mailer
def to
order.marketplace.notify_emails.split(",")
order.marketplace.notify_emails.split(",") + order.marketplace.order_notification_methods.map(&:contact_location)
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions app/furniture/marketplace/order_notification_method.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class Marketplace
class OrderNotificationMethod < Record
self.table_name = :marketplace_order_notification_methods
location(parent: :marketplace)
belongs_to :marketplace, inverse_of: :order_notification_methods

has_encrypted :contact_location
validates :contact_location, presence: true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class AddMarketplaceOrderNotificationMethods < ActiveRecord::Migration[7.0]
def change
create_table :marketplace_order_notification_methods, id: :uuid do |t|
t.references :marketplace, type: :uuid, foreign_key: {to_table: :furnitures}
t.string :contact_method, default: :email, null: false
t.string :contact_location_ciphertext, null: false
t.timestamps
end
end
end
12 changes: 11 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2023_05_18_014356) do
ActiveRecord::Schema[7.0].define(version: 2023_06_15_002110) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
Expand Down Expand Up @@ -139,6 +139,15 @@
t.index ["marketplace_id"], name: "index_marketplace_delivery_areas_on_marketplace_id"
end

create_table "marketplace_order_notification_methods", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.uuid "marketplace_id"
t.string "contact_method", default: "email", null: false
t.string "contact_location_ciphertext", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["marketplace_id"], name: "index_marketplace_order_notification_methods_on_marketplace_id"
end

create_table "marketplace_orders", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.uuid "marketplace_id"
t.datetime "created_at", null: false
Expand Down Expand Up @@ -270,6 +279,7 @@
add_foreign_key "marketplace_cart_products", "marketplace_orders", column: "cart_id"
add_foreign_key "marketplace_cart_products", "marketplace_products", column: "product_id"
add_foreign_key "marketplace_delivery_areas", "furnitures", column: "marketplace_id"
add_foreign_key "marketplace_order_notification_methods", "furnitures", column: "marketplace_id"
add_foreign_key "marketplace_orders", "marketplace_delivery_areas", column: "delivery_area_id"
add_foreign_key "marketplace_orders", "marketplace_shoppers", column: "shopper_id"
add_foreign_key "marketplace_product_tax_rates", "marketplace_products", column: "product_id"
Expand Down
1 change: 1 addition & 0 deletions spec/furniture/marketplace/marketplace_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
RSpec.describe Marketplace::Marketplace, type: :model do
it { is_expected.to have_many(:products).inverse_of(:marketplace).dependent(:destroy) }
it { is_expected.to have_many(:orders).inverse_of(:marketplace) }
it { is_expected.to have_many(:order_notification_methods).inverse_of(:marketplace).dependent(:destroy) }
it { is_expected.to have_many(:carts).inverse_of(:marketplace).dependent(:destroy) }
it { is_expected.to have_many(:delivery_areas).inverse_of(:marketplace).dependent(:destroy) }
it { is_expected.to have_many(:tax_rates).inverse_of(:marketplace) }
Expand Down
11 changes: 10 additions & 1 deletion spec/furniture/marketplace/order/received_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
require "rails_helper"

RSpec.describe Marketplace::Order::ReceivedMailer, type: :mailer do
let(:marketplace) { build(:marketplace, notify_emails: "[email protected],[email protected]") }
let(:marketplace) { create(:marketplace, notify_emails: "[email protected],[email protected]") }
let(:order) { build(:marketplace_order, placed_at: 1.hour.ago, marketplace: marketplace) }

describe "#notification" do
subject(:notification) { described_class.notification(order) }

it { is_expected.to be_to(marketplace.notify_emails.split(",")) }

context "when the marketplace has a order notification contact location" do
before do
marketplace.order_notification_methods.create(contact_location: "[email protected]")
end

it { is_expected.to be_to(marketplace.notify_emails.split(",") + marketplace.order_notification_methods.map(&:contact_location)) }
end

it { is_expected.to have_subject(t(".notification.subject", marketplace_name: order.marketplace_name, order_id: order.id)) }

specify do
Expand Down
5 changes: 5 additions & 0 deletions spec/furniture/marketplace/order_notification_method_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "rails_helper"

RSpec.describe Marketplace::OrderNotificationMethod, type: :model do
it { is_expected.to belong_to(:marketplace).inverse_of(:order_notification_methods) }
end

0 comments on commit 52941ca

Please sign in to comment.