Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increment challenge score #186

Merged
merged 12 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
25 changes: 24 additions & 1 deletion src/lib/server/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,30 @@ 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();
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:
challenge.mode === 'moneyAmount'
? toSatoshis(order.totalPrice.amount, order.totalPrice.currency)
: sumProduct
}
},
{ 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
18 changes: 11 additions & 7 deletions src/routes/admin/challenge/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,24 @@
</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}
{challenge.goal.amount} products
{/if}
</div>
</div>
{:else}
Expand Down