@@ -14,57 +14,57 @@ import {
14
14
} from "../store/errors" ;
15
15
import { resendWebhook } from "../webhooks/cannon" ;
16
16
import sql from "sql-template-strings" ;
17
- import { WebhookResponse } from "../schema/types" ;
17
+ import { WebhookLog } from "../schema/types" ;
18
18
import { DBWebhook } from "../store/webhook-table" ;
19
19
import { Request , Router } from "express" ;
20
20
21
21
const app = Router ( { mergeParams : true } ) ;
22
22
23
23
const requestsFieldsMap : FieldsMap = {
24
- id : `webhook_response .ID` ,
25
- createdAt : { val : `webhook_response .data->'createdAt'` , type : "int" } ,
26
- userId : `webhook_response .data->>'userId'` ,
27
- event : `webhook_response .data->>'event'` ,
28
- statusCode : `webhook_response .data->'response'->>'status'` ,
24
+ id : `webhook_log .ID` ,
25
+ createdAt : { val : `webhook_log .data->'createdAt'` , type : "int" } ,
26
+ userId : `webhook_log .data->>'userId'` ,
27
+ event : `webhook_log .data->>'event'` ,
28
+ statusCode : `webhook_log .data->'response'->>'status'` ,
29
29
} ;
30
30
31
31
app . post ( "/:requestId/resend" , authorizer ( { } ) , async ( req , res ) => {
32
32
const webhook = await db . webhook . get ( req . params . id ) ;
33
- const webhookResponse = await db . webhookResponse . get ( req . params . requestId ) ;
34
- await checkRequest ( req , webhook , webhookResponse ) ;
33
+ const webhookLog = await db . webhookLog . get ( req . params . requestId ) ;
34
+ await checkRequest ( req , webhook , webhookLog ) ;
35
35
36
- const resent = await resendWebhook ( webhook , webhookResponse ) ;
36
+ const resent = await resendWebhook ( webhook , webhookLog ) ;
37
37
res . status ( 200 ) ;
38
- return res . json ( db . webhookResponse . cleanWriteOnlyResponse ( resent ) ) ;
38
+ return res . json ( db . webhookLog . cleanWriteOnlyResponse ( resent ) ) ;
39
39
} ) ;
40
40
41
41
app . get ( "/:requestId" , authorizer ( { } ) , async ( req , res ) => {
42
42
const webhook = await db . webhook . get ( req . params . id ) ;
43
- const webhookResponse = await db . webhookResponse . get ( req . params . requestId ) ;
44
- await checkRequest ( req , webhook , webhookResponse ) ;
43
+ const webhookLog = await db . webhookLog . get ( req . params . requestId ) ;
44
+ await checkRequest ( req , webhook , webhookLog ) ;
45
45
46
46
res . status ( 200 ) ;
47
- return res . json ( db . webhookResponse . cleanWriteOnlyResponse ( webhookResponse ) ) ;
47
+ return res . json ( db . webhookLog . cleanWriteOnlyResponse ( webhookLog ) ) ;
48
48
} ) ;
49
49
50
50
async function checkRequest (
51
51
req : Request ,
52
52
webhook : DBWebhook ,
53
- webhookResponse : WebhookResponse
53
+ webhookLog : WebhookLog
54
54
) {
55
55
if ( ! webhook || webhook . deleted ) {
56
56
throw new NotFoundError ( `webhook not found` ) ;
57
57
}
58
- if ( ! webhookResponse || webhookResponse . deleted ) {
58
+ if ( ! webhookLog || webhookLog . deleted ) {
59
59
throw new NotFoundError ( `webhook log not found` ) ;
60
60
}
61
61
if (
62
62
! req . user . admin &&
63
- ( req . user . id !== webhook . userId || req . user . id !== webhookResponse . userId )
63
+ ( req . user . id !== webhook . userId || req . user . id !== webhookLog . userId )
64
64
) {
65
65
throw new ForbiddenError ( `invalid user` ) ;
66
66
}
67
- if ( webhookResponse . webhookId !== webhook . id ) {
67
+ if ( webhookLog . webhookId !== webhook . id ) {
68
68
throw new BadRequestError ( `mismatch between webhook and webhook log` ) ;
69
69
}
70
70
}
@@ -82,18 +82,18 @@ app.get("/", authorizer({}), async (req, res) => {
82
82
83
83
if ( req . user . admin && allUsers && allUsers !== "false" ) {
84
84
const query = parseFilters ( requestsFieldsMap , filters ) ;
85
- query . push ( sql `webhook_response .data->>'webhookId' = ${ req . params . id } ` ) ;
85
+ query . push ( sql `webhook_log .data->>'webhookId' = ${ req . params . id } ` ) ;
86
86
if ( ! all || all === "false" ) {
87
- query . push ( sql `webhook_response .data->>'deleted' IS NULL` ) ;
87
+ query . push ( sql `webhook_log .data->>'deleted' IS NULL` ) ;
88
88
}
89
89
90
90
let fields =
91
- " webhook_response .id as id, webhook_response .data as data, users.id as usersId, users.data as usersdata" ;
91
+ " webhook_log .id as id, webhook_log .data as data, users.id as usersId, users.data as usersdata" ;
92
92
if ( count ) {
93
93
fields = fields + ", count(*) OVER() AS count" ;
94
94
}
95
- const from = `webhook_response left join users on webhook_response .data->>'userId' = users.id` ;
96
- const [ output , newCursor ] = await db . webhookResponse . find ( query , {
95
+ const from = `webhook_log left join users on webhook_log .data->>'userId' = users.id` ;
96
+ const [ output , newCursor ] = await db . webhookLog . find ( query , {
97
97
limit,
98
98
cursor,
99
99
fields,
@@ -116,19 +116,19 @@ app.get("/", authorizer({}), async (req, res) => {
116
116
}
117
117
118
118
const query = parseFilters ( requestsFieldsMap , filters ) ;
119
- query . push ( sql `webhook_response .data->>'userId' = ${ req . user . id } ` ) ;
120
- query . push ( sql `webhook_response .data->>'webhookId' = ${ req . params . id } ` ) ;
119
+ query . push ( sql `webhook_log .data->>'userId' = ${ req . user . id } ` ) ;
120
+ query . push ( sql `webhook_log .data->>'webhookId' = ${ req . params . id } ` ) ;
121
121
122
122
if ( ! all || all === "false" || ! req . user . admin ) {
123
- query . push ( sql `webhook_response .data->>'deleted' IS NULL` ) ;
123
+ query . push ( sql `webhook_log .data->>'deleted' IS NULL` ) ;
124
124
}
125
125
126
- let fields = " webhook_response .id as id, webhook_response .data as data" ;
126
+ let fields = " webhook_log .id as id, webhook_log .data as data" ;
127
127
if ( count ) {
128
128
fields = fields + ", count(*) OVER() AS count" ;
129
129
}
130
- const from = `webhook_response ` ;
131
- const [ output , newCursor ] = await db . webhookResponse . find ( query , {
130
+ const from = `webhook_log ` ;
131
+ const [ output , newCursor ] = await db . webhookLog . find ( query , {
132
132
limit,
133
133
cursor,
134
134
fields,
@@ -148,7 +148,7 @@ app.get("/", authorizer({}), async (req, res) => {
148
148
res . links ( { next : makeNextHREF ( req , newCursor ) } ) ;
149
149
}
150
150
151
- return res . json ( db . webhookResponse . cleanWriteOnlyResponses ( output ) ) ;
151
+ return res . json ( db . webhookLog . cleanWriteOnlyResponses ( output ) ) ;
152
152
} ) ;
153
153
154
154
export default app ;
0 commit comments