Migrate some DAO to ts (#2633)

* Migrate some DAO to ts

* Oops

* Added constant name
This commit is contained in:
Bruce Berrios 2022-03-04 16:50:15 -05:00 committed by GitHub
parent 3de06f4e41
commit 3566992e45
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 115 additions and 63 deletions

View file

@ -49,7 +49,7 @@ class QuotesController {
const data = await QuoteRatingsDAO.get(
parseInt(quoteId as string),
language
language as string
);
return new MonkeyResponse("Rating retrieved", data);
@ -107,25 +107,18 @@ class QuotesController {
throw new MonkeyError(400, "Captcha check failed.");
}
const newReport = {
const newReport: MonkeyTypes.Report = {
id: uuidv4(),
type: "quote",
timestamp: new Date().getTime(),
uid,
details: {
contentId: `${quoteLanguage}-${quoteId}`,
reason,
comment,
},
contentId: `${quoteLanguage}-${quoteId}`,
reason,
comment,
};
await ReportDAO.createReport(newReport, maxReports, contentReportLimit);
Logger.log("report_created", {
type: newReport.type,
details: newReport.details,
});
return new MonkeyResponse("Quote reported");
}
}

View file

@ -1,9 +0,0 @@
import db from "../init/db";
class PsaDAO {
static async get(_uid, _config) {
return await db.collection("psa").find().toArray();
}
}
export default PsaDAO;

9
backend/dao/psa.ts Normal file
View file

@ -0,0 +1,9 @@
import db from "../init/db";
class PsaDAO {
static async get(): Promise<MonkeyTypes.PSA[]> {
return await db.collection<MonkeyTypes.PSA>("psa").find().toArray();
}
}
export default PsaDAO;

View file

@ -3,15 +3,17 @@ import { roundTo2 } from "../utils/misc";
class PublicStatsDAO {
//needs to be rewritten, this is public stats not user stats
static async updateStats(restartCount, time) {
time = roundTo2(time);
await db.collection("public").updateOne(
static async updateStats(
restartCount: number,
time: number
): Promise<boolean> {
await db.collection<MonkeyTypes.PublicStats>("public").updateOne(
{ type: "stats" },
{
$inc: {
testsCompleted: 1,
testsStarted: restartCount + 1,
timeTyping: time,
timeTyping: roundTo2(time),
},
},
{ upsert: true }

View file

@ -1,10 +1,15 @@
import db from "../init/db";
class QuoteRatingsDAO {
static async submit(quoteId, language, rating, update) {
static async submit(
quoteId: number,
language: string,
rating: number,
update: boolean
): Promise<void> {
if (update) {
await db
.collection("quote-rating")
.collection<MonkeyTypes.QuoteRating>("quote-rating")
.updateOne(
{ quoteId, language },
{ $inc: { totalRating: rating } },
@ -12,28 +17,33 @@ class QuoteRatingsDAO {
);
} else {
await db
.collection("quote-rating")
.collection<MonkeyTypes.QuoteRating>("quote-rating")
.updateOne(
{ quoteId, language },
{ $inc: { ratings: 1, totalRating: rating } },
{ upsert: true }
);
}
let quoteRating = await this.get(quoteId, language);
let average = parseFloat(
const quoteRating = await this.get(quoteId, language);
const average = parseFloat(
(
Math.round((quoteRating.totalRating / quoteRating.ratings) * 10) / 10
).toFixed(1)
);
return await db
.collection("quote-rating")
await db
.collection<MonkeyTypes.QuoteRating>("quote-rating")
.updateOne({ quoteId, language }, { $set: { average } });
}
static async get(quoteId, language) {
return await db.collection("quote-rating").findOne({ quoteId, language });
static async get(
quoteId: number,
language: string
): Promise<MonkeyTypes.QuoteRating> {
return await db
.collection<MonkeyTypes.QuoteRating>("quote-rating")
.findOne({ quoteId, language });
}
}

View file

@ -1,29 +0,0 @@
import MonkeyError from "../utils/error";
import db from "../init/db";
class ReportDAO {
static async createReport(report, maxReports, contentReportLimit) {
const reports = await db.collection("reports").find().toArray();
if (reports.length >= maxReports) {
throw new MonkeyError(
503,
"Reports are not being accepted at this time. Please try again later."
);
}
const sameReports = reports.filter((existingReport) => {
return existingReport.details.contentId === report.details.contentId;
});
if (sameReports.length >= contentReportLimit) {
throw new MonkeyError(
409,
"A report limit for this content has been reached."
);
}
await db.collection("reports").insertOne(report);
}
}
export default ReportDAO;

39
backend/dao/report.ts Normal file
View file

@ -0,0 +1,39 @@
import MonkeyError from "../utils/error";
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();
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);
}
}
export default ReportDAO;

View file

@ -78,6 +78,7 @@ declare namespace MonkeyTypes {
type Mode2<M extends Mode> = keyof PersonalBests[M];
type Difficulty = "normal" | "expert" | "master";
interface PersonalBest {
acc: number;
consistency: number;
@ -89,6 +90,7 @@ declare namespace MonkeyTypes {
wpm: number;
timestamp: number;
}
interface PersonalBests {
time: {
[key: number]: PersonalBest[];
@ -169,4 +171,39 @@ declare namespace MonkeyTypes {
delimiter: string;
textLen?: number;
}
interface PSA {
sticky?: boolean;
message: string;
level?: number;
}
type ReportTypes = "quote";
interface Report {
id: string;
type: ReportTypes;
timestamp: number;
uid: string;
contentId: string;
reason: string;
comment: string;
}
interface PublicStats {
_id: string;
testsCompleted: number;
testsStarted: number;
timeTyping: number;
type: string;
}
interface QuoteRating {
_id: string;
average: number;
language: string;
quoteId: number;
ratings: number;
totalRating: number;
}
}