Skip to content
Snippets Groups Projects
Commit bd17e55a authored by Aaron Dötsch's avatar Aaron Dötsch
Browse files

Implement transfer send and receive notifications

parent 94b19b8e
No related branches found
No related tags found
No related merge requests found
...@@ -62,9 +62,12 @@ export async function POST(event) { ...@@ -62,9 +62,12 @@ export async function POST(event) {
const user = event.locals.user; 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.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}}); 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}}); 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); 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 // 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}}); return new Response(JSON.stringify(from), {headers: {'content-type': 'application/json', 'status': 200}});
}catch(e){ }catch(e){
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment