forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhooks.controller.ts
More file actions
182 lines (170 loc) · 4.94 KB
/
Copy pathwebhooks.controller.ts
File metadata and controls
182 lines (170 loc) · 4.94 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
import {
Controller,
Get,
Post,
Body,
Patch,
Param,
Delete,
Query,
HttpCode,
HttpStatus,
ValidationPipe,
} from '@nestjs/common';
import {
ApiTags,
ApiOperation,
ApiResponse,
ApiParam,
ApiQuery,
} from '@nestjs/swagger';
import { WebhooksService } from './webhooks.service';
import { WebhookDeliveryService } from './webhook-delivery.service';
import { CreateWebhookDto } from './dto/create-webhook.dto';
import { UpdateWebhookDto } from './dto/update-webhook.dto';
import { TestWebhookDto } from './dto/test-webhook.dto';
import { Webhook } from './webhook.entity';
import { WebhookDelivery } from './webhook-delivery.entity';
@ApiTags('Webhooks')
@Controller('webhooks')
export class WebhooksController {
constructor(
private readonly webhooksService: WebhooksService,
private readonly deliveryService: WebhookDeliveryService,
) {}
@Post()
@HttpCode(HttpStatus.CREATED)
@ApiOperation({ summary: 'Create a new webhook' })
@ApiResponse({
status: 201,
description: 'Webhook created successfully',
type: Webhook,
})
@ApiResponse({ status: 400, description: 'Invalid webhook data' })
async create(@Body(ValidationPipe) createWebhookDto: CreateWebhookDto) {
return await this.webhooksService.create(createWebhookDto);
}
@Get()
@ApiOperation({ summary: 'Get all webhooks' })
@ApiQuery({
name: 'userId',
required: false,
description: 'Filter webhooks by user ID',
})
@ApiResponse({
status: 200,
description: 'List of webhooks',
type: [Webhook],
})
async findAll(@Query('userId') userId?: string) {
return await this.webhooksService.findAll(userId);
}
@Get(':id')
@ApiOperation({ summary: 'Get a webhook by ID' })
@ApiParam({ name: 'id', description: 'Webhook ID' })
@ApiResponse({
status: 200,
description: 'Webhook details',
type: Webhook,
})
@ApiResponse({ status: 404, description: 'Webhook not found' })
async findOne(@Param('id') id: string) {
return await this.webhooksService.findOne(id);
}
@Patch(':id')
@ApiOperation({ summary: 'Update a webhook' })
@ApiParam({ name: 'id', description: 'Webhook ID' })
@ApiResponse({
status: 200,
description: 'Webhook updated successfully',
type: Webhook,
})
@ApiResponse({ status: 404, description: 'Webhook not found' })
async update(
@Param('id') id: string,
@Body(ValidationPipe) updateWebhookDto: UpdateWebhookDto,
) {
return await this.webhooksService.update(id, updateWebhookDto);
}
@Delete(':id')
@HttpCode(HttpStatus.NO_CONTENT)
@ApiOperation({ summary: 'Delete a webhook' })
@ApiParam({ name: 'id', description: 'Webhook ID' })
@ApiResponse({ status: 204, description: 'Webhook deleted successfully' })
@ApiResponse({ status: 404, description: 'Webhook not found' })
async remove(@Param('id') id: string) {
await this.webhooksService.remove(id);
}
@Post(':id/test')
@HttpCode(HttpStatus.OK)
@ApiOperation({ summary: 'Test a webhook with a sample event' })
@ApiParam({ name: 'id', description: 'Webhook ID' })
@ApiResponse({
status: 200,
description: 'Test webhook triggered successfully',
})
@ApiResponse({ status: 404, description: 'Webhook not found' })
async testWebhook(
@Param('id') id: string,
@Body(ValidationPipe) testDto: TestWebhookDto,
) {
const webhook = await this.webhooksService.findOne(id);
// Use the delivery service to trigger a test event
const payload = testDto.payload || {
test: true,
message: 'This is a test webhook',
timestamp: new Date().toISOString(),
};
await this.deliveryService.triggerEvent(
testDto.eventType,
payload,
webhook.userId,
);
return {
message: 'Test webhook triggered',
webhookId: id,
eventType: testDto.eventType,
};
}
@Get(':id/deliveries')
@ApiOperation({ summary: 'Get delivery logs for a webhook' })
@ApiParam({ name: 'id', description: 'Webhook ID' })
@ApiQuery({
name: 'limit',
required: false,
description: 'Maximum number of deliveries to return',
type: Number,
})
@ApiResponse({
status: 200,
description: 'List of webhook deliveries',
type: [WebhookDelivery],
})
async getDeliveries(
@Param('id') id: string,
@Query('limit') limit?: number,
) {
await this.webhooksService.findOne(id); // Verify webhook exists
return await this.deliveryService.getDeliveryLogs(id, limit);
}
@Get(':id/stats')
@ApiOperation({ summary: 'Get delivery statistics for a webhook' })
@ApiParam({ name: 'id', description: 'Webhook ID' })
@ApiResponse({
status: 200,
description: 'Webhook delivery statistics',
schema: {
example: {
total: 100,
success: 95,
failed: 5,
pending: 0,
successRate: 95.0,
},
},
})
async getStats(@Param('id') id: string) {
await this.webhooksService.findOne(id); // Verify webhook exists
return await this.deliveryService.getDeliveryStats(id);
}
}