From b97d9bbf0451d85167618ba95973206e38bf9849 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Aaron=20D=C3=B6tsch?= <aaron@fsmpi.rwth-aachen.de>
Date: Sat, 5 Aug 2023 19:19:12 +0200
Subject: [PATCH] Implement withdraw and deposit notificaions

---
 src/routes/admin/user/[id]/+page.server.js | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/src/routes/admin/user/[id]/+page.server.js b/src/routes/admin/user/[id]/+page.server.js
index 6d438b4..e40ded3 100644
--- a/src/routes/admin/user/[id]/+page.server.js
+++ b/src/routes/admin/user/[id]/+page.server.js
@@ -3,6 +3,8 @@ import { fail } from "@sveltejs/kit";
 import { createTransaction } from "../../api/transactions";
 import { ChannelType } from "$lib/server/notifications/channelTypes";
 import { createNotificationChannel, updateNotificationChannel } from "../../api/notificationChannels";
+import { sendNotification } from "$lib/server/notifications/handler";
+import { NotificationType } from "$lib/notifications/notificationTypes";
 
 export const load = async ({ params }) => {
 	const { id } = params;
@@ -42,6 +44,21 @@ export const actions = {
 		const amount = parseInt(data.get('amount'));
 		if(Number.isNaN(amount) || !Number.isInteger(amount)) return fail(400, { error: "Invalid form data" });
 		const user = await createTransaction(userId, amount, event.locals.user.id);
+		if(amount < 0){
+			// no need to await
+			sendNotification(user.id, NotificationType.WITHDRAW, {
+				amount: -amount,
+				balanceAfter: user.balance,
+				balanceBefore: user.balance - amount
+			});
+		}else{
+			// no need to await
+			sendNotification(user.id, NotificationType.DEPOSIT, {
+				amount,
+				balanceAfter: user.balance,
+				balanceBefore: user.balance - amount
+			});
+		}
 		delete user.createdAt;
 		delete user.updatedAt;
 		return { success: !!user, user };
-- 
GitLab