This repository has no description
1.5 kB
49 lines
1import { Controller } from '../../../../../shared/infrastructure/http/Controller';
2import { Response } from 'express';
3import { UpdateNoteCardUseCase } from '../../../application/useCases/commands/UpdateNoteCardUseCase';
4import { AuthenticatedRequest } from '../../../../../shared/infrastructure/http/middleware/AuthMiddleware';
5import { AuthenticationError } from '../../../../../shared/core/AuthenticationError';
6
7export class UpdateNoteCardController extends Controller {
8 constructor(private updateNoteCardUseCase: UpdateNoteCardUseCase) {
9 super();
10 }
11
12 async executeImpl(req: AuthenticatedRequest, res: Response): Promise<any> {
13 try {
14 const { cardId } = req.params;
15 const { note } = req.body;
16 const curatorId = req.did;
17
18 if (!curatorId) {
19 return this.unauthorized(res);
20 }
21
22 if (!cardId) {
23 return this.badRequest(res, 'Card ID is required');
24 }
25
26 if (!note) {
27 return this.badRequest(res, 'Note text is required');
28 }
29
30 const result = await this.updateNoteCardUseCase.execute({
31 cardId,
32 note,
33 curatorId,
34 });
35
36 if (result.isErr()) {
37 // Check if the error is an authentication error
38 if (result.error instanceof AuthenticationError) {
39 return this.unauthorized(res, result.error.message);
40 }
41 return this.fail(res, result.error);
42 }
43
44 return this.ok(res, result.value);
45 } catch (error: any) {
46 return this.handleError(res, error);
47 }
48 }
49}