Skip to content

Commit

Permalink
feat: Added dynamic query params and intent for deep linking (#97)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
Nazi-pikachu and yuvrajsab authored Sep 13, 2023
1 parent b281c5d commit c2acfb8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
33 changes: 28 additions & 5 deletions apps/api/src/app/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Put,
Res,
UseInterceptors,
Query
} from '@nestjs/common';
import {
ClientProxy,
Expand Down Expand Up @@ -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<string, string | string[]>, @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,
Expand All @@ -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,
}
Expand Down
28 changes: 3 additions & 25 deletions apps/api/src/app/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>('POSTHOG_DISTINCT_KEY'), "Exception in fetching data from redis falling back to DB", {error: err.message})
Expand Down Expand Up @@ -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<string>('POSTHOG_DISTINCT_KEY'), "Exception in getLinkFromHashIdOrCustomHashId query", {error: err.message})
return { reRouteurl : "" , redirectedLink: null };
// return '';
});
}
}
2 changes: 1 addition & 1 deletion apps/api/src/app/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ model link {
customHashId String? @unique
createdAt DateTime? @default(now())
params Json?
intent Boolean @default(false)
@@unique([userID, project, url, customHashId])
}

Expand Down

0 comments on commit c2acfb8

Please sign in to comment.