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 Notifications to the new API #3103

Open
wants to merge 104 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
104 commits
Select commit Hold shift + click to select a range
883b8fb
Add taxa seeds
cesarvarela Aug 2, 2024
a3ee9c1
Merge remote-tracking branch 'upstream/staging' into feature-migrate-…
cesarvarela Aug 2, 2024
981de5f
Reapply "Add classifications collection to api"
cesarvarela Aug 2, 2024
fb9e113
Fix upsert mutations
cesarvarela Aug 7, 2024
ed00861
Fix classification editor tests
cesarvarela Aug 8, 2024
69f1b90
Update components
cesarvarela Aug 8, 2024
b5bb1bc
Move to correct folder
cesarvarela Aug 8, 2024
b7583c4
Fix mutations handling of relationships
cesarvarela Aug 8, 2024
7439b4e
Update client side query variables
cesarvarela Aug 8, 2024
5b374df
Undo debug comments
cesarvarela Aug 8, 2024
efcc1b8
Merge remote-tracking branch 'upstream/staging' into feature-migrate-…
cesarvarela Aug 8, 2024
d86c861
Update generated code
cesarvarela Aug 8, 2024
0d41b90
temporary silence warning
cesarvarela Aug 8, 2024
fa8d9c4
Update playwright login util to allow mocking local storage session
cesarvarela Aug 9, 2024
727f314
migrate classifications test
cesarvarela Aug 9, 2024
b9c3c5d
Update test
cesarvarela Aug 12, 2024
ef7056e
Update tests to use new login fixture
cesarvarela Aug 12, 2024
fb94f2f
Fix tests
cesarvarela Aug 12, 2024
b97dbaf
Merge remote-tracking branch 'upstream/staging' into feature-migrate-…
cesarvarela Aug 15, 2024
ac28656
Skip data dependant test
cesarvarela Aug 15, 2024
ee5f86d
Prevent login util from dropping users database
cesarvarela Aug 15, 2024
de3f3c6
WIP
cesarvarela Aug 15, 2024
86cf496
Fix custom data mocking
cesarvarela Aug 16, 2024
b37aaac
Merge remote-tracking branch 'upstream/staging' into feature-migrate-…
cesarvarela Aug 16, 2024
6bc4117
check for window to fix crash on build time
cesarvarela Aug 16, 2024
d19ff74
Migrate csettool
cesarvarela Aug 16, 2024
9706d6a
Fix tests
cesarvarela Aug 19, 2024
9971e1c
Merge remote-tracking branch 'upstream/staging' into feature-migrate-…
cesarvarela Aug 19, 2024
2aef16a
Make it easier to debug playwright on ci
cesarvarela Aug 19, 2024
6419083
Add subscriptions collection to api
cesarvarela Aug 21, 2024
42df4f3
Migrate duplicates collection to api
cesarvarela Aug 22, 2024
caca25e
Update duplicates tests
cesarvarela Aug 22, 2024
1569920
Add subscription seeds
cesarvarela Sep 6, 2024
3cef35d
Remove mocks
cesarvarela Sep 6, 2024
11d4727
Merge remote-tracking branch 'upstream/staging' into feature-migrate-…
cesarvarela Sep 6, 2024
71c6846
Migrate subscription tests
cesarvarela Sep 10, 2024
4f6a9a3
Restore submit test
cesarvarela Sep 10, 2024
0e3f570
Merge remote-tracking branch 'upstream/staging' into feature-migrate-…
cesarvarela Sep 11, 2024
a309f99
Merge remote-tracking branch 'upstream/staging' into feature-migrate-…
cesarvarela Sep 11, 2024
72c768e
Add missing header
cesarvarela Sep 11, 2024
31ef41f
Merge remote-tracking branch 'upstream/staging' into feature-migrate-…
cesarvarela Sep 12, 2024
11eabfa
Migrate unsubscribe tests
cesarvarela Sep 12, 2024
5a29734
delete log
cesarvarela Sep 12, 2024
a0147ad
Migrate account tests
cesarvarela Sep 12, 2024
ce66f37
Migrate entity tests
cesarvarela Sep 12, 2024
ba9eb9a
Update subscriptions toggle
cesarvarela Sep 12, 2024
63b3462
Update code generated files
cesarvarela Sep 12, 2024
600643e
Refactor files to solve circular dep
cesarvarela Sep 17, 2024
5575d52
Add notifications fields and mutation
cesarvarela Sep 18, 2024
7278c7f
Delete old functions
cesarvarela Sep 18, 2024
4f73c99
Fix import
cesarvarela Sep 18, 2024
6f0a234
Fixes
cesarvarela Sep 18, 2024
147e96a
Had new custom header secret rule and update process notifications pl…
cesarvarela Sep 19, 2024
8ce9a46
Merge remote-tracking branch 'upstream/staging' into feature-migrate-…
cesarvarela Sep 23, 2024
77f0ed8
Add new env vars to test workflow
cesarvarela Sep 23, 2024
2c812c9
Add variables to smoke tests
cesarvarela Sep 24, 2024
f448c9f
Delete unused files
cesarvarela Sep 24, 2024
642bb07
Merge remote-tracking branch 'upstream/staging' into feature-migrate-…
cesarvarela Sep 24, 2024
47eafd7
Fix issue with fields that have different name in database
cesarvarela Sep 28, 2024
1434361
Fix header secret rule
cesarvarela Sep 28, 2024
79c132b
Add typings for subscriptions and notifications types
cesarvarela Sep 28, 2024
9d92e78
Migrate on new incident trigger
cesarvarela Sep 28, 2024
b85ca02
Add schema to db field mapping
cesarvarela Sep 28, 2024
fcadbef
Create notifications even if no subscriptions were created
cesarvarela Sep 28, 2024
c365bbf
Create notifications on incident updates
cesarvarela Oct 3, 2024
1d0dda8
Use sendgrid node package
cesarvarela Oct 4, 2024
8476e32
Merge remote-tracking branch 'upstream/staging' into feature-migrate-…
cesarvarela Oct 4, 2024
fcf0ddc
Update lockfile
cesarvarela Oct 4, 2024
8a3b2a7
Prevent some inserts from crashing
cesarvarela Oct 4, 2024
9c64d9b
Only run special plugins in production context
cesarvarela Oct 4, 2024
4821a63
Fix test mocks
cesarvarela Oct 4, 2024
bf6a93b
Add new site url env variable
cesarvarela Oct 4, 2024
8299bd1
Bring back non migrated functions
cesarvarela Oct 8, 2024
89f7ddb
add missing await
cesarvarela Oct 8, 2024
9e1ff4e
undo config change
cesarvarela Oct 8, 2024
799add4
Remove not used templates
cesarvarela Oct 8, 2024
b0d99f4
Delete character
cesarvarela Oct 8, 2024
0b87710
Use mailersend instead of sendgrid
cesarvarela Oct 8, 2024
a784302
Make email urls configurable
cesarvarela Oct 9, 2024
f9e0e3a
Trigger Build
cesarvarela Oct 9, 2024
e46897d
Get deply url from netlify contexto
cesarvarela Oct 9, 2024
1f5f21a
Update tests
cesarvarela Oct 9, 2024
56faac7
Use variable
cesarvarela Oct 9, 2024
969eeb8
Fix netlify variable name
cesarvarela Oct 9, 2024
e4b6f6a
Merge remote-tracking branch 'upstream/staging' into feature-migrate-…
cesarvarela Oct 9, 2024
2897636
Set branch url via netlify click
cesarvarela Oct 10, 2024
fc43103
Trigger Build
cesarvarela Oct 10, 2024
8b66222
Delete custom env site url step
cesarvarela Oct 10, 2024
29cdeee
Refactor main readme
cesarvarela Oct 11, 2024
6d1bb7d
Update js docs
cesarvarela Oct 11, 2024
90eb959
Update site/gatsby-site/README.md
cesarvarela Oct 16, 2024
6c75eb4
Update site/gatsby-site/README.md
cesarvarela Oct 16, 2024
d4c7231
Update wording
cesarvarela Oct 16, 2024
c115c05
Update site/gatsby-site/README.md
cesarvarela Oct 16, 2024
1aa788c
Merge branch 'feature-migrate-notifications' of github.com:aiidtest/a…
cesarvarela Oct 16, 2024
d00eaf8
Wording
cesarvarela Oct 16, 2024
42d9d18
Improve typings
cesarvarela Oct 16, 2024
ccfc275
Update dummy site_url
cesarvarela Oct 16, 2024
dbb2f84
Remove debug call
cesarvarela Oct 18, 2024
daa82bb
Delete old triggers
cesarvarela Oct 18, 2024
993f428
Only trigger notifications for specific fields
cesarvarela Oct 18, 2024
f76388e
Delete old tests
cesarvarela Oct 18, 2024
505ac7d
Update site/gatsby-site/README.md
cesarvarela Oct 18, 2024
cb141c5
Update test title
cesarvarela Oct 18, 2024
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
5 changes: 5 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ on:
type: string
required: false
default: ""
site-url:
description: The URL of the site to deploy
type: string
required: true

jobs:
netlify-deploy:
Expand Down Expand Up @@ -119,6 +123,7 @@ jobs:
REALM_API_PRIVATE_KEY: ${{ secrets.REALM_API_PRIVATE_KEY }}
ROLLBAR_POST_SERVER_ITEM_ACCESS_TOKEN: ${{ secrets.GATSBY_ROLLBAR_TOKEN }}
API_MONGODB_CONNECTION_STRING: ${{ secrets.API_MONGODB_CONNECTION_STRING }}
SITE_URL: ${{ inputs.site-url }}

- name: Build size
run: |
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
with:
sha: ${{ github.event.pull_request.head.sha }}
environment: staging
netlify-context: deploy-preview
netlify-context: branch-deploy
kepae marked this conversation as resolved.
Show resolved Hide resolved
runner-label: ${{ vars.PREVIEW_RUNNER_LABEL }}

call-test:
Expand Down Expand Up @@ -90,6 +90,7 @@ jobs:
with:
sha: ${{ github.event.pull_request.head.sha }}
environment: staging
netlify-context: deploy-preview
netlify-context: branch-deploy
netlify-alias: pr-${{ github.event.pull_request.number }}
runner-label: ${{ vars.PREVIEW_RUNNER_LABEL }}
site-url: https://pr-${{ github.event.pull_request.number }}--staging-aiid.netlify.app
1 change: 1 addition & 0 deletions .github/workflows/production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,4 @@ jobs:
netlify-alias:
runner-label: ${{ vars.PRODUCTION_RUNNER_LABEL }}
cache-modifier: ${{ needs.cache-modifier.outputs.cache-modifier }}
site-url: ${{ vars.SITE_URL }}
cesarvarela marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions .github/workflows/staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,4 @@ jobs:
netlify-alias:
runner-label: ${{ vars.STAGING_RUNNER_LABEL }}
cache-modifier: ${{ needs.cache-modifier.outputs.cache-modifier }}
site-url: ${{ vars.SITE_URL }}
2 changes: 1 addition & 1 deletion .github/workflows/test-playwright-full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ jobs:
NOTIFICATIONS_SENDER_NAME: Test Preview
NOTIFICATIONS_SENDER: [email protected]
PROCESS_NOTIFICATIONS_SECRET: sarasa
SITE_URL: https://incidentdatabase.test
SITE_URL: http://localhost:8000

- name: Upload Playwright traces
if: failure()
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-playwright.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ jobs:
NOTIFICATIONS_SENDER_NAME: Test Preview
NOTIFICATIONS_SENDER: [email protected]
PROCESS_NOTIFICATIONS_SECRET: sarasa
SITE_URL: https://incidentdatabase.test
SITE_URL: http://localhost:8000

- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
Expand Down
115 changes: 0 additions & 115 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -823,121 +823,6 @@ On Facebook Authentication settings, set the "Client ID" with the Facebook App I

About Facebook Authentication instructions: https://www.mongodb.com/docs/realm/web/authenticate/#facebook-authentication

## Email subscriptions

### Subscription types

- **All**: This subscription type is not defined yet.
```
{
"userId": "63320ce63ec803072c9f529c"
"type": "all",
}
```
- **Incident**: Users with this subscription type will be notified when the incident associated is updated. This subscription type needs an incident_id value associated.
```
{
"userId": "63320ce63ec803072c9f529c"
"type": "incident",
"incident_id": 10,
}
```
- **New Incident**: Users with this subscription type will be notified when a new Incident is created. The notification will be sent after finish the next site build when the Incident page is actually created.
```
{
"userId": "63320ce63ec803072c9f529c"
"type": "new-incidents",
}
```
- **Entities**: Users can subscribe to a specific Entity. The user with this subscription type will be notified when a new Incident associated with an specific Entity is created or when an existing Incident is updated to be associated with that Entity.
```
{
"userId": "63320ce63ec803072c9f529c",
"type": "entity"
"entityId": "openai",
}
```
- **Submission Promoted**: Users that submit a new Incident Report are automatically subscribed to its promotion. Once the submission has been approved by an editor, the user will recieve an email informing that the submission is now an incident/issue/report.
```
{
"userId": "63320ce63ec803072c9f529c",
"type": "submission-promoted"
}
```

These subscription types are also documented in [subscriptions.js](site/gatsby-site/src/utils/subscriptions.js) file.

### Email notifications

[Sendgrid](https://sendgrid.com/) is used to send email notifications.

To allow email notifications add these secret values to your Atlas App Service following the instructions in the [Atlas App Services documentation](https://www.mongodb.com/docs/atlas/app-services/values-and-secrets/define-and-manage-secrets/).

To disable all email noticications, fill the following `SendGridApiKey` secret value with an empty string. If this secret value is an empty string, the emails will be ignored.

```
SendGridApiKey = [SendGrid API key from https://app.sendgrid.com/settings/api_keys]
publicApiKey = [Public API key from the Atlas Organization. See comment below for more information]
privateApiKey = [Private API key from the Atlas Organization. See comment below for more information]
groupId = [Atlas Service App group ID, eg: "62cc90978bc4600cafdcf16e"]
appId = [Atlas Service App ID, eg: "62cc98647e6a26c53d5b4b53"]
```

To get your Public and Private API Key, follow these [instructions](https://www.mongodb.com/docs/atlas/configure-api-access/#std-label-create-org-api-key).

To get the group ID and the app ID, the easiest way is to navigate to your Atlas Service App dashboard and copy from the URL.
The URL format is https://services.cloud.mongodb.com/groups/[groupId]/apps/[appId]/dashboard

Email notifications to New Incidents (subscription type **New Incident**), Incident updates (subscription type **Incident**) and Submission Promoted (subscription type **Submission Promoted**) are sent when the next build finishes. This is because we have to wait until the new Incident page is generated and accessible.
When a new Incident is created or updates, a pending notification item is saved into the `notifications` DB collection with `processed=false` field.
And finally, as part of the site build process, we processed all pending notifications (`processed=false`), send the emails to all recipients, and update the items with `processed=true` and `sentDate=[now]`.

### Notifications collection definition

- **Incident Updated**
```
{
"type": "incident-updated",
"incident_id": 374,
"processed": false
}
```
- **New Incident Report**
```
{
"type": "new-report-incident",
"incident_id": 374,
"report_number": 2172,
"processed": false
}
```
- **New Incident**
```
{
"type": 'new-incidents',
"incident_id": incidentId,
"processed": false,
}
```
- **Entities**
```
{
"type": "entity",
"incident_id": 374,
"entity_id": "openai",
"isUpdate": true,
"processed": false
}
```
- **Submission Promoted**
```
{
"type": "submission-promoted",
"incident_id": 374,
"processed": false
}
```

### Error logging

This project uses [Rollbar](https://rollbar.com) for error logging for the whole site, including background processes.
Expand Down
2 changes: 1 addition & 1 deletion site/gatsby-site/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ node_modules
/blob-report/
/playwright/.cache/

/netlify/functions/lookupIndex.json
/netlify/functions/lookupIndex.json
186 changes: 183 additions & 3 deletions site/gatsby-site/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
## Setting Up a Local Development Environment

### Setting Up a Local Development Environment

To set up a local development environment for the AIID project, follow these steps:
Once you have cloned the repository, to set up a local development environment for the AIID project, follow these steps:

1. **Navigate to the Gatsby Site Directory**

Expand Down Expand Up @@ -48,6 +47,10 @@ To set up a local development environment for the AIID project, follow these ste
CLOUDFLARE_R2_SECRET_ACCESS_KEY= # Secret access key for Cloudflare R2 storage
CLOUDFLARE_R2_BUCKET_NAME= # Name of the Cloudflare R2 bucket for storage
GATSBY_CLOUDFLARE_R2_PUBLIC_BUCKET_URL= # Public URL for accessing the Cloudflare R2 bucket from the Gatsby app
MAILERSEND_API_KEY= # API key for MailerSend email service or dummy value if you don't plan to send emails
NOTIFICATIONS_SENDER_NAME=AIID Notifications # Name of the sender for email notifications
[email protected] # Email address of the sender for email notifications
SITE_URL=http://localhost:8000
```

Ensure that each variable is set correctly to match your development environment's requirements.
Expand All @@ -69,3 +72,180 @@ To set up a local development environment for the AIID project, follow these ste
```

Follow these steps to get your local environment up and running for development with the AIID project. Make sure to replace the placeholder values in the `.env` file with your actual credentials to ensure proper functionality.

## AIID API

### Overview

The AIID API is built to facilitate interactions with the AI Incident Database. It is implemented as a collection of serverless functions that are composed ("stitched") into a singular GraphQL endpoint.

1. **Access the Apollo Explorer**

Navigate to `http://localhost:8000/graphql` in your web browser. The Apollo Explorer instance should be displayed, allowing you to introspect and run queries against the API.

### Performing Queries

Here is an example query you can run in the Apollo Explorer to retrieve incidents:

```graphql
query {
incident {
incident_id
title
}
}
```

### Expected Response

The query should return a response similar to this:

```json
{
"data": {
"incident": {
"incident_id": 1,
"title": "Incident 1"
}
}
}
```

### Project Structure

#### Folders

The API is contained within the `server` directory. The following folders are present in the project:

- **fields/**: Contains the field definitions for the GraphQL root fields.
- **generated/**: Holds the generated GraphQL types derived from the schema using the GraphQL code generator CLI.
- **tests/**: Contains the test cases written using Jest.
- **types/**: Contains the base GraphQL types.

#### Important Files

- **`remote.ts`**: Handles the auto-generated MongoDB Atlas schema, ignoring fields that have migrated to the local GraphQL schema.
- **`local.ts`**: Handles the local GraphQL schema, where migrated fields from the remote schema are added. These fields are ignored in `remote.ts`.
- **`schema.ts`**: Combines the remote and local schemas into the final schema using **schema stitching** from GraphQL Tools.
- **`netlify/functions/graphql.ts`**: Sets up the **GraphQL server** and exposes it as a **Netlify function**, loading the schema from `schema.ts`.
-
### Running Tests

To run Jest tests locally:

```sh
npm run test:api
```

It is recommended to install the Jest extension for VS Code to enhance the testing experience.

### Running Code Generation

To run the GraphQL code generation CLI to generate TypeScript typings from the schema, allowing statically typed Apollo client mutations and queries, and statically typed server code:

```sh
npm run codegen
```

### Schema and API Stitching

> [!IMPORTANT]
The API previously relied on the MongoDB Atlas Realm GraphQL API to fetch data, [but now that it has been deprecated](https://www.mongodb.com/developer/products/atlas/deprecating-mongodb-atlas-graphql-hosting-services/), we have migrated to a new API that we implement in this codebase. During the migration from Realm to our API, we stitch two GraphQL schemas: the auto-generated one from Atlas and the one defined in this codebase. These schemas can be found in the `remote.ts` and `local.ts` files respectively. The migration process involves progressively transferring GraphQL fields and functionality from the remote schema to the local one. The final combined schema is found in `schema.ts`.

### Email notifications

#### Subscription types

- **All**: This subscription type is not defined yet.
```
{
"userId": "63320ce63ec803072c9f529c"
"type": "all",
}
```
- **Incident**: Users with this subscription type will be notified when the incident associated is updated. This subscription type needs an incident_id value associated.
```
{
"userId": "63320ce63ec803072c9f529c"
"type": "incident",
"incident_id": 10,
}
```
- **New Incident**: Users with this subscription type will be notified when a new Incident is created. The notification will be sent after finish the next site build when the Incident page is actually created.
```
{
"userId": "63320ce63ec803072c9f529c"
"type": "new-incidents",
}
```
- **Entities**: Users can subscribe to a specific Entity. The user with this subscription type will be notified when a new Incident associated with an specific Entity is created or when an existing Incident is updated to be associated with that Entity.
```
{
"userId": "63320ce63ec803072c9f529c",
"type": "entity"
"entityId": "openai",
}
```
- **Submission Promoted**: Users that submit a new Incident Report are automatically subscribed to its promotion. Once the submission has been approved by an editor, the user will receive an email informing that the submission is now an incident/issue/report.
```
{
"userId": "63320ce63ec803072c9f529c",
"type": "submission-promoted"
}
```

These subscription types are also documented in [subscriptions.js](site/gatsby-site/src/utils/subscriptions.js) file.

#### Sending Email Notifications

[MailerSend](https://www.mailersend.com/) is used to send email notifications.

Email notifications to New Incidents (subscription type **New Incident**), Incident updates (subscription type **Incident**) and Submission Promoted (subscription type **Submission Promoted**) are sent when the next build finishes. This is because we have to wait until the new Incident page is generated and accessible.
When a new Incident is created or updates, a pending notification item is saved into the `notifications` DB collection with `processed=false` field.
And finally, as part of the site build process, we processed all pending notifications (`processed=false`), send the emails to all recipients, and update the items with `processed=true` and `sentDate=[now]`.

#### Notifications collection definition

- **Incident Updated**
```
{
"type": "incident-updated",
"incident_id": 374,
"processed": false
}
```
- **New Incident Report**
```
{
"type": "new-report-incident",
"incident_id": 374,
"report_number": 2172,
"processed": false
}
```
- **New Incident**
```
{
"type": "new-incidents",
"incident_id": 374,
"processed": false
}
```
- **Entities**
```
{
"type": "entity",
"incident_id": 374,
"entity_id": "openai",
"isUpdate": true,
"processed": false
}
```
- **Submission Promoted**
```
{
"type": "submission-promoted",
"incident_id": 374,
"processed": false
}
```
Loading
Loading