converted report dao to dal

This commit is contained in:
Miodec 2022-05-04 00:17:34 +02:00
parent 12a63b1cb4
commit a4ca55cb73
2 changed files with 29 additions and 33 deletions

View file

@ -1,7 +1,7 @@
import _ from "lodash";
import { v4 as uuidv4 } from "uuid";
import { getUser, updateQuoteRatings } from "../../dao/user";
import ReportDAO from "../../dao/report";
import ReportDAL from "../../dao/report";
import * as NewQuotesDAL from "../../dao/new-quotes";
import QuoteRatingsDAO from "../../dao/quote-ratings";
import MonkeyError from "../../utils/error";
@ -149,7 +149,7 @@ export async function reportQuote(
comment,
};
await ReportDAO.createReport(newReport, maxReports, contentReportLimit);
await ReportDAL.createReport(newReport, maxReports, contentReportLimit);
return new MonkeyResponse("Quote reported");
}

View file

@ -3,37 +3,33 @@ import db from "../init/db";
const COLLECTION_NAME = "reports";
class ReportDAO {
static async createReport(
report: MonkeyTypes.Report,
maxReports: number,
contentReportLimit: number
): Promise<void> {
const reportsCount = await db
.collection<MonkeyTypes.Report>(COLLECTION_NAME)
.estimatedDocumentCount();
export async function createReport(
report: MonkeyTypes.Report,
maxReports: number,
contentReportLimit: number
): Promise<void> {
const reportsCount = await db
.collection<MonkeyTypes.Report>(COLLECTION_NAME)
.estimatedDocumentCount();
if (reportsCount >= maxReports) {
throw new MonkeyError(
503,
"Reports are not being accepted at this time. Please try again later."
);
}
const sameReports = await db
.collection<MonkeyTypes.Report>(COLLECTION_NAME)
.find({ contentId: report.contentId })
.toArray();
if (sameReports.length >= contentReportLimit) {
throw new MonkeyError(
409,
"A report limit for this content has been reached."
);
}
await db.collection<MonkeyTypes.Report>(COLLECTION_NAME).insertOne(report);
if (reportsCount >= maxReports) {
throw new MonkeyError(
503,
"Reports are not being accepted at this time. Please try again later."
);
}
}
export default ReportDAO;
const sameReports = await db
.collection<MonkeyTypes.Report>(COLLECTION_NAME)
.find({ contentId: report.contentId })
.toArray();
if (sameReports.length >= contentReportLimit) {
throw new MonkeyError(
409,
"A report limit for this content has been reached."
);
}
await db.collection<MonkeyTypes.Report>(COLLECTION_NAME).insertOne(report);
}