Skip to content

Commit

Permalink
feat(api): update route and add error mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
alicegoarnisson authored and xav-car committed Nov 15, 2024
1 parent 7fa587e commit 638e233
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HttpErrors } from '../../../shared/application/http-errors.js';
import { AggregateImportError } from '../domain/errors.js';
import { AggregateImportError, CouldNotDeleteLearnersError } from '../domain/errors.js';

const learnerManagementDomainErrorMappingConfiguration = [
{
Expand All @@ -8,6 +8,12 @@ const learnerManagementDomainErrorMappingConfiguration = [
return new HttpErrors.PreconditionFailedError(error.message, error.code, error.meta);
},
},
{
name: CouldNotDeleteLearnersError.name,
httpErrorFn: (error) => {
return new HttpErrors.PreconditionFailedError(error.message);
},
},
];

export { learnerManagementDomainErrorMappingConfiguration };
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import { usecases } from '../domain/usecases/index.js';
const deleteOrganizationLearners = async function (request, h) {
const authenticatedUserId = request.auth.credentials.userId;
const listLearners = request.payload.listLearners;
const organizationId = request.params.organizationId;

await DomainTransaction.execute(async () => {
await usecases.deleteOrganizationLearners({
organizationLearnerIds: listLearners,
userId: authenticatedUserId,
organizationId,
});
});
return h.response().code(200);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const register = async (server) => {
server.route([
{
method: 'DELETE',
path: '/api/organizations/{id}/organization-learners',
path: '/api/organizations/{organizationId}/organization-learners',
config: {
pre: [
{
Expand All @@ -27,7 +27,7 @@ const register = async (server) => {
],
validate: {
params: Joi.object({
id: identifiersType.organizationId,
organizationId: identifiersType.organizationId,
}),
payload: Joi.object({
listLearners: Joi.array().required().items(Joi.number().required()),
Expand Down
7 changes: 7 additions & 0 deletions api/src/prescription/learner-management/domain/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,15 @@ class ReconcileCommonOrganizationLearnerError extends DomainError {
}
}

class CouldNotDeleteLearnersError extends DomainError {
constructor() {
super(`Could not delete the following organization learners.`);
}
}

export {
AggregateImportError,
CouldNotDeleteLearnersError,
OrganizationDoesNotHaveFeatureEnabledError,
OrganizationLearnerImportFormatNotFoundError,
OrganizationLearnersCouldNotBeSavedError,
Expand Down
2 changes: 1 addition & 1 deletion api/src/shared/application/security-pre-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ async function checkUserDoesNotBelongsToScoOrganizationManagingStudents(
return _replyForbiddenError(h);
}

const organizationId = request.params.id;
const organizationId = request.params.organizationId || request.params.id;

const isOrganizationScoManagingStudent = await dependencies.checkOrganizationIsScoAndManagingStudentUsecase.execute({
organizationId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '../../../../test-helper.js';

describe('Integration | Application | Organization Learners Management | Routes', function () {
describe('DELETE /organizations/{id}/organization-learners', function () {
describe('DELETE /organizations/{organizationId}/organization-learners', function () {
const method = 'DELETE';

let headers, httpTestServer, organizationId, url, payload;
Expand Down

0 comments on commit 638e233

Please sign in to comment.