Skip to content

Commit

Permalink
chore: failover implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-gomes-v committed Sep 29, 2024
1 parent 6c75e75 commit 0b0f9a9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 49 deletions.
13 changes: 2 additions & 11 deletions src/app/modules/conclusion/conclusion.controller.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
import { Body, Controller, Get, Post, Query } from '@nestjs/common';
import { Controller, Get, Query } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { ConclusionService } from './conclusion.service';
import {
ConclusionResponseDto,
GetConclusionDto,
GenerateConclusionDto,
} from './conclusion.dto';
import { ConclusionResponseDto, GetConclusionDto } from './conclusion.dto';
import { Conclusion } from './conclusion.interface';

@ApiTags('Conclusion')
@Controller('Conclusion')
export class ConclusionController {
constructor(private readonly conclusionService: ConclusionService) {}

@Post('generate')
async generate(@Body() data: GenerateConclusionDto) {
return await this.conclusionService.generate(data.url);
}

@Get()
async get(@Query() data: GetConclusionDto): Promise<ConclusionResponseDto> {
return this.buildResponse(await this.conclusionService.get(data.url));
Expand Down
31 changes: 23 additions & 8 deletions src/app/modules/conclusion/conclusion.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,27 @@ export class ConclusionService {
private readonly siteReportService: SiteReportService,
) {}

async generate(url: string) {
async generate(url: string): Promise<Conclusion> {
const reports: SiteReportResponseDto[] =
await this.siteReportService.getNotEq(url, 'trustPilot');

const customerReviews = await this.siteReportService.getEqual(
url,
'trustPilot',
);

if (reports.length === 0) {
return `No reports found for ${url}`;
throw new Error(`No reports found for ${url}`);
}

try {
const sumarizedObj = await this.summarize(reports);
const sumarized = {
...JSON.parse(sumarizedObj),
customerReviews: customerReviews.data,
};
await this.create(sumarized);
return `${url} new conclusion generated`;
const result = await this.create(sumarized);
return result;
} catch (error) {
return `Error generating conclusion for ${url}`;
throw new Error(error);
}
}

Expand All @@ -52,6 +50,23 @@ export class ConclusionService {
}

async get(url: string): Promise<Conclusion> {
return this.conclusionRepository.get(url);
let conclusion = await this.conclusionRepository.get(url);
if (!conclusion) {
const siteReportReviews = await this.siteReportService.getEqual(
url,
'trustPilot',
);
const siteReportAnalysis = await this.siteReportService.getNotEq(
url,
'trustPilot',
);
if (siteReportReviews && siteReportAnalysis) {
conclusion = await this.generate(url);
} else {
await this.siteReportService.crawler(url);
conclusion = await this.generate(url);
}
}
return conclusion;
}
}
28 changes: 0 additions & 28 deletions src/app/modules/site-report/site-report.controller.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/app/modules/site-report/site-report.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Module } from '@nestjs/common';
import { SiteReportController } from './site-report.controller';
import { SiteReportService } from './site-report.service';
import { MongooseModule } from '@nestjs/mongoose';
import { SiteReportSchema } from './site-report.schema';
Expand All @@ -13,7 +12,6 @@ import { ProviderModule } from '../provider/provider.module';
]),
ProviderModule,
],
controllers: [SiteReportController],
providers: [SiteReportService, SiteReportRepository],
exports: [SiteReportService],
})
Expand Down

0 comments on commit 0b0f9a9

Please sign in to comment.