From 52941cac927c3544e9c0a8569f663ee3ba775929 Mon Sep 17 00:00:00 2001 From: Zee <50284+zspencer@users.noreply.github.com> Date: Wed, 14 Jun 2023 19:32:28 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=A5=94=E2=9C=A8=20`Marketplace`:=20Sprout?= =?UTF-8?q?=20model=20for=20`OrderNotificationMethod`=20(#1561)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - https://github.com/zinc-collective/convene/issues/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. --- app/furniture/marketplace/marketplace.rb | 2 ++ app/furniture/marketplace/order/received_mailer.rb | 2 +- .../marketplace/order_notification_method.rb | 12 ++++++++++++ ...110_add_marketplace_order_notification_methods.rb | 10 ++++++++++ db/schema.rb | 12 +++++++++++- spec/furniture/marketplace/marketplace_spec.rb | 1 + .../marketplace/order/received_mailer_spec.rb | 11 ++++++++++- .../marketplace/order_notification_method_spec.rb | 5 +++++ 8 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 app/furniture/marketplace/order_notification_method.rb create mode 100644 db/migrate/20230615002110_add_marketplace_order_notification_methods.rb create mode 100644 spec/furniture/marketplace/order_notification_method_spec.rb diff --git a/app/furniture/marketplace/marketplace.rb b/app/furniture/marketplace/marketplace.rb index 605abec60..6f9466041 100644 --- a/app/furniture/marketplace/marketplace.rb +++ b/app/furniture/marketplace/marketplace.rb @@ -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 diff --git a/app/furniture/marketplace/order/received_mailer.rb b/app/furniture/marketplace/order/received_mailer.rb index a6755dafb..dc70908fc 100644 --- a/app/furniture/marketplace/order/received_mailer.rb +++ b/app/furniture/marketplace/order/received_mailer.rb @@ -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 diff --git a/app/furniture/marketplace/order_notification_method.rb b/app/furniture/marketplace/order_notification_method.rb new file mode 100644 index 000000000..dd5fe3fa3 --- /dev/null +++ b/app/furniture/marketplace/order_notification_method.rb @@ -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 diff --git a/db/migrate/20230615002110_add_marketplace_order_notification_methods.rb b/db/migrate/20230615002110_add_marketplace_order_notification_methods.rb new file mode 100644 index 000000000..0664ca9c1 --- /dev/null +++ b/db/migrate/20230615002110_add_marketplace_order_notification_methods.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 951aeb394..5d7d2f8be 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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 @@ -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" diff --git a/spec/furniture/marketplace/marketplace_spec.rb b/spec/furniture/marketplace/marketplace_spec.rb index 4a3dc3358..86ca16744 100644 --- a/spec/furniture/marketplace/marketplace_spec.rb +++ b/spec/furniture/marketplace/marketplace_spec.rb @@ -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) } diff --git a/spec/furniture/marketplace/order/received_mailer_spec.rb b/spec/furniture/marketplace/order/received_mailer_spec.rb index 87fa1a9b6..f72ffe514 100644 --- a/spec/furniture/marketplace/order/received_mailer_spec.rb +++ b/spec/furniture/marketplace/order/received_mailer_spec.rb @@ -1,13 +1,22 @@ require "rails_helper" RSpec.describe Marketplace::Order::ReceivedMailer, type: :mailer do - let(:marketplace) { build(:marketplace, notify_emails: "vendor@example.com,distributor@example.com") } + let(:marketplace) { create(:marketplace, notify_emails: "vendor@example.com,distributor@example.com") } 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: "another_place@example.com") + 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 diff --git a/spec/furniture/marketplace/order_notification_method_spec.rb b/spec/furniture/marketplace/order_notification_method_spec.rb new file mode 100644 index 000000000..bc7a720cd --- /dev/null +++ b/spec/furniture/marketplace/order_notification_method_spec.rb @@ -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