From c7986b67bfa4e03c10f06530b22345a341eb7c39 Mon Sep 17 00:00:00 2001 From: Aleksey Svetlitskiy Date: Wed, 15 May 2024 03:10:24 -0300 Subject: [PATCH 1/3] feat(QR scanner) fix bug when qr scanner return false of tickets generated for multiple collectibles under an event --- rxb/check-event-query.json | 12 ++---- rxb/integration-scripts/scanner-check.js | 49 ++++++++++++++++-------- src/stores/app.store.ts | 2 +- src/stores/scanner.store.ts | 27 +++++++++++-- 4 files changed, 62 insertions(+), 28 deletions(-) diff --git a/rxb/check-event-query.json b/rxb/check-event-query.json index 779a493..8ac6f16 100644 --- a/rxb/check-event-query.json +++ b/rxb/check-event-query.json @@ -1,26 +1,22 @@ { + "size": 1000, "query": { "bool": { - "must": [ + "filter": [ { "match": { - "payload.trigger": "DAVINCI_QR_CODE_VALIDATOR_USE_TICKET" + "event_type": "DAVINCI_QR_CODE_VALIDATOR_USE_TICKET" } }, { "match": { - "payload.nftId": "{{event.payload.nftId}}" + "payload.eventId": "{{event.payload.eventId}}" } }, { "match": { "payload.wallet": "{{event.payload.wallet}}" } - }, - { - "match": { - "payload.collectionId": "{{event.payload.collectionId}}" - } } ] } diff --git a/rxb/integration-scripts/scanner-check.js b/rxb/integration-scripts/scanner-check.js index 9a1ff1f..4ac7149 100644 --- a/rxb/integration-scripts/scanner-check.js +++ b/rxb/integration-scripts/scanner-check.js @@ -1,3 +1,7 @@ +function compareInLowerCase(a, b) { + return String(a).toLowerCase() === String(b).toLowerCase(); +} + function getFreeportOwnedNfts(wallet) { if (!wallet) { throw new Error('Wallet address is not defined'); @@ -37,7 +41,7 @@ function getFreeportOwnedNfts(wallet) { }); } -async function checkData(collectionId, nftId, wallet, usedTicketCount) { +async function getNftForUsing(eventNfts, wallet, usedNfts) { let userNfts; try { userNfts = await getFreeportOwnedNfts(wallet); @@ -45,30 +49,40 @@ async function checkData(collectionId, nftId, wallet, usedTicketCount) { throw new Error(`Cannot get onwned nfts for the wallet ${wallet}`); } - const userNft = userNfts.find( - (nft) => nft.collection.address.toLowerCase() === collectionId.toLowerCase() && nft.nftId === nftId, - ); - if (!userNft) { - throw new Error(`Wallet doesn't have this NFT`); - } + const enrichedEventNfts = eventNfts?.map((eventNft) => { + const walletCount = userNfts + .filter( + (userNft) => + userNft?.nftId === eventNft.nftId && compareInLowerCase(userNft?.collection?.address, eventNft.collection), + ) + .map((userNft) => userNft.balance) + .reduce((acc, val) => +val + acc, 0); + + const walletUsedCount = usedNfts.filter( + (nft) => nft.nftId === eventNft.nftId && compareInLowerCase(nft.collectionId, eventNft.collection), + ).length; + + return {...eventNft, walletCount, walletUsedCount}; + }); + + const result = enrichedEventNfts.find((nft) => nft.walletCount > nft.walletUsedCount); - if (usedTicketCount >= userNft?.balance) { - throw new Error(`The number of tickets used is greater than the number of tickets the user has`); + if (!result) { + throw new Error(`Wallet doesn't any unused NFT for this event`); } - return {userNft}; + return result; } sdk.transform(async (params) => { let errorMessage; - const collectionId = params.event.payload?.collectionId; - const nftId = params.event.payload?.nftId; const wallet = params.event.payload?.wallet; - const usedTicketCount = params.queryData[0]?.hits?.total?.value || 0; + const eventNfts = params.event.payload?.eventNfts; + const usedNfts = (params.queryData[0]?.hits?.hits || []).map((hit) => hit?._source?.payload); let checkResult; try { - checkResult = await checkData(collectionId, nftId, wallet, usedTicketCount); + checkResult = await getNftForUsing(eventNfts, wallet, usedNfts); } catch (e) { errorMessage = e?.message || JSON.stringify(e); } @@ -79,7 +93,12 @@ sdk.transform(async (params) => { errorMessage, } : { - data: {collectionId, nftId, wallet, checkResult, usedTicketCount}, + data: { + collectionId: checkResult.collection, + nftId: checkResult.nftId, + walletCount: checkResult.walletCount, + walletUsedCount: checkResult.walletUsedCount, + }, result: true, }; diff --git a/src/stores/app.store.ts b/src/stores/app.store.ts index 9d55ed8..5c2ad8f 100644 --- a/src/stores/app.store.ts +++ b/src/stores/app.store.ts @@ -13,6 +13,6 @@ export class AppStore { this.notificationStore = new NotificationStore(); this.userStore = new UserStore(this.notificationStore); this.eventStore = new EventStore(this.notificationStore, this.userStore); - this.scannerStore = new ScannerStore(this.notificationStore, this.userStore); + this.scannerStore = new ScannerStore(this.eventStore, this.userStore); } } diff --git a/src/stores/scanner.store.ts b/src/stores/scanner.store.ts index e7695b3..2b85dbc 100644 --- a/src/stores/scanner.store.ts +++ b/src/stores/scanner.store.ts @@ -5,15 +5,16 @@ import {makeAutoObservable, reaction} from 'mobx'; import {UserStore} from './user.store'; import {SdkTriggerEnum} from '../enums/sdk-trigger.enum'; import {ScannerStatusEnum} from '../enums/scanner-status.enum'; -import {NotificationStore} from './notification.store'; +import {EventStore} from './event.store'; export class ScannerStore { private _eventSubscription: UnsubscribeEngagementHandler | undefined; private _scanResult: Record | undefined; private _status: ScannerStatusEnum = ScannerStatusEnum.INIT; private _errorMessage: string | undefined = undefined; + private _nftForUsing: {collectionId: string; nftId: string} | undefined; constructor( - private notificationStore: NotificationStore, + private eventStore: EventStore, private userStore: UserStore, ) { makeAutoObservable(this); @@ -36,6 +37,10 @@ export class ScannerStore { if (response.trigger === SdkTriggerEnum.TICKET_CHECK) { if (response?.data?.result === true) { this._status = ScannerStatusEnum.SUCCESS; + this._nftForUsing = { + collectionId: response?.data?.data?.collectionId, + nftId: response?.data?.data?.nftId, + }; } else if (response?.data?.result === false) { this._throwError(response?.data?.errorMessage); } else { @@ -67,8 +72,16 @@ export class ScannerStore { return; } + const eventData = this.eventStore.events?.find((e) => e.id === eventId); + // @ts-ignore + const eventNfts = (eventData?.nfts || []) + .filter((nft: {cmsNft?: {collection: string; nftId: string}}) => nft?.cmsNft?.collection && nft?.cmsNft?.nftId) + .map((nft: {cmsNft: {collection: string; nftId: string}}) => ({ + collection: nft.cmsNft.collection, + nftId: nft.cmsNft.nftId, + })); const trigger = SdkTriggerEnum.TICKET_CHECK; - this.userStore.sdkInstance?.sendEvent(trigger, {...data, trigger}); + this.userStore.sdkInstance?.sendEvent(trigger, {...data, trigger, eventNfts}); this._status = ScannerStatusEnum.PROCESSING; this._scanResult = data; } @@ -80,12 +93,18 @@ export class ScannerStore { this._status = ScannerStatusEnum.READY; this._scanResult = undefined; this._errorMessage = undefined; + this._nftForUsing = undefined; } public useTicket(): void { if (this._status === ScannerStatusEnum.SUCCESS) { const trigger = SdkTriggerEnum.TICKET_USE; - this.userStore.sdkInstance?.sendEvent(trigger, {...this._scanResult, trigger}); + this.userStore.sdkInstance?.sendEvent(trigger, { + ...this._scanResult, + collectionId: this._nftForUsing?.collectionId, + nftId: this._nftForUsing?.nftId, + trigger, + }); this._status = ScannerStatusEnum.USE_TICKET_PROCESSING; } } From 5aaf6d0a09d08505b5bdb291fb92fdfbefa5fd16 Mon Sep 17 00:00:00 2001 From: Aleksey Svetlitskiy Date: Thu, 16 May 2024 10:51:12 -0300 Subject: [PATCH 2/3] release 1.1.0 --- CHANGELOG.md | 4 ++++ package-lock.json | 2 +- package.json | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8666928..ff19065 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ - +### v1.1.0 + +- Add sending nft list of the event to the check rxb event + ### v1.0.0 - Product ready version diff --git a/package-lock.json b/package-lock.json index 2565b0a..0bf02a1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "qr-scanner-rxb", - "version": "1.0.0", + "version": "1.1.0", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/package.json b/package.json index dfa8d45..d60584b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "qr-scanner-rxb", - "version": "1.0.0", + "version": "1.1.0", "private": true, "repository": "git://github.com/cere-io/nft-marketplace-client", "dependencies": { From 529e97f0de90f84eca848449e86325942d60f6bf Mon Sep 17 00:00:00 2001 From: Aleksey Svetlitskiy Date: Thu, 16 May 2024 11:28:45 -0300 Subject: [PATCH 3/3] release 1.1.0 --- rxb/integration-scripts/scanner-check.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rxb/integration-scripts/scanner-check.js b/rxb/integration-scripts/scanner-check.js index 4ac7149..38481ce 100644 --- a/rxb/integration-scripts/scanner-check.js +++ b/rxb/integration-scripts/scanner-check.js @@ -49,7 +49,7 @@ async function getNftForUsing(eventNfts, wallet, usedNfts) { throw new Error(`Cannot get onwned nfts for the wallet ${wallet}`); } - const enrichedEventNfts = eventNfts?.map((eventNft) => { + const enrichedEventNfts = (eventNfts || [])?.map((eventNft) => { const walletCount = userNfts .filter( (userNft) =>