-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathintent.controller.ts
More file actions
executable file
·604 lines (581 loc) · 19 KB
/
intent.controller.ts
File metadata and controls
executable file
·604 lines (581 loc) · 19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
import {
ApiBearerAuth,
ApiForbiddenResponse,
ApiOperation,
ApiParam,
ApiQuery,
ApiResponse,
ApiTags,
ApiUnauthorizedResponse
} from '@nestjs/swagger';
import {
BadRequestException,
Body,
Controller,
Delete,
Get,
HttpStatus,
Param,
ParseUUIDPipe,
Post,
Put,
Query,
Res,
UseFilters,
UseGuards
} from '@nestjs/common';
import type { user as PrismaUser } from '@prisma/client';
import { ApiResponseDto } from '../../dtos/apiResponse.dto';
import { Response } from 'express';
import { AuthGuard } from '@nestjs/passport';
import { CustomExceptionFilter } from 'apps/api-gateway/common/exception-handler';
import { IResponse } from '@credebl/common/interfaces/response.interface';
import { OrgRoles } from 'libs/org-roles/enums';
import { ResponseMessages } from '@credebl/common/response-messages';
import { Roles } from '../../authz/decorators/roles.decorator';
import { UnauthorizedErrorDto } from '../../dtos/unauthorized-error.dto';
import { EcosystemRolesGuard } from '../../authz/guards/ecosystem-roles.guard';
import { User } from '../../authz/decorators/user.decorator';
import { IIntentTemplateList } from '@credebl/common/interfaces/intents-template.interface';
import { CreateIntentDto } from 'apps/ecosystem/dtos/create-intent.dto';
import { UpdateIntentDto } from 'apps/ecosystem/dtos/update-intent.dto';
import { GetAllIntentTemplatesResponseDto } from '../../utilities/dtos/get-all-intent-templates-response.dto';
import { GetAllIntentTemplatesDto } from '../../utilities/dtos/get-all-intent-templates.dto';
import { CreateIntentTemplateDto, UpdateIntentTemplateDto } from '../../utilities/dtos/intent-template.dto';
import { EcosystemFeatureGuard } from '../../authz/guards/ecosystem-feature-guard';
import { PaginationDto } from '@credebl/common/dtos/pagination.dto';
import { TrimStringParamPipe } from '@credebl/common/cast.helper';
import { EcosystemService } from '../ecosystem.service';
import { ForbiddenErrorDto } from '../../dtos/forbidden-error.dto';
@UseFilters(CustomExceptionFilter)
@Controller('intent')
@ApiTags('intent')
@UseGuards(EcosystemFeatureGuard)
@ApiUnauthorizedResponse({
description: 'Unauthorized',
type: UnauthorizedErrorDto
})
@ApiForbiddenResponse({
description: 'Forbidden',
type: ForbiddenErrorDto
})
export class IntentController {
constructor(private readonly ecosystemService: EcosystemService) {}
/**
* Create Intent
* @param createIntentDto
* @returns Created intent
*/
@Post('/ecosystem/:ecosystemId')
@Roles(OrgRoles.ECOSYSTEM_LEAD)
@UseGuards(AuthGuard('jwt'), EcosystemRolesGuard)
@ApiBearerAuth()
@ApiOperation({
summary: 'Create Intent',
description: 'Creates a new intent within the specified ecosystem.'
})
@ApiResponse({
status: HttpStatus.CREATED,
description: 'Intent created successfully',
type: ApiResponseDto
})
async createIntent(
@Body() createIntentDto: CreateIntentDto,
@Param(
'ecosystemId',
TrimStringParamPipe,
new ParseUUIDPipe({
exceptionFactory: (): Error => {
throw new BadRequestException(ResponseMessages.ecosystem.error.invalidFormatOfEcosystemId);
}
})
)
ecosystemId: string,
@User() user: PrismaUser,
@Res() res: Response
): Promise<Response> {
createIntentDto.ecosystemId = ecosystemId;
createIntentDto.userId = user?.id;
const intent = await this.ecosystemService.createIntent(createIntentDto);
const finalResponse: IResponse = {
statusCode: HttpStatus.CREATED,
message: ResponseMessages.ecosystem.success.intentCreated,
data: intent
};
return res.status(HttpStatus.CREATED).json(finalResponse);
}
@Get('/ecosystem/:ecosystemId')
@ApiBearerAuth()
@Roles(OrgRoles.ECOSYSTEM_LEAD)
@UseGuards(AuthGuard('jwt'), EcosystemRolesGuard)
@ApiOperation({
summary: 'Get intents by ecosystem',
description: 'Retrieves all intents associated with a specific ecosystem.'
})
@ApiQuery({
name: 'intentId',
required: false,
type: String
})
@ApiResponse({
status: HttpStatus.OK,
description: 'Intents fetched successfully',
type: ApiResponseDto
})
async getIntents(
@Res() res: Response,
@Param(
'ecosystemId',
TrimStringParamPipe,
new ParseUUIDPipe({
exceptionFactory: (): Error => {
throw new BadRequestException(ResponseMessages.ecosystem.error.invalidFormatOfEcosystemId);
}
})
)
ecosystemId: string,
@Query() pageDto: PaginationDto,
@Query(
'intentId',
new ParseUUIDPipe({
optional: true,
exceptionFactory: (): Error => {
throw new BadRequestException(ResponseMessages.ecosystem.error.invalidFormatOfIntentId);
}
})
)
intentId?: string
): Promise<Response> {
const intents = await this.ecosystemService.getIntents(ecosystemId, pageDto, intentId?.trim());
return res.status(HttpStatus.OK).json({
statusCode: HttpStatus.OK,
message: ResponseMessages.ecosystem.success.fetchIntents,
data: intents
});
}
/**
* Update intent
* @param id Intent ID
* @param updateIntentDto
* @returns Updated intent
*/
@Put('/ecosystem/:ecosystemId/:intentId')
@ApiBearerAuth()
@Roles(OrgRoles.ECOSYSTEM_LEAD)
@UseGuards(AuthGuard('jwt'), EcosystemRolesGuard)
@ApiOperation({
summary: 'Update Intent',
description: 'Updates an existing intent within the specified ecosystem.'
})
@ApiResponse({
status: HttpStatus.OK,
description: 'Intent updated successfully',
type: ApiResponseDto
})
async updateIntent(
@Param(
'ecosystemId',
TrimStringParamPipe,
new ParseUUIDPipe({
exceptionFactory: (): Error => {
throw new BadRequestException(ResponseMessages.ecosystem.error.invalidFormatOfEcosystemId);
}
})
)
ecosystemId: string,
@Param(
'intentId',
TrimStringParamPipe,
new ParseUUIDPipe({
exceptionFactory: (): Error => {
throw new BadRequestException(ResponseMessages.ecosystem.error.invalidFormatOfIntentId);
}
})
)
intentId: string,
@Body() updateIntentDto: UpdateIntentDto,
@User() user: PrismaUser,
@Res() res: Response
): Promise<Response> {
updateIntentDto.userId = user?.id;
updateIntentDto.intentId = intentId;
updateIntentDto.ecosystemId = ecosystemId;
const intent = await this.ecosystemService.updateIntent(updateIntentDto);
const finalResponse: IResponse = {
statusCode: HttpStatus.OK,
message: ResponseMessages.ecosystem.success.intentUpdated,
data: intent
};
return res.status(HttpStatus.OK).json(finalResponse);
}
/**
* Delete intent
* @param id Intent ID
* @returns Deleted intent
*/
@Delete('/ecosystem/:ecosystemId/:intentId')
@ApiBearerAuth()
@Roles(OrgRoles.ECOSYSTEM_LEAD)
@UseGuards(AuthGuard('jwt'), EcosystemRolesGuard)
@ApiOperation({
summary: 'Delete Intent',
description: 'Deletes an intent from the specified ecosystem.'
})
@ApiResponse({
status: HttpStatus.OK,
description: 'Intent deleted successfully',
type: ApiResponseDto
})
async deleteIntent(
@Param(
'ecosystemId',
TrimStringParamPipe,
new ParseUUIDPipe({
exceptionFactory: (): Error => {
throw new BadRequestException(ResponseMessages.ecosystem.error.invalidFormatOfEcosystemId);
}
})
)
ecosystemId: string,
@Param(
'intentId',
TrimStringParamPipe,
new ParseUUIDPipe({
exceptionFactory: (): Error => {
throw new BadRequestException(ResponseMessages.ecosystem.error.invalidFormatOfIntentId);
}
})
)
intentId: string,
@User() user: PrismaUser,
@Res() res: Response
): Promise<Response> {
const intent = await this.ecosystemService.deleteIntent(ecosystemId, intentId, user.id);
return res.status(HttpStatus.OK).json({
statusCode: HttpStatus.OK,
message: ResponseMessages.ecosystem.success.deleteIntent,
data: intent
});
}
// verification template details by org Id
/**
* Get template details by org ID
*/
@Get('/org/:orgId/verification-templates')
@Roles(OrgRoles.ECOSYSTEM_LEAD, OrgRoles.OWNER)
@UseGuards(AuthGuard('jwt'), EcosystemRolesGuard)
@ApiBearerAuth()
@ApiOperation({
summary: 'Get template details by orgId',
description: 'Retrieve verification template details by orgId'
})
@ApiParam({
name: 'orgId',
required: true,
description: 'Organization ID'
})
@ApiResponse({
status: HttpStatus.OK,
description: 'Template details fetched successfully'
})
async getTemplateByIntentId(
@Param(
'orgId',
TrimStringParamPipe,
new ParseUUIDPipe({
exceptionFactory: (): Error => {
throw new BadRequestException(ResponseMessages.ecosystem.error.invalidOrgId);
}
})
)
orgId: string,
@Res() res: Response,
@Query() pageDto: PaginationDto
): Promise<Response> {
const templates = await this.ecosystemService.getVerificationTemplates(orgId, pageDto);
return res.status(HttpStatus.OK).json({
statusCode: HttpStatus.OK,
message: ResponseMessages.ecosystem.success.fetchVerificationTemplates,
data: templates
});
}
// Intent Template CRUD operations
/**
* Create a new intent template mapping
* @param createIntentTemplateDto The intent template mapping details
* @param res The response object
* @returns The created intent template mapping
*/
@Post('/template')
@ApiBearerAuth()
@Roles(OrgRoles.ECOSYSTEM_LEAD)
@UseGuards(AuthGuard('jwt'), EcosystemRolesGuard)
@ApiOperation({ summary: 'Create intent template', description: 'Creates a new intent template mapping.' })
@ApiResponse({
status: HttpStatus.CREATED,
description: 'Intent template created successfully',
type: ApiResponseDto
})
async createIntentTemplate(
@Body() createIntentTemplateDto: CreateIntentTemplateDto,
@User() user: PrismaUser,
@Res() res: Response
): Promise<Response> {
const intentTemplate = await this.ecosystemService.createIntentTemplate(createIntentTemplateDto, user);
const finalResponse: IResponse = {
statusCode: HttpStatus.CREATED,
message: 'Intent template created successfully',
data: intentTemplate
};
return res.status(HttpStatus.CREATED).json(finalResponse);
}
/**
* Get all intent templates
* @param res The response object
* @returns List of all intent templates
*/
@Get('/templates')
@ApiBearerAuth()
@Roles(OrgRoles.ECOSYSTEM_LEAD)
@UseGuards(AuthGuard('jwt'), EcosystemRolesGuard)
@ApiOperation({
summary: 'Get All Intent Templates',
description: 'Retrieves a list of all available intent templates.'
})
@ApiResponse({
status: HttpStatus.OK,
description: 'Intent templates retrieved successfully',
type: GetAllIntentTemplatesResponseDto
})
async getAllIntentTemplates(
@Query() intentTemplateSearchCriteria: GetAllIntentTemplatesDto,
@Res() res: Response
): Promise<Response> {
const intentTemplates: IIntentTemplateList =
await this.ecosystemService.getAllIntentTemplatesByQuery(intentTemplateSearchCriteria);
const finalResponse: IResponse = {
statusCode: HttpStatus.OK,
message: 'Intent templates retrieved successfully',
data: intentTemplates
};
return res.status(HttpStatus.OK).json(finalResponse);
}
/**
* Get intent template by intent name and verifier organization ID
* @param body The intent name and verifier organization ID
* @param res The response object
* @returns The intent template details (org-specific if exists, otherwise global)
*/
@Get('/:intentName/org/:orgId/templates')
@ApiBearerAuth()
@Roles(OrgRoles.ECOSYSTEM_LEAD)
@UseGuards(AuthGuard('jwt'), EcosystemRolesGuard)
@ApiOperation({
summary: 'Get Intent Template by Intent and Organization',
description:
'Retrieves the template mapped to a specific intent and organization. Returns organization-specific template if available, otherwise default template.'
})
@ApiResponse({ status: HttpStatus.OK, description: 'Intent template retrieved successfully', type: ApiResponseDto })
async getIntentTemplateByIntentAndOrg(
@Param('intentName') intentName: string,
@Param(
'orgId',
new ParseUUIDPipe({
exceptionFactory: (): Error => {
throw new BadRequestException(ResponseMessages.organisation.error.invalidOrgId);
}
})
)
orgId: string,
@Res() res: Response
): Promise<Response> {
const intentTemplate = await this.ecosystemService.getIntentTemplateByIntentAndOrg(intentName, orgId);
const finalResponse: IResponse = {
statusCode: HttpStatus.OK,
message: intentTemplate ? 'Intent template retrieved successfully' : 'No intent template found',
data: intentTemplate
};
return res.status(HttpStatus.OK).json(finalResponse);
}
/**
* Get intent templates by intent ID
* @param intentId The intent ID
* @param res The response object
* @returns List of intent templates for the intent
*/
@Get('/:intentId/templates')
@ApiBearerAuth()
@Roles(OrgRoles.ECOSYSTEM_LEAD)
@UseGuards(AuthGuard('jwt'), EcosystemRolesGuard)
@ApiOperation({
summary: 'Get templates by intent',
description: 'Retrieves all templates associated with a specific intent.'
})
@ApiResponse({ status: HttpStatus.OK, description: 'Intent templates retrieved successfully', type: ApiResponseDto })
async getIntentTemplatesByIntentId(
@Param(
'intentId',
TrimStringParamPipe,
new ParseUUIDPipe({
exceptionFactory: (): Error => {
throw new BadRequestException('Invalid intent ID format');
}
})
)
intentId: string,
@Res() res: Response
): Promise<Response> {
const intentTemplates = await this.ecosystemService.getIntentTemplatesByIntentId(intentId);
const finalResponse: IResponse = {
statusCode: HttpStatus.OK,
message: 'Intent templates retrieved successfully',
data: intentTemplates
};
return res.status(HttpStatus.OK).json(finalResponse);
}
/**
* Get intent templates by organization ID
* @param orgId The organization ID
* @param res The response object
* @returns List of intent templates for the organization
*/
@Get('/org/:orgId/templates/')
@ApiBearerAuth()
@Roles(OrgRoles.ECOSYSTEM_LEAD, OrgRoles.OWNER)
@UseGuards(AuthGuard('jwt'), EcosystemRolesGuard)
@ApiOperation({
summary: 'Get templates by organization',
description: 'Retrieves all templates associated with a specific organization.'
})
@ApiParam({
name: 'orgId',
required: true,
description: 'Organization ID'
})
@ApiResponse({ status: HttpStatus.OK, description: 'Intent templates retrieved successfully', type: ApiResponseDto })
async getIntentTemplatesByOrgId(
@Param(
'orgId',
new ParseUUIDPipe({
exceptionFactory: (): Error => {
throw new BadRequestException('Invalid orgId format');
}
})
)
orgId: string,
@Res() res: Response
): Promise<Response> {
const intentTemplates = await this.ecosystemService.getIntentTemplatesByOrgId(orgId);
const finalResponse: IResponse = {
statusCode: HttpStatus.OK,
message: 'Intent templates retrieved successfully',
data: intentTemplates
};
return res.status(HttpStatus.OK).json(finalResponse);
}
/**
* Get intent template by ID
* @param id The intent template ID
* @param res The response object
* @returns The intent template details
*/
@Get('/template/:id')
@ApiBearerAuth()
@Roles(OrgRoles.ECOSYSTEM_LEAD)
@UseGuards(AuthGuard('jwt'), EcosystemRolesGuard)
@ApiOperation({
summary: 'Get template by ID',
description: 'Retrieves details of a specific intent template using its unique identifier.'
})
@ApiResponse({ status: HttpStatus.OK, description: 'Intent template retrieved successfully', type: ApiResponseDto })
async getIntentTemplateById(
@Param(
'id',
TrimStringParamPipe,
new ParseUUIDPipe({
exceptionFactory: (): Error => {
throw new BadRequestException(ResponseMessages.oid4vpIntentToTemplate.error.invalidId);
}
})
)
id: string,
@Res() res: Response
): Promise<Response> {
const intentTemplate = await this.ecosystemService.getIntentTemplateById(id);
const finalResponse: IResponse = {
statusCode: HttpStatus.OK,
message: 'Intent template retrieved successfully',
data: intentTemplate
};
return res.status(HttpStatus.OK).json(finalResponse);
}
/**
* Update intent template
* @param id The intent template ID
* @param updateIntentTemplateDto The updated intent template details
* @param res The response object
* @returns The updated intent template
*/
@Put('/template/:id')
@ApiBearerAuth()
@Roles(OrgRoles.ECOSYSTEM_LEAD)
@UseGuards(AuthGuard('jwt'), EcosystemRolesGuard)
@ApiOperation({ summary: 'Update Intent Template', description: 'Updates an existing intent template mapping.' })
@ApiResponse({ status: HttpStatus.OK, description: 'Intent template updated successfully', type: ApiResponseDto })
async updateIntentTemplate(
@Param(
'id',
TrimStringParamPipe,
new ParseUUIDPipe({
exceptionFactory: (): Error => {
throw new BadRequestException(ResponseMessages.oid4vpIntentToTemplate.error.invalidId);
}
})
)
id: string,
@Body() updateIntentTemplateDto: UpdateIntentTemplateDto,
@User() user: PrismaUser,
@Res() res: Response
): Promise<Response> {
const intentTemplate = await this.ecosystemService.updateIntentTemplate(id, updateIntentTemplateDto, user);
const finalResponse: IResponse = {
statusCode: HttpStatus.OK,
message: 'Intent template updated successfully',
data: intentTemplate
};
return res.status(HttpStatus.OK).json(finalResponse);
}
/**
* Delete intent template
* @param id The intent template ID
* @param res The response object
* @returns The deleted intent template
*/
@Delete('/template/:id')
@ApiBearerAuth()
@Roles(OrgRoles.ECOSYSTEM_LEAD)
@UseGuards(AuthGuard('jwt'), EcosystemRolesGuard)
@ApiOperation({ summary: 'Delete Intent Template', description: 'Deletes an existing intent template mapping.' })
@ApiResponse({ status: HttpStatus.OK, description: 'Intent template deleted successfully', type: ApiResponseDto })
async deleteIntentTemplate(
@Param(
'id',
TrimStringParamPipe,
new ParseUUIDPipe({
exceptionFactory: (): Error => {
throw new BadRequestException(ResponseMessages.oid4vpIntentToTemplate.error.invalidId);
}
})
)
id: string,
@Res() res: Response
): Promise<Response> {
const intentTemplate = await this.ecosystemService.deleteIntentTemplate(id);
const finalResponse: IResponse = {
statusCode: HttpStatus.OK,
message: 'Intent template deleted successfully',
data: intentTemplate
};
return res.status(HttpStatus.OK).json(finalResponse);
}
}