-
-
Notifications
You must be signed in to change notification settings - Fork 467
Feat: email notifications aggregation #2027
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
246965c
feat: add notifications aggregation
chrismclarke a7005bc
Merge branch 'chore/emulator-data-update-2022-12' of https://github.c…
chrismclarke b652111
chore: code tidying
chrismclarke 39cd878
Merge branch 'chore/emulator-data-update-2022-12' of https://github.c…
chrismclarke 3939471
feat: add admin notifications page
chrismclarke a850143
Merge branch 'master' into feat/email-notifications-backend
chrismclarke e80ac92
Merge branch 'master' into feat/email-notifications-backend
chrismclarke 4f97d41
Merge branch 'master' into feat/email-notifications-backend
chrismclarke 846a8f9
Merge branch 'master' into feat/email-notifications-backend
chrismclarke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| exports.user = require('./user.aggregations') | ||
| exports.userNotifications = require('./userNotifications.aggregations') |
42 changes: 42 additions & 0 deletions
42
functions/src/aggregations/userNotifications.aggregations.ts
This file contains hidden or 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,42 @@ | ||
| import * as functions from 'firebase-functions' | ||
| import { DB_ENDPOINTS, IUserDB } from '../models' | ||
| import { handleDBAggregations, VALUE_MODIFIERS } from './common.aggregations' | ||
| import type { IAggregation } from './common.aggregations' | ||
|
|
||
| interface INotificationAggregation extends IAggregation { | ||
| sourceFields: (keyof IUserDB)[] | ||
| } | ||
|
|
||
| const aggregations: INotificationAggregation[] = [ | ||
| // When a user's list of notifications changes reflect to aggregation | ||
| { | ||
| sourceCollection: 'users', | ||
| sourceFields: ['notifications', 'notification_settings'], | ||
| changeType: 'updated', | ||
| targetCollection: 'user_notifications', | ||
| targetDocId: 'emails_pending', | ||
| process: ({ dbChange }) => { | ||
| const user: IUserDB = dbChange.after.data() as any | ||
| const { _id, _authID, notification_settings, notifications } = user | ||
| const emailFrequency = notification_settings?.emailFrequency || null | ||
| const pending = notifications.filter((n) => !n.notified && !n.read) | ||
| // remove user from list if they do not have emails enabled or no pending notifications | ||
| if (!emailFrequency || pending.length === 0) { | ||
| return { | ||
| [_id]: VALUE_MODIFIERS.delete(), | ||
| } | ||
| } | ||
| // return list of pending notifications alongside metadata | ||
| return { | ||
| [_id]: { _authID, emailFrequency, notifications: pending }, | ||
| } | ||
| }, | ||
| }, | ||
| ] | ||
|
|
||
| /** Watch changes to all user docs and apply aggregations */ | ||
| exports.default = functions.firestore | ||
| .document(`${DB_ENDPOINTS.users}/{id}`) | ||
| .onUpdate((change) => { | ||
| return handleDBAggregations(change, aggregations) | ||
| }) | ||
This file contains hidden or 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 hidden or 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 hidden or 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,99 @@ | ||
| import styled from '@emotion/styled' | ||
| import { observer } from 'mobx-react' | ||
| import { NotificationItem } from 'oa-components' | ||
| import { useCallback, useEffect, useState } from 'react' | ||
| import { Box, Text } from 'theme-ui' | ||
| import { useDB } from 'src/App' | ||
| import type { INotification, INotificationSettings } from 'src/models' | ||
| import { getFormattedNotificationMessage } from 'src/pages/common/Header/getFormattedNotifications' | ||
| import Table from '../components/Table/Table' | ||
| import type { ICellRenderProps, ITableProps } from '../components/Table/Table' | ||
|
|
||
| interface IPendingEmails { | ||
| _authID: 'string' | ||
| emailFrequency?: INotificationSettings['emailFrequency'] | ||
| notifications: INotification[] | ||
| } | ||
| type IPendingEmailsDBDoc = Record<string, IPendingEmails> | ||
|
|
||
| const EMAILS_PENDING_COLUMNS: ITableProps<IPendingEmails>['columns'] = [ | ||
| { | ||
| Header: 'Auth ID', | ||
| accessor: '_authID', | ||
| minWidth: 100, | ||
| }, | ||
| { | ||
| Header: 'Frequency', | ||
| accessor: 'emailFrequency', | ||
| minWidth: 100, | ||
| }, | ||
| { | ||
| Header: 'Notifications', | ||
| accessor: 'notifications', | ||
| minWidth: 140, | ||
| }, | ||
| ] | ||
| const NotificationListContainer = styled(Box)` | ||
| overflow: auto; | ||
| height: 6rem; | ||
| ` | ||
|
|
||
| const AdminNotifictions = observer(() => { | ||
| const { db } = useDB() | ||
| const [emailsPending, setEmailsPending] = useState<IPendingEmails[]>([]) | ||
|
|
||
| // Load list of pending approvals on mount only, dependencies empty to avoid reloading | ||
| useEffect(() => { | ||
| getPendingEmails() | ||
| }, []) | ||
|
|
||
| const getPendingEmails = useCallback(async () => { | ||
| db.collection<IPendingEmailsDBDoc>('user_notifications') | ||
| .doc('emails_pending') | ||
| .stream() | ||
| .subscribe((emailsPending) => { | ||
| if (emailsPending) { | ||
| const values = Object.values(emailsPending) | ||
| if (values.length > 0) { | ||
| setEmailsPending(values) | ||
| } | ||
| } | ||
| }) | ||
| }, []) | ||
|
|
||
| /** Function applied to render each table row cell */ | ||
| const RenderContent: React.FC<ICellRenderProps> = ( | ||
| props: ICellRenderProps, | ||
| ) => { | ||
| const { col } = props | ||
| const { field, value } = col | ||
| const tableField = field as keyof IPendingEmails | ||
| if (!value) return null | ||
| if (tableField === 'notifications') { | ||
| const notifications = value as INotification[] | ||
| return ( | ||
| <NotificationListContainer> | ||
| {notifications.map((n) => ( | ||
| <NotificationItem key={n._id} type={n.type}> | ||
| {getFormattedNotificationMessage(n)} | ||
| </NotificationItem> | ||
| ))} | ||
| </NotificationListContainer> | ||
| ) | ||
| } | ||
| return <Text>{`${value}`}</Text> | ||
| } | ||
|
|
||
| return ( | ||
| <> | ||
| <h2>Admin Notifictions</h2> | ||
| <h4>Pending Emails</h4> | ||
| <Table | ||
| data={emailsPending} | ||
| columns={EMAILS_PENDING_COLUMNS} | ||
| rowComponent={RenderContent} | ||
| /> | ||
| </> | ||
| ) | ||
| }) | ||
| export default AdminNotifictions |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we be using the
notification_settings.enabledfield here instead/also to filter on type of notif?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @evakill , I hadn't thought about how it relates to filtering the specific types of notifications users are subscribed to or not. I'm assuming it might be a future PR anyway that handles filtering, so for now I'd say probably better to keep this simple and let all notifications pass through, and then filter out at a later stage (likely before hitting the user profile notifications, so that they don't receive on web and before we reach this aggregation step)