Skip to content

Instantly share code, notes, and snippets.

@alexrohleder
Created December 17, 2020 21:53
Show Gist options
  • Save alexrohleder/3bae7be8a093cd8049fb91e310593b96 to your computer and use it in GitHub Desktop.
Save alexrohleder/3bae7be8a093cd8049fb91e310593b96 to your computer and use it in GitHub Desktop.
create-bid-fauna.js
Query(
Lambda(
["bidderRef", "auctionId", "amount"],
Let(
{ auction: Get(Ref(Collection("auction"), Var("auctionId"))) },
If(
Equals(Select(["data", "isSettled"], Var("auction"), false), true),
Abort("Cannot bid on already settled auction"),
Let(
{
startingPrice: Select(
["data", "startingPrice"],
Var("auction"),
null
)
},
If(
And(
Not(IsNull(Var("startingPrice"))),
LT(Var("amount"), Var("startingPrice"))
),
Abort(
Concat([
"Cannot bid lower than the starting price [",
ToString(Var("startingPrice")),
"]"
])
),
Let(
{
highestBidAmount: Select(
["data", "amount"],
Get(
Max(
Match(
Index("getBidsByAuctionId"),
Ref(Collection("auction"), Var("auctionId"))
)
)
),
null
)
},
If(
And(
Not(IsNull(Var("highestBidAmount"))),
LTE(Var("amount"), Var("highestBidAmount"))
),
Abort(
Concat([
"Cannot bid lower or equal than the last bid [",
ToString(Var("highestBidAmount")),
"]"
])
),
Let(
{
bidIncrement: Select(
["data", "bidIncrement"],
Var("auction"),
null
)
},
If(
And(
Not(IsNull(Var("bidIncrement"))),
LT(
Subtract(Var("amount"), Var("highestBidAmount")),
Var("bidIncrement")
)
),
Abort(
Concat([
"Cannot bid less than the bid increment [",
ToString(Var("bidIncrement")),
"]. Provided an increment of ",
ToString(
Subtract(Var("amount"), Var("highestBidAmount"))
),
" instead"
])
),
Create(Collection("bid"), {
data: {
bidderRef: Var("bidderRef"),
amount: Var("amount"),
auction: Ref(Collection("auction"), Var("auctionId"))
}
})
)
)
)
)
)
)
)
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment