2024-12-03 16:59:34 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
# -- copyright
|
|
|
|
|
# OpenProject is an open source project management software.
|
|
|
|
|
# Copyright (C) the OpenProject GmbH
|
|
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
|
# modify it under the terms of the GNU General Public License version 3.
|
|
|
|
|
#
|
|
|
|
|
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
|
|
|
|
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
|
|
|
|
# Copyright (C) 2010-2013 the ChiliProject Team
|
|
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
#
|
|
|
|
|
# See COPYRIGHT and LICENSE files for more details.
|
|
|
|
|
# ++
|
|
|
|
|
|
|
|
|
|
class WorkPackages::RemindersController < ApplicationController
|
|
|
|
|
include OpTurbo::ComponentStream
|
2025-06-19 08:51:14 +03:00
|
|
|
include Redmine::I18n
|
|
|
|
|
|
2024-12-03 16:59:34 +01:00
|
|
|
layout false
|
2025-06-19 08:51:14 +03:00
|
|
|
|
2024-12-03 16:59:34 +01:00
|
|
|
before_action :find_work_package
|
2025-06-11 18:01:39 +03:00
|
|
|
before_action :find_or_build_reminder, only: %i[modal_body create]
|
2024-12-03 16:59:34 +01:00
|
|
|
before_action :find_reminder, only: %i[update destroy]
|
|
|
|
|
|
|
|
|
|
before_action :authorize
|
|
|
|
|
|
|
|
|
|
def modal_body
|
2025-06-11 18:01:39 +03:00
|
|
|
render WorkPackages::Reminder::ModalBodyComponent.new(
|
2024-12-03 16:59:34 +01:00
|
|
|
remindable: @work_package,
|
2025-05-28 20:43:34 +03:00
|
|
|
reminder: @reminder,
|
|
|
|
|
preset: params[:preset]
|
2024-12-03 16:59:34 +01:00
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create
|
|
|
|
|
service_result = Reminders::CreateService.new(user: current_user)
|
|
|
|
|
.call(reminder_params)
|
|
|
|
|
|
|
|
|
|
if service_result.success?
|
2026-02-18 07:47:11 +01:00
|
|
|
message = helpers.t("work_package.reminders.create_success_message_html",
|
|
|
|
|
reminder_time: reminder_chosen_time(service_result.result))
|
2025-06-19 08:51:14 +03:00
|
|
|
respond_with_success_flash_message(message:)
|
2024-12-03 16:59:34 +01:00
|
|
|
else
|
2025-06-11 18:01:39 +03:00
|
|
|
respond_with_error_modal_component(service_result)
|
2024-12-03 16:59:34 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def update
|
|
|
|
|
service_result = Reminders::UpdateService.new(user: current_user,
|
|
|
|
|
model: @reminder)
|
|
|
|
|
.call(reminder_params)
|
|
|
|
|
|
|
|
|
|
if service_result.success?
|
2025-06-11 18:01:39 +03:00
|
|
|
respond_with_success_flash_message(message: I18n.t("work_package.reminders.success_update_message"))
|
2024-12-03 16:59:34 +01:00
|
|
|
else
|
2025-06-11 18:01:39 +03:00
|
|
|
respond_with_error_modal_component(service_result)
|
2024-12-03 16:59:34 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
|
service_result = Reminders::DeleteService.new(user: current_user,
|
|
|
|
|
model: @reminder)
|
|
|
|
|
.call
|
|
|
|
|
|
|
|
|
|
if service_result.success?
|
2025-06-11 18:01:39 +03:00
|
|
|
respond_with_success_flash_message(message: I18n.t("work_package.reminders.success_deletion_message"))
|
2024-12-03 16:59:34 +01:00
|
|
|
else
|
2024-12-18 18:57:40 +01:00
|
|
|
render_error_flash_message_via_turbo_stream(message: service_result.errors.full_messages)
|
2024-12-03 16:59:34 +01:00
|
|
|
respond_with_turbo_streams(status: :unprocessable_entity)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
2025-06-11 18:01:39 +03:00
|
|
|
def respond_with_success_flash_message(message:)
|
|
|
|
|
render_success_flash_message_via_turbo_stream(message:)
|
|
|
|
|
respond_with_turbo_streams
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def respond_with_error_modal_component(service_result)
|
|
|
|
|
replace_via_turbo_stream(
|
|
|
|
|
component: WorkPackages::Reminder::ModalBodyComponent.new(
|
|
|
|
|
remindable: @work_package,
|
|
|
|
|
reminder: service_result.result,
|
|
|
|
|
errors: service_result.errors,
|
|
|
|
|
remind_at_date: reminder_params[:remind_at_date],
|
|
|
|
|
remind_at_time: reminder_params[:remind_at_time]
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
respond_with_turbo_streams(status: :unprocessable_entity)
|
2024-12-03 16:59:34 +01:00
|
|
|
end
|
|
|
|
|
|
2025-06-19 08:51:14 +03:00
|
|
|
def reminder_chosen_time(reminder)
|
|
|
|
|
OpPrimer::RelativeTimeComponent.new(
|
2025-06-26 09:58:04 +03:00
|
|
|
datetime: in_user_zone(reminder.remind_at),
|
|
|
|
|
month: :long
|
2025-06-19 08:51:14 +03:00
|
|
|
).render_in(view_context)
|
|
|
|
|
end
|
|
|
|
|
|
2024-12-03 16:59:34 +01:00
|
|
|
def find_work_package
|
|
|
|
|
@work_package = WorkPackage.visible.find(params[:work_package_id])
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# We assume for now that there is only one reminder per work package
|
2025-06-11 18:01:39 +03:00
|
|
|
def find_or_build_reminder
|
|
|
|
|
@reminder = reminders.last || @work_package.reminders.build
|
2024-12-03 16:59:34 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def find_reminder
|
2025-06-11 18:01:39 +03:00
|
|
|
@reminder = reminders.find(params[:id])
|
2024-12-11 12:21:09 +01:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
2024-12-18 18:57:40 +01:00
|
|
|
render_error_flash_message_via_turbo_stream(message: I18n.t(:error_reminder_not_found))
|
2024-12-11 12:21:09 +01:00
|
|
|
respond_with_turbo_streams(status: :not_found)
|
|
|
|
|
false
|
2024-12-03 16:59:34 +01:00
|
|
|
end
|
|
|
|
|
|
2025-06-11 18:01:39 +03:00
|
|
|
def reminders
|
|
|
|
|
@work_package.reminders.upcoming_and_visible_to(User.current)
|
|
|
|
|
end
|
|
|
|
|
|
2024-12-03 16:59:34 +01:00
|
|
|
def reminder_params
|
2025-06-11 15:34:52 +03:00
|
|
|
params.expect(reminder: %i[remind_at_date remind_at_time note])
|
|
|
|
|
.merge(remindable: @work_package, creator: User.current)
|
2024-12-03 16:59:34 +01:00
|
|
|
end
|
|
|
|
|
end
|