From a4dd237e47844f946ff7624398a3a4aefd15780f Mon Sep 17 00:00:00 2001 From: RupaliD Date: Sat, 14 Dec 2024 13:55:20 +0530 Subject: [PATCH 1/2] Implementation of send notification --- src/lib/options/aha.options.ts | 4 +- src/lib/options/gmu.options.ts | 2 +- src/lib/options/rean.options.ts | 2 +- src/lib/types/notification.topics.ts | 22 +++ .../api/services/reancare/notifications.ts | 19 +++ .../[userId]/notifications/+page.server.ts | 87 +++++++++-- .../users/[userId]/notifications/+page.svelte | 144 ++++++++++++++++-- 7 files changed, 252 insertions(+), 28 deletions(-) create mode 100644 src/lib/types/notification.topics.ts diff --git a/src/lib/options/aha.options.ts b/src/lib/options/aha.options.ts index 440ce4c4..5f873a49 100755 --- a/src/lib/options/aha.options.ts +++ b/src/lib/options/aha.options.ts @@ -126,7 +126,7 @@ export const Options: FeatureOptions[] = [ }, //.............................. { - Name: 'Miscellaneous', + Name: 'Add-ons', Enabled: true }, { @@ -147,7 +147,7 @@ export const Options: FeatureOptions[] = [ }, { Name: 'Newsfeeds', - Enabled: true + Enabled: false }, //.............................. { diff --git a/src/lib/options/gmu.options.ts b/src/lib/options/gmu.options.ts index 347a7cd7..cdc01a3d 100644 --- a/src/lib/options/gmu.options.ts +++ b/src/lib/options/gmu.options.ts @@ -126,7 +126,7 @@ export const Options: FeatureOptions[] = [ }, //.............................. { - Name: 'Miscellaneous', + Name: 'Add-ons', Enabled: false }, { diff --git a/src/lib/options/rean.options.ts b/src/lib/options/rean.options.ts index a82b5a8f..ac09dbfb 100755 --- a/src/lib/options/rean.options.ts +++ b/src/lib/options/rean.options.ts @@ -126,7 +126,7 @@ export const Options: FeatureOptions[] = [ }, //.............................. { - Name: 'Miscellaneous', + Name: 'Add-ons', Enabled: true }, { diff --git a/src/lib/types/notification.topics.ts b/src/lib/types/notification.topics.ts new file mode 100644 index 00000000..60425c91 --- /dev/null +++ b/src/lib/types/notification.topics.ts @@ -0,0 +1,22 @@ +export enum NotificationTopics { + All_Users = 'All Users', + Cholesterol = 'Cholesterol', + Stroke = 'Stroke', + SMBP = 'SMBP', + CholesterolMini = 'Cholesterol Mini', + HFMotivator = 'HF Motivator', + KenyaMaternityPhase2 = 'Kenya Maternity Phase 2', + HeartFailure = 'Heart Failure' +} + +export enum NotificationTypes{ + General = 'General', + Email = 'Email', + SMS = 'SMS', + WebPush = 'Web Push', + MobilePush = 'Mobile Push', + Webhook = 'Webhook', + WhatsApp = 'Whats App', + Telegram = 'Telegram', + Slack = 'Slack', +} \ No newline at end of file diff --git a/src/routes/api/services/reancare/notifications.ts b/src/routes/api/services/reancare/notifications.ts index 1a14fe32..558eb6af 100644 --- a/src/routes/api/services/reancare/notifications.ts +++ b/src/routes/api/services/reancare/notifications.ts @@ -71,3 +71,22 @@ export const deleteNotification = async (sessionId: string, notificationId: stri const url = BACKEND_API_URL + `/general/notifications/${notificationId}`; return await del(sessionId, url, true, API_CLIENT_INTERNAL_KEY); }; + +export const sendNotification = async ( + sessionId: string, + topic:string, + title: string, + Body: string, + type: string, + url: string +) => { + const body = { + Topic: topic, + Title: title, + Body: Body ? Body : null, + Type: type ? type : null, + url: url ? url : null + }; + const backendUrl = BACKEND_API_URL + '/user-device-details/notificaion-topic'; + return await post(sessionId, backendUrl, body, true, API_CLIENT_INTERNAL_KEY); +}; \ No newline at end of file diff --git a/src/routes/users/[userId]/notifications/+page.server.ts b/src/routes/users/[userId]/notifications/+page.server.ts index 61d871dd..8a95889f 100755 --- a/src/routes/users/[userId]/notifications/+page.server.ts +++ b/src/routes/users/[userId]/notifications/+page.server.ts @@ -1,25 +1,82 @@ import type { RequestEvent } from '@sveltejs/kit'; import { error } from '@sveltejs/kit'; import type { PageServerLoad } from './$types'; -import { searchNotifications } from '../../../api/services/reancare/notifications'; +import { createNotification, searchNotifications, sendNotification } from '../../../api/services/reancare/notifications'; //////////////////////////////////////////////////////////////////////////// -export const load: PageServerLoad = async (event: RequestEvent) => { - const sessionId = event.cookies.get('sessionId'); +// export const load: PageServerLoad = async (event: RequestEvent) => { +// const sessionId = event.cookies.get('sessionId'); - try { - const response = await searchNotifications(sessionId); - if (response.Status === 'failure' || response.HttpCode !== 200) { - throw error(response.HttpCode, response.Message); +// try { +// const response = await searchNotifications(sessionId); +// if (response.Status === 'failure' || response.HttpCode !== 200) { +// throw error(response.HttpCode, response.Message); +// } +// const notification = response.Data.NotificationRecords; +// return { +// notification, +// sessionId, +// message: response.Message +// }; +// } catch (error) { +// console.error(`Error retriving notification: ${error.message}`); +// } +// }; + +import { redirect } from 'sveltekit-flash-message/server'; +import { z } from 'zod'; +import { zfd } from 'zod-form-data'; +import { errorMessage, successMessage } from '$lib/utils/message.utils'; + +///////////////////////////////////////////////////////////////////////// + +const createNotificationSchema = zfd.formData({ + topic: z.string(), + title: z.string().min(2).max(50), + body: z.string().min(2).max(150), + type: z.string().optional(), + url: z.string().optional() +}); + +export const actions = { + createNotificationAction: async (event: RequestEvent) => { + const request = event.request; + const userId = event.params.userId; + const sessionId = event.cookies.get('sessionId'); + const formData = Object.fromEntries(await request.formData()); + type NotificationSchema = z.infer; + let result: NotificationSchema = {}; + console.log('result', result); + try { + result = createNotificationSchema.parse(formData); + console.log('result', result); + } catch (err: any) { + const { fieldErrors: errors } = err.flatten(); + console.log(errors); + const { ...rest } = formData; + return { + data: rest, + errors + }; } - const notification = response.Data.NotificationRecords; - return { - notification, + const response = await sendNotification( sessionId, - message: response.Message - }; - } catch (error) { - console.error(`Error retriving notification: ${error.message}`); - } + result.topic, + result.title, + result.body, + result.type, + result.url + ); + + if (response.Status === 'failure' || response.HttpCode !== 201) { + throw redirect(303, `/users/${userId}/notifications`, errorMessage(response.Message), event); + } + throw redirect( + 303, + `/users/${userId}/notifications`, + successMessage(`Notification sent successfully!`), + event + ); + } }; diff --git a/src/routes/users/[userId]/notifications/+page.svelte b/src/routes/users/[userId]/notifications/+page.svelte index d10e95e0..f7455ea2 100755 --- a/src/routes/users/[userId]/notifications/+page.svelte +++ b/src/routes/users/[userId]/notifications/+page.svelte @@ -1,4 +1,4 @@ - --> + + + - - + --> + + + + + + +
+ + + + + + + + + + + + + + + + + + +
Send Notification + + + +
Topic * + +
Title * + + {#if form?.errors?.title} +

{form?.errors?.title[0]}

+ {/if} +
Body * +