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 e37cec8 commit facfbb0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
16 changes: 6 additions & 10 deletions src/lib/components/ChallengeWidget.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,21 @@
</div>
<GoalProgress
class="font-bold mt-3"
text="{Number(challenge.progress.amount).toLocaleString('en', {
text="{Number(challenge.progress).toLocaleString('en', {
style: 'currency',
currency: challenge.progress.currency,
currency: challenge.goal.currency,
minimumFractionDigits: 0
})} 🙂"
percentage={(challenge.progress.amount / challenge.goal.amount) * 100}
percentage={(challenge.progress / challenge.goal.amount) * 100}
/>
<div class="flex justify-between mt-1 items-center">
<a href="/" class="text-blue underline">How can I contribute?</a>
{#if challenge.progress.amount == challenge.goal.amount}
{#if challenge.progress == challenge.goal.amount}
<p>Good job guys! 👏👏</p>
{:else if challenge.progress.amount > challenge.goal.amount}
{:else if challenge.progress > challenge.goal.amount}
<p>You are amazing guys! 🤭</p>
{:else}
<PriceTag
amount={challenge.goal.amount}
class="text-gray-800 text-base"
currency={challenge.goal.currency}
/>
<PriceTag amount={challenge.goal.amount} class="text-gray-800 text-base" currency="SAT" />
{/if}
</div>
</div>
15 changes: 8 additions & 7 deletions src/lib/server/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,18 @@ export async function onOrderPaid(order: Order, session: ClientSession) {
})
.toArray();
const challenges = await collections.challenges.find({}).toArray();
for (const challenge of challenges) {
for (const challenge of challenges.filter(
(chall) =>
chall.mode === 'moneyAmount' && chall.beginsAt! < new Date() && chall.endsAt > new Date()
)) {
await collections.challenges.updateOne(
{ _id: challenge._id },
{
$set: {
progress: {
amount: challenge.progress.amount + order.totalPrice.amount,
currency: 'SAT'
}
$inc: {
progress: toSatoshis(order.totalPrice.amount, order.totalPrice.currency)
}
}
},
{ session }
);
}
for (const subscription of order.items.filter((item) => item.product.type === 'subscription')) {
Expand Down
10 changes: 3 additions & 7 deletions src/routes/admin/challenge/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,16 @@
</div>
<GoalProgress
class="font-bold mt-3"
text="{Number(challenge.progress.amount).toLocaleString('en', {
text="{Number(challenge.progress).toLocaleString('en', {
style: 'currency',
currency: challenge.goal.currency,
minimumFractionDigits: 0
})} 🙂"
percentage={(challenge.progress.amount / challenge.goal.amount) * 100}
percentage={(challenge.progress / challenge.goal.amount) * 100}
/>
<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={challenge.goal.currency}
/>
<PriceTag amount={challenge.goal.amount} class="text-gray-800 text-base" currency="SAT" />
</div>
</div>
{:else}
Expand Down

0 comments on commit facfbb0

Please sign in to comment.