-
Notifications
You must be signed in to change notification settings - Fork 355
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 #4366 from coralproject/feat/add-dsareports-initial
Add DSAReport schema, mutation, model to DSA launch pad
- Loading branch information
Showing
7 changed files
with
273 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import GraphContext from "coral-server/graph/context"; | ||
import { createDSAReport } from "coral-server/services/dsaReports/reports"; | ||
|
||
import { GQLCreateDSAReportInput } from "coral-server/graph/schema/__generated__/types"; | ||
|
||
export const DSAReports = (ctx: GraphContext) => ({ | ||
createDSAReport: ({ | ||
commentID, | ||
userID, | ||
lawBrokenDescription, | ||
additionalInformation, | ||
submissionID, | ||
}: GQLCreateDSAReportInput) => | ||
createDSAReport(ctx.mongo, ctx.tenant, { | ||
commentID, | ||
userID, | ||
lawBrokenDescription, | ||
additionalInformation, | ||
submissionID, | ||
}), | ||
}); |
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
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,100 @@ | ||
import { v4 as uuid } from "uuid"; | ||
|
||
import { Sub } from "coral-common/common/lib/types"; | ||
import { MongoContext } from "coral-server/data/context"; | ||
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 { | ||
readonly id: string; | ||
|
||
userID: string; | ||
|
||
createdAt: Date; | ||
|
||
lawBrokenDescription: string; | ||
|
||
additionalInformation: string; | ||
|
||
commentID: string; | ||
|
||
submissionID?: string; | ||
|
||
publicID: string; | ||
|
||
status: GQLDSAReportStatus; | ||
} | ||
|
||
export type CreateDSAReportInput = Omit< | ||
DSAReport, | ||
"id" | "tenantID" | "createdAt" | "publicID" | "status" | ||
>; | ||
|
||
export interface CreateDSAReportResultObject { | ||
/** | ||
* dsaReport contains the resultant DSAReport that was created. | ||
*/ | ||
dsaReport: DSAReport; | ||
} | ||
|
||
export async function createDSAReport( | ||
mongo: MongoContext, | ||
tenantID: string, | ||
input: CreateDSAReportInput, | ||
now = new Date() | ||
): Promise<CreateDSAReportResultObject> { | ||
const { userID, commentID, submissionID } = input; | ||
|
||
// Create a new ID for the DSAReport. | ||
const id = uuid(); | ||
let submissionIDToUse = submissionID; | ||
if (!submissionIDToUse) { | ||
submissionIDToUse = uuid(); | ||
} | ||
|
||
// TODO: update how publicID generated | ||
const publicID = uuid(); | ||
|
||
// defaults are the properties set by the application when a new DSAReport is | ||
// created. | ||
const defaults: Sub<DSAReport, CreateDSAReportInput> = { | ||
id, | ||
tenantID, | ||
createdAt: now, | ||
publicID, | ||
status: GQLDSAReportStatus.AWAITING_REVIEW, | ||
}; | ||
|
||
// Extract the filter parameters. | ||
const filter: FilterQuery<DSAReport> = { | ||
tenantID, | ||
commentID, | ||
userID, | ||
}; | ||
|
||
// Merge the defaults with the input. | ||
const report: Readonly<DSAReport> = { | ||
...defaults, | ||
...input, | ||
submissionID: submissionIDToUse, | ||
}; | ||
|
||
// 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); | ||
|
||
if (alreadyExisting) { | ||
// TODO: update error thrown | ||
throw new Error( | ||
"dsa report submitted by user for this comment already exists" | ||
); | ||
} | ||
|
||
await mongo.dsaReports().insertOne(report); | ||
|
||
return { | ||
dsaReport: 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,22 @@ | ||
import { MongoContext } from "coral-server/data/context"; | ||
import { createDSAReport as createReport } from "coral-server/models/dsaReport/report"; | ||
import { Tenant } from "coral-server/models/tenant"; | ||
|
||
export interface CreateDSAReportInput { | ||
commentID: string; | ||
userID: string; | ||
lawBrokenDescription: string; | ||
additionalInformation: string; | ||
submissionID?: string; | ||
} | ||
|
||
export async function createDSAReport( | ||
mongo: MongoContext, | ||
tenant: Tenant, | ||
input: CreateDSAReportInput, | ||
now = new Date() | ||
) { | ||
const result = await createReport(mongo, tenant.id, input, now); | ||
const { dsaReport } = result; | ||
return dsaReport; | ||
} |