Skip to content

Commit

Permalink
feat(admin): call route to detach target-profile
Browse files Browse the repository at this point in the history
from training details

Co-authored-by: Clément Latzarus <[email protected]>
Co-authored-by: Eric Lim <[email protected]>
  • Loading branch information
3 people committed Feb 14, 2025
1 parent e10f890 commit ed7d663
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ export default class TrainingDetailsTargetProfilesController extends Controller
}
}

@action
async detachTargetProfile(targetProfileSummary) {
const { id: trainingId } = this.model.training;
const { targetProfileSummaries } = this.model;
const { id: targetProfileId } = targetProfileSummary;
try {
const adapter = this.store.adapterFor('training');
await adapter.detachTargetProfile({
trainingId,
targetProfileId,
});
await targetProfileSummaries.reload();
return this.pixToast.sendSuccessNotification({
message: `Profil cible détaché avec succès !`,
});
} catch (responseError) {
this._handleResponseError(responseError);
}
}

_handleResponseError({ errors }) {
if (!errors) {
return this.pixToast.sendErrorNotification({ message: 'Une erreur est survenue.' });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
Actions
</:header>
<:cell>
<PixButton @variant="error" @iconBefore="delete" >
<PixButton @variant="error" @iconBefore="delete" @triggerAction={{fn this.detachTargetProfile row}}>
Détacher
</PixButton>
</:cell>
Expand Down
2 changes: 2 additions & 0 deletions admin/mirage/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
attachTargetProfilesToTraining,
createOrUpdateTrainingTrigger,
createTraining,
detachTargetProfileFromTraining,
findPaginatedTrainingSummaries,
getTargetProfileSummariesForTraining,
getTraining,
Expand Down Expand Up @@ -395,6 +396,7 @@ function routes() {
this.patch('/admin/trainings/:id', updateTraining);
this.get('/admin/trainings/:id/target-profile-summaries', getTargetProfileSummariesForTraining);
this.post('/admin/trainings/:id/attach-target-profiles', attachTargetProfilesToTraining);
this.delete('/admin/trainings/:trainingId/target-profiles/:targetProfileId', detachTargetProfileFromTraining);
this.put('/admin/trainings/:id/triggers', createOrUpdateTrainingTrigger);

this.get('/admin/certifications/:id');
Expand Down
12 changes: 12 additions & 0 deletions admin/mirage/handlers/trainings.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ function attachTargetProfilesToTraining(schema, request) {
return new Response(204);
}

function detachTargetProfileFromTraining(schema, request) {
const { trainingId, targetProfileId } = request.params;

const training = schema.trainings.find(trainingId);

const updatedTargetProfiles = training.targetProfileSummaryIds.filter((profileId) => profileId !== targetProfileId);
training.update({ targetProfileSummaryIds: updatedTargetProfiles });

return new Response(204);
}

function updateTraining(schema, request) {
const trainingId = request.params.id;
const params = JSON.parse(request.requestBody);
Expand Down Expand Up @@ -127,6 +138,7 @@ export {
attachTargetProfilesToTraining,
createOrUpdateTrainingTrigger,
createTraining,
detachTargetProfileFromTraining,
findPaginatedTrainingSummaries,
getTargetProfileSummariesForTraining,
getTraining,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module('Acceptance | Trainings | Target profiles', function (hooks) {
hooks.beforeEach(async function () {
trainingId = 2;

const targetProfileSummary = server.create('target-profile-summary', { id: 1111, name: 'Super profil cible 2' });
server.create('training', {
id: 2,
title: 'Devenir tailleur de citrouille',
Expand All @@ -27,6 +28,7 @@ module('Acceptance | Trainings | Target profiles', function (hooks) {
editorLogoUrl: 'https://mon-logo.svg',
prerequisiteThreshold: null,
goalThreshold: null,
targetProfileSummaries: [targetProfileSummary],
});
server.create('target-profile', {});
});
Expand Down Expand Up @@ -65,6 +67,17 @@ module('Acceptance | Trainings | Target profiles', function (hooks) {
// then
assert.dom(await screen.findByRole('link', { name: 'Super profil cible' })).exists();
});

test('it should be able to detach a target profile to a training', async function (assert) {
await authenticateAdminMemberWithRole({ isSuperAdmin: true })(server);

// when
const screen = await visit(`/trainings/${trainingId}/target-profiles`);
await clickByName('Détacher');

// then
assert.dom(await screen.findByText('Aucun profil cible associé à ce contenu formatif')).exists();
});
});

module('when admin role is "SUPPORT"', function () {
Expand Down

0 comments on commit ed7d663

Please sign in to comment.