forked from inflection-zone/rean-admin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from REAN-Foundation/notifications_change
Notifications change
- Loading branch information
Showing
7 changed files
with
272 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,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', | ||
} |
This file contains 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 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,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<typeof createNotificationSchema>; | ||
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 | ||
); | ||
} | ||
}; |
This file contains 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