generated from DFE-Digital/govuk-rails-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CPDLP-3046] Add accept applications endpoint (#1368)
* [CPDLP-3046] Add accept applications endpoint * [CPDLP-3046] Address PR comments * [CPDLP-3048] Add change funding application endpoint * [CPDLP-3048] Address PR comments * [CPDLP-3049] TS4 - Add swagger docs for all three new action endpoints in all versions * [CPDLP-3049] Add more api docs specs * [CPDLP-3049] Address PR comments * [CPDLP-3046] Address PR comments --------- Co-authored-by: Mooktakim Ahmed <[email protected]>
- Loading branch information
1 parent
26cc532
commit bd4dcff
Showing
38 changed files
with
1,776 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# frozen_string_literal: true | ||
|
||
module Applications | ||
class Accept | ||
include ActiveModel::Model | ||
include ActiveModel::Attributes | ||
|
||
attribute :application | ||
attribute :funded_place, :boolean | ||
|
||
validates :application, presence: { message: I18n.t("application.missing_application") } | ||
validate :not_already_accepted | ||
validate :cannot_change_from_rejected | ||
validate :other_accepted_applications_with_same_course? | ||
validate :eligible_for_funded_place | ||
validate :validate_funded_place | ||
|
||
def accept | ||
return false unless valid? | ||
|
||
ApplicationRecord.transaction do | ||
accept_application! | ||
reject_other_applications_in_same_cohort! | ||
set_funded_place_on_npq_application! | ||
end | ||
|
||
true | ||
end | ||
|
||
private | ||
|
||
delegate :cohort, :user, :course, to: :application | ||
|
||
def not_already_accepted | ||
return if application.blank? | ||
|
||
errors.add(:application, I18n.t("application.has_already_been_accepted")) if application.accepted? | ||
end | ||
|
||
def cannot_change_from_rejected | ||
return if application.blank? | ||
|
||
errors.add(:application, I18n.t("application.cannot_change_from_rejected")) if application.rejected? | ||
end | ||
|
||
def other_accepted_applications_with_same_course? | ||
errors.add(:application, I18n.t("application.has_another_accepted_application")) if other_accepted_applications_with_same_course.present? | ||
end | ||
|
||
def accept_application! | ||
application.update!(lead_provider_approval_status: "accepted") | ||
end | ||
|
||
def reject_other_applications_in_same_cohort! | ||
return if other_applications_in_same_cohort.blank? | ||
|
||
other_applications_in_same_cohort.update!(lead_provider_approval_status: "rejected") | ||
end | ||
|
||
def other_accepted_applications_with_same_course | ||
return if application.blank? | ||
|
||
@other_accepted_applications_with_same_course ||= Application | ||
.where(lead_provider_approval_status: "accepted", course: course.rebranded_alternative_courses, user: [user, same_trn_users].flatten.compact.uniq) | ||
.where.not(id: application.id) | ||
end | ||
|
||
def other_applications_in_same_cohort | ||
return if cohort.blank? | ||
|
||
@other_applications_in_same_cohort ||= Application | ||
.where(cohort:, course:, user:) | ||
.where.not(id: application.id) | ||
end | ||
|
||
def trn | ||
@trn ||= user.trn_verified? ? user.trn : nil | ||
end | ||
|
||
def same_trn_users | ||
return if trn.blank? | ||
|
||
@same_trn_users ||= User | ||
.where(trn:) | ||
.where.not(id: user.id) | ||
end | ||
|
||
def set_funded_place_on_npq_application! | ||
return unless cohort&.funding_cap? | ||
|
||
application.update!(funded_place:) | ||
end | ||
|
||
def eligible_for_funded_place | ||
return if errors.any? | ||
return unless cohort&.funding_cap? | ||
|
||
if funded_place && !application.eligible_for_funding | ||
errors.add(:application, I18n.t("application.not_eligible_for_funded_place")) | ||
end | ||
end | ||
|
||
def validate_funded_place | ||
return if errors.any? | ||
return unless cohort&.funding_cap? | ||
|
||
if funded_place.nil? | ||
errors.add(:application, I18n.t("application.funded_place_required")) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
module Applications | ||
class ChangeFundedPlace | ||
include ActiveModel::Model | ||
include ActiveModel::Attributes | ||
|
||
attribute :application | ||
attribute :funded_place, :boolean | ||
|
||
validates :application, presence: { message: I18n.t("application.missing_application") } | ||
validates :funded_place, | ||
inclusion: { | ||
in: [true, false], | ||
message: I18n.t("application.missing_funded_place"), | ||
} | ||
validate :accepted_application | ||
validate :eligible_for_funding | ||
validate :cohort_has_funding_cap | ||
|
||
def change | ||
return false unless valid? | ||
|
||
application.update!(funded_place:) | ||
end | ||
|
||
private | ||
|
||
delegate :cohort, to: :application | ||
|
||
def accepted_application | ||
return if application.accepted? | ||
|
||
errors.add(:application, I18n.t("application.cannot_change_funded_status_from_non_accepted")) | ||
end | ||
|
||
def eligible_for_funding | ||
return unless funded_place | ||
return if application.eligible_for_funding? | ||
|
||
errors.add(:application, I18n.t("application.cannot_change_funded_status_non_eligible")) | ||
end | ||
|
||
def cohort_has_funding_cap | ||
return if errors.any? | ||
return if cohort&.funding_cap? | ||
|
||
errors.add(:application, I18n.t("application.cohort_does_not_accept_capping")) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.