Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate created_at related triggers to the API #3294

Open
wants to merge 4 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 11 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 @@
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 @@

// 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 @@ -108,18 +108,19 @@
implicated_systems: submission.implicated_systems || [],
editor_notes: submission.editor_notes ?? '',
flagged_dissimilar_incidents: [],
created_at: new Date(),
}
if (submission.embedding) {
newIncident.embedding = {
vector: submission.embedding.vector,
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 @@ -131,14 +132,16 @@
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 @@ -204,6 +207,7 @@
...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 @@ -245,11 +249,11 @@
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
19 changes: 19 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 @@ -1517,6 +1523,7 @@ export type Incident = {
AllegedDeveloperOfAISystem?: Maybe<Array<Maybe<Entity>>>;
AllegedHarmedOrNearlyHarmedParties?: Maybe<Array<Maybe<Entity>>>;
_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 @@ -1624,6 +1631,7 @@ export type IncidentFilterType = {
NOR?: InputMaybe<Array<InputMaybe<IncidentFilterType>>>;
OR?: InputMaybe<Array<InputMaybe<IncidentFilterType>>>;
_id?: InputMaybe<ObjectIdFilter>;
created_at?: InputMaybe<DateTimeFilter>;
date?: InputMaybe<StringFilter>;
description?: InputMaybe<StringFilter>;
editor_dissimilar_incidents?: InputMaybe<IntFilter>;
Expand Down Expand Up @@ -1654,6 +1662,7 @@ export type IncidentInsertType = {
AllegedDeveloperOfAISystem?: InputMaybe<IncidentAllegeddeveloperofaisystemRelationInput>;
AllegedHarmedOrNearlyHarmedParties?: InputMaybe<IncidentAllegedharmedornearlyharmedpartiesRelationInput>;
_id?: InputMaybe<Scalars['ObjectId']['input']>;
created_at?: InputMaybe<Scalars['DateTime']['input']>;
date: Scalars['String']['input'];
description?: InputMaybe<Scalars['String']['input']>;
editor_dissimilar_incidents?: InputMaybe<Array<InputMaybe<Scalars['Int']['input']>>>;
Expand Down Expand Up @@ -1835,6 +1844,7 @@ export type IncidentSetType = {
AllegedDeveloperOfAISystem?: InputMaybe<IncidentAllegeddeveloperofaisystemRelationInput>;
AllegedHarmedOrNearlyHarmedParties?: InputMaybe<IncidentAllegedharmedornearlyharmedpartiesRelationInput>;
_id?: InputMaybe<Scalars['ObjectId']['input']>;
created_at?: InputMaybe<Scalars['DateTime']['input']>;
date?: InputMaybe<Scalars['String']['input']>;
description?: InputMaybe<Scalars['String']['input']>;
editor_dissimilar_incidents?: InputMaybe<Array<InputMaybe<Scalars['Int']['input']>>>;
Expand Down Expand Up @@ -1871,6 +1881,7 @@ export enum IncidentSortByInput {

export type IncidentSortType = {
_id?: InputMaybe<SortType>;
created_at?: InputMaybe<SortType>;
date?: InputMaybe<SortType>;
description?: InputMaybe<SortType>;
editor_notes?: InputMaybe<SortType>;
Expand Down Expand Up @@ -2557,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 @@ -2572,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 @@ -2652,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 @@ -3085,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 @@ -3184,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 @@ -3216,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 @@ -3248,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 @@ -3324,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
2 changes: 2 additions & 0 deletions site/gatsby-site/server/types/incidents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getListRelationshipConfig, getListRelationshipExtension, getListRelatio
import { UserType } from "./user";
import { IncidentEmbeddingType, NlpSimilarIncidentType, TsneType } from "./types";
import { ReportType } from "./report";
import { GraphQLDateTime } from "graphql-scalars";

export const IncidentType = new GraphQLObjectType({
name: 'Incident',
Expand Down Expand Up @@ -49,6 +50,7 @@ export const IncidentType = new GraphQLObjectType({
nlp_similar_incidents: { type: new GraphQLList(NlpSimilarIncidentType) },
reports: getListRelationshipConfig(ReportType, GraphQLInt, 'reports', 'report_number', 'reports', 'aiidprod'),
tsne: { type: TsneType },
created_at: { type: GraphQLDateTime },
},
});

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
4 changes: 2 additions & 2 deletions site/gatsby-site/server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ export function generateMutationFields({
const db = context.client.db(databaseName);
const collection = db.collection(collectionName);

const result = await collection.insertOne(insert);
const result = await collection.insertOne({ ...insert, created_at: new Date() });

const inserted = await collection.findOne({ _id: result.insertedId });

Expand Down Expand Up @@ -822,7 +822,7 @@ export function generateMutationFields({
let update: any = await parseRelationshipFields(Type, args.update, mongoUpdate, context);
update = await parseDBMappings(Type, update);

await collection.updateOne(filter, { $set: update }, { ...projection, upsert: true });
await collection.updateOne(filter, { $set: update, $setOnInsert: { created_at: new Date() } }, { ...projection, upsert: true });

const updated = await collection.findOne(filter);

Expand Down
Loading
Loading