Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added dynamic query params and intent for deep linking #97

Merged
merged 5 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading