Skip to content

Commit

Permalink
Merge pull request #258 from msupply-foundation/255-parameters-in-eve…
Browse files Browse the repository at this point in the history
…nts-table

255 parameters in events table
  • Loading branch information
lache-melvin authored Nov 22, 2023
2 parents ae7f78e + cacc8e4 commit 37a53fb
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ impl NotificationEventNode {
self.row().send_attempts
}

pub async fn context(&self) -> Option<String> {
self.row().context.to_owned()
}

pub async fn notification_config(
&self,
ctx: &Context<'_>,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- This file should undo anything in `up.sql`
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE notification_event ADD COLUMN context TEXT;
2 changes: 2 additions & 0 deletions backend/repository/src/db_diesel/notification_event_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ table! {
retry_at -> Nullable<Timestamp>,
send_attempts -> Integer,
error_message -> Nullable<Text>,
context -> Nullable<Text>,
}
}

Expand Down Expand Up @@ -69,6 +70,7 @@ pub struct NotificationEventRow {
pub retry_at: Option<chrono::NaiveDateTime>,
pub send_attempts: i32,
pub error_message: Option<String>,
pub context: Option<String>, // JSON object, the tera context for the event
}

pub struct NotificationEventRowRepository<'a> {
Expand Down
7 changes: 7 additions & 0 deletions backend/service/src/notification/enqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ pub fn create_notification_events(
notification_config_id: config_id.clone(),
notification_type,
retry_at: None,
context: match serde_json::to_string(&tera_context.clone().into_json()) {
Ok(context) => Some(context),
Err(e) => {
log::error!("Failed to stringify tera context: {:?}", e);
None
}
},
..Default::default()
};

Expand Down
3 changes: 3 additions & 0 deletions frontend/packages/common/src/intl/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"label.comment": "Comment",
"label.contact-name": "Contact Name",
"label.country": "Country",
"label.created": "Created",
"label.date": "Date",
"label.date-created": "Date Created",
"label.description": "Description",
Expand Down Expand Up @@ -141,6 +142,7 @@
"label.total-value": "Total Value",
"label.type": "Type",
"label.unit": "Unit",
"label.updated": "Updated",
"label.upload": "Upload",
"label.username": "Username",
"label.website": "Website",
Expand Down Expand Up @@ -169,6 +171,7 @@
"messages.import-error": "Errors were encountered during processing, please fix and re-upload",
"messages.import-invalid-file": "An invalid file was supplied. Make sure you have the right file format and try again",
"messages.loading": "Loading",
"messages.no-error": "No Error",
"messages.no-errors": "No errors found",
"messages.no-log-entries": "No log entries available",
"messages.nothing-selected": "Nothing selected",
Expand Down
3 changes: 3 additions & 0 deletions frontend/packages/common/src/intl/locales/en/system.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"label.parameters-as-json": "JSON Parameters",
"label.error": "Error",
"label.filter-by-notification-config": "Filter by Notification Config",
"label.generated-notification": "Generated Notification",
"label.invite-user": "Invite User",
"label.new-recipient": "New Recipient",
"label.new-recipient-list": "New Recipient List",
Expand All @@ -51,6 +52,7 @@
"label.notification-type-EMAIL": "Email",
"label.notification-type-TELEGRAM": "Telegram",
"label.notification-type-UNKNOWN": "ERROR-UNSUPPORTED-NOTIFICATION-TYPE",
"label.parameters-query-results": "Parameters and Query Results",
"label.password-strength": "Password Strength",
"label.password-warning": "Warning",
"label.phone-number": "Phone Number",
Expand All @@ -62,6 +64,7 @@
"label.send-password-reset": "Send Password Reset",
"label.setup-notification": "Setup {{type}} Notification",
"label.telegram": "Telegram",
"label.to": "To",
"label.update-contact": "Update Contact",
"label.update-details": "Update Details",
"label.update-profile": "Update Profile",
Expand Down
1 change: 1 addition & 0 deletions frontend/packages/common/src/types/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,7 @@ export type NotificationEventFilterInput = {

export type NotificationEventNode = {
__typename: 'NotificationEventNode';
context?: Maybe<Scalars['String']['output']>;
createdAt: Scalars['DateTime']['output'];
errorMessage?: Maybe<Scalars['String']['output']>;
id: Scalars['String']['output'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from 'packages/common/src';
import { configRoute } from '../../Notifications/navigate';
import { NotificationStatusChip } from '../components/NotificationStatusChip';
import { useParsedEventContext } from './eventContext';

export const DetailView = () => {
const t = useTranslation('system');
Expand All @@ -37,6 +38,8 @@ export const DetailView = () => {
const { data, isLoading } = useNotificationEvents(queryParams);
const entity = data?.nodes[0];

const eventContext = useParsedEventContext(entity?.context);

useEffect(() => {
const listName = entity?.title;
if (!suffix && listName) {
Expand Down Expand Up @@ -81,18 +84,16 @@ export const DetailView = () => {
{entity?.errorMessage ? (
<TextArea value={entity?.errorMessage} />
) : (
<Typography variant="body1">No Error</Typography>
<Typography variant="body1">{t('messages.no-error')}</Typography>
)}
</Box>
<Box flex={1} justifyContent="right" display="flex" gap={1}>
<Stack gap={1}>
<Typography variant="body1">
Created:
<RelativeTimeDate d={entity?.createdAt} />
{t('label.created')}: <RelativeTimeDate d={entity?.createdAt} />
</Typography>
<Typography variant="body1">
Updated:
<RelativeTimeDate d={entity?.updatedAt} />
{t('label.updated')}: <RelativeTimeDate d={entity?.updatedAt} />
</Typography>
</Stack>
</Box>
Expand All @@ -109,9 +110,12 @@ export const DetailView = () => {
<BasicSpinner />
) : (
<>
<Typography variant="h6">Generated Notification</Typography>
<Typography variant="h6">
{t('label.generated-notification')}
</Typography>
<TextArea
label="To"
label={t('label.to')}
InputLabelProps={{ shrink: true }} // label always visisble
minRows={1}
maxRows={1}
sx={{
Expand All @@ -121,8 +125,10 @@ export const DetailView = () => {
}}
value={`${entity?.toAddress} (${entity?.notificationType})`}
/>

<TextArea
label="Title"
label={t('label.title')}
InputLabelProps={{ shrink: true }}
minRows={1}
maxRows={1}
sx={{
Expand All @@ -134,7 +140,8 @@ export const DetailView = () => {
/>

<TextArea
label="Message"
label={t('label.message')}
InputLabelProps={{ shrink: true }}
minRows={2}
maxRows={25}
sx={{
Expand All @@ -144,6 +151,19 @@ export const DetailView = () => {
}}
value={entity?.message}
/>

<TextArea
label={t('label.parameters-query-results')}
InputLabelProps={{ shrink: true }}
minRows={2}
maxRows={15}
sx={{
border: '1px solid',
borderColor: 'grey.100',
width: '100%',
}}
value={JSON.stringify(eventContext, null, 2)}
/>
</>
)}
</Box>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export type EventContext = {
[key: string]: unknown;
};

export const useParsedEventContext = (
context: string | null | undefined
): EventContext => {
if (!context) return {};
try {
return JSON.parse(context);
} catch (e) {
return {};
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as Types from '@notify-frontend/common';
import { GraphQLClient } from 'graphql-request';
import * as Dom from 'graphql-request/dist/types.dom';
import gql from 'graphql-tag';
export type NotificationEventRowFragment = { __typename: 'NotificationEventNode', id: string, title: string, sentAt?: string | null, message: string, errorMessage?: string | null, createdAt: string, status: Types.EventStatus, toAddress: string, updatedAt: string, notificationType: Types.NotificationTypeNode, notificationConfigId?: string | null, notificationConfig?: { __typename: 'NotificationConfigNode', title: string, kind: Types.ConfigKind } | null };
export type NotificationEventRowFragment = { __typename: 'NotificationEventNode', id: string, title: string, sentAt?: string | null, message: string, errorMessage?: string | null, createdAt: string, status: Types.EventStatus, toAddress: string, updatedAt: string, notificationType: Types.NotificationTypeNode, notificationConfigId?: string | null, context?: string | null, notificationConfig?: { __typename: 'NotificationConfigNode', title: string, kind: Types.ConfigKind } | null };

export type NotificationEventsQueryVariables = Types.Exact<{
filter?: Types.InputMaybe<Types.NotificationEventFilterInput>;
Expand All @@ -12,7 +12,7 @@ export type NotificationEventsQueryVariables = Types.Exact<{
}>;


export type NotificationEventsQuery = { __typename: 'FullQuery', notificationEvents: { __typename: 'NotificationEventConnector', totalCount: number, nodes: Array<{ __typename: 'NotificationEventNode', id: string, title: string, sentAt?: string | null, message: string, errorMessage?: string | null, createdAt: string, status: Types.EventStatus, toAddress: string, updatedAt: string, notificationType: Types.NotificationTypeNode, notificationConfigId?: string | null, notificationConfig?: { __typename: 'NotificationConfigNode', title: string, kind: Types.ConfigKind } | null }> } };
export type NotificationEventsQuery = { __typename: 'FullQuery', notificationEvents: { __typename: 'NotificationEventConnector', totalCount: number, nodes: Array<{ __typename: 'NotificationEventNode', id: string, title: string, sentAt?: string | null, message: string, errorMessage?: string | null, createdAt: string, status: Types.EventStatus, toAddress: string, updatedAt: string, notificationType: Types.NotificationTypeNode, notificationConfigId?: string | null, context?: string | null, notificationConfig?: { __typename: 'NotificationConfigNode', title: string, kind: Types.ConfigKind } | null }> } };

export const NotificationEventRowFragmentDoc = gql`
fragment NotificationEventRow on NotificationEventNode {
Expand All @@ -31,6 +31,7 @@ export const NotificationEventRowFragmentDoc = gql`
title
kind
}
context
}
`;
export const NotificationEventsDocument = gql`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fragment NotificationEventRow on NotificationEventNode {
title
kind
}
context
}

query NotificationEvents(
Expand Down

0 comments on commit 37a53fb

Please sign in to comment.