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

Fix : Clicks_count published on RMQ #81

Closed
Closed
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
132 changes: 77 additions & 55 deletions apps/api/src/app/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,37 @@ import {
Put,
Res,
UseInterceptors,
} from '@nestjs/common';
} from "@nestjs/common";
import {
ClientProxy,
Ctx,
MessagePattern,
Payload,
RmqContext,
} from '@nestjs/microservices';
} from "@nestjs/microservices";

import { HealthCheckService, HttpHealthIndicator, HealthCheck } from '@nestjs/terminus';
import { PrismaHealthIndicator } from './prisma/prisma.health';
import { RedisService } from '@liaoliaots/nestjs-redis';
import { RedisHealthIndicator } from '@liaoliaots/nestjs-redis/health';
import Redis from 'ioredis';
import { Link } from './app.interface';
import {
HealthCheckService,
HttpHealthIndicator,
HealthCheck,
} from "@nestjs/terminus";
import { PrismaHealthIndicator } from "./prisma/prisma.health";
import { RedisService } from "@liaoliaots/nestjs-redis";
import { RedisHealthIndicator } from "@liaoliaots/nestjs-redis/health";
import Redis from "ioredis";
import { Link } from "./app.interface";

import { AppService } from './app.service';
import { RouterService } from './router/router.service';
import { link as LinkModel } from '@prisma/client';
import { AddROToResponseInterceptor } from './interceptors/addROToResponseInterceptor';
import { ApiBody, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { ConfigService } from '@nestjs/config';
import { AppService } from "./app.service";
import { RouterService } from "./router/router.service";
import { link as LinkModel } from "@prisma/client";
import { AddROToResponseInterceptor } from "./interceptors/addROToResponseInterceptor";
import { ApiBody, ApiOperation, ApiResponse } from "@nestjs/swagger";
import { ConfigService } from "@nestjs/config";

@Controller()
@UseInterceptors(AddROToResponseInterceptor)
export class AppController {
private readonly redis: Redis
private readonly redis: Redis;

constructor(
private readonly appService: AppService,
Expand All @@ -47,94 +51,113 @@ export class AppController {
private prismaIndicator: PrismaHealthIndicator,
private readonly configService: ConfigService,
private readonly redisService: RedisService,
@Inject('CLICK_SERVICE') private clickServiceClient: ClientProxy
@Inject("CLICK_SERVICE") private clickServiceClient: ClientProxy
) {
this.redis = redisService.getClient(configService.get('REDIS_NAME'));
this.redis = redisService.getClient(configService.get("REDIS_NAME"));
}


@Get('/health')
@Get("/health")
@HealthCheck()
@ApiOperation({ summary: 'Get Health Check Status' })
@ApiResponse({ status: 200, description: 'Result Report for All the Health Check Services' })
@ApiOperation({ summary: "Get Health Check Status" })
@ApiResponse({
status: 200,
description: "Result Report for All the Health Check Services",
})
async checkHealth() {
return this.healthCheckService.check([
async () => this.http.pingCheck('RabbitMQ', this.configService.get('RABBITMQ_HEALTH_URL')),
async () => this.http.pingCheck('Basic Check', this.configService.get('BASE_URL')),
async () => this.redisIndicator.checkHealth('Redis', { type: 'redis', client: this.redis, timeout: 500 }),
async () => this.prismaIndicator.isHealthy('Db'),
])
async () =>
this.http.pingCheck(
"RabbitMQ",
this.configService.get("RABBITMQ_HEALTH_URL")
),
async () =>
this.http.pingCheck("Basic Check", this.configService.get("BASE_URL")),
async () =>
this.redisIndicator.checkHealth("Redis", {
type: "redis",
client: this.redis,
timeout: 500,
}),
async () => this.prismaIndicator.isHealthy("Db"),
]);
}
/*

/*
@Deprecated
*/
@Get('/sr/:code')
@ApiOperation({ summary: 'Redirect with encoded parameters' })
@ApiResponse({ status: 301, description: 'will be redirected to the specified encoded link'})
async handler(@Param('code') code: string, @Res() res) {
const resp = await this.routerService.decodeAndRedirect(code)
@Get("/sr/:code")
@ApiOperation({ summary: "Redirect with encoded parameters" })
@ApiResponse({
status: 301,
description: "will be redirected to the specified encoded link",
})
async handler(@Param("code") code: string, @Res() res) {
const resp = await this.routerService.decodeAndRedirect(code);
this.clickServiceClient
.send('onClick', {
.send("onClick", {
hashid: resp.hashid,
})
.subscribe();
if (resp.url !== '') {
if (resp.url !== "") {
return res.redirect(resp.url);
} else {
throw new NotFoundException();
}
}

//http://localhost:3333/api/redirect/208
@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) {
@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);
this.clickServiceClient
.send('onClick', {
.send("onClick", {
hashid: hashid,
})
.subscribe();
if (reRouteURL !== '') {
console.log({reRouteURL});
if (reRouteURL !== "") {
console.log({ reRouteURL });
return res.redirect(302, reRouteURL);
} else {
throw new NotFoundException();
}
}


@Post('/register')
@ApiOperation({ summary: 'Create New Links' })
@Post("/register")
@ApiOperation({ summary: "Create New Links" })
@ApiBody({ type: Link })
@ApiResponse({ type: Link, status: 200})
@ApiResponse({ type: Link, status: 200 })
async register(@Body() link: Link): Promise<LinkModel> {
return this.appService.createLink(link);
}


@Patch('update/:id')
@ApiOperation({ summary: 'Update Existing Links' })
@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> {
@ApiResponse({ type: Link, status: 200 })
async update(
@Param("id") id: string,
@Body() link: Link
): Promise<LinkModel> {
return this.appService.updateLink({
where: { customHashId: id },
data: {
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,
},
},
});
}

@MessagePattern('onClick')
@MessagePattern("onClick")
async getNotifications(
@Payload() data: number[],
@Ctx() context: RmqContext
Expand All @@ -145,5 +168,4 @@ export class AppController {
console.log(`Message: ${originalMsg}`);
await this.appService.updateClicks(JSON.parse(originalMsg).data.hashid);
}

}
Loading