From dc0d5f34abee8eadcc69549ebfed8f87cce8af0e Mon Sep 17 00:00:00 2001 From: Kathryn Beaty Date: Wed, 11 Oct 2023 12:35:47 -0400 Subject: [PATCH] update errors and comments for dsareport creation --- common/lib/errors.ts | 6 +++ server/src/core/server/errors/index.ts | 9 ++++ server/src/core/server/errors/translations.ts | 1 + .../src/core/server/locales/en-US/errors.ftl | 1 + .../core/server/models/dsaReport/report.ts | 52 ++++++++++++++++--- 5 files changed, 63 insertions(+), 6 deletions(-) diff --git a/common/lib/errors.ts b/common/lib/errors.ts index dabe213204..e617bc5073 100644 --- a/common/lib/errors.ts +++ b/common/lib/errors.ts @@ -50,6 +50,12 @@ export enum ERROR_CODES { */ DUPLICATE_STORY_URL = "DUPLICATE_STORY_URL", + /** + * DUPLICATE_DSA_REPORT is used when trying to create a DSA report for a user who has already + * reported a comment for illegal content. + */ + DUPLICATE_DSA_REPORT = "DUPLICATE_DSA_REPORT", + /** * DUPLICATE_FLAIR_BADGE is used when trying to create a flair badge for a * tenant that already includes that flair badge. diff --git a/server/src/core/server/errors/index.ts b/server/src/core/server/errors/index.ts index e5b114da21..5c31aa58bb 100644 --- a/server/src/core/server/errors/index.ts +++ b/server/src/core/server/errors/index.ts @@ -322,6 +322,15 @@ export class DuplicateEmailError extends CoralError { } } +export class DuplicateDSAReportError extends CoralError { + constructor(reportID: string) { + super({ + code: ERROR_CODES.DUPLICATE_DSA_REPORT, + context: { pub: { reportID } }, + }); + } +} + export class DuplicateEmailDomainError extends CoralError { constructor(emailDomain: string) { super({ diff --git a/server/src/core/server/errors/translations.ts b/server/src/core/server/errors/translations.ts index 8b4cc7f204..f1f1594b8f 100644 --- a/server/src/core/server/errors/translations.ts +++ b/server/src/core/server/errors/translations.ts @@ -10,6 +10,7 @@ export const ERROR_TRANSLATIONS: Record = { COMMENTING_DISABLED: "error-commentingDisabled", DUPLICATE_EMAIL: "error-duplicateEmail", DUPLICATE_EMAIL_DOMAIN: "error-duplicateEmailDomain", + DUPLICATE_DSA_REPORT: "error-duplicateDSAReport", DUPLICATE_STORY_ID: "error-duplicateStoryID", DUPLICATE_STORY_URL: "error-duplicateStoryURL", DUPLICATE_FLAIR_BADGE: "error-duplicateFlairBadge", diff --git a/server/src/core/server/locales/en-US/errors.ftl b/server/src/core/server/locales/en-US/errors.ftl index a4c01acb98..aa4eea1da2 100644 --- a/server/src/core/server/locales/en-US/errors.ftl +++ b/server/src/core/server/locales/en-US/errors.ftl @@ -22,6 +22,7 @@ error-duplicateUser = Specified user already exists with a different login method. error-duplicateEmail = Specified email address is already in use. error-duplicateEmailDomain = Specified email domain is already configured. +error-duplicateDSAReport = User has already reported this comment for illegal content. error-localProfileAlreadySet = Specified account already has a password set. error-localProfileNotSet = diff --git a/server/src/core/server/models/dsaReport/report.ts b/server/src/core/server/models/dsaReport/report.ts index 1bbbf94935..67858f6019 100644 --- a/server/src/core/server/models/dsaReport/report.ts +++ b/server/src/core/server/models/dsaReport/report.ts @@ -2,28 +2,62 @@ import { v4 as uuid } from "uuid"; import { Sub } from "coral-common/common/lib/types"; import { MongoContext } from "coral-server/data/context"; +import { + CommentNotFoundError, + DuplicateDSAReportError, +} from "coral-server/errors"; import { FilterQuery } from "coral-server/models/helpers"; import { TenantResource } from "coral-server/models/tenant"; import { GQLDSAReportStatus } from "coral-server/graph/schema/__generated__/types"; export interface DSAReport extends TenantResource { + /** + * id identifies this DSA Report specifically. + */ readonly id: string; + /** + * userID is the id of the user who reported this comment for illegal content. + */ userID: string; + /** + * createdAt is the date that this DSAReport was created + */ createdAt: Date; + /** + * lawBrokenDescription is the description of the law this comment is being + * reported for breaking. + */ lawBrokenDescription: string; + /** + * additionalInformation is more explanation of how this comment being reported + * breaks the law. + */ additionalInformation: string; + /** + * commentID is the id of the comment being reported. + */ commentID: string; + /** + * submissionID is the id that keeps track of all comments that are submitted together + * as part of one illegal content report form by a user. + */ submissionID?: string; + /** + * publicID is a user-friendly id used to reference the DSA Report. + */ publicID: string; + /** + * status keeps track of the current status of the DSA Report + */ status: GQLDSAReportStatus; } @@ -81,15 +115,21 @@ export async function createDSAReport( submissionID: submissionIDToUse, }; + // check that a comment for the comment ID exists and throw an error if not + const commentExists = await mongo + .comments() + .findOne({ tenantID, id: commentID }); + + if (!commentExists) { + throw new CommentNotFoundError(commentID); + } + // check if there's already a dsareport submitted by this user for this comment // and return a duplicate error if so - const alreadyExisting = await mongo.dsaReports().findOne(filter); + const alreadyExistingReport = await mongo.dsaReports().findOne(filter); - if (alreadyExisting) { - // TODO: update error thrown - throw new Error( - "dsa report submitted by user for this comment already exists" - ); + if (alreadyExistingReport) { + throw new DuplicateDSAReportError(alreadyExistingReport.id); } await mongo.dsaReports().insertOne(report);