|
| 1 | +import { Request, Response } from "express"; |
| 2 | +import z from "zod"; |
| 3 | +import * as webhookSchema from "../schemas/webhook.schema"; |
| 4 | +import { |
| 5 | + getWebhookLogsService, |
| 6 | + getWebhookLogDetailsService, |
| 7 | + retryWebhookService, |
| 8 | + sendTestWebhookService, |
| 9 | +} from "../services/webhook.service"; |
| 10 | +import { AuthRequest } from "../types/express"; |
| 11 | +import { validateUserId } from "../helpers/request.helper"; |
| 12 | + |
| 13 | +type GetWebhookLogsQuery = z.infer<typeof webhookSchema.getWebhookLogsSchema>; |
| 14 | +type SendTestWebhookBody = z.infer<typeof webhookSchema.sendTestWebhookSchema>; |
| 15 | + |
| 16 | +export async function getWebhookLogs(req: AuthRequest, res: Response) { |
| 17 | + try { |
| 18 | + const merchantId = await validateUserId(req); |
| 19 | + const query = req.query as unknown as GetWebhookLogsQuery; |
| 20 | + |
| 21 | + const result = await getWebhookLogsService({ |
| 22 | + merchantId, |
| 23 | + event_type: query.event_type as any, |
| 24 | + status: query.status as any, |
| 25 | + date_from: query.date_from, |
| 26 | + date_to: query.date_to, |
| 27 | + search: query.search, |
| 28 | + page: Number(query.page) || 1, |
| 29 | + limit: Number(query.limit) || 10, |
| 30 | + }); |
| 31 | + |
| 32 | + res.status(200).json(result); |
| 33 | + } catch (err: any) { |
| 34 | + console.error(err); |
| 35 | + res.status(err.status || 500).json({ message: err.message || "Server error" }); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +export async function getWebhookLogDetails(req: AuthRequest, res: Response) { |
| 40 | + try { |
| 41 | + const merchantId = await validateUserId(req); |
| 42 | + const { log_id } = req.params; |
| 43 | + |
| 44 | + if (!log_id || Array.isArray(log_id)) { |
| 45 | + return res.status(400).json({ message: "Log ID is required" }); |
| 46 | + } |
| 47 | + |
| 48 | + const result = await getWebhookLogDetailsService({ |
| 49 | + merchantId, |
| 50 | + log_id, |
| 51 | + }); |
| 52 | + |
| 53 | + res.status(200).json(result); |
| 54 | + } catch (err: any) { |
| 55 | + console.error(err); |
| 56 | + res.status(err.status || 500).json({ message: err.message || "Server error" }); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +export async function retryWebhook(req: AuthRequest, res: Response) { |
| 61 | + try { |
| 62 | + const merchantId = await validateUserId(req); |
| 63 | + const { log_id } = req.params; |
| 64 | + |
| 65 | + if (!log_id || Array.isArray(log_id)) { |
| 66 | + return res.status(400).json({ message: "Log ID is required" }); |
| 67 | + } |
| 68 | + |
| 69 | + const result = await retryWebhookService({ |
| 70 | + merchantId, |
| 71 | + log_id, |
| 72 | + }); |
| 73 | + |
| 74 | + res.status(200).json(result); |
| 75 | + } catch (err: any) { |
| 76 | + console.error(err); |
| 77 | + res.status(err.status || 500).json({ message: err.message || "Server error" }); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +export async function sendTestWebhook(req: AuthRequest, res: Response) { |
| 82 | + try { |
| 83 | + const merchantId = await validateUserId(req); |
| 84 | + const body = req.body as SendTestWebhookBody; |
| 85 | + |
| 86 | + const result = await sendTestWebhookService({ |
| 87 | + merchantId, |
| 88 | + event_type: body.event_type as any, |
| 89 | + endpoint_url: body.endpoint_url, |
| 90 | + payload_override: body.payload_override, |
| 91 | + }); |
| 92 | + |
| 93 | + res.status(200).json(result); |
| 94 | + } catch (err: any) { |
| 95 | + console.error(err); |
| 96 | + res.status(err.status || 500).json({ message: err.message || "Server error" }); |
| 97 | + } |
| 98 | +} |
0 commit comments