Skip to content

Commit

Permalink
Add more created at field
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarvarela committed Dec 23, 2024
1 parent 6aaf382 commit 352ed17
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 13 deletions.
4 changes: 4 additions & 0 deletions site/gatsby-site/server/fields/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ export const createNotificationsOnNewIncident = async (fullDocument: DBIncident,
type: 'new-incidents',
incident_id: incidentId,
processed: false,
created_at: new Date(),
});

const entityFields: (keyof DBIncident)[] = [
Expand Down Expand Up @@ -239,6 +240,7 @@ export const createNotificationsOnNewIncident = async (fullDocument: DBIncident,
incident_id: incidentId,
entity_id: entityId,
processed: false,
created_at: new Date(),
});
}
}
Expand Down Expand Up @@ -363,6 +365,7 @@ export const logReportHistory = async (updated: DBReport, context: Context) => {
const reportHistory: DBReportHistory = {
...updated,
modifiedBy: context.user?.id ?? '',
created_at: new Date(),
_id: undefined,
}

Expand All @@ -376,6 +379,7 @@ export const logIncidentHistory = async (updated: DBIncident, context: Context)
const incidentHistory: DBIncidentHistory = {
...updated,
modifiedBy: context.user?.id ?? '',
created_at: new Date(),
_id: undefined,
}

Expand Down
12 changes: 8 additions & 4 deletions site/gatsby-site/server/fields/reports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { generateMutationFields, generateQueryFields, getQueryResolver } from ".
import { isRole } from "../rules";
import { linkReportsToIncidents, logReportHistory } from "./common";
import { ReportType } from "../types/report";
import { Context, DBReport } from "../interfaces";


export const queryFields: GraphQLFieldConfigMap<any, any> = {
Expand Down Expand Up @@ -185,18 +186,20 @@ export const mutationFields: GraphQLFieldConfigMap<any, any> = {
type: new GraphQLNonNull(CreateVariantInput),
},
},
resolve: async (source, { input }, context) => {
resolve: async (source, { input }, context: Context) => {

const incidents = context.client.db('aiidprod').collection("incidents");
const reports = context.client.db('aiidprod').collection("reports");
const reports = context.client.db('aiidprod').collection<DBReport>("reports");

const parentIncident = await incidents.findOne({ incident_id: input.incidentId });

if (!parentIncident) {
throw `Incident ${input.incidentId} not found`;
}

const report_number = (await reports.find({}).sort({ report_number: -1 }).limit(1).next()).report_number + 1;
const lastReport = await reports.find({}).sort({ report_number: -1 }).limit(1).next();

const report_number = lastReport ? lastReport.report_number + 1 : 1;

const now = new Date();

Expand Down Expand Up @@ -225,9 +228,10 @@ export const mutationFields: GraphQLFieldConfigMap<any, any> = {
language: 'en',
tags: ['variant:unreviewed'],
inputs_outputs: input.variant.inputs_outputs,
user: context.user?.id ?? '',
};

await reports.insertOne({ ...newReport, report_number: newReport.report_number });
await reports.insertOne({ ...newReport, report_number: newReport.report_number, created_at: new Date() });

const incident_ids = [input.incidentId];
const report_numbers = [newReport.report_number];
Expand Down
17 changes: 10 additions & 7 deletions site/gatsby-site/server/fields/submissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
GraphQLInputObjectType
} from 'graphql';
import { generateMutationFields, generateQueryFields } from '../utils';
import { Context, DBIncident, DBSubmission } from '../interfaces';
import { Context, DBIncident, DBNotification, DBSubmission } from '../interfaces';
import { allow } from 'graphql-shield';
import { ObjectIdScalar } from '../scalars';
import { isRole } from '../rules';
Expand Down Expand Up @@ -62,7 +62,7 @@ export const mutationFields: GraphQLFieldConfigMap<any, Context> = {

// TODO: Strictly type these collections using the DB* types
const reports = context.client.db('aiidprod').collection("reports");
const notificationsCollection = context.client.db('customData').collection("notifications");
const notificationsCollection = context.client.db('customData').collection<DBNotification>("notifications");
const incidentsHistory = context.client.db('history').collection("incidents");
const reportsHistory = context.client.db('history').collection("reports");

Expand Down Expand Up @@ -116,11 +116,11 @@ export const mutationFields: GraphQLFieldConfigMap<any, Context> = {
from_reports: [report_number]
}
}
if(submission.epoch_date_modified) {
if (submission.epoch_date_modified) {
newIncident.epoch_date_modified = submission.epoch_date_modified;
}

await incidents.insertOne({ ...newIncident, incident_id: newIncident.incident_id });
await incidents.insertOne({ ...newIncident, incident_id: newIncident.incident_id, created_at: new Date() });


await createNotificationsOnNewIncident(newIncident, context);
Expand All @@ -132,14 +132,16 @@ export const mutationFields: GraphQLFieldConfigMap<any, Context> = {
type: 'submission-promoted',
incident_id: newIncident.incident_id,
processed: false,
userId: submission.user
userId: submission.user,
created_at: new Date(),
});
}

const incidentHistory: Record<string, unknown> = {
...newIncident,
reports: [report_number],
incident_id: newIncident.incident_id,
created_at: new Date(),
};

if (submission.user) {
Expand Down Expand Up @@ -205,6 +207,7 @@ export const mutationFields: GraphQLFieldConfigMap<any, Context> = {
...incidentValues,
reports: [...incidentValues.reports!, report_number],
embedding,
created_at: new Date(),

Check warning on line 210 in site/gatsby-site/server/fields/submissions.ts

View check run for this annotation

Codecov / codecov/patch

site/gatsby-site/server/fields/submissions.ts#L210

Added line #L210 was not covered by tests
}

if (submission.user) {
Expand Down Expand Up @@ -246,11 +249,11 @@ export const mutationFields: GraphQLFieldConfigMap<any, Context> = {
newReport.user = submission.user;
}

if(submission.epoch_date_modified) {
if (submission.epoch_date_modified) {
newReport.epoch_date_modified = submission.epoch_date_modified;
}

await reports.insertOne({ ...newReport, report_number: newReport.report_number });
await reports.insertOne({ ...newReport, report_number: newReport.report_number, created_at: new Date() });

const incident_ids: number[] = parentIncidents.map(incident => incident.incident_id!);
const report_numbers: number[] = [newReport.report_number];
Expand Down
14 changes: 14 additions & 0 deletions site/gatsby-site/server/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,7 @@ export type History_Incident = {
AllegedDeveloperOfAISystem?: Maybe<Array<Maybe<Scalars['String']['output']>>>;
AllegedHarmedOrNearlyHarmedParties?: Maybe<Array<Maybe<Scalars['String']['output']>>>;
_id?: Maybe<Scalars['ObjectId']['output']>;
created_at?: Maybe<Scalars['DateTime']['output']>;
date: Scalars['String']['output'];
description?: Maybe<Scalars['String']['output']>;
editor_dissimilar_incidents?: Maybe<Array<Maybe<Scalars['Int']['output']>>>;
Expand Down Expand Up @@ -1193,6 +1194,7 @@ export type History_IncidentFilterType = {
NOR?: InputMaybe<Array<InputMaybe<History_IncidentFilterType>>>;
OR?: InputMaybe<Array<InputMaybe<History_IncidentFilterType>>>;
_id?: InputMaybe<ObjectIdFilter>;
created_at?: InputMaybe<DateTimeFilter>;
date?: InputMaybe<StringFilter>;
description?: InputMaybe<StringFilter>;
editor_dissimilar_incidents?: InputMaybe<IntFilter>;
Expand Down Expand Up @@ -1275,6 +1277,7 @@ export enum History_IncidentSortByInput {

export type History_IncidentSortType = {
_id?: InputMaybe<SortType>;
created_at?: InputMaybe<SortType>;
date?: InputMaybe<SortType>;
description?: InputMaybe<SortType>;
editor_notes?: InputMaybe<SortType>;
Expand Down Expand Up @@ -1334,6 +1337,7 @@ export type History_Report = {
_id?: Maybe<Scalars['ObjectId']['output']>;
authors?: Maybe<Array<Maybe<Scalars['String']['output']>>>;
cloudinary_id?: Maybe<Scalars['String']['output']>;
created_at?: Maybe<Scalars['DateTime']['output']>;
date_downloaded: Scalars['DateTime']['output'];
date_modified: Scalars['DateTime']['output'];
date_published: Scalars['DateTime']['output'];
Expand Down Expand Up @@ -1406,6 +1410,7 @@ export type History_ReportFilterType = {
_id?: InputMaybe<ObjectIdFilter>;
authors?: InputMaybe<StringFilter>;
cloudinary_id?: InputMaybe<StringFilter>;
created_at?: InputMaybe<DateTimeFilter>;
date_downloaded?: InputMaybe<DateTimeFilter>;
date_modified?: InputMaybe<DateTimeFilter>;
date_published?: InputMaybe<DateTimeFilter>;
Expand Down Expand Up @@ -1485,6 +1490,7 @@ export enum History_ReportSortByInput {
export type History_ReportSortType = {
_id?: InputMaybe<SortType>;
cloudinary_id?: InputMaybe<SortType>;
created_at?: InputMaybe<SortType>;
date_downloaded?: InputMaybe<SortType>;
date_modified?: InputMaybe<SortType>;
date_published?: InputMaybe<SortType>;
Expand Down Expand Up @@ -2562,6 +2568,7 @@ export type MutationUpsertOneSubscriptionArgs = {
export type Notification = {
__typename?: 'Notification';
_id?: Maybe<Scalars['ObjectId']['output']>;
created_at?: Maybe<Scalars['DateTime']['output']>;
entity_id?: Maybe<Scalars['String']['output']>;
incident_id?: Maybe<Scalars['Int']['output']>;
isUpdate?: Maybe<Scalars['Boolean']['output']>;
Expand All @@ -2577,6 +2584,7 @@ export type NotificationFilterType = {
NOR?: InputMaybe<Array<InputMaybe<NotificationFilterType>>>;
OR?: InputMaybe<Array<InputMaybe<NotificationFilterType>>>;
_id?: InputMaybe<ObjectIdFilter>;
created_at?: InputMaybe<DateTimeFilter>;
entity_id?: InputMaybe<StringFilter>;
incident_id?: InputMaybe<IntFilter>;
isUpdate?: InputMaybe<BooleanFilter>;
Expand Down Expand Up @@ -2657,6 +2665,7 @@ export enum NotificationSortByInput {

export type NotificationSortType = {
_id?: InputMaybe<SortType>;
created_at?: InputMaybe<SortType>;
entity_id?: InputMaybe<SortType>;
incident_id?: InputMaybe<SortType>;
isUpdate?: InputMaybe<SortType>;
Expand Down Expand Up @@ -3090,6 +3099,7 @@ export type Report = {
_id?: Maybe<Scalars['ObjectId']['output']>;
authors: Array<Maybe<Scalars['String']['output']>>;
cloudinary_id: Scalars['String']['output'];
created_at?: Maybe<Scalars['DateTime']['output']>;
date_downloaded: Scalars['DateTime']['output'];
date_modified: Scalars['DateTime']['output'];
date_published: Scalars['DateTime']['output'];
Expand Down Expand Up @@ -3189,6 +3199,7 @@ export type ReportFilterType = {
_id?: InputMaybe<ObjectIdFilter>;
authors?: InputMaybe<StringFilter>;
cloudinary_id?: InputMaybe<StringFilter>;
created_at?: InputMaybe<DateTimeFilter>;
date_downloaded?: InputMaybe<DateTimeFilter>;
date_modified?: InputMaybe<DateTimeFilter>;
date_published?: InputMaybe<DateTimeFilter>;
Expand Down Expand Up @@ -3221,6 +3232,7 @@ export type ReportInsertType = {
_id?: InputMaybe<Scalars['ObjectId']['input']>;
authors: Array<InputMaybe<Scalars['String']['input']>>;
cloudinary_id: Scalars['String']['input'];
created_at?: InputMaybe<Scalars['DateTime']['input']>;
date_downloaded: Scalars['DateTime']['input'];
date_modified: Scalars['DateTime']['input'];
date_published: Scalars['DateTime']['input'];
Expand Down Expand Up @@ -3253,6 +3265,7 @@ export type ReportSetType = {
_id?: InputMaybe<Scalars['ObjectId']['input']>;
authors?: InputMaybe<Array<InputMaybe<Scalars['String']['input']>>>;
cloudinary_id?: InputMaybe<Scalars['String']['input']>;
created_at?: InputMaybe<Scalars['DateTime']['input']>;
date_downloaded?: InputMaybe<Scalars['DateTime']['input']>;
date_modified?: InputMaybe<Scalars['DateTime']['input']>;
date_published?: InputMaybe<Scalars['DateTime']['input']>;
Expand Down Expand Up @@ -3329,6 +3342,7 @@ export enum ReportSortByInput {
export type ReportSortType = {
_id?: InputMaybe<SortType>;
cloudinary_id?: InputMaybe<SortType>;
created_at?: InputMaybe<SortType>;
date_downloaded?: InputMaybe<SortType>;
date_modified?: InputMaybe<SortType>;
date_published?: InputMaybe<SortType>;
Expand Down
2 changes: 2 additions & 0 deletions site/gatsby-site/server/types/incidentHistory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { GraphQLInt, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLString } from "graphql";
import { ObjectIdScalar } from "../scalars";
import { IncidentEmbeddingType, NlpSimilarIncidentType, TsneType } from "./types";
import { GraphQLDateTime } from "graphql-scalars";

export const IncidentHistoryType = new GraphQLObjectType({
name: 'History_incident',
Expand All @@ -9,6 +10,7 @@ export const IncidentHistoryType = new GraphQLObjectType({
AllegedDeployerOfAISystem: { type: new GraphQLList(GraphQLString), resolve: (source) => source['Alleged deployer of AI system'] },
AllegedDeveloperOfAISystem: { type: new GraphQLList(GraphQLString), resolve: (source) => source['Alleged developer of AI system'] },
AllegedHarmedOrNearlyHarmedParties: { type: new GraphQLList(GraphQLString), resolve: (source) => source['Alleged harmed or nearly harmed parties'] },
created_at: { type: GraphQLDateTime },
implicated_systems: { type: new GraphQLList(GraphQLString) },
date: { type: new GraphQLNonNull(GraphQLString) },
description: { type: GraphQLString },
Expand Down
1 change: 1 addition & 0 deletions site/gatsby-site/server/types/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ export const NotificationType = new GraphQLObjectType({
sentDate: { type: GraphQLDateTime },
type: { type: GraphQLString },
userId: getRelationshipConfig(UserType, GraphQLString, 'userId', 'userId', 'users', 'customData'),
created_at: { type: GraphQLDateTime },
}),
});
3 changes: 2 additions & 1 deletion site/gatsby-site/server/types/report.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GraphQLBoolean, GraphQLInt, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLString } from "graphql";
import { DateTimeResolver } from "graphql-scalars";
import { DateTimeResolver, GraphQLDateTime } from "graphql-scalars";
import { ObjectIdScalar } from "../scalars";
import { getRelationshipConfig } from "../utils";
import { Context } from "../interfaces";
Expand All @@ -20,6 +20,7 @@ export const ReportType = new GraphQLObjectType({
_id: { type: ObjectIdScalar },
authors: { type: new GraphQLNonNull(new GraphQLList(GraphQLString)) },
cloudinary_id: { type: new GraphQLNonNull(GraphQLString) },
created_at: { type: GraphQLDateTime },
date_downloaded: { type: new GraphQLNonNull(DateTimeResolver) },
date_modified: { type: new GraphQLNonNull(DateTimeResolver) },
date_published: { type: new GraphQLNonNull(DateTimeResolver) },
Expand Down
3 changes: 2 additions & 1 deletion site/gatsby-site/server/types/reportHistory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GraphQLBoolean, GraphQLInt, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLString } from "graphql";
import { DateTimeResolver } from "graphql-scalars";
import { DateTimeResolver, GraphQLDateTime } from "graphql-scalars";
import { ObjectIdScalar } from "../scalars";
import { EmbeddingType } from "./types";

Expand All @@ -9,6 +9,7 @@ export const ReportHistoryType = new GraphQLObjectType({
_id: { type: ObjectIdScalar },
authors: { type: new GraphQLList(GraphQLString) },
cloudinary_id: { type: GraphQLString },
created_at: { type: GraphQLDateTime },
date_downloaded: { type: new GraphQLNonNull(DateTimeResolver) },
date_modified: { type: new GraphQLNonNull(DateTimeResolver) },
date_published: { type: new GraphQLNonNull(DateTimeResolver) },
Expand Down

0 comments on commit 352ed17

Please sign in to comment.