Skip to content

Commit

Permalink
modified edit form of notifications and webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
kortirso committed Jul 5, 2024
1 parent c1d67fd commit e32cc0e
Show file tree
Hide file tree
Showing 38 changed files with 552 additions and 615 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased
### Modified
- Notification and Webhook models
- edit form of notifications and webhooks

## [1.8.0] - 2024-07-04
### Added
- co-owners for accounts
Expand Down
7 changes: 0 additions & 7 deletions app/contracts/notification_contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ class NotificationContract < ApplicationContract
config.messages.namespace = :notification

params do
required(:source).filled(:string)
required(:notification_type).filled(:string)
end

rule(:source) do
if Notification.sources.keys.exclude?(values[:source])
key(:source).failure(:unexisting)
end
end
end
19 changes: 9 additions & 10 deletions app/controllers/api/frontend/notifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ module Frontend
class NotificationsController < Api::Frontend::BaseController
include Deps[create_form: 'forms.notifications.create']

before_action :find_notifyable, only: %i[create]
before_action :find_company, only: %i[create]
before_action :find_webhook, only: %i[create]
before_action :find_notification, only: %i[destroy]

def create
# commento: notifications.source, notifications.notification_type
case create_form.call(notifyable: @notifyable, params: notification_params)
# commento: notifications.webhook_id, notifications.notification_type
case create_form.call(notifyable: @company, webhook: @webhook, params: notification_params)
in { errors: errors } then render json: { errors: errors }, status: :ok
in { result: result }
render json: { result: NotificationSerializer.new(result).serializable_hash }, status: :ok
Expand All @@ -25,22 +26,20 @@ def destroy

private

def find_notifyable
find_company if params[:company_id]

page_not_found unless @notifyable
def find_company
@company = current_user.available_write_companies.find_by!(uuid: params[:company_id])
end

def find_company
@notifyable = current_user.available_write_companies.find_by!(uuid: params[:company_id])
def find_webhook
@webhook = @company.webhooks.find_by!(uuid: params[:webhook_id])
end

def find_notification
@notification = Notification.find_by!(uuid: params[:id])
end

def notification_params
params.require(:notification).permit(:notification_type, :source)
params.require(:notification).permit(:notification_type)
end
end
end
Expand Down
33 changes: 6 additions & 27 deletions app/controllers/api/frontend/webhooks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,36 @@ module Frontend
class WebhooksController < Api::Frontend::BaseController
include Deps[create_form: 'forms.webhooks.create']

before_action :find_webhookable, only: %i[create]
before_action :find_company, only: %i[create]
before_action :find_webhook, only: %i[destroy]

def create
# commento: webhooks.source, webhooks.url
case create_form.call(webhookable: @webhookable, params: webhook_params)
# commento: webhooks.source, webhooks.url, webhooks.company_id
case create_form.call(company: @company, params: webhook_params)
in { errors: errors } then render json: { errors: errors }, status: :ok
in { result: result }
render json: { result: WebhookSerializer.new(result).serializable_hash }, status: :ok
end
end

def destroy
webhookable = @webhook.webhookable
authorize! (webhookable.is_a?(Notification) ? webhookable.notifyable : webhookable), to: :update?
authorize! @webhook.company, to: :update?
@webhook.destroy
render json: { result: :ok }, status: :ok
end

private

def find_webhookable
find_company if params[:company_id]
if params[:notification_id]
find_notification
authorize_company
end

page_not_found unless @webhookable
end

def find_company
@webhookable = current_user.available_write_companies.find_by!(uuid: params[:company_id])
end

def find_notification
@webhookable = Notification.find_by!(uuid: params[:notification_id])
end

def authorize_company
current_user.available_write_companies.find(@webhookable.notifyable_id)
@company = current_user.available_write_companies.find_by!(uuid: params[:company_id])
end

def find_webhook
@webhook = Webhook.find_by!(uuid: params[:id])
end

def webhook_params
hashable_params = params.require(:webhook).permit(:source, :url).to_h
hashable_params[:source] = @webhookable.source if @webhookable.is_a?(Notification)
hashable_params
params.require(:webhook).permit(:source, :url)
end
end
end
Expand Down
17 changes: 3 additions & 14 deletions app/controllers/companies/configurations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,12 @@ def find_invites
end

def find_notifications
@notifications = @company.notifications.hashable_pluck(:id, :uuid, :source, :notification_type)
@notifications =
@company.notifications.joins(:webhook).hashable_pluck(:uuid, :notification_type, 'webhooks.uuid')
end

def find_webhooks
@webhooks =
Webhook.where(webhookable: @company)
.or(
Webhook.where(webhookable_id: @notifications.pluck(:id), webhookable_type: 'Notification')
)
.hashable_pluck(:uuid, :source, :url, :webhookable_id, :webhookable_type)
.map { |webhook|
next webhook if webhook[:webhookable_type] == 'Company'

webhook.merge(
webhookable_uuid: @notifications.find { |element| element[:id] == webhook[:webhookable_id] }[:uuid]
)
}
@webhooks = @company.webhooks.hashable_pluck(:uuid, :source, :url)
end

def find_excludes_groups
Expand Down
47 changes: 5 additions & 42 deletions app/deliveries/company_delivery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,57 +13,20 @@ class CompanyDelivery < ApplicationDelivery
private

def ensure_webhook_enabled
return false if webhook_sources.exclude?(Webhook::CUSTOM)
return false if notification_sources.exclude?(Notification::CUSTOM)

true
sources.include?(Webhook::CUSTOM)
end

def ensure_slack_enabled
return false if webhook_sources.exclude?(Webhook::SLACK)
return false if notification_sources.exclude?(Notification::SLACK)

true
sources.include?(Webhook::SLACK)
end

def ensure_discord_enabled
return false if webhook_sources.exclude?(Webhook::DISCORD)
return false if notification_sources.exclude?(Notification::DISCORD)

true
sources.include?(Webhook::DISCORD)
end

def ensure_telegram_enabled
return false if webhook_sources.exclude?(Webhook::TELEGRAM)
return false if notification_sources.exclude?(Notification::TELEGRAM)

true
end

def webhook_sources
@webhook_sources ||=
Webhook.where(webhookable: insightable)
.or(Webhook.where(webhookable_type: 'Notification', webhookable_id: notifications.pluck(:id)))
.pluck(:source)
.uniq
end

def notifications
@notifications ||=
insightable.notifications.where(notification_type: notification_type).hashable_pluck(:id, :source)
end

def notification_sources
@notification_sources ||= notifications.pluck(:source)
end

def notification_type
case notification_name
when :insights_report then Notification::INSIGHTS_DATA
when :repository_insights_report then Notification::REPOSITORY_INSIGHTS_DATA
when :long_time_review_report then Notification::LONG_TIME_REVIEW_DATA
end
sources.include?(Webhook::TELEGRAM)
end

def insightable = params[:insightable]
def sources = params[:company].notifications.joins(:webhook).pluck('webhooks.source').uniq
end
4 changes: 2 additions & 2 deletions app/forms/notifications/create_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ module Notifications
class CreateForm
include Deps[validator: 'validators.notification']

def call(notifyable:, params:)
def call(notifyable:, webhook:, params:)
errors = validator.call(params: params)
return { errors: errors } if errors.any?

{ result: notifyable.notifications.create!(params) }
{ result: notifyable.notifications.create!(params.merge(webhook: webhook)) }
rescue ActiveRecord::RecordNotUnique => _e
{ errors: ['Notification already exists'] }
end
Expand Down
4 changes: 2 additions & 2 deletions app/forms/webhooks/create_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ module Webhooks
class CreateForm
include Deps[validator: 'validators.webhook']

def call(webhookable:, params:)
def call(company:, params:)
errors = validator.call(params: params)
return { errors: errors } if errors.any?

error = validate_url(params)
return { errors: [error] } if error.present?

{ result: Webhook.create!(params.merge(webhookable: webhookable)) }
{ result: company.webhooks.create!(params) }
rescue ActiveRecord::RecordNotUnique => _e
{ errors: ['Webhook already exists'] }
end
Expand Down
Loading

0 comments on commit e32cc0e

Please sign in to comment.