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(Link-expiry): Added link expiry using redis #83

Merged
merged 11 commits into from
Aug 11, 2023
4 changes: 3 additions & 1 deletion apps/api/src/app/app.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { RedisModule } from '@liaoliaots/nestjs-redis';
import { HttpModule } from '@nestjs/axios';
import { RedisHealthModule } from '@liaoliaots/nestjs-redis/health';
import { PrismaHealthIndicator } from './prisma/prisma.health';
import { RedisUtils } from './utils/redis.utils';

describe('AppController', () => {
let controller: AppController;
Expand Down Expand Up @@ -91,7 +92,8 @@ describe('AppController', () => {
PrismaService,
RouterService,
TelemetryService,
PrismaHealthIndicator
PrismaHealthIndicator,
RedisUtils
],
})
.overrideProvider(RedisService)
Expand Down
41 changes: 17 additions & 24 deletions apps/api/src/app/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { Link } from './app.interface';

import { AppService } from './app.service';
import { RouterService } from './router/router.service';
import { link as LinkModel } from '@prisma/client';
import { link as LinkModel, Prisma, link } from '@prisma/client';
import { AddROToResponseInterceptor } from './interceptors/addROToResponseInterceptor';
import { ApiBody, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { ConfigService } from '@nestjs/config';
Expand Down Expand Up @@ -76,7 +76,7 @@ export class AppController {
const resp = await this.routerService.decodeAndRedirect(code)
this.clickServiceClient
.send('onClick', {
hashid: resp.hashid,
hashid: resp?.hashid,
})
.subscribe();
if (resp.url !== '') {
Expand All @@ -91,14 +91,16 @@ export class AppController {
@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);
this.clickServiceClient

const reRouteURL: string = await this.appService.resolveRedirect(hashid);

if (reRouteURL !== '') {
console.log({reRouteURL});
this.clickServiceClient
.send('onClick', {
hashid: hashid,
})
.subscribe();
if (reRouteURL !== '') {
console.log({reRouteURL});
return res.redirect(302, reRouteURL);
} else {
throw new NotFoundException();
Expand All @@ -110,28 +112,17 @@ export class AppController {
@ApiOperation({ summary: 'Create New Links' })
@ApiBody({ type: Link })
@ApiResponse({ type: Link, status: 200})
async register(@Body() link: Link): Promise<LinkModel> {
return this.appService.createLink(link);
async register(@Body() link: Link): Promise<LinkModel> {
const response:Promise<link> = this.appService.createLinkInDB(link);
return response;
}


@Patch('update/:id')
@ApiOperation({ summary: 'Update Existing Links' })
@ApiBody({ type: Link })
@ApiResponse({ type: Link, status: 200})
async update(@Param('id') id: string, @Body() link: Link ): Promise<LinkModel> {
return this.appService.updateLink({
where: { customHashId: id },
data: {
userID: link.user || null,
tags: link.tags || null,
clicks: link.clicks || null,
url: link.url || null,
hashid: link.hashid || null,
project: link.project || null,
customHashId: link.customHashId || null,
},
});
async update(@Param('id') id: string, @Body() link: link ): Promise<LinkModel> {
return this.appService.updateLink(id, link);
}

@MessagePattern('onClick')
Expand All @@ -143,7 +134,9 @@ export class AppController {
const channel = context.getChannelRef();
const originalMsg = context.getMessage().content.toString();
console.log(`Message: ${originalMsg}`);
await this.appService.updateClicks(JSON.parse(originalMsg).data.hashid);
// await this.appService.updateClicks(JSON.parse(originalMsg).data.hashid);
let id = JSON.parse(originalMsg).data.hashid;
await this.appService.updateClicksInPostgresDB(id).then((res) => {console.log("UPDATED IN DB SUCCESS")}).catch((err) => {console.log(err)});
}

}
4 changes: 4 additions & 0 deletions apps/api/src/app/app.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ export class Link {
description: 'Custom HashID of Link',
})
customHashId?: string | null
@ApiProperty({
description: 'Timestamp of Link creation',
})
createdAt?: string | null
}
3 changes: 2 additions & 1 deletion apps/api/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { TerminusModule } from '@nestjs/terminus';
import { PosthogModule } from 'nestjs-posthog';
import { ScheduleModule } from '@nestjs/schedule';
import { SchedulerService } from './scheduler/scheduler.service';
import { RedisUtils } from './utils/redis.utils';

@Module({
imports: [
Expand Down Expand Up @@ -70,6 +71,6 @@ import { SchedulerService } from './scheduler/scheduler.service';
ScheduleModule.forRoot()
],
controllers: [AppController],
providers: [AppService, ConfigService, RouterService, PrismaService, TelemetryService, PrismaHealthIndicator, SchedulerService],
providers: [AppService, ConfigService, RouterService, PrismaService, TelemetryService, PrismaHealthIndicator, SchedulerService,RedisUtils],
})
export class AppModule {}
2 changes: 2 additions & 0 deletions apps/api/src/app/app.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { RedisService } from 'nestjs-redis';
import { AppService } from './app.service';
import { PrismaService } from './prisma.service';
import { TelemetryService } from './telemetry/telemetry.service';
import { RedisUtils } from './utils/redis.utils';

describe('AppService', () => {
let service: AppService;
Expand Down Expand Up @@ -83,6 +84,7 @@ describe('AppService', () => {
AppService,
PrismaService,
TelemetryService,
RedisUtils
],
})
.overrideProvider(RedisService)
Expand Down
Loading
Loading