2022-03-09 21:13:48 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ReminderRepositoryCellJoinable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2022-03-11 19:38:45 +08:00
|
|
|
def self.reminder_repository_cells_scope(relation, user)
|
2022-03-09 21:13:48 +08:00
|
|
|
relation.joins( # datetime reminders
|
|
|
|
'LEFT OUTER JOIN "repository_date_time_values" ON '\
|
|
|
|
'"repository_date_time_values"."id" = "repository_cells"."value_id" AND '\
|
|
|
|
'"repository_cells"."value_type" = \'RepositoryDateTimeValueBase\' '\
|
2022-03-31 20:59:29 +08:00
|
|
|
'AND repository_columns.metadata ->> \'reminder_delta\' <> \'\' AND '\
|
2022-03-09 21:13:48 +08:00
|
|
|
'(repository_date_time_values.data - NOW()) <= '\
|
2022-03-22 20:33:58 +08:00
|
|
|
'(repository_columns.metadata ->> \'reminder_delta\')::int * interval \'1 sec\''
|
2022-03-09 21:13:48 +08:00
|
|
|
).joins( # stock reminders
|
2022-06-08 19:23:39 +08:00
|
|
|
'LEFT OUTER JOIN "repository_stock_values" ON '\
|
2022-03-09 21:13:48 +08:00
|
|
|
'"repository_cells"."value_type" = \'RepositoryStockValue\' AND '\
|
2022-06-08 19:23:39 +08:00
|
|
|
'"repository_stock_values"."id" = "repository_cells"."value_id" AND '\
|
2022-04-25 18:29:10 +08:00
|
|
|
'(repository_stock_values.amount <= repository_stock_values.low_stock_threshold OR '\
|
|
|
|
' repository_stock_values.amount <= 0)'
|
2022-03-11 19:38:45 +08:00
|
|
|
).joins(
|
|
|
|
'LEFT OUTER JOIN "hidden_repository_cell_reminders" ON '\
|
2022-06-17 16:01:36 +08:00
|
|
|
'"repository_cells"."id" = "hidden_repository_cell_reminders"."repository_cell_id" AND '\
|
|
|
|
'"hidden_repository_cell_reminders"."user_id" = ' + user.id.to_s
|
2022-03-09 21:13:48 +08:00
|
|
|
).where(
|
2022-03-11 19:38:45 +08:00
|
|
|
'hidden_repository_cell_reminders.id IS NULL AND '\
|
2022-06-17 16:01:36 +08:00
|
|
|
'(repository_date_time_values.id IS NOT NULL OR repository_stock_values.id IS NOT NULL)'
|
2022-03-09 21:13:48 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|