Skip to content

Commit

Permalink
show warning if auto issue of certificates cant be done (pupilfirst#493)
Browse files Browse the repository at this point in the history
Co-authored-by: Mahesh Krishna Kumar <[email protected]>
  • Loading branch information
a-c-sreedhar-reddy and mahesh-krishnakumar authored Oct 12, 2020
1 parent 4bdb85a commit 904ed71
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 7 deletions.
10 changes: 8 additions & 2 deletions app/javascript/packs/SchoolsCoursesCertificatesPack.re
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ let decodeProps = json =>
field("course", Course.decode, json),
field("certificates", array(Certificate.decode), json),
field("verifyImageUrl", string, json),
field("canBeAutoIssued", bool, json),
);

let (course, certificates, verifyImageUrl) =
let (course, certificates, verifyImageUrl, canBeAutoIssued) =
DomUtils.parseJSONTag(~id="schools-courses-certificates__props", ())
|> decodeProps;

ReactDOMRe.renderToElementWithId(
<CourseCertificates__Root course certificates verifyImageUrl />,
<CourseCertificates__Root
course
certificates
verifyImageUrl
canBeAutoIssued
/>,
"schools-courses-certificates__root",
);
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ let deleteCertificate = (certificate, send) => {
};

[@react.component]
let make = (~course, ~certificates, ~verifyImageUrl) => {
let make = (~course, ~certificates, ~verifyImageUrl, ~canBeAutoIssued) => {
let (state, send) =
React.useReducerWithMapState(reducer, certificates, computeInitialState);

Expand All @@ -129,6 +129,7 @@ let make = (~course, ~certificates, ~verifyImageUrl) => {
verifyImageUrl
closeDrawerCB={() => send(CloseDrawer)}
updateCertificateCB={updateCertificate(state, send)}
canBeAutoIssued
/>
| Closed => React.null
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,13 @@ let activateQrCode = (state, send, _event) =>

[@react.component]
let make =
(~certificate, ~verifyImageUrl, ~closeDrawerCB, ~updateCertificateCB) => {
(
~certificate,
~verifyImageUrl,
~closeDrawerCB,
~updateCertificateCB,
~canBeAutoIssued,
) => {
let (state, send) =
React.useReducerWithMapState(reducer, certificate, computeInitialState);

Expand Down Expand Up @@ -245,6 +251,18 @@ let make =
{str("No")}
</button>
</div>
{!canBeAutoIssued
? <div
className="flex p-4 bg-yellow-100 text-yellow-900 border border-yellow-500 border-l-4 rounded-r-md mt-2">
<div className="w-6 h-6 text-yellow-500 flex-shrink-0">
<i className="fas fa-exclamation-triangle" />
</div>
<span className="ml-2">
"Please note that the last level of this course does not have any milestone targets. This certificate will be auto-issued only if the last level has at least one milestone target."
->React.string
</span>
</div>
: React.null}
</div>
<h5 className="mt-6 text-sm uppercase font-bold pb-1 border-b">
{str("Design")}
Expand Down
6 changes: 6 additions & 0 deletions app/presenters/schools/courses/certificates_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def props
course: course_details,
certificates: certificates,
verify_image_url: view.image_path('issued_certificates/verify.png'),
can_be_auto_issued: can_be_auto_issued,
}
end

Expand All @@ -26,6 +27,11 @@ def self.certificate_details(certificate)

private

def can_be_auto_issued
highest_level_id = @course.levels.order(number: :desc).pick(:id)
TargetGroup.where(level_id: highest_level_id, milestone: true, archived: false).exists?
end

def certificates
active_certificate = @course.certificates.active.includes_image.limit(1)
ActiveRecord::Precounter.new(active_certificate).precount(:issued_certificates)
Expand Down
48 changes: 45 additions & 3 deletions spec/system/school/courses/certificates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@
attach_file 'Certificate Base Image', File.absolute_path(Rails.root.join('spec/support/uploads/certificates/sample.png')), visible: false
click_button 'Create Certificate'

expect(page).to have_text("Done!")
expect(page).to have_text('Done!')

dismiss_notification

expect(page).to have_text("Never issued")
expect(page).to have_text('Never issued')

certificate = Certificate.last

expect(certificate.name).to include(Time.zone.now.strftime("%-d %b %Y %-l"))
expect(certificate.name).to include(Time.zone.now.strftime('%-d %b %Y %-l'))
expect(certificate.course).to eq(course)
expect(certificate.image.attached?).to eq(true)
expect(certificate.active).to eq(false)
Expand Down Expand Up @@ -153,4 +153,46 @@
expect(Certificate.first).to eq(certificate_issued)
end
end

context 'school has courses with/without milestone targets in highest level' do
# course without milestone target group
let(:course_without_targets) { create :course, school: school }
let!(:certificate_c1) { create :certificate, :active, course: course_without_targets }

# course with milestone target group
let(:course_with_milestone_target) { create :course, school: school }
let!(:level_c2) { create :level, :one, course: course_with_milestone_target }
let!(:target_group_c2) { create :target_group, level: level_c2, milestone: true }
let!(:certificate_c2) { create :certificate, :active, course: course_with_milestone_target }

# course with only archived target milestone group
let(:course_with_archived_milestone) { create :course, school: school }
let!(:level_c3) { create :level, :one, course: course_with_archived_milestone }
let!(:target_group_c3) { create :target_group, level: level_c3, milestone: true, archived: true, safe_to_archive: true }
let!(:certificate_c3) { create :certificate, :active, course: course_with_archived_milestone }

scenario 'user visits certificate editor for course without milestone targets in highest level' do
sign_in_user school_admin.user, referrer: certificates_school_course_path(course_without_targets)

find("a[title='Edit Certificate #{certificate_c1.name}'").click

expect(page).to have_text('Please note that the last level of this course does not have any milestone targets. This certificate will be auto-issued only if the last level has at least one milestone target.')
end

scenario 'user visits certificate editor for course with milestone targets in highest level' do
sign_in_user school_admin.user, referrer: certificates_school_course_path(course_with_milestone_target)

find("a[title='Edit Certificate #{certificate_c2.name}'").click

expect(page).not_to have_text('Please note that the last level of this course does not have any milestone targets. This certificate will be auto-issued only if the last level has at least one milestone target.')
end

scenario 'user visits certificate editor for course with no live milestone target groups' do
sign_in_user school_admin.user, referrer: certificates_school_course_path(course_with_archived_milestone)

find("a[title='Edit Certificate #{certificate_c3.name}'").click

expect(page).to have_text('Please note that the last level of this course does not have any milestone targets. This certificate will be auto-issued only if the last level has at least one milestone target.')
end
end
end

0 comments on commit 904ed71

Please sign in to comment.