From bd17e55ab6cbb1220b957d9f3ae9f30c86383668 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:21:33 +0200
Subject: [PATCH] Implement transfer send and receive notifications

---
 src/routes/api/[slug]/+server.js | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/src/routes/api/[slug]/+server.js b/src/routes/api/[slug]/+server.js
index 2aea31c..a7dcebb 100644
--- a/src/routes/api/[slug]/+server.js
+++ b/src/routes/api/[slug]/+server.js
@@ -62,9 +62,12 @@ export async function POST(event) {
 				const user = event.locals.user;
 				if(user.id == data.target) return new Response(JSON.stringify({message: "Can't transfer to yourself"}), {headers: {'content-type': 'application/json', 'status': 400}});
 				if(user.balance < data.amount) return new Response(JSON.stringify({message: "Not enough balance"}), {headers: {'content-type': 'application/json', 'status': 400}});
-				const target = await getUserById(data.target);
+				const target = await getUserById(data.target, false, true);
 				if(!target) return new Response(JSON.stringify({message: "Target not found"}), {headers: {'content-type': 'application/json', 'status': 404}});
 				const { from } = await transferMoney(user.id, user.card, target.id, data.amount);
+				// no need to await
+				sendNotification(user, NotificationType.SEND_TRANSFER, {amount: data.amount, balanceBefore: user.balance, balanceAfter: user.balance - data.amount, receiver: {name: target.name, id: target.id}});
+				sendNotification(target, NotificationType.RECEIVE_TRANSFER, {amount: data.amount, balanceBefore: target.balance, balanceAfter: target.balance + data.amount, sender: {name: user.name, id: user.id}});
 				// don't return the to-user, because it contains the target's information
 				return new Response(JSON.stringify(from), {headers: {'content-type': 'application/json', 'status': 200}});
 			}catch(e){
-- 
GitLab