Implement hidden repository cell reminders [SCI-6504] (#3933)

This commit is contained in:
artoscinote 2022-03-11 12:38:45 +01:00 committed by GitHub
parent 959b86b40c
commit 35dfa15e57
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 145 additions and 7 deletions

View file

@ -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

View file

@ -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 = {

View file

@ -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

View file

@ -0,0 +1,6 @@
# frozen_string_literal: true
class HiddenRepositoryCellReminder < ApplicationRecord
belongs_to :repository_cell
belongs_to :user
end

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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');