From c2acfb82879dd74bde8e6b16f4cc9dcf296b582d Mon Sep 17 00:00:00 2001 From: Rishabh jain Date: Wed, 13 Sep 2023 15:56:32 +0530 Subject: [PATCH] feat: Added dynamic query params and intent for deep linking (#97) * feat: Added intent for deep linking * Feat: Support for KV-Data * fix : params in case of already params in original url * refactor code and remove comments * fix typo and uncomment query param in redirect endpoint --------- Co-authored-by: yuvraj --- apps/api/src/app/app.controller.ts | 33 +++++++++++++++++++++++---- apps/api/src/app/app.service.ts | 28 +++-------------------- apps/api/src/app/prisma/schema.prisma | 2 +- 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/apps/api/src/app/app.controller.ts b/apps/api/src/app/app.controller.ts index 70007302..d625aa11 100644 --- a/apps/api/src/app/app.controller.ts +++ b/apps/api/src/app/app.controller.ts @@ -10,6 +10,7 @@ import { Put, Res, UseInterceptors, + Query } from '@nestjs/common'; import { ClientProxy, @@ -92,14 +93,36 @@ 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) { + async redirect(@Param('hashid') hashid: string, @Query() queryParams: Record, @Res() res) { const response = await this.appService.resolveRedirect(hashid); - const reRouteURL: string = response?.reRouteurl; + let reRouteURL: string = response?.reRouteurl; const redirectedLink: LinkModel = response?.redirectedLink; + const urlContainParams = new URL(reRouteURL).searchParams.toString() !== ""; + + if (queryParams && Object.keys(queryParams).length) { + if (!urlContainParams) { + reRouteURL += "?"; + } else { + reRouteURL += "&"; + } + const qParamList = []; + Object.keys(queryParams).forEach((d: string) => { + if (Array.isArray(queryParams[d])) { + (queryParams[d] as string[]).forEach(val => { + qParamList.push(encodeURIComponent(d) + "=" + encodeURIComponent(val)); + }); + } else { + qParamList.push(encodeURIComponent(d) + "=" + encodeURIComponent(queryParams[d] as string)); + } + }); + reRouteURL += qParamList.join("&") || ""; + } + + console.log("ReRouted URL is: ",{reRouteURL}); + if (reRouteURL !== '') { - console.log({reRouteURL}); this.clickServiceClient .send('onClick', { hashid: hashid, @@ -111,8 +134,8 @@ export class AppController { `Redirected Link`, { linkId:redirectedLink.id, - routeName: `/${hashid}}`, - // queryParams : redirectedLink?.params, + routeName: `/${hashid}`, + queryParams : queryParams, originalUrl: redirectedLink?.url, redirectUrl: reRouteURL, } diff --git a/apps/api/src/app/app.service.ts b/apps/api/src/app/app.service.ts index 13e5ff63..d3ed92b6 100644 --- a/apps/api/src/app/app.service.ts +++ b/apps/api/src/app/app.service.ts @@ -227,19 +227,8 @@ export class AppService { if(params?.["status"] == "expired"){ return { reRouteurl : '' , redirectedLink:null }; - // return ""; - } - - if(params == null){ - return { reRouteurl : url , redirectedLink:link }; - // return url; - }else { - Object.keys(params).forEach(function(d) { - ret.push(encodeURIComponent(d) + '=' + encodeURIComponent(params[d])); - }) - return { reRouteurl : `${url}?${ret.join('&')}` || '' , redirectedLink:link }; - // return `${url}?${ret.join('&')}` || ''; } + return { reRouteurl : url , redirectedLink:link }; }) .catch(err => { this.telemetryService.sendEvent(this.configService.get('POSTHOG_DISTINCT_KEY'), "Exception in fetching data from redis falling back to DB", {error: err.message}) @@ -282,26 +271,15 @@ export class AppService { // this.deleteLink({id: response[0].id}); // don't delete from DB keep it there this.redisUtils.clearKey(response[0]); return { reRouteurl : "" , redirectedLink: null }; - // return ""; } this.redisUtils.setKey(response[0]); - - if(params == null){ - return { reRouteurl : url , redirectedLink: null }; - // return url; - }else { - Object.keys(params).forEach(function(d) { - ret.push(encodeURIComponent(d) + '=' + encodeURIComponent(params[d])); - }) - return { reRouteurl : `${url}?${ret.join('&')}` || '' , redirectedLink:response[0] }; - // return `${url}?${ret.join('&')}` || ''; - } + + return { reRouteurl : url , redirectedLink: response[0] }; }) .catch(err => { this.telemetryService.sendEvent(this.configService.get('POSTHOG_DISTINCT_KEY'), "Exception in getLinkFromHashIdOrCustomHashId query", {error: err.message}) return { reRouteurl : "" , redirectedLink: null }; - // return ''; }); } } diff --git a/apps/api/src/app/prisma/schema.prisma b/apps/api/src/app/prisma/schema.prisma index 07aa38f1..04f3b969 100644 --- a/apps/api/src/app/prisma/schema.prisma +++ b/apps/api/src/app/prisma/schema.prisma @@ -18,7 +18,7 @@ model link { customHashId String? @unique createdAt DateTime? @default(now()) params Json? - + intent Boolean @default(false) @@unique([userID, project, url, customHashId]) }