From 03a150b4edc58810e56fc6977ec126e41f28e9a0 Mon Sep 17 00:00:00 2001 From: Alexis Tyler Date: Wed, 30 Jun 2021 21:40:30 +0930 Subject: [PATCH] feat: add notifications mutation --- app/graphql/resolvers/index.ts | 2 + app/graphql/resolvers/mutation/index.ts | 9 ++++ .../resolvers/mutation/notifications.ts | 42 +++++++++++++++++++ app/graphql/schema/index.ts | 1 + .../types/notifications/notifications.graphql | 28 +++++++++++++ 5 files changed, 82 insertions(+) create mode 100644 app/graphql/resolvers/mutation/index.ts create mode 100644 app/graphql/resolvers/mutation/notifications.ts create mode 100644 app/graphql/schema/types/notifications/notifications.graphql diff --git a/app/graphql/resolvers/index.ts b/app/graphql/resolvers/index.ts index a251e4e1c..8bfc5da7c 100644 --- a/app/graphql/resolvers/index.ts +++ b/app/graphql/resolvers/index.ts @@ -8,6 +8,7 @@ import GraphQLLong from 'graphql-type-long'; import GraphQLUUID from 'graphql-type-uuid'; import { GraphQLDateTime } from 'graphql-iso-date'; import { Query } from './query'; +import { Mutation } from './mutation'; import { Subscription } from './subscription'; import { UserAccount } from './user-account'; @@ -18,6 +19,7 @@ export const DateTime = GraphQLDateTime; export { Query, + Mutation, Subscription, UserAccount }; diff --git a/app/graphql/resolvers/mutation/index.ts b/app/graphql/resolvers/mutation/index.ts new file mode 100644 index 000000000..1d3e41447 --- /dev/null +++ b/app/graphql/resolvers/mutation/index.ts @@ -0,0 +1,9 @@ +/*! + * Copyright 2019-2020 Lime Technology Inc. All rights reserved. + * Written by: Alexis Tyler + */ +import notifications from './notifications'; + +export const Mutation = { + notifications +}; diff --git a/app/graphql/resolvers/mutation/notifications.ts b/app/graphql/resolvers/mutation/notifications.ts new file mode 100644 index 000000000..08541e465 --- /dev/null +++ b/app/graphql/resolvers/mutation/notifications.ts @@ -0,0 +1,42 @@ +/*! + * Copyright 2021 Lime Technology Inc. All rights reserved. + * Written by: Alexis Tyler + */ + +import { ensurePermission } from '../../../core/utils'; +import { mothership } from '../../../mothership/subscribe-to-servers'; +import { Context } from '../../schema/utils'; + +export default async (_: unknown, __: unknown, context: Context) => { + const { user } = context; + + // Check permissions + ensurePermission(user, { + resource: 'notifications', + action: 'create', + possession: 'own' + }); + + console.log('notification mutation', { _, __, context }); + + const notification = {}; + + // Prepare query + const query = mothership.request({ + query: 'mutation ($notification: notificationInput) {\n sendNotification(notification: $notification)\n}\n', + variables: { + notification + } + }); + + // Send notification to mothership + query.subscribe({ + next: async ({ data, errors }) => { + if (!errors || errors.length === 0) { + return; + } + + console.log('FAILED_SENDING_NOTIFICATION', errors); + } + }); +}; diff --git a/app/graphql/schema/index.ts b/app/graphql/schema/index.ts index 5ed05c751..8e95f7a6c 100644 --- a/app/graphql/schema/index.ts +++ b/app/graphql/schema/index.ts @@ -27,6 +27,7 @@ const files = [ './dist/types/graphql/schema/types/info/os.graphql', './dist/types/graphql/schema/types/info/system.graphql', './dist/types/graphql/schema/types/info/versions.graphql', + './dist/types/graphql/schema/types/notifications/notifications.graphql', './dist/types/graphql/schema/types/owner/owner.graphql', './dist/types/graphql/schema/types/plugins/plugin.graphql', './dist/types/graphql/schema/types/registration/registration.graphql', diff --git a/app/graphql/schema/types/notifications/notifications.graphql b/app/graphql/schema/types/notifications/notifications.graphql new file mode 100644 index 000000000..0616cd810 --- /dev/null +++ b/app/graphql/schema/types/notifications/notifications.graphql @@ -0,0 +1,28 @@ +input NotificationInput { + title: String + subject: String + description: String + importance: Importance! + link: String +} + +type Mutation { + sendNotification(notification: NotificationInput!): Notification +} + +enum Importance { + # Logs + info + # Warnings + warning + # Errors + alert +} + +type Notification { + title: String! + subject: String! + description: String! + importance: Importance! + link: String! +} \ No newline at end of file