Skip to content

Commit

Permalink
Increment challenge score (#186)
Browse files Browse the repository at this point in the history
Co-authored-by: Eliott C. <[email protected]>
  • Loading branch information
ithiame and coyotte508 authored Jun 16, 2023
1 parent 1df658b commit ccaff10
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
24 changes: 14 additions & 10 deletions src/lib/components/ChallengeWidget.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,35 @@
</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.amount).toLocaleString('en', {
style: 'currency',
currency: challenge.progress.currency,
minimumFractionDigits: 0
})} 🙂"
percentage={(challenge.progress.amount / challenge.goal.amount) * 100}
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">
<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}
{:else 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>
23 changes: 22 additions & 1 deletion src/lib/server/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { toSatoshis } from '$lib/utils/toSatoshis';
import { currentWallet, getNewAddress, orderAddressLabel } from './bitcoin';
import { lndCreateInvoice } from './lightning';
import { ORIGIN } from '$env/static/private';
import { sum } from '$lib/utils/sum';

async function generateOrderNumber(): Promise<number> {
const res = await collections.runtimeConfig.findOneAndUpdate(
Expand All @@ -34,7 +35,27 @@ export async function onOrderPaid(order: Order, session: ClientSession) {
productId: { $in: order.items.map((item) => item.product._id) }
})
.toArray();

const challenges = await collections.challenges
.find({
$or: [{ beginsAt: { $exists: false } }, { beginsAt: { $lt: new Date() } }],
endsAt: { $gt: new Date() }
})
.toArray();
const numberOfProducts = sum(order.items.map((item) => item.quantity));
for (const challenge of challenges) {
await collections.challenges.updateOne(
{ _id: challenge._id },
{
$inc: {
progress:
challenge.mode === 'moneyAmount'
? toSatoshis(order.totalPrice.amount, order.totalPrice.currency)
: numberOfProducts
}
},
{ session }
);
}
for (const subscription of order.items.filter((item) => item.product.type === 'subscription')) {
const existingSubscription = subscriptions.find(
(sub) => sub.productId === subscription.product._id
Expand Down
20 changes: 13 additions & 7 deletions src/routes/admin/challenge/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,26 @@
</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}
/>
{#if challenge.goal.currency}
<PriceTag
amount={challenge.goal.amount}
class="text-gray-800 text-base"
currency={challenge.goal.currency}
/>
{:else}
<span class="text-gray-800 text-base">
{challenge.goal.amount} products
</span>
{/if}
</div>
</div>
{:else}
Expand Down

0 comments on commit ccaff10

Please sign in to comment.