From 2e9e51166bf3dd7db4d1a4bbea8f04cf16f52c69 Mon Sep 17 00:00:00 2001 From: yuvraj Date: Mon, 6 Feb 2023 12:41:20 +0530 Subject: [PATCH 1/3] add query param support in redirect --- apps/api/src/app/app.controller.ts | 5 +++-- apps/api/src/app/app.service.ts | 31 ++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/apps/api/src/app/app.controller.ts b/apps/api/src/app/app.controller.ts index 591debd5..17461341 100644 --- a/apps/api/src/app/app.controller.ts +++ b/apps/api/src/app/app.controller.ts @@ -8,6 +8,7 @@ import { Patch, Post, Put, + Query, Res, UseInterceptors, } from '@nestjs/common'; @@ -84,8 +85,8 @@ export class AppController { @Get('/:hashid') @ApiOperation({ summary: 'Redirect Links' }) @ApiResponse({ status: 301, description: 'will be redirected to the specified link'}) - async redirect(@Param('hashid') hashid: string, @Res() res) { - const reRouteURL: string = await this.appService.redirect(hashid); + async redirect(@Param('hashid') hashid: string, @Query() queryParams: Record, @Res() res) { + const reRouteURL: string = await this.appService.redirect(hashid, queryParams); this.clickServiceClient .send('onClick', { hashid: hashid, diff --git a/apps/api/src/app/app.service.ts b/apps/api/src/app/app.service.ts index 72261256..31c78b41 100644 --- a/apps/api/src/app/app.service.ts +++ b/apps/api/src/app/app.service.ts @@ -96,7 +96,7 @@ export class AppService { }); } - async redirect(hashid: string): Promise { + async redirect(hashid: string, queryParams?: Record): Promise { return this.prisma.link.findMany({ where: { OR: [ @@ -113,17 +113,36 @@ export class AppService { take: 1 }) .then(response => { - const url = response[0].url + let url = response[0].url const params = response[0].params const ret = []; - if(params == null){ - return url; - }else { + if(params != null){ Object.keys(params).forEach(function(d) { ret.push(encodeURIComponent(d) + '=' + encodeURIComponent(params[d])); }) - return `${url}?${ret.join('&')}` || ''; + url = `${url}?${ret.join('&')}` || ''; } + + if (queryParams && Object.keys(queryParams).length) { + if (params == null) { + url += "?"; + } else { + url += "&"; + } + const qparams = []; + Object.keys(queryParams).forEach((d: string) => { + if (Array.isArray(queryParams[d])) { + (queryParams[d] as string[]).forEach(val => { + qparams.push(encodeURIComponent(d) + "=" + encodeURIComponent(val)); + }); + } else { + qparams.push(encodeURIComponent(d) + "=" + encodeURIComponent(queryParams[d] as string)); + } + }); + url += qparams.join("&") || ""; + } + + return url; }) .catch(err => { this.telemetryService.sendEvent(this.configService.get('POSTHOG_DISTINCT_KEY'), "Exception in getLinkFromHashIdOrCustomHashId query", {error: err.message}) From d42e371a80fb395f6d6c1550282089d7ce645220 Mon Sep 17 00:00:00 2001 From: yuvraj Date: Mon, 6 Feb 2023 13:55:44 +0530 Subject: [PATCH 2/3] add testcase for query param support feature --- apps/api/src/app/app.service.spec.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/apps/api/src/app/app.service.spec.ts b/apps/api/src/app/app.service.spec.ts index 9633732d..40ff4258 100644 --- a/apps/api/src/app/app.service.spec.ts +++ b/apps/api/src/app/app.service.spec.ts @@ -11,9 +11,11 @@ import { RedisService } from 'nestjs-redis'; import { AppService } from './app.service'; import { PrismaService } from './prisma.service'; import { TelemetryService } from './telemetry/telemetry.service'; +import { Link } from './app.interface'; describe('AppService', () => { let service: AppService; + let prisma: PrismaService; let mockRedisSet; const mockRedis = { @@ -90,11 +92,36 @@ describe('AppService', () => { .compile(); service = module.get(AppService); + prisma = module.get(PrismaService); }); it('should be defined', () => { expect(service).toBeDefined(); }); + it("should return url with query params appended to it", async () => { + const url = "https://google.com"; + const hashId = "1"; + const queryParams = { + hello: "1234", + world: "5678", + }; + const link: Link = { + id: "1", + user: null, + tags: [], + clicks: 0, + url: url, + hashid: +hashId, + project: null, + customHashId: null, + }; + + prisma.link.findMany = jest.fn().mockResolvedValueOnce([link]); + + expect(await service.redirect(hashId, queryParams)).toBe( + `${url}?hello=1234&world=5678` + ); + }); }); From 7184040949e54e78e16977a55e8e94fbd9a1d517 Mon Sep 17 00:00:00 2001 From: yuvraj Date: Fri, 13 Oct 2023 15:33:18 +0530 Subject: [PATCH 3/3] fix rabbitmq health check --- apps/api/src/app/app.controller.ts | 5 ++++- sample.env | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/apps/api/src/app/app.controller.ts b/apps/api/src/app/app.controller.ts index a02dece9..8de71ed4 100644 --- a/apps/api/src/app/app.controller.ts +++ b/apps/api/src/app/app.controller.ts @@ -60,7 +60,10 @@ export class AppController { @ApiResponse({ status: 200, description: 'Result Report for All the Health Check Services' }) async checkHealth() { return this.healthCheckService.check([ - async () => this.http.pingCheck('RabbitMQ', 'http://localhost:15672/'), + async () => this.http.pingCheck( + "RabbitMQ", + this.configService.get("RABBITMQ_HEALTH_URL") + ), async () => this.http.pingCheck('Basic Check', 'http://localhost:3333/api'), async () => this.redisIndicator.checkHealth('Redis', { type: 'redis', client: this.redis, timeout: 500 }), async () => this.prismaIndicator.isHealthy('Db'), diff --git a/sample.env b/sample.env index 7c727095..e3157e71 100644 --- a/sample.env +++ b/sample.env @@ -18,6 +18,8 @@ POSTHOG_BATCH_SIZE=20 POSTHOG_FLUSH_INTERVAL=10000 CLICK_BACKUP_CRON='* * 1 * * *' +RABBITMQ_HEALTH_URL=http://localhost:15672/ + # This was inserted by `prisma init`: # Environment variables declared in this file are automatically made available to Prisma. # See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema @@ -25,4 +27,4 @@ CLICK_BACKUP_CRON='* * 1 * * *' # Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB (Preview). # See the documentation for all the connection string options: https://pris.ly/d/connection-strings -DATABASE_URL="postgresql://postgres:yoursupersecret@localhost:5432/shortdb?schema=public" \ No newline at end of file +DATABASE_URL="postgresql://postgres:yoursupersecret@localhost:5432/shortdb?schema=public"