From ccaff1054def7e6090a103cff2430f6a3c60befd Mon Sep 17 00:00:00 2001 From: ithiame <133223073+ithiame@users.noreply.github.com> Date: Fri, 16 Jun 2023 12:38:25 +0000 Subject: [PATCH] Increment challenge score (#186) Co-authored-by: Eliott C. --- src/lib/components/ChallengeWidget.svelte | 24 +++++++++++++---------- src/lib/server/orders.ts | 23 +++++++++++++++++++++- src/routes/admin/challenge/+page.svelte | 20 ++++++++++++------- 3 files changed, 49 insertions(+), 18 deletions(-) diff --git a/src/lib/components/ChallengeWidget.svelte b/src/lib/components/ChallengeWidget.svelte index e7941c1a1..cdabc6069 100644 --- a/src/lib/components/ChallengeWidget.svelte +++ b/src/lib/components/ChallengeWidget.svelte @@ -14,31 +14,35 @@ Ends {format(challenge.endsAt, 'MMMM dd')}
How can I contribute? - {#if challenge.progress.amount == challenge.goal.amount} + {#if challenge.progress == challenge.goal.amount}

Good job guys! 👏👏

- {:else if challenge.progress.amount > challenge.goal.amount} + {:else if challenge.progress > challenge.goal.amount}

You are amazing guys! 🤭

- {:else} + {:else if challenge.goal.currency} + {:else} + {challenge.goal.amount} products {/if}
diff --git a/src/lib/server/orders.ts b/src/lib/server/orders.ts index 332048fce..f07bb2399 100644 --- a/src/lib/server/orders.ts +++ b/src/lib/server/orders.ts @@ -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 { const res = await collections.runtimeConfig.findOneAndUpdate( @@ -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 diff --git a/src/routes/admin/challenge/+page.svelte b/src/routes/admin/challenge/+page.svelte index cc2b5784a..fcd0e1f9d 100644 --- a/src/routes/admin/challenge/+page.svelte +++ b/src/routes/admin/challenge/+page.svelte @@ -24,20 +24,26 @@
How can I contribute? - + {#if challenge.goal.currency} + + {:else} + + {challenge.goal.amount} products + + {/if}
{:else}