Skip to content

Commit

Permalink
Merge pull request #31 from OpenWebhook/migrate-searchable-path
Browse files Browse the repository at this point in the history
Migrate searchable path
  • Loading branch information
Samox authored Jul 5, 2022
2 parents 1ccc0bf + ad41361 commit 89f8b80
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- This is an empty migration.

SELECT path, REGEXP_REPLACE(CONCAT(path, '/'), '\/[0-9]*\/', ':id/') FROM "Webhook";
4 changes: 2 additions & 2 deletions src/application/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ const whereConditionFactory = (
OR: [
{
host,
searchablePath: { startsWith: validPath },
searchablePath: { contains: validPath },
},
{
host,
path: { startsWith: validPath },
path: { contains: validPath },
},
],
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
function removeTrailingSlash(str: string): string {
return str.replace(/\/+$/, '');
}

export const pathToSearchablePath = (path: string): string => {
const pathWithATrailingSlash =
path[path.length - 1] === '/' ? path : `${path}/`;
Expand All @@ -11,10 +15,7 @@ export const pathToSearchablePath = (path: string): string => {
'/:id/',
);

const pathWithoutTrailingSlash = pathWithoutNumeralIds.substring(
0,
pathWithoutNumeralIds.length - 1,
);
const pathWithoutTrailingSlash = removeTrailingSlash(pathWithoutNumeralIds);
return pathWithoutTrailingSlash;
};

Expand Down
27 changes: 27 additions & 0 deletions src/test/webhook.resolver.filters.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,32 @@ describe('CustomerResolver (e2e)', () => {
expect(Array.isArray(res.body.data.webhooks)).toBe(true);
expect(res.body.data.webhooks).toHaveLength(2);
});

it('should find webhooks with exact id', async () => {
const paths = [
'/organisation/123432/update',
'/organisation/123432/create',
'/organisation/123432/delete',
];
const webhookWithComplexPath: Prisma.WebhookCreateInput[] = paths.map(
(path) => ({
host: hostname,
path,
body: {},
headers: {},
ip: 'random.ip',
searchablePath: pathToSearchablePath(path),
}),
);
await prismaService.webhook.createMany({ data: webhookWithComplexPath });
const res = await request(app.getHttpServer())
.post(gql)
.send({
query: 'query {webhooks(path: "/123432") {path}}',
})
.expect(200);
expect(Array.isArray(res.body.data.webhooks)).toBe(true);
expect(res.body.data.webhooks).toHaveLength(3);
});
});
});

0 comments on commit 89f8b80

Please sign in to comment.