Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/graphql/types/coupons/create_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CreateInput < Types::BaseInputObject

argument :amount_cents, GraphQL::Types::BigInt, required: false
argument :amount_currency, Types::CurrencyEnum, required: false
argument :code, String, required: false
argument :code, String, required: true
Comment thread
murparreira marked this conversation as resolved.
argument :coupon_type, Types::Coupons::CouponTypeEnum, required: true
argument :description, String, required: false
argument :frequency, Types::Coupons::FrequencyEnum, required: true
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/types/coupons/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Object < Types::BaseObject

field :amount_cents, GraphQL::Types::BigInt, null: true
field :amount_currency, Types::CurrencyEnum, null: true
field :code, String, null: true
field :code, String, null: false
field :coupon_type, Types::Coupons::CouponTypeEnum, null: false
field :description, String, null: true
field :frequency, Types::Coupons::FrequencyEnum, null: false
Expand Down
1 change: 1 addition & 0 deletions app/graphql/types/coupons/update_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Coupons
class UpdateInput < Types::Coupons::CreateInput
graphql_name "UpdateCouponInput"

argument :code, String, required: false
argument :id, String, required: true
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/coupon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Coupon < ApplicationRecord
monetize :amount_cents, disable_validation: true, allow_nil: true

validates :name, presence: true
validates :code, uniqueness: {conditions: -> { where(deleted_at: nil) }, scope: :organization_id}
validates :code, presence: true, uniqueness: {conditions: -> { where(deleted_at: nil) }, scope: :organization_id}

validates :amount_cents, presence: true, if: :fixed_amount?
validates :amount_cents, numericality: {greater_than: 0}, allow_nil: true
Expand Down Expand Up @@ -101,7 +101,7 @@ def parent_and_overriden_plans
# id :uuid not null, primary key
# amount_cents :bigint
# amount_currency :string
# code :string
# code :string not null
# coupon_type :integer default("fixed_amount"), not null
# deleted_at :datetime
# description :text
Expand Down
16 changes: 16 additions & 0 deletions db/migrate/20260512155310_backfill_coupon_codes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

class BackfillCouponCodes < ActiveRecord::Migration[8.0]
disable_ddl_transaction!

def up
Coupon.unscoped.where(code: nil).find_in_batches(batch_size: 1000) do |batch|
Coupon.unscoped.where(id: batch.pluck(:id))
.update_all("code = 'coupon-' || id::text") # rubocop:disable Rails/SkipsModelValidations
end
Comment thread
murparreira marked this conversation as resolved.
end

def down
# irreversible
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class AddCodeNotNullCheckConstraintToCoupons < ActiveRecord::Migration[8.0]
def change
add_check_constraint :coupons, "code IS NOT NULL",
name: "coupons_code_not_null", validate: false, if_not_exists: true
end
end
16 changes: 16 additions & 0 deletions db/migrate/20260513105210_validate_coupons_code_not_null.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

class ValidateCouponsCodeNotNull < ActiveRecord::Migration[8.0]
disable_ddl_transaction!

def up
validate_check_constraint :coupons, name: "coupons_code_not_null"
change_column_null :coupons, :code, false
remove_check_constraint :coupons, name: "coupons_code_not_null"
end

def down
add_check_constraint :coupons, "code IS NOT NULL", name: "coupons_code_not_null", validate: false
change_column_null :coupons, :code, true
end
end
6 changes: 4 additions & 2 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2109,7 +2109,7 @@ CREATE TABLE public.coupons (
id uuid DEFAULT gen_random_uuid() NOT NULL,
organization_id uuid NOT NULL,
name character varying NOT NULL,
code character varying,
code character varying NOT NULL,
status integer DEFAULT 0 NOT NULL,
terminated_at timestamp(6) without time zone,
amount_cents bigint,
Expand Down Expand Up @@ -12216,6 +12216,9 @@ SET search_path TO "$user", public;

INSERT INTO "schema_migrations" (version) VALUES
('20260517101105'),
('20260513105210'),
('20260513105209'),
('20260512155310'),
('20260512142543'),
('20260504134804'),
('20260430102814'),
Expand Down Expand Up @@ -13214,4 +13217,3 @@ INSERT INTO "schema_migrations" (version) VALUES
('20220530091046'),
('20220526101535'),
('20220525122759');

4 changes: 2 additions & 2 deletions schema.graphql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 14 additions & 6 deletions schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion spec/graphql/types/coupons/object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
expect(subject).to have_field(:organization).of_type("Organization")
expect(subject).to have_field(:amount_cents).of_type("BigInt")
expect(subject).to have_field(:amount_currency).of_type("CurrencyEnum")
expect(subject).to have_field(:code).of_type("String")
expect(subject).to have_field(:code).of_type("String!")
expect(subject).to have_field(:coupon_type).of_type("CouponTypeEnum!")
expect(subject).to have_field(:description).of_type("String")
expect(subject).to have_field(:frequency).of_type("CouponFrequency!")
Expand Down
1 change: 1 addition & 0 deletions spec/models/coupon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

describe "validations" do
it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_presence_of(:code) }
it { is_expected.to validate_exclusion_of(:reusable).in_array([nil]) }

describe "of amount cents" do
Expand Down
Loading