-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1236 from AletheiaFact/create-daily-report-document
Send Unsolicited Latest Reviews in Daily Report
- Loading branch information
Showing
14 changed files
with
217 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Controller, Param, Post, UseGuards } from "@nestjs/common"; | ||
import { | ||
AdminUserAbility, | ||
CheckAbilities, | ||
} from "../auth/ability/ability.decorator"; | ||
import { AbilitiesGuard } from "../auth/ability/abilities.guard"; | ||
import { DailyReportService } from "../daily-report/daily-report.service"; | ||
import { ClaimReviewService } from "../claim-review/claim-review.service"; | ||
import { NotificationService } from "../notifications/notifications.service"; | ||
|
||
@Controller() | ||
export class DailyReportController { | ||
constructor( | ||
private readonly dailyReportService: DailyReportService, | ||
private claimReviewService: ClaimReviewService, | ||
private notificationService: NotificationService | ||
) {} | ||
|
||
@Post("api/daily-report/topic/:topic/send/:nameSpace") | ||
@UseGuards(AbilitiesGuard) | ||
@CheckAbilities(new AdminUserAbility()) | ||
async sendDailyReport( | ||
@Param("topic") topic, | ||
@Param("nameSpace") nameSpace | ||
) { | ||
const queryParams: any = { isHidden: false, isDeleted: false }; | ||
const [lastDailyReportSent] = | ||
await this.dailyReportService.getLastDailyReportSent(); | ||
|
||
if (lastDailyReportSent) { | ||
queryParams.date = { $gt: lastDailyReportSent?.date }; | ||
} | ||
|
||
const dailyClaimReviews = | ||
await this.claimReviewService.listDailyClaimReviews({ | ||
page: 0, | ||
pageSize: 30, | ||
order: "asc", | ||
nameSpace, | ||
query: queryParams, | ||
}); | ||
|
||
if (dailyClaimReviews.length > 0) { | ||
const reportIds = dailyClaimReviews.map(({ report }) => report._id); | ||
await this.dailyReportService.create({ | ||
reports: reportIds, | ||
date: new Date(), | ||
}); | ||
} | ||
|
||
const dailyReport = await this.dailyReportService.generateDailyReport( | ||
dailyClaimReviews, | ||
nameSpace | ||
); | ||
|
||
this.notificationService.sendDailyReviewsEmail(topic, dailyReport); | ||
|
||
if (dailyClaimReviews.length < 1) { | ||
throw new Error("No daily reports today"); | ||
} | ||
|
||
return dailyClaimReviews; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { DailyReport, DailyReportSchema } from "./schemas/daily-report.schema"; | ||
import { DailyReportService } from "./daily-report.service"; | ||
import { MongooseModule } from "@nestjs/mongoose"; | ||
import { AbilityModule } from "../auth/ability/ability.module"; | ||
import { SummarizationModule } from "../summarization/summarization.module"; | ||
import { ClaimReviewModule } from "../claim-review/claim-review.module"; | ||
import { NotificationModule } from "../notifications/notifications.module"; | ||
import { DailyReportController } from "./daily-report.controller"; | ||
import { ConfigModule } from "@nestjs/config"; | ||
|
||
export const DailyReportModel = MongooseModule.forFeature([ | ||
{ | ||
name: DailyReport.name, | ||
schema: DailyReportSchema, | ||
}, | ||
]); | ||
|
||
@Module({ | ||
imports: [ | ||
DailyReportModel, | ||
ClaimReviewModule, | ||
SummarizationModule, | ||
AbilityModule, | ||
NotificationModule, | ||
ConfigModule, | ||
], | ||
providers: [DailyReportService], | ||
controllers: [DailyReportController], | ||
exports: [DailyReportService], | ||
}) | ||
export class DailyReportModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Injectable, Scope, Logger } from "@nestjs/common"; | ||
import { Model } from "mongoose"; | ||
import { | ||
DailyReport, | ||
DailyReportDocument, | ||
} from "./schemas/daily-report.schema"; | ||
import { InjectModel } from "@nestjs/mongoose"; | ||
import { SummarizationService } from "../summarization/summarization.service"; | ||
|
||
@Injectable({ scope: Scope.REQUEST }) | ||
export class DailyReportService { | ||
private readonly logger = new Logger("SummarizationLogger"); | ||
constructor( | ||
@InjectModel(DailyReport.name) | ||
private DailyReportModel: Model<DailyReportDocument>, | ||
private summarizationService: SummarizationService | ||
) {} | ||
|
||
async create(dailyReportBody: DailyReport): Promise<DailyReport> { | ||
return new this.DailyReportModel(dailyReportBody).save(); | ||
} | ||
|
||
async getLastDailyReportSent(): Promise<DailyReport[]> { | ||
return await this.DailyReportModel.find({}).sort({ date: -1 }).limit(1); | ||
} | ||
|
||
async generateDailyReport( | ||
dailyClaimReviews, | ||
nameSpace?: string | ||
): Promise<string> { | ||
try { | ||
const summarizedReviews = | ||
await this.summarizationService.getSummarizedReviews( | ||
dailyClaimReviews | ||
); | ||
|
||
return this.summarizationService.generateHTMLReport( | ||
summarizedReviews, | ||
nameSpace | ||
); | ||
} catch (error) { | ||
this.logger.error("Error generating daily report:", error); | ||
throw new Error("Failed to generate daily report"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import * as mongoose from "mongoose"; | ||
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose"; | ||
import { Report } from "../../report/schemas/report.schema"; | ||
|
||
export type DailyReportDocument = DailyReport & mongoose.Document; | ||
|
||
@Schema({ toObject: { virtuals: true }, toJSON: { virtuals: true } }) | ||
export class DailyReport { | ||
@Prop({ | ||
type: [ | ||
{ | ||
type: mongoose.Types.ObjectId, | ||
required: true, | ||
ref: "Report", | ||
}, | ||
], | ||
}) | ||
reports: Report[]; | ||
|
||
@Prop({ required: true }) | ||
date: Date; | ||
} | ||
|
||
export const DailyReportSchema = SchemaFactory.createForClass(DailyReport); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.