diff --git a/src/modules/cards/application/useCases/commands/AddCardToCollectionUseCase.ts b/src/modules/cards/application/useCases/commands/AddCardToCollectionUseCase.ts index 45623969..37277146 100644 --- a/src/modules/cards/application/useCases/commands/AddCardToCollectionUseCase.ts +++ b/src/modules/cards/application/useCases/commands/AddCardToCollectionUseCase.ts @@ -9,6 +9,7 @@ import { CardId } from '../../../domain/value-objects/CardId'; import { CollectionId } from '../../../domain/value-objects/CollectionId'; import { CuratorId } from '../../../domain/value-objects/CuratorId'; import { CardCollectionService } from '../../../domain/services/CardCollectionService'; +import { AuthenticationError } from '../../../../../shared/core/AuthenticationError'; export interface AddCardToCollectionDTO { cardId: string; @@ -30,7 +31,7 @@ export class AddCardToCollectionUseCase extends BaseUseCase< AddCardToCollectionDTO, Result< AddCardToCollectionResponseDTO, - ValidationError | AppError.UnexpectedError + ValidationError | AuthenticationError | AppError.UnexpectedError > > { constructor( @@ -46,7 +47,7 @@ export class AddCardToCollectionUseCase extends BaseUseCase< ): Promise< Result< AddCardToCollectionResponseDTO, - ValidationError | AppError.UnexpectedError + ValidationError | AuthenticationError | AppError.UnexpectedError > > { try { @@ -104,6 +105,10 @@ export class AddCardToCollectionUseCase extends BaseUseCase< curatorId, ); if (addToCollectionsResult.isErr()) { + // Propagate authentication errors + if (addToCollectionsResult.error instanceof AuthenticationError) { + return err(addToCollectionsResult.error); + } if (addToCollectionsResult.error instanceof AppError.UnexpectedError) { return err(addToCollectionsResult.error); } diff --git a/src/modules/cards/application/useCases/commands/AddCardToLibraryUseCase.ts b/src/modules/cards/application/useCases/commands/AddCardToLibraryUseCase.ts index 2fa040ef..3fa9332d 100644 --- a/src/modules/cards/application/useCases/commands/AddCardToLibraryUseCase.ts +++ b/src/modules/cards/application/useCases/commands/AddCardToLibraryUseCase.ts @@ -8,6 +8,7 @@ import { CollectionId } from '../../../domain/value-objects/CollectionId'; import { CuratorId } from '../../../domain/value-objects/CuratorId'; import { CardLibraryService } from '../../../domain/services/CardLibraryService'; import { CardCollectionService } from '../../../domain/services/CardCollectionService'; +import { AuthenticationError } from '../../../../../shared/core/AuthenticationError'; export interface AddCardToLibraryDTO { cardId: string; @@ -31,7 +32,7 @@ export class AddCardToLibraryUseCase AddCardToLibraryDTO, Result< AddCardToLibraryResponseDTO, - ValidationError | AppError.UnexpectedError + ValidationError | AuthenticationError | AppError.UnexpectedError > > { @@ -46,7 +47,7 @@ export class AddCardToLibraryUseCase ): Promise< Result< AddCardToLibraryResponseDTO, - ValidationError | AppError.UnexpectedError + ValidationError | AuthenticationError | AppError.UnexpectedError > > { try { @@ -87,6 +88,10 @@ export class AddCardToLibraryUseCase curatorId, ); if (addToLibraryResult.isErr()) { + // Propagate authentication errors + if (addToLibraryResult.error instanceof AuthenticationError) { + return err(addToLibraryResult.error); + } if (addToLibraryResult.error instanceof AppError.UnexpectedError) { return err(addToLibraryResult.error); } @@ -118,6 +123,10 @@ export class AddCardToLibraryUseCase curatorId, ); if (addToCollectionsResult.isErr()) { + // Propagate authentication errors + if (addToCollectionsResult.error instanceof AuthenticationError) { + return err(addToCollectionsResult.error); + } if ( addToCollectionsResult.error instanceof AppError.UnexpectedError ) { diff --git a/src/modules/cards/application/useCases/commands/UpdateUrlCardAssociationsUseCase.ts b/src/modules/cards/application/useCases/commands/UpdateUrlCardAssociationsUseCase.ts index 37c00430..65c6955e 100644 --- a/src/modules/cards/application/useCases/commands/UpdateUrlCardAssociationsUseCase.ts +++ b/src/modules/cards/application/useCases/commands/UpdateUrlCardAssociationsUseCase.ts @@ -14,6 +14,7 @@ import { CardCollectionService } from '../../../domain/services/CardCollectionSe import { CardContent } from '../../../domain/value-objects/CardContent'; import { CardFactory } from '../../../domain/CardFactory'; import { CardLibraryService } from '../../../domain/services/CardLibraryService'; +import { AuthenticationError } from '../../../../../shared/core/AuthenticationError'; export interface UpdateUrlCardAssociationsDTO { cardId: string; @@ -40,7 +41,7 @@ export class UpdateUrlCardAssociationsUseCase extends BaseUseCase< UpdateUrlCardAssociationsDTO, Result< UpdateUrlCardAssociationsResponseDTO, - ValidationError | AppError.UnexpectedError + ValidationError | AuthenticationError | AppError.UnexpectedError > > { constructor( @@ -57,7 +58,7 @@ export class UpdateUrlCardAssociationsUseCase extends BaseUseCase< ): Promise< Result< UpdateUrlCardAssociationsResponseDTO, - ValidationError | AppError.UnexpectedError + ValidationError | AuthenticationError | AppError.UnexpectedError > > { try { @@ -190,6 +191,10 @@ export class UpdateUrlCardAssociationsUseCase extends BaseUseCase< const addNoteCardToLibraryResult = await this.cardLibraryService.addCardToLibrary(noteCard, curatorId); if (addNoteCardToLibraryResult.isErr()) { + // Propagate authentication errors + if (addNoteCardToLibraryResult.error instanceof AuthenticationError) { + return err(addNoteCardToLibraryResult.error); + } if ( addNoteCardToLibraryResult.error instanceof AppError.UnexpectedError @@ -232,6 +237,10 @@ export class UpdateUrlCardAssociationsUseCase extends BaseUseCase< curatorId, ); if (addToCollectionsResult.isErr()) { + // Propagate authentication errors + if (addToCollectionsResult.error instanceof AuthenticationError) { + return err(addToCollectionsResult.error); + } if ( addToCollectionsResult.error instanceof AppError.UnexpectedError ) { @@ -282,6 +291,10 @@ export class UpdateUrlCardAssociationsUseCase extends BaseUseCase< curatorId, ); if (removeFromCollectionsResult.isErr()) { + // Propagate authentication errors + if (removeFromCollectionsResult.error instanceof AuthenticationError) { + return err(removeFromCollectionsResult.error); + } if ( removeFromCollectionsResult.error instanceof AppError.UnexpectedError diff --git a/src/modules/cards/infrastructure/http/controllers/AddCardToCollectionController.ts b/src/modules/cards/infrastructure/http/controllers/AddCardToCollectionController.ts index f9745863..c558b949 100644 --- a/src/modules/cards/infrastructure/http/controllers/AddCardToCollectionController.ts +++ b/src/modules/cards/infrastructure/http/controllers/AddCardToCollectionController.ts @@ -2,6 +2,7 @@ import { Controller } from '../../../../../shared/infrastructure/http/Controller import { Response } from 'express'; import { AddCardToCollectionUseCase } from '../../../application/useCases/commands/AddCardToCollectionUseCase'; import { AuthenticatedRequest } from '../../../../../shared/infrastructure/http/middleware/AuthMiddleware'; +import { AuthenticationError } from '../../../../../shared/core/AuthenticationError'; export class AddCardToCollectionController extends Controller { constructor(private addCardToCollectionUseCase: AddCardToCollectionUseCase) { @@ -32,12 +33,16 @@ export class AddCardToCollectionController extends Controller { }); if (result.isErr()) { + // Check if the error is an authentication error + if (result.error instanceof AuthenticationError) { + return this.unauthorized(res, result.error.message); + } return this.fail(res, result.error); } return this.ok(res, result.value); } catch (error: any) { - return this.fail(res, error); + return this.handleError(res, error); } } } diff --git a/src/modules/cards/infrastructure/http/controllers/AddCardToLibraryController.ts b/src/modules/cards/infrastructure/http/controllers/AddCardToLibraryController.ts index befde33b..0999dd2d 100644 --- a/src/modules/cards/infrastructure/http/controllers/AddCardToLibraryController.ts +++ b/src/modules/cards/infrastructure/http/controllers/AddCardToLibraryController.ts @@ -2,6 +2,7 @@ import { Controller } from '../../../../../shared/infrastructure/http/Controller import { Response } from 'express'; import { AddCardToLibraryUseCase } from '../../../application/useCases/commands/AddCardToLibraryUseCase'; import { AuthenticatedRequest } from '../../../../../shared/infrastructure/http/middleware/AuthMiddleware'; +import { AuthenticationError } from '../../../../../shared/core/AuthenticationError'; export class AddCardToLibraryController extends Controller { constructor(private addCardToLibraryUseCase: AddCardToLibraryUseCase) { @@ -33,12 +34,16 @@ export class AddCardToLibraryController extends Controller { }); if (result.isErr()) { + // Check if the error is an authentication error + if (result.error instanceof AuthenticationError) { + return this.unauthorized(res, result.error.message); + } return this.fail(res, result.error); } return this.ok(res, result.value); } catch (error: any) { - return this.fail(res, error); + return this.handleError(res, error); } } } diff --git a/src/modules/cards/infrastructure/http/controllers/UpdateUrlCardAssociationsController.ts b/src/modules/cards/infrastructure/http/controllers/UpdateUrlCardAssociationsController.ts index 8be4f155..bd48963f 100644 --- a/src/modules/cards/infrastructure/http/controllers/UpdateUrlCardAssociationsController.ts +++ b/src/modules/cards/infrastructure/http/controllers/UpdateUrlCardAssociationsController.ts @@ -2,6 +2,7 @@ import { Controller } from '../../../../../shared/infrastructure/http/Controller import { Response } from 'express'; import { UpdateUrlCardAssociationsUseCase } from '../../../application/useCases/commands/UpdateUrlCardAssociationsUseCase'; import { AuthenticatedRequest } from '../../../../../shared/infrastructure/http/middleware/AuthMiddleware'; +import { AuthenticationError } from '../../../../../shared/core/AuthenticationError'; export class UpdateUrlCardAssociationsController extends Controller { constructor( @@ -33,12 +34,16 @@ export class UpdateUrlCardAssociationsController extends Controller { }); if (result.isErr()) { + // Check if the error is an authentication error + if (result.error instanceof AuthenticationError) { + return this.unauthorized(res, result.error.message); + } return this.fail(res, result.error); } return this.ok(res, result.value); } catch (error: any) { - return this.fail(res, error); + return this.handleError(res, error); } } }