Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
data : {
item : q.Select("ref", q.Var("item")),
price : q.Var("itemPrice"),
buyer : q.Select("ref", q.Var("buyer")),
seller : q.Select("ref", q.Var("seller"))
}
}),
// Update the balances of the selling player and the purchasing player.
q.Update(q.Select("ref", q.Var("buyer")), {
data : {
credits : q.Subtract(q.Var("buyerBalance"), q.Var("itemPrice"))
}
}),
q.Update(q.Select("ref", q.Var("seller")), {
data : {
credits : q.Add(q.Select(["data", "credits"], q.Var("seller")), q.Var("itemPrice"))
}
}),
// Update the item with the new owner.
q.Update(q.Select("ref", q.Var("item")), {
data : {
owner : q.Select("ref", q.Var("buyer")),
for_sale : false
}
}),
"purchase success"
)
)
)
)))
);
}
"purchase failed: insufficient funds",
// All clear! Preconditions passed.
q.Do(
// Write a purchase record.
q.Create(q.Class("purchases"), {
data : {
item : q.Select("ref", q.Var("item")),
price : q.Var("itemPrice"),
buyer : q.Select("ref", q.Var("buyer")),
seller : q.Select("ref", q.Var("seller"))
}
}),
// Update the balances of the selling player and the purchasing player.
q.Update(q.Select("ref", q.Var("buyer")), {
data : {
credits : q.Subtract(q.Var("buyerBalance"), q.Var("itemPrice"))
}
}),
q.Update(q.Select("ref", q.Var("seller")), {
data : {
credits : q.Add(q.Select(["data", "credits"], q.Var("seller")), q.Var("itemPrice"))
}
}),
// Update the item with the new owner.
q.Update(q.Select("ref", q.Var("item")), {
data : {
owner : q.Select("ref", q.Var("buyer")),
for_sale : false
}
}),
"purchase success"
)
for_sale : false
}
}),
"item removed from sale"
),
// Check the credit balance of the purchasing player to ensure
// they have available funds.
q.If(q.LT(q.Var("buyerBalance"), q.Var("itemPrice")),
"purchase failed: insufficient funds",
// All clear! Preconditions passed.
q.Do(
// Write a purchase record.
q.Create(q.Class("purchases"), {
data : {
item : q.Select("ref", q.Var("item")),
price : q.Var("itemPrice"),
buyer : q.Select("ref", q.Var("buyer")),
seller : q.Select("ref", q.Var("seller"))
}
}),
// Update the balances of the selling player and the purchasing player.
q.Update(q.Select("ref", q.Var("buyer")), {
data : {
credits : q.Subtract(q.Var("buyerBalance"), q.Var("itemPrice"))
}
}),
q.Update(q.Select("ref", q.Var("seller")), {
data : {
credits : q.Add(q.Select(["data", "credits"], q.Var("seller")), q.Var("itemPrice"))
}
}),
// Update the item with the new owner.
(row) =>
q.Let({doc : q.Get(row)}, {
ref : q.Select(["ref"], q.Var("doc")),
data : {
label : q.Select(["data","label"], q.Var("doc")),
price : q.Select(["data","price"], q.Var("doc")),
owner : q.Select(["data","owner"], q.Var("doc")),
owner_name :
q.Select(["data","name"],
q.Get(q.Select(["data","owner"], q.Var("doc"))))
}
})
)