Skip to content

Commit 921462b

Browse files
authored
Rename webhook response table (#2032)
* Rename webhook response table * rename variables
1 parent 6913261 commit 921462b

File tree

5 files changed

+61
-61
lines changed

5 files changed

+61
-61
lines changed

packages/api/src/controllers/webhook-log.ts

+29-29
Original file line numberDiff line numberDiff line change
@@ -14,57 +14,57 @@ import {
1414
} from "../store/errors";
1515
import { resendWebhook } from "../webhooks/cannon";
1616
import sql from "sql-template-strings";
17-
import { WebhookResponse } from "../schema/types";
17+
import { WebhookLog } from "../schema/types";
1818
import { DBWebhook } from "../store/webhook-table";
1919
import { Request, Router } from "express";
2020

2121
const app = Router({ mergeParams: true });
2222

2323
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'`,
2929
};
3030

3131
app.post("/:requestId/resend", authorizer({}), async (req, res) => {
3232
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);
3535

36-
const resent = await resendWebhook(webhook, webhookResponse);
36+
const resent = await resendWebhook(webhook, webhookLog);
3737
res.status(200);
38-
return res.json(db.webhookResponse.cleanWriteOnlyResponse(resent));
38+
return res.json(db.webhookLog.cleanWriteOnlyResponse(resent));
3939
});
4040

4141
app.get("/:requestId", authorizer({}), async (req, res) => {
4242
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);
4545

4646
res.status(200);
47-
return res.json(db.webhookResponse.cleanWriteOnlyResponse(webhookResponse));
47+
return res.json(db.webhookLog.cleanWriteOnlyResponse(webhookLog));
4848
});
4949

5050
async function checkRequest(
5151
req: Request,
5252
webhook: DBWebhook,
53-
webhookResponse: WebhookResponse
53+
webhookLog: WebhookLog
5454
) {
5555
if (!webhook || webhook.deleted) {
5656
throw new NotFoundError(`webhook not found`);
5757
}
58-
if (!webhookResponse || webhookResponse.deleted) {
58+
if (!webhookLog || webhookLog.deleted) {
5959
throw new NotFoundError(`webhook log not found`);
6060
}
6161
if (
6262
!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)
6464
) {
6565
throw new ForbiddenError(`invalid user`);
6666
}
67-
if (webhookResponse.webhookId !== webhook.id) {
67+
if (webhookLog.webhookId !== webhook.id) {
6868
throw new BadRequestError(`mismatch between webhook and webhook log`);
6969
}
7070
}
@@ -82,18 +82,18 @@ app.get("/", authorizer({}), async (req, res) => {
8282

8383
if (req.user.admin && allUsers && allUsers !== "false") {
8484
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}`);
8686
if (!all || all === "false") {
87-
query.push(sql`webhook_response.data->>'deleted' IS NULL`);
87+
query.push(sql`webhook_log.data->>'deleted' IS NULL`);
8888
}
8989

9090
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";
9292
if (count) {
9393
fields = fields + ", count(*) OVER() AS count";
9494
}
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, {
9797
limit,
9898
cursor,
9999
fields,
@@ -116,19 +116,19 @@ app.get("/", authorizer({}), async (req, res) => {
116116
}
117117

118118
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}`);
121121

122122
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`);
124124
}
125125

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";
127127
if (count) {
128128
fields = fields + ", count(*) OVER() AS count";
129129
}
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, {
132132
limit,
133133
cursor,
134134
fields,
@@ -148,7 +148,7 @@ app.get("/", authorizer({}), async (req, res) => {
148148
res.links({ next: makeNextHREF(req, newCursor) });
149149
}
150150

151-
return res.json(db.webhookResponse.cleanWriteOnlyResponses(output));
151+
return res.json(db.webhookLog.cleanWriteOnlyResponses(output));
152152
});
153153

154154
export default app;

packages/api/src/schema/api-schema.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ components:
215215
Timestamp (in milliseconds) at which the webhook last was
216216
triggered
217217
example: 1587667174725
218-
webhook-response:
218+
webhook-log:
219219
type: object
220220
required:
221221
- id
@@ -2841,7 +2841,7 @@ paths:
28412841
schema:
28422842
type: array
28432843
items:
2844-
$ref: "#/components/schemas/webhook-response"
2844+
$ref: "#/components/schemas/webhook-log"
28452845
default:
28462846
description: Error
28472847
content:
@@ -2868,7 +2868,7 @@ paths:
28682868
content:
28692869
application/json:
28702870
schema:
2871-
$ref: "#/components/schemas/webhook-response"
2871+
$ref: "#/components/schemas/webhook-log"
28722872
default:
28732873
description: Error
28742874
content:
@@ -2899,7 +2899,7 @@ paths:
28992899
content:
29002900
application/json:
29012901
schema:
2902-
$ref: "#/components/schemas/webhook-response"
2902+
$ref: "#/components/schemas/webhook-log"
29032903
default:
29042904
description: Error
29052905
content:

packages/api/src/schema/db-schema.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -586,13 +586,13 @@ components:
586586
deleted:
587587
type: boolean
588588
default: false
589-
webhook-response:
590-
table: webhook_response
589+
webhook-log:
590+
table: webhook_log
591591
properties:
592592
kind:
593593
readOnly: true
594594
type: string
595-
example: webhookResponse
595+
example: webhookLog
596596
userId:
597597
readOnly: true
598598
type: string

packages/api/src/store/db.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import {
1111
PasswordResetToken,
1212
Usage,
1313
Region,
14-
WebhookResponse,
1514
Session,
1615
SigningKey,
1716
Room,
1817
Attestation,
1918
JwtRefreshToken,
19+
WebhookLog,
2020
} from "../schema/types";
2121
import BaseTable, { TableOptions } from "./table";
2222
import StreamTable from "./stream-table";
@@ -60,7 +60,7 @@ export class DB {
6060
attestation: AttestationTable;
6161
usage: Table<Usage>;
6262
webhook: WebhookTable;
63-
webhookResponse: Table<WebhookResponse>;
63+
webhookLog: Table<WebhookLog>;
6464
passwordResetToken: Table<PasswordResetToken>;
6565
region: Table<Region>;
6666
session: SessionTable;
@@ -169,9 +169,9 @@ export class DB {
169169
});
170170

171171
this.region = makeTable<Region>({ db: this, schema: schemas["region"] });
172-
this.webhookResponse = makeTable<WebhookResponse>({
172+
this.webhookLog = makeTable<WebhookLog>({
173173
db: this,
174-
schema: schemas["webhook-response"],
174+
schema: schemas["webhook-log"],
175175
});
176176
this.session = new SessionTable({ db: this, schema: schemas["session"] });
177177
this.room = makeTable<Room>({ db: this, schema: schemas["room"] });

packages/api/src/webhooks/cannon.ts

+21-21
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { BadRequestError, UnprocessableEntityError } from "../store/errors";
2121
import { db } from "../store";
2222
import { buildRecordingUrl } from "../controllers/session";
2323
import { isExperimentSubject } from "../store/experiment-table";
24-
import { WebhookResponse } from "../schema/types";
24+
import { WebhookLog } from "../schema/types";
2525

2626
const WEBHOOK_TIMEOUT = 5 * 1000;
2727
const MAX_BACKOFF = 60 * 60 * 1000;
@@ -596,13 +596,13 @@ async function storeResponse(
596596
responseBody: string,
597597
sharedSecret: string,
598598
params
599-
): Promise<WebhookResponse> {
599+
): Promise<WebhookLog> {
600600
const hrDuration = process.hrtime(startTime);
601601
const encodedResponseBody = Buffer.from(
602602
responseBody.substring(0, 1024)
603603
).toString("base64");
604604

605-
const webhookResponse = {
605+
const webhookLog = {
606606
id: uuid(),
607607
webhookId: webhook.id,
608608
eventId: eventId,
@@ -624,14 +624,14 @@ async function storeResponse(
624624
},
625625
sharedSecret: sharedSecret,
626626
};
627-
await db.webhookResponse.create(webhookResponse);
628-
return webhookResponse;
627+
await db.webhookLog.create(webhookLog);
628+
return webhookLog;
629629
}
630630

631631
export async function resendWebhook(
632632
webhook: DBWebhook,
633-
webhookResponse: WebhookResponse
634-
): Promise<WebhookResponse> {
633+
webhookLog: WebhookLog
634+
): Promise<WebhookLog> {
635635
const triggerTime = Date.now();
636636
const startTime = process.hrtime();
637637
let resp: Response;
@@ -640,26 +640,26 @@ export async function resendWebhook(
640640
let errorMessage: string;
641641
try {
642642
const timestamp = Date.now();
643-
const requestBody = JSON.parse(webhookResponse.request.body);
644-
webhookResponse.request.body = JSON.stringify({
643+
const requestBody = JSON.parse(webhookLog.request.body);
644+
webhookLog.request.body = JSON.stringify({
645645
...requestBody,
646646
timestamp,
647647
});
648648
const sigHeaders = signatureHeaders(
649-
webhookResponse.request.body,
650-
webhookResponse.sharedSecret,
649+
webhookLog.request.body,
650+
webhookLog.sharedSecret,
651651
timestamp
652652
);
653-
webhookResponse.request.headers = {
654-
...webhookResponse.request.headers,
653+
webhookLog.request.headers = {
654+
...webhookLog.request.headers,
655655
...sigHeaders,
656656
};
657657

658-
resp = await fetchWithTimeout(webhookResponse.request.url, {
659-
method: webhookResponse.request.method,
660-
headers: webhookResponse.request.headers,
658+
resp = await fetchWithTimeout(webhookLog.request.url, {
659+
method: webhookLog.request.method,
660+
headers: webhookLog.request.headers,
661661
timeout: WEBHOOK_TIMEOUT,
662-
body: webhookResponse.request.body,
662+
body: webhookLog.request.body,
663663
});
664664
responseBody = await resp.text();
665665
statusCode = resp.status;
@@ -676,13 +676,13 @@ export async function resendWebhook(
676676
);
677677
return await storeResponse(
678678
webhook,
679-
webhookResponse.eventId,
680-
webhookResponse.event,
679+
webhookLog.eventId,
680+
webhookLog.event,
681681
resp,
682682
startTime,
683683
responseBody,
684-
webhookResponse.sharedSecret,
685-
webhookResponse.request
684+
webhookLog.sharedSecret,
685+
webhookLog.request
686686
);
687687
}
688688
}

0 commit comments

Comments
 (0)