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

Add used card to money transfers

parent 72c54232
Branches
No related tags found
No related merge requests found
-- AlterTable
ALTER TABLE "MoneyTransfer" ADD COLUMN "card" TEXT NOT NULL DEFAULT '';
...@@ -97,4 +97,5 @@ model MoneyTransfer { ...@@ -97,4 +97,5 @@ model MoneyTransfer {
fromId Int fromId Int
to User @relation("to", fields: [toId], references: [id]) to User @relation("to", fields: [toId], references: [id])
toId Int toId Int
card String @default("")
} }
...@@ -44,7 +44,7 @@ export async function POST(event) { ...@@ -44,7 +44,7 @@ export async function POST(event) {
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);
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, target.id, data.amount); const { from } = await transferMoney(user.id, data.card, target.id, data.amount);
// 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){
......
...@@ -29,7 +29,7 @@ export async function buyArticles(userId, card, items, noBalance=false) { ...@@ -29,7 +29,7 @@ export async function buyArticles(userId, card, items, noBalance=false) {
return { user, itemTransactions }; return { user, itemTransactions };
} }
export async function transferMoney(fromId, toId, amount){ export async function transferMoney(fromId, card, toId, amount){
const [to, from] = await db.$transaction([ const [to, from] = await db.$transaction([
db.user.update({ db.user.update({
where: { id: toId }, where: { id: toId },
...@@ -40,6 +40,7 @@ export async function transferMoney(fromId, toId, amount){ ...@@ -40,6 +40,7 @@ export async function transferMoney(fromId, toId, amount){
moneyTransfersReceived: { moneyTransfersReceived: {
create: { create: {
amount, amount,
card,
from: { connect: { id: fromId } } from: { connect: { id: fromId } }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment