This repository has no description
semble
/
src
/
modules
/
cards
/
infrastructure
/
http
/
controllers
/
UpdateCollectionController.ts
2.1 kB
65 lines
1import { Controller } from '../../../../../shared/infrastructure/http/Controller';
2import { Response } from 'express';
3import { UpdateCollectionUseCase } from '../../../application/useCases/commands/UpdateCollectionUseCase';
4import { AuthenticatedRequest } from '../../../../../shared/infrastructure/http/middleware/AuthMiddleware';
5import { AuthenticationError } from '../../../../../shared/core/AuthenticationError';
6import { CollectionAccessType } from '../../../domain/Collection';
7
8export class UpdateCollectionController extends Controller {
9 constructor(private updateCollectionUseCase: UpdateCollectionUseCase) {
10 super();
11 }
12
13 async executeImpl(req: AuthenticatedRequest, res: Response): Promise<any> {
14 try {
15 const { collectionId } = req.params;
16 const { name, description, accessType } = req.body;
17 const curatorId = req.did;
18
19 if (!curatorId) {
20 return this.unauthorized(res);
21 }
22
23 if (!collectionId) {
24 return this.badRequest(res, 'Collection ID is required');
25 }
26
27 if (!name) {
28 return this.badRequest(res, 'Collection name is required');
29 }
30
31 // Validate accessType if provided
32 if (
33 accessType !== undefined &&
34 !Object.values(CollectionAccessType).includes(
35 accessType as CollectionAccessType,
36 )
37 ) {
38 return this.badRequest(
39 res,
40 'Invalid accessType. Must be OPEN or CLOSED',
41 );
42 }
43
44 const result = await this.updateCollectionUseCase.execute({
45 collectionId,
46 name,
47 description,
48 accessType: accessType as CollectionAccessType | undefined,
49 curatorId,
50 });
51
52 if (result.isErr()) {
53 // Check if the error is an authentication error
54 if (result.error instanceof AuthenticationError) {
55 return this.unauthorized(res, result.error.message);
56 }
57 return this.fail(res, result.error);
58 }
59
60 return this.ok(res, result.value);
61 } catch (error: any) {
62 return this.handleError(res, error);
63 }
64 }
65}