From a3fc7c3a8790c188dfd8af4be4b8708ecccc6669 Mon Sep 17 00:00:00 2001 From: Saurabh Bhatia Date: Tue, 17 Sep 2024 22:09:58 +1000 Subject: [PATCH 1/2] Add delete user mutation --- Gemfile.lock | 42 +++++++++++++++--------------- app/graphql/types/mutation_type.rb | 13 +++++++++ app/models/creator_profile.rb | 2 +- app/models/user.rb | 13 +++++---- 4 files changed, 41 insertions(+), 29 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 9e273fcd..ecea014d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -9,7 +9,7 @@ GIT GIT remote: https://github.com/faker-ruby/faker.git - revision: caa55073c3217c65c4929c49d48cf5b0617d5fb6 + revision: 6ba06393f47d4018b5fdbdaaa04eb9891ae5fb55 branch: main specs: faker (3.4.2) @@ -96,17 +96,17 @@ GEM public_suffix (>= 2.0.2, < 7.0) ast (2.4.2) aws-eventstream (1.3.0) - aws-partitions (1.971.0) - aws-sdk-core (3.203.0) + aws-partitions (1.976.0) + aws-sdk-core (3.205.0) aws-eventstream (~> 1, >= 1.3.0) aws-partitions (~> 1, >= 1.651.0) aws-sigv4 (~> 1.9) jmespath (~> 1, >= 1.6.1) - aws-sdk-kms (1.89.0) - aws-sdk-core (~> 3, >= 3.203.0) + aws-sdk-kms (1.91.0) + aws-sdk-core (~> 3, >= 3.205.0) aws-sigv4 (~> 1.5) - aws-sdk-s3 (1.160.0) - aws-sdk-core (~> 3, >= 3.203.0) + aws-sdk-s3 (1.162.0) + aws-sdk-core (~> 3, >= 3.205.0) aws-sdk-kms (~> 1) aws-sigv4 (~> 1.5) aws-sigv4 (1.9.1) @@ -178,7 +178,7 @@ GEM activesupport (>= 6.1) graphiql-rails (1.10.1) railties - graphql (2.3.14) + graphql (2.3.16) base64 fiber-storage graphql_devise (1.5.0) @@ -192,7 +192,7 @@ GEM csv mini_mime (>= 1.0.0) multi_xml (>= 0.5.2) - i18n (1.14.5) + i18n (1.14.6) concurrent-ruby (~> 1.0) image_processing (1.13.0) mini_magick (>= 4.9.5, < 5) @@ -203,7 +203,7 @@ GEM irb (1.14.0) rdoc (>= 4.0.0) reline (>= 0.4.2) - jbuilder (2.12.0) + jbuilder (2.13.0) actionview (>= 5.0.0) activesupport (>= 5.0.0) jmespath (1.6.2) @@ -261,7 +261,7 @@ GEM rack (>= 2.2.3) rack-protection orm_adapter (0.5.0) - pagy (9.0.8) + pagy (9.0.9) parallel (1.26.3) parser (3.3.5.0) ast (~> 2.4.1) @@ -270,7 +270,7 @@ GEM pg_search (2.3.7) activerecord (>= 6.1) activesupport (>= 6.1) - propshaft (0.9.1) + propshaft (1.0.0) actionpack (>= 7.0.0) activesupport (>= 7.0.0) rack @@ -346,7 +346,7 @@ GEM rolify (6.0.1) rspec-core (3.13.1) rspec-support (~> 3.13.0) - rspec-expectations (3.13.2) + rspec-expectations (3.13.3) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.13.0) rspec-mocks (3.13.1) @@ -373,17 +373,17 @@ GEM unicode-display_width (>= 2.4.0, < 3.0) rubocop-ast (1.32.3) parser (>= 3.3.1.0) - rubocop-md (1.2.2) - rubocop (>= 1.0) + rubocop-md (1.2.3) + rubocop (>= 1.45) rubocop-minitest (0.36.0) rubocop (>= 1.61, < 2.0) rubocop-ast (>= 1.31.1, < 2.0) rubocop-packaging (0.5.2) rubocop (>= 1.33, < 2.0) - rubocop-performance (1.21.1) + rubocop-performance (1.22.1) rubocop (>= 1.48.1, < 2.0) rubocop-ast (>= 1.31.1, < 2.0) - rubocop-rails (2.26.0) + rubocop-rails (2.26.1) activesupport (>= 4.2.0) rack (>= 1.1) rubocop (>= 1.52.0, < 2.0) @@ -396,7 +396,7 @@ GEM rubocop-packaging (~> 0.5) rubocop-performance (~> 1.11) rubocop-rails (~> 2.0) - rubocop-rspec (3.0.4) + rubocop-rspec (3.0.5) rubocop (~> 1.61) ruby-progressbar (1.13.0) ruby-vips (2.2.2) @@ -419,16 +419,16 @@ GEM stimulus-rails (1.3.4) railties (>= 6.0.0) stringio (3.1.1) - stripe (12.5.0) + stripe (12.6.0) thor (1.3.2) timeout (0.4.1) - turbo-rails (2.0.6) + turbo-rails (2.0.7) actionpack (>= 6.0.0) activejob (>= 6.0.0) railties (>= 6.0.0) tzinfo (2.0.6) concurrent-ruby (~> 1.0) - unicode-display_width (2.5.0) + unicode-display_width (2.6.0) view_component (3.14.0) activesupport (>= 5.2.0, < 8.0) concurrent-ruby (~> 1.0) diff --git a/app/graphql/types/mutation_type.rb b/app/graphql/types/mutation_type.rb index 54e678dd..d9b28d8e 100644 --- a/app/graphql/types/mutation_type.rb +++ b/app/graphql/types/mutation_type.rb @@ -5,8 +5,21 @@ class MutationType < Types::BaseObject field_class GraphqlDevise::Types::BaseField field :test_field, String, null: false, description: "An example field added by the generator" + field :users_destroy, String, null: false, description: "Delete and Logout a user" do + argument :user_id, ID, required: true + end def test_field "Hello World" end + + def users_destroy(params) + user_id = params[:user_id].to_i + user = User.find(user_id) + if user.destroy + { success: true, errors: [] } + else + { success: false, errors: user.errors.full_messages } + end + end end end diff --git a/app/models/creator_profile.rb b/app/models/creator_profile.rb index 56293bd1..f4086076 100644 --- a/app/models/creator_profile.rb +++ b/app/models/creator_profile.rb @@ -42,6 +42,6 @@ class CreatorProfile < ApplicationRecord foreign_key: :user_id, through: :creator_profiles_managers - has_many :collaborations + has_many :collaborations, dependent: :destroy has_many :products, through: :collaborations end diff --git a/app/models/user.rb b/app/models/user.rb index fdbb61d2..f9e0c2f9 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -45,20 +45,19 @@ class User < ApplicationRecord :confirmable, :omniauthable include GraphqlDevise::Authenticatable rolify - has_many :projects - has_one :store - has_one :creator_profile - has_one_attached :avatar + has_one :store, dependent: :destroy + has_one :creator_profile, dependent: :destroy + has_one_attached :avatar, dependent: :destroy has_many :backings, dependent: :destroy - has_many :projects, through: :backings + has_many :projects, through: :backings, dependent: :destroy has_many :creator_profiles_managers, dependent: :destroy has_many :managed_profiles, class_name: "CreatorProfile", foreign_key: :creator_profile_id, - through: :creator_profiles_managers + through: :creator_profiles_managers, dependent: :destroy has_many :purchases, dependent: :destroy has_many :products, through: :purchases - has_many :orders + has_many :orders, dependent: :destroy # validates :first_name, :last_name, :email, :password, presence: true # validates_confirmation_of :password before_validation :set_uid_provider From 4c0aabc233e2748ada76e473505e4d4075e97342 Mon Sep 17 00:00:00 2001 From: Saurabh Bhatia Date: Thu, 19 Sep 2024 21:57:05 +1000 Subject: [PATCH 2/2] Add delete user mutation --- app/graphql/types/mutation_type.rb | 10 +++------- app/models/user.rb | 1 + .../20240919114023_add_mark_for_deletion_to_users.rb | 7 +++++++ db/schema.rb | 3 ++- spec/factories/users.rb | 1 + test/fixtures/users.yml | 1 + test/models/user_test.rb | 1 + 7 files changed, 16 insertions(+), 8 deletions(-) create mode 100644 db/migrate/20240919114023_add_mark_for_deletion_to_users.rb diff --git a/app/graphql/types/mutation_type.rb b/app/graphql/types/mutation_type.rb index d9b28d8e..1f689dd6 100644 --- a/app/graphql/types/mutation_type.rb +++ b/app/graphql/types/mutation_type.rb @@ -5,21 +5,17 @@ class MutationType < Types::BaseObject field_class GraphqlDevise::Types::BaseField field :test_field, String, null: false, description: "An example field added by the generator" - field :users_destroy, String, null: false, description: "Delete and Logout a user" do + field :mark_user_for_deletion, String, null: false, description: "Delete and Logout a user" do argument :user_id, ID, required: true end def test_field "Hello World" end - def users_destroy(params) + def mark_user_for_deletion(params) user_id = params[:user_id].to_i user = User.find(user_id) - if user.destroy - { success: true, errors: [] } - else - { success: false, errors: user.errors.full_messages } - end + user.update(mark_for_deletion: true) end end end diff --git a/app/models/user.rb b/app/models/user.rb index f9e0c2f9..5c8ebe11 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -17,6 +17,7 @@ # last_name :string # last_sign_in_at :datetime # last_sign_in_ip :string +# mark_for_deletion :boolean # provider :string # publishable_key :string # remember_created_at :datetime diff --git a/db/migrate/20240919114023_add_mark_for_deletion_to_users.rb b/db/migrate/20240919114023_add_mark_for_deletion_to_users.rb new file mode 100644 index 00000000..143a22ae --- /dev/null +++ b/db/migrate/20240919114023_add_mark_for_deletion_to_users.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddMarkForDeletionToUsers < ActiveRecord::Migration[7.1] + def change + add_column :users, :mark_for_deletion, :boolean + end +end diff --git a/db/schema.rb b/db/schema.rb index 78abab91..ef2c36f6 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.1].define(version: 2024_05_28_070710) do +ActiveRecord::Schema[7.1].define(version: 2024_09_19_114023) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -322,6 +322,7 @@ t.json "tokens" t.string "first_name" t.string "last_name" + t.boolean "mark_for_deletion" t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true diff --git a/spec/factories/users.rb b/spec/factories/users.rb index df22c01c..fe9b47f3 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -17,6 +17,7 @@ # last_name :string # last_sign_in_at :datetime # last_sign_in_ip :string +# mark_for_deletion :boolean # provider :string # publishable_key :string # remember_created_at :datetime diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 7e79da56..bc3cf47b 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -15,6 +15,7 @@ # last_name :string # last_sign_in_at :datetime # last_sign_in_ip :string +# mark_for_deletion :boolean # provider :string # publishable_key :string # remember_created_at :datetime diff --git a/test/models/user_test.rb b/test/models/user_test.rb index e03bb9fa..482c0d6b 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -17,6 +17,7 @@ # last_name :string # last_sign_in_at :datetime # last_sign_in_ip :string +# mark_for_deletion :boolean # provider :string # publishable_key :string # remember_created_at :datetime