forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook.controller.ts
More file actions
126 lines (115 loc) · 3.02 KB
/
Copy pathwebhook.controller.ts
File metadata and controls
126 lines (115 loc) · 3.02 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
import { Controller, Post, Get, Body, Param, Delete, UseGuards, Request, Query } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { WebhookService } from './webhook.service';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { IsUrl, IsOptional, IsNumber } from 'class-validator';
class RegisterWebhookDto {
@IsUrl()
url: string;
}
class GetDeliveriesDto {
@IsOptional()
@IsNumber()
limit?: number;
}
@ApiTags('webhooks')
@Controller('webhooks')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth('JWT-auth')
export class WebhookController {
constructor(private readonly webhookService: WebhookService) {}
@Post()
@ApiOperation({ summary: 'Register a new webhook URL' })
@ApiResponse({
status: 201,
description: 'Webhook registered successfully',
})
@ApiResponse({
status: 400,
description: 'Invalid URL or too many webhooks',
})
async registerWebhook(
@Request() req: any,
@Body() body: RegisterWebhookDto,
) {
const merchantId = req.user.merchantId;
const webhook = await this.webhookService.registerWebhook(
merchantId,
body.url,
);
return {
success: true,
message: 'Webhook registered successfully',
webhook: {
id: webhook.id,
url: webhook.url,
secret: webhook.secret,
isActive: webhook.isActive,
createdAt: webhook.createdAt,
},
};
}
@Get()
@ApiOperation({ summary: 'Get all webhooks for merchant' })
@ApiResponse({
status: 200,
description: 'Webhooks retrieved successfully',
})
async getWebhooks(@Request() req: any) {
const merchantId = req.user.merchantId;
const webhooks = await this.webhookService.getMerchantWebhooks(merchantId);
return {
success: true,
webhooks,
};
}
@Delete(':id')
@ApiOperation({ summary: 'Delete a webhook' })
@ApiResponse({
status: 200,
description: 'Webhook deleted successfully',
})
@ApiResponse({
status: 404,
description: 'Webhook not found',
})
async deleteWebhook(
@Param('id') webhookId: string,
@Request() req: any,
) {
const merchantId = req.user.merchantId;
await this.webhookService.deleteWebhook(webhookId, merchantId);
return {
success: true,
message: 'Webhook deleted successfully',
};
}
@Get(':id/deliveries')
@ApiOperation({ summary: 'Get delivery history for a webhook' })
@ApiResponse({
status: 200,
description: 'Delivery history retrieved successfully',
})
@ApiResponse({
status: 404,
description: 'Webhook not found',
})
async getDeliveryHistory(
@Param('id') webhookId: string,
@Request() req: any,
@Query() query: GetDeliveriesDto,
) {
const merchantId = req.user.merchantId;
const limit = query.limit || 50;
const deliveries = await this.webhookService.getDeliveryHistory(
webhookId,
merchantId,
limit,
);
return {
success: true,
deliveries,
count: deliveries.length,
};
}
}