diff --git a/app/controllers/hidden_repository_cell_reminders_controller.rb b/app/controllers/hidden_repository_cell_reminders_controller.rb new file mode 100644 index 000000000..5ed53b5f1 --- /dev/null +++ b/app/controllers/hidden_repository_cell_reminders_controller.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +class HiddenRepositoryCellRemindersController < ApplicationController + before_action :load_repository + before_action :load_repository_row + before_action :check_read_permissions + + def create + hidden_repository_cell_reminder = + current_user.hidden_repository_cell_reminders.create!(repository_cell_id: params[:repository_cell_id]) + + render json: hidden_repository_cell_reminder, status: :ok + end + + private + + def load_repository + @repository = Repository.accessible_by_teams(current_team).find_by(id: params[:repository_id]) + render_404 unless @repository + end + + def load_repository_row + @repository_row = @repository.repository_rows.find_by(id: params[:repository_row_id]) + render_404 unless @repository_row + end + + def check_read_permissions + render_403 unless can_read_repository?(@repository) + end +end diff --git a/app/helpers/repository_datatable_helper.rb b/app/helpers/repository_datatable_helper.rb index 897abc67f..e821be3ea 100644 --- a/app/helpers/repository_datatable_helper.rb +++ b/app/helpers/repository_datatable_helper.rb @@ -4,7 +4,7 @@ module RepositoryDatatableHelper include InputSanitizeHelper def prepare_row_columns(repository_rows, repository, columns_mappings, team, options = {}) - repository_row_with_active_reminder_ids = repository_rows.with_active_reminders.pluck(:id) + repository_row_with_active_reminder_ids = repository_rows.with_active_reminders(current_user).pluck(:id).uniq repository_rows.map do |record| default_cells = { diff --git a/app/models/concerns/reminder_repository_cell_joinable.rb b/app/models/concerns/reminder_repository_cell_joinable.rb index 53bb7fb05..29f76e85e 100644 --- a/app/models/concerns/reminder_repository_cell_joinable.rb +++ b/app/models/concerns/reminder_repository_cell_joinable.rb @@ -4,7 +4,7 @@ module ReminderRepositoryCellJoinable extend ActiveSupport::Concern included do - def self.reminder_repository_cells_scope(relation) + def self.reminder_repository_cells_scope(relation, user) relation.joins( # datetime reminders 'LEFT OUTER JOIN "repository_date_time_values" ON '\ '"repository_date_time_values"."id" = "repository_cells"."value_id" AND '\ @@ -16,7 +16,12 @@ module ReminderRepositoryCellJoinable 'LEFT OUTER JOIN "repository_stock_values" ON "repository_stock_values"."id" = "repository_cells"."value_id" AND '\ '"repository_cells"."value_type" = \'RepositoryStockValue\' AND '\ 'repository_stock_values.amount <= repository_stock_values.low_stock_threshold' + ).joins( + 'LEFT OUTER JOIN "hidden_repository_cell_reminders" ON '\ + '"repository_cells"."id" = "hidden_repository_cell_reminders"."repository_cell_id" AND '\ + '"hidden_repository_cell_reminders"."user_id" = ' + user.id.to_s ).where( + 'hidden_repository_cell_reminders.id IS NULL AND '\ 'repository_date_time_values.id IS NOT NULL OR repository_stock_values.id IS NOT NULL' ) end diff --git a/app/models/hidden_repository_cell_reminder.rb b/app/models/hidden_repository_cell_reminder.rb new file mode 100644 index 000000000..27a87462f --- /dev/null +++ b/app/models/hidden_repository_cell_reminder.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +class HiddenRepositoryCellReminder < ApplicationRecord + belongs_to :repository_cell + belongs_to :user +end diff --git a/app/models/repository_cell.rb b/app/models/repository_cell.rb index 06d81203c..9f2c27e0b 100644 --- a/app/models/repository_cell.rb +++ b/app/models/repository_cell.rb @@ -32,6 +32,8 @@ class RepositoryCell < ApplicationRecord optional: true, foreign_key: :value_id, inverse_of: :repository_cell end + has_many :hidden_repository_cell_reminders, dependent: :destroy + validates :repository_column, inclusion: { in: (lambda do |repository_cell| repository_cell.repository_row&.repository&.repository_columns || [] @@ -43,8 +45,8 @@ class RepositoryCell < ApplicationRecord uniqueness: { scope: :repository_column }, unless: :importing - scope :with_active_reminder, lambda { - reminder_repository_cells_scope(joins(:repository_column)) + scope :with_active_reminder, lambda { |user| + reminder_repository_cells_scope(joins(:repository_column), user) } def self.create_with_value!(row, column, data, user) diff --git a/app/models/repository_row.rb b/app/models/repository_row.rb index 57ce75130..b1f633271 100644 --- a/app/models/repository_row.rb +++ b/app/models/repository_row.rb @@ -86,8 +86,8 @@ class RepositoryRow < ApplicationRecord scope :active, -> { where(archived: false) } scope :archived, -> { where(archived: true) } - scope :with_active_reminders, lambda { - reminder_repository_cells_scope(joins(repository_cells: :repository_column)).distinct + scope :with_active_reminders, lambda { |user| + reminder_repository_cells_scope(joins(repository_cells: :repository_column), user) } def code diff --git a/app/models/user.rb b/app/models/user.rb index 679ed0e6e..95dba4118 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -313,6 +313,8 @@ class User < ApplicationRecord foreign_key: :resource_owner_id, dependent: :delete_all + has_many :hidden_repository_cell_reminders, dependent: :destroy + before_validation :downcase_email! before_destroy :destroy_notifications diff --git a/config/routes.rb b/config/routes.rb index df54823f5..b981167b7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -604,6 +604,9 @@ Rails.application.routes.draw do post 'repository_stock_value', to: 'repository_stock_values#create_or_update', as: 'update_repository_stock' end resources :repository_stock_values, only: %i(new create edit update) + resources :repository_cells, only: :hide_reminder do + post :hide_reminder, to: 'hidden_repository_cell_reminders#create' + end end collection do diff --git a/db/migrate/20220310105144_create_hidden_repository_cell_reminders.rb b/db/migrate/20220310105144_create_hidden_repository_cell_reminders.rb new file mode 100644 index 000000000..fa085a816 --- /dev/null +++ b/db/migrate/20220310105144_create_hidden_repository_cell_reminders.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class CreateHiddenRepositoryCellReminders < ActiveRecord::Migration[6.1] + def change + create_table :hidden_repository_cell_reminders do |t| + t.references :repository_cell, null: false, foreign_key: true + t.references :user, null: false, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/structure.sql b/db/structure.sql index 731792d8b..85ac264a8 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -585,6 +585,38 @@ CREATE SEQUENCE public.experiments_id_seq ALTER SEQUENCE public.experiments_id_seq OWNED BY public.experiments.id; +-- +-- Name: hidden_repository_cell_reminders; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.hidden_repository_cell_reminders ( + id bigint NOT NULL, + repository_cell_id bigint NOT NULL, + user_id bigint NOT NULL, + created_at timestamp(6) without time zone NOT NULL, + updated_at timestamp(6) without time zone NOT NULL +); + + +-- +-- Name: hidden_repository_cell_reminders_id_seq; Type: SEQUENCE; Schema: public; Owner: - +-- + +CREATE SEQUENCE public.hidden_repository_cell_reminders_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: hidden_repository_cell_reminders_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - +-- + +ALTER SEQUENCE public.hidden_repository_cell_reminders_id_seq OWNED BY public.hidden_repository_cell_reminders.id; + + -- -- Name: label_printers; Type: TABLE; Schema: public; Owner: - -- @@ -3304,6 +3336,13 @@ ALTER TABLE ONLY public.delayed_jobs ALTER COLUMN id SET DEFAULT nextval('public ALTER TABLE ONLY public.experiments ALTER COLUMN id SET DEFAULT nextval('public.experiments_id_seq'::regclass); +-- +-- Name: hidden_repository_cell_reminders id; Type: DEFAULT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.hidden_repository_cell_reminders ALTER COLUMN id SET DEFAULT nextval('public.hidden_repository_cell_reminders_id_seq'::regclass); + + -- -- Name: label_printers id; Type: DEFAULT; Schema: public; Owner: - -- @@ -3934,6 +3973,14 @@ ALTER TABLE ONLY public.experiments ADD CONSTRAINT experiments_pkey PRIMARY KEY (id); +-- +-- Name: hidden_repository_cell_reminders hidden_repository_cell_reminders_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.hidden_repository_cell_reminders + ADD CONSTRAINT hidden_repository_cell_reminders_pkey PRIMARY KEY (id); + + -- -- Name: label_printers label_printers_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- @@ -4849,6 +4896,20 @@ CREATE INDEX index_experiments_on_project_id ON public.experiments USING btree ( CREATE INDEX index_experiments_on_restored_by_id ON public.experiments USING btree (restored_by_id); +-- +-- Name: index_hidden_repository_cell_reminders_on_repository_cell_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_hidden_repository_cell_reminders_on_repository_cell_id ON public.hidden_repository_cell_reminders USING btree (repository_cell_id); + + +-- +-- Name: index_hidden_repository_cell_reminders_on_user_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_hidden_repository_cell_reminders_on_user_id ON public.hidden_repository_cell_reminders USING btree (user_id); + + -- -- Name: index_label_templates_on_language_type; Type: INDEX; Schema: public; Owner: - -- @@ -6471,6 +6532,14 @@ ALTER TABLE ONLY public.repository_ledger_records ADD CONSTRAINT fk_rails_062bed0c26 FOREIGN KEY (repository_stock_value_id) REFERENCES public.repository_stock_values(id); +-- +-- Name: hidden_repository_cell_reminders fk_rails_08be8c52e0; Type: FK CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.hidden_repository_cell_reminders + ADD CONSTRAINT fk_rails_08be8c52e0 FOREIGN KEY (user_id) REFERENCES public.users(id); + + -- -- Name: repository_stock_values fk_rails_08ce900341; Type: FK CONSTRAINT; Schema: public; Owner: - -- @@ -7183,6 +7252,14 @@ ALTER TABLE ONLY public.repository_checklist_values ADD CONSTRAINT fk_rails_98a7704432 FOREIGN KEY (last_modified_by_id) REFERENCES public.users(id); +-- +-- Name: hidden_repository_cell_reminders fk_rails_98e782ebf2; Type: FK CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.hidden_repository_cell_reminders + ADD CONSTRAINT fk_rails_98e782ebf2 FOREIGN KEY (repository_cell_id) REFERENCES public.repository_cells(id); + + -- -- Name: activities fk_rails_992865be13; Type: FK CONSTRAINT; Schema: public; Owner: - -- @@ -8004,6 +8081,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20211103115450'), ('20220110151005'), ('20220110151006'), -('20220224153705'); +('20220224153705'), +('20220310105144');