From cf0980ae3820881cdb7695ecab28c25408cd33a6 Mon Sep 17 00:00:00 2001 From: Peter Date: Mon, 27 May 2024 10:39:07 +0200 Subject: [PATCH] Fix coupon reward payments list and reward supply for coupon codes --- .../api/src/app/services/RewardCoinService.ts | 5 +-- .../src/app/services/RewardCouponService.ts | 8 ++--- .../src/app/services/RewardCustomService.ts | 6 ++-- apps/api/src/app/services/RewardService.ts | 5 ++- .../src/components/card/BaseCardReward.vue | 32 ++++++++++++------- apps/app/src/stores/Wallet.ts | 17 +++++----- 6 files changed, 41 insertions(+), 32 deletions(-) diff --git a/apps/api/src/app/services/RewardCoinService.ts b/apps/api/src/app/services/RewardCoinService.ts index 405895c06..7816742c7 100644 --- a/apps/api/src/app/services/RewardCoinService.ts +++ b/apps/api/src/app/services/RewardCoinService.ts @@ -3,6 +3,7 @@ import { ERC20Document, RewardCoin, RewardCoinDocument, + RewardCoinPaymentDocument, Transaction, WalletDocument, } from '@thxnetwork/api/models'; @@ -27,8 +28,8 @@ export default class RewardCoinService implements IRewardService { return { ...reward.toJSON(), erc20 }; } - async decoratePayment(payment: TBaseRewardPayment) { - return payment; + async decoratePayment(payment: RewardCoinPaymentDocument) { + return payment.toJSON(); } findById(id: string) { diff --git a/apps/api/src/app/services/RewardCouponService.ts b/apps/api/src/app/services/RewardCouponService.ts index 403b70151..867626ba6 100644 --- a/apps/api/src/app/services/RewardCouponService.ts +++ b/apps/api/src/app/services/RewardCouponService.ts @@ -9,14 +9,14 @@ export default class RewardCouponService implements IRewardService { async decorate({ reward }) { const couponCodes = await CouponCode.find({ couponRewardId: reward._id }); - const progress = { + const limitSupply = { count: await this.models.payment.countDocuments({ - rewardId: reward._id, + rewardId: reward.id, }), - limit: couponCodes.length, + max: couponCodes.length, }; - return { ...reward.toJSON(), progress, limit: couponCodes.length }; + return { ...reward.toJSON(), limitSupply }; } async decoratePayment(payment: TRewardPayment) { diff --git a/apps/api/src/app/services/RewardCustomService.ts b/apps/api/src/app/services/RewardCustomService.ts index 5937849e2..89d77e724 100644 --- a/apps/api/src/app/services/RewardCustomService.ts +++ b/apps/api/src/app/services/RewardCustomService.ts @@ -1,4 +1,4 @@ -import { Identity, RewardCustom, RewardCustomPayment, Webhook } from '../models'; +import { Identity, RewardCustom, RewardCustomPayment, RewardCustomPaymentDocument, Webhook } from '../models'; import { IRewardService } from './interfaces/IRewardService'; import { Event } from '@thxnetwork/common/enums'; import WebhookService from './WebhookService'; @@ -14,8 +14,8 @@ export default class RewardCustomService implements IRewardService { return { ...reward.toJSON(), isDisabled: !identities.length }; } - async decoratePayment(payment: TRewardPayment): Promise { - return payment; + async decoratePayment(payment: RewardCustomPaymentDocument): Promise { + return payment.toJSON(); } async getValidationResult({ reward, account }: { reward: TReward; account?: TAccount }) { diff --git a/apps/api/src/app/services/RewardService.ts b/apps/api/src/app/services/RewardService.ts index b4b9c7cc5..3b3b2da4a 100644 --- a/apps/api/src/app/services/RewardService.ts +++ b/apps/api/src/app/services/RewardService.ts @@ -96,10 +96,10 @@ export default class RewardService { author: { username: owner.username, }, + limitSupply, // Decorated properties may override generic properties ...decorated, limit, - limitSupply, }; } catch (error) { logger.error(error); @@ -174,12 +174,11 @@ export default class RewardService { const payments = await serviceMap[rewardVariant].models.payment.find({ sub }); const callback = payments.map(async (p: Document & TRewardPayment) => { const decorated = await serviceMap[rewardVariant].decoratePayment(p); - return { ...decorated.toJSON(), rewardVariant }; + return { ...decorated, rewardVariant }; }); return await Promise.all(callback); }), ); - return payments .filter((result) => result.status === 'fulfilled') .map((result: any) => result.value) diff --git a/apps/app/src/components/card/BaseCardReward.vue b/apps/app/src/components/card/BaseCardReward.vue index c402b3bde..f1327f6e8 100644 --- a/apps/app/src/components/card/BaseCardReward.vue +++ b/apps/app/src/components/card/BaseCardReward.vue @@ -33,14 +33,8 @@
Supply: - - {{ reward.limitSupply.count }} + + {{ reward.limitSupply.max - reward.limitSupply.count }} /{{ reward.limitSupply.max }} @@ -63,8 +57,8 @@ {{ btnLabel }} = 0.9) return 'text-danger'; + if (this.limitSupplyPerct > 0.75 && this.limitSupplyPerct < 0.9) return 'text-warning'; + if (this.limitSupplyPerct >= 0 && this.limitSupplyPerct <= 0.75) return 'text-success'; + }, + limitVariant() { + if (this.limitPerct >= 0.75) return 'danger'; + if (this.limitPerct > 0.5 && this.limitPerct < 0.75) return 'warning'; + if (this.limitPerct >= 0 && this.limitPerct <= 0.5) return 'success'; + }, btnLabel() { if (this.reward.isLimitSupplyReached) { return 'Sold out'; @@ -138,10 +142,14 @@ export default defineComponent({ isDisabled() { return !this.reward.isAvailable; }, - progressPercentage: function () { - if (!this.reward.limitSupply.max) return 100; + limitSupplyPerct: function () { + if (!this.reward.limitSupply.max) return 1; return this.reward.limitSupply.count / this.reward.limitSupply.max; }, + limitPerct: function () { + if (!this.reward.limit.max) return 1; + return this.reward.limit.count / this.reward.limit.max; + }, expiryDate: function () { return !this.reward.isExpired && this.reward.expiry ? formatDistance(new Date(this.reward.expiry.date), new Date(this.reward.expiry.now), { diff --git a/apps/app/src/stores/Wallet.ts b/apps/app/src/stores/Wallet.ts index 91278979c..61fa05f74 100644 --- a/apps/app/src/stores/Wallet.ts +++ b/apps/app/src/stores/Wallet.ts @@ -216,21 +216,22 @@ export const useWalletStore = defineStore('wallet', { this.erc721[index] = { ...token, component: 'BaseCardERC721' }; }, async list() { - if (!this.wallet) return; const { api } = useAccountStore(); this.isLoading = true; - const [erc20, erc721, erc1155, payments] = await Promise.all([ - api.erc20.list({ walletId: this.wallet._id }), - api.erc721.list({ walletId: this.wallet._id }), - api.erc1155.list({ walletId: this.wallet._id }), + const [payments] = await Promise.all([ + // api.erc20.list(this.wallet ? { walletId: this.wallet._id } : {}), + // api.erc721.list(this.wallet ? { walletId: this.wallet._id } : {}), + // api.erc1155.list(this.wallet ? { walletId: this.wallet._id } : {}), api.request.get('/v1/rewards/payments'), ]); + console.log(payments); + // TODO Refactor to using a component map with r.variant as key - this.erc20 = erc20.map((t: TERC20Token) => ({ ...t, component: 'BaseCardERC20' })); - this.erc721 = erc721.map((t: TERC721Token) => ({ ...t, component: 'BaseCardERC721' })); - this.erc1155 = erc1155.map((t: TERC721Token) => ({ ...t, component: 'BaseCardERC721' })); + // this.erc20 = erc20.map((t: TERC20Token) => ({ ...t, component: 'BaseCardERC20' })); + // this.erc721 = erc721.map((t: TERC721Token) => ({ ...t, component: 'BaseCardERC721' })); + // this.erc1155 = erc1155.map((t: TERC721Token) => ({ ...t, component: 'BaseCardERC721' })); this.couponCodes = payments .filter((p: { rewardVariant: RewardVariant }) => p.rewardVariant === RewardVariant.Coupon) .map((t: TRewardCouponPayment[]) => ({