Skip to content

Commit

Permalink
Increment challenge score when a whitelisted product is ordered & paid
Browse files Browse the repository at this point in the history
  • Loading branch information
ithiame committed Jun 16, 2023
1 parent e57e5ed commit 8b70f7b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
22 changes: 15 additions & 7 deletions src/lib/components/ChallengeWidget.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@
</h3>
<span class="text-base font-light text-gray-550"
>Ends <time datetime={challenge.endsAt.toJSON()} title={challenge.endsAt.toLocaleString('en')}
>{format(challenge.endsAt, 'MMMM, dd')}</time
>{format(challenge.endsAt, 'MMMM dd')}</time
></span
>
</div>
<GoalProgress
class="font-bold mt-3"
text="{Number(challenge.progress).toLocaleString('en', {
style: 'currency',
currency: challenge.goal.currency,
minimumFractionDigits: 0
})} 🙂"
text="{challenge.goal.currency
? Number(challenge.progress).toLocaleString('en', {
style: 'currency',
currency: challenge.goal.currency,
minimumFractionDigits: 0
})
: challenge.progress} 🙂"
percentage={(challenge.progress / challenge.goal.amount) * 100}
/>
<div class="flex justify-between mt-1 items-center">
Expand All @@ -33,8 +35,14 @@
<p>Good job guys! 👏👏</p>
{:else if challenge.progress > challenge.goal.amount}
<p>You are amazing guys! 🤭</p>
{:else if challenge.goal.currency}
<PriceTag
amount={challenge.goal.amount}
class="text-gray-800 text-base"
currency={challenge.goal.currency}
/>
{:else}
<PriceTag amount={challenge.goal.amount} class="text-gray-800 text-base" currency="SAT" />
{challenge.goal.amount} products
{/if}
</div>
</div>
11 changes: 9 additions & 2 deletions src/lib/server/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,23 @@ export async function onOrderPaid(order: Order, session: ClientSession) {
.toArray();
const challenges = await collections.challenges
.find({
$or: [{beginsAt: {$exists: false}}, {beginsAt: {$lt: new Date()}}]
$or: [{ beginsAt: { $exists: false } }, { beginsAt: { $lt: new Date() } }],
endsAt: { $gt: new Date() }
})
.toArray();
let sumProduct = 0;
for (let i = 0; i < order.items.length; i++) {
sumProduct += order.items[i].quantity;
}
for (const challenge of challenges) {
await collections.challenges.updateOne(
{ _id: challenge._id },
{
$inc: {
progress: toSatoshis(order.totalPrice.amount, order.totalPrice.currency)
progress:
challenge.mode === 'moneyAmount'
? toSatoshis(order.totalPrice.amount, order.totalPrice.currency)
: sumProduct
}
},
{ session }
Expand Down
10 changes: 9 additions & 1 deletion src/routes/admin/challenge/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@
/>
<div class="flex justify-between mt-1 items-center">
<a href="/" class="text-blue underline">How can I contribute?</a>
<PriceTag amount={challenge.goal.amount} class="text-gray-800 text-base" currency="SAT" />
{#if challenge.goal.currency}
<PriceTag
amount={challenge.goal.amount}
class="text-gray-800 text-base"
currency={challenge.goal.currency}
/>
{:else}
{challenge.goal.amount} products
{/if}
</div>
</div>
{:else}
Expand Down

0 comments on commit 8b70f7b

Please sign in to comment.