-
Notifications
You must be signed in to change notification settings - Fork 78
feat(marketplace): admin publish control for assessments #8477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
adi-herwana-nus
merged 1 commit into
lws49/feat-marketplace-pr1-foundation
from
lws49/feat-marketplace-pr2-publish
Jul 29, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
app/controllers/course/assessment/marketplace_listings_controller.rb
This file contains hidden or 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,44 @@ | ||
| # frozen_string_literal: true | ||
| class Course::Assessment::MarketplaceListingsController < Course::Assessment::Controller | ||
| before_action :authorize_publish_to_marketplace! | ||
|
|
||
| def create | ||
| listing = Course::Assessment::Marketplace::Listing.find_or_initialize_by(assessment: @assessment) | ||
| now = Time.zone.now | ||
| listing.published = true | ||
| listing.first_published_at ||= now | ||
| listing.last_published_at = now | ||
| # `publisher` is an audit userstamp for the *latest* publish (design D29), so it moves with | ||
| # `last_published_at`. `creator` already retains whoever first created the row. | ||
| listing.publisher = current_user | ||
| if listing.save | ||
| render json: { published: true }, status: :ok | ||
| else | ||
| render json: { errors: listing.errors.full_messages }, status: :unprocessable_content | ||
| end | ||
| end | ||
|
|
||
| def destroy | ||
| listing = @assessment.marketplace_listing | ||
| if listing&.update(published: false) | ||
| head :ok | ||
| else | ||
| head :unprocessable_content | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # Publishing is admin-only. `authorize!(:publish_to_marketplace, @assessment)` alone is | ||
| # insufficient: teaching staff hold `can :manage, Course::Assessment` over their own course's | ||
| # assessments (assessment_ability.rb:189), and CanCan's `:manage` wildcard subsumes every | ||
| # custom action — including `:publish_to_marketplace`. Gate explicitly on administrator status. | ||
| def authorize_publish_to_marketplace! | ||
| authorize!(:publish_to_marketplace, @assessment) | ||
| raise CanCan::AccessDenied unless current_user&.administrator? | ||
| end | ||
|
|
||
| def component | ||
| current_component_host[:course_assessments_component] | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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,21 @@ | ||
| import { AxiosResponse } from 'axios'; | ||
|
|
||
| import BaseCourseAPI from './Base'; | ||
|
|
||
| export default class MarketplaceAPI extends BaseCourseAPI { | ||
| get #urlPrefix(): string { | ||
| return `/courses/${this.courseId}/marketplace`; | ||
| } | ||
|
|
||
| publishListing(assessmentId: number): Promise<AxiosResponse> { | ||
| return this.client.post( | ||
| `/courses/${this.courseId}/assessments/${assessmentId}/marketplace_listing`, | ||
| ); | ||
| } | ||
|
|
||
| removeListing(assessmentId: number): Promise<AxiosResponse> { | ||
| return this.client.delete( | ||
| `/courses/${this.courseId}/assessments/${assessmentId}/marketplace_listing`, | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
90 changes: 90 additions & 0 deletions
90
...app/bundles/course/assessment/pages/AssessmentShow/__test__/AssessmentShowHeader.test.tsx
This file contains hidden or 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,90 @@ | ||
| import { createMockAdapter } from 'mocks/axiosMock'; | ||
| import { fireEvent, render, waitFor, within } from 'test-utils'; | ||
|
|
||
| import CourseAPI from 'api/course'; | ||
|
|
||
| import AssessmentShowHeader from '../AssessmentShowHeader'; | ||
|
|
||
| const mock = createMockAdapter(CourseAPI.marketplace.client); | ||
| beforeEach(() => mock.reset()); | ||
|
|
||
| // Minimal AssessmentData: only `deleteUrl` + `title` are needed for the delete | ||
| // Prompt to render (see AssessmentShowHeader.tsx:71 / DeleteButton.tsx). All other | ||
| // action buttons stay hidden by leaving their URLs undefined, and the publish | ||
| // button stays hidden via `canPublishToMarketplace: false`. | ||
| const baseAssessment = { | ||
| id: 1, | ||
| title: 'Sample Assessment', | ||
| deleteUrl: '/courses/1/assessments/1', | ||
| status: 'open', | ||
| permissions: { | ||
| canAttempt: false, | ||
| canManage: true, | ||
| canObserve: true, | ||
| canInviteToKoditsu: false, | ||
| canPublishToMarketplace: false, | ||
| }, | ||
| isPublishedToMarketplace: false, | ||
| }; | ||
|
|
||
| // Test the conditional <PromptText> in the delete Prompt whose | ||
| // message contains this phrase, rendered only when `isPublishedToMarketplace`. | ||
| const MARKETPLACE_WARNING = /removes it from the marketplace/i; | ||
|
|
||
| describe('<AssessmentShowHeader />', () => { | ||
| it('warns that deletion removes the marketplace listing when the assessment is listed', async () => { | ||
| const page = render( | ||
| <AssessmentShowHeader | ||
| with={{ ...baseAssessment, isPublishedToMarketplace: true } as never} | ||
| />, | ||
| ); | ||
|
|
||
| // First query awaits the i18n LoadingIndicator; subsequent getBy* are sync. | ||
| fireEvent.click(await page.findByLabelText('Delete Assessment')); // opens the delete Prompt | ||
| expect(page.getByText(MARKETPLACE_WARNING)).toBeVisible(); | ||
| }); | ||
|
|
||
| it('shows no marketplace warning when the assessment is not listed', async () => { | ||
| const page = render( | ||
| <AssessmentShowHeader | ||
| with={{ ...baseAssessment, isPublishedToMarketplace: false } as never} | ||
| />, | ||
| ); | ||
|
|
||
| fireEvent.click(await page.findByLabelText('Delete Assessment')); // delete Prompt still opens | ||
| expect(page.queryByText(MARKETPLACE_WARNING)).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('warns after the assessment is published in the same session', async () => { | ||
| mock | ||
| .onPost(`/courses/${global.courseId}/assessments/1/marketplace_listing`) | ||
| .reply(200, { published: true }); | ||
|
|
||
| const page = render( | ||
| <AssessmentShowHeader | ||
| with={ | ||
| { | ||
| ...baseAssessment, | ||
| permissions: { | ||
| ...baseAssessment.permissions, | ||
| canPublishToMarketplace: true, | ||
| }, | ||
| } as never | ||
| } | ||
| />, | ||
| ); | ||
|
|
||
| fireEvent.click(await page.findByText('Publish to Marketplace')); // trigger button | ||
| const publishPrompt = await page.findByRole('dialog'); | ||
| fireEvent.click( | ||
| within(publishPrompt).getByRole('button', { | ||
| name: /Publish to Marketplace/, | ||
| }), | ||
| ); | ||
| await waitFor(() => expect(mock.history.post).toHaveLength(1)); | ||
|
|
||
| // The warning reads the live state, not the initial `isPublishedToMarketplace` prop. | ||
| fireEvent.click(page.getByLabelText('Delete Assessment')); | ||
| expect(await page.findByText(MARKETPLACE_WARNING)).toBeVisible(); | ||
| }); | ||
| }); |
90 changes: 90 additions & 0 deletions
90
client/app/bundles/course/marketplace/components/PublishToMarketplaceButton.tsx
This file contains hidden or 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,90 @@ | ||
| import { useState } from 'react'; | ||
| import { useIntl } from 'react-intl'; | ||
| import { Button } from '@mui/material'; | ||
| import { AssessmentData } from 'types/course/assessment/assessments'; | ||
|
|
||
| import CourseAPI from 'api/course'; | ||
| import Prompt, { PromptText } from 'lib/components/core/dialogs/Prompt'; | ||
| import toast from 'lib/hooks/toast'; | ||
|
|
||
| import translations from '../translations'; | ||
|
|
||
| interface Props { | ||
| assessment: Pick< | ||
| AssessmentData, | ||
| 'id' | 'isPublishedToMarketplace' | 'permissions' | ||
| >; | ||
| onChange: (published: boolean) => void; | ||
| } | ||
|
|
||
| const PublishToMarketplaceButton = ({ | ||
| assessment, | ||
| onChange, | ||
| }: Props): JSX.Element | null => { | ||
| const { formatMessage: t } = useIntl(); | ||
| const [open, setOpen] = useState(false); | ||
| const [submitting, setSubmitting] = useState(false); | ||
| const listed = assessment.isPublishedToMarketplace; | ||
|
|
||
| if (!assessment.permissions.canPublishToMarketplace) return null; | ||
|
|
||
| // `Prompt`'s primary button does not await this handler, so every rejection must be caught | ||
| // here: an uncaught one surfaces nothing to the user and becomes an unhandled rejection. | ||
| const confirm = async (): Promise<void> => { | ||
| setSubmitting(true); | ||
| try { | ||
| if (listed) { | ||
| await CourseAPI.marketplace.removeListing(assessment.id); | ||
| toast.success(t(translations.removed)); | ||
| onChange(false); | ||
| } else { | ||
| await CourseAPI.marketplace.publishListing(assessment.id); | ||
| toast.success(t(translations.published)); | ||
| onChange(true); | ||
| } | ||
| setOpen(false); | ||
| } catch { | ||
| // Dialog stays open so the user can retry. | ||
| toast.error( | ||
| t(listed ? translations.removeFailed : translations.publishFailed), | ||
| ); | ||
| } finally { | ||
| setSubmitting(false); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| <Button | ||
| color={listed ? 'error' : 'primary'} | ||
| onClick={(): void => setOpen(true)} | ||
| variant="outlined" | ||
| > | ||
| {t(listed ? translations.remove : translations.publish)} | ||
| </Button> | ||
| <Prompt | ||
| disabled={submitting} | ||
| onClickPrimary={confirm} | ||
| onClose={(): void => setOpen(false)} | ||
| open={open} | ||
| primaryColor={listed ? 'error' : 'primary'} | ||
| primaryLabel={t(listed ? translations.remove : translations.publish)} | ||
| title={t( | ||
| listed | ||
| ? translations.removeConfirmTitle | ||
| : translations.publishConfirmTitle, | ||
| )} | ||
| > | ||
| <PromptText> | ||
| {t( | ||
| listed | ||
| ? translations.removeConfirmBody | ||
| : translations.publishConfirmBody, | ||
| )} | ||
| </PromptText> | ||
| </Prompt> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| export default PublishToMarketplaceButton; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.