Skip to content
This repository was archived by the owner on Oct 19, 2023. It is now read-only.

Commit 090277e

Browse files
authored
Merge pull request #266 from nevermined-io/feature/get-purchased-subscriptions
Feature get purchased subscriptions
2 parents cbb6826 + 33958b8 commit 090277e

File tree

10 files changed

+158
-75
lines changed

10 files changed

+158
-75
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file. Dates are d
44

55
Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
66

7+
#### [v1.1.5](https://github.com/nevermined-io/components-catalog/compare/v1.1.4...v1.1.5)
8+
9+
> 8 March 2023
10+
11+
- Improve get subscriptions [`#265`](https://github.com/nevermined-io/components-catalog/pull/265)
12+
- Fix: Add missing types [`#260`](https://github.com/nevermined-io/components-catalog/pull/260)
13+
- improve get subscription implementation in order to get services [`c839c47`](https://github.com/nevermined-io/components-catalog/commit/c839c4788acf91984afc805fe1837cd1b1cfa611)
14+
- add catch [`f8bf98c`](https://github.com/nevermined-io/components-catalog/commit/f8bf98cc3dbaf7199187644e3b8e1f5e9b252cf6)
15+
- fix: types [`12f5999`](https://github.com/nevermined-io/components-catalog/commit/12f5999092151261da86010b48a72780fe253bbd)
16+
717
#### [v1.1.4](https://github.com/nevermined-io/components-catalog/compare/v1.1.3...v1.1.4)
818

919
> 8 March 2023

catalog/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "@nevermined-io/catalog",
3-
"version": "1.1.5",
3+
"version": "1.1.6",
44
"main": "./dist/index.js",
55
"types": "./dist/index.d.ts",
66
"dependencies": {
7-
"@nevermined-io/sdk-dtp": "^0.4.0",
8-
"@nevermined-io/sdk": "^1.1.0",
7+
"@nevermined-io/sdk-dtp": "^0.4.1",
8+
"@nevermined-io/sdk": "^1.2.0",
99
"@types/jsonwebtoken": "^8.5.8",
1010
"axios": "^0.27.2",
1111
"axios-retry": "^3.3.1",

catalog/src/catalog.tsx

Lines changed: 17 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
SearchQuery,
99
ClientError,
1010
QueryResult,
11-
NeverminedNFT721Type,
1211
AccountModule,
1312
AssetsModule,
1413
ContractEventSubscription,
@@ -22,7 +21,7 @@ import {
2221
SubscribeModule,
2322
TransferNFTConditionMethod,
2423
BigNumber,
25-
PublishedSubscriptions
24+
SubscriptionsAndServicesDDOs
2625
} from './types'
2726
import {
2827
conductOrder,
@@ -31,6 +30,7 @@ import {
3130
loadFulfilledEvents,
3231
getAgreementId,
3332
handlePostRequest,
33+
getSubscriptionsAndServices,
3434
} from './utils'
3535
import { _getCryptoConfig, _getDTPInstance, _grantAccess } from './utils/dtp'
3636
import { getAddressTokenSigner, isTokenValid, newMarketplaceApiToken } from './utils/marketplace_token'
@@ -206,65 +206,28 @@ export const NeverminedProvider = ({ children, config, verbose }: NeverminedProv
206206
}
207207
},
208208

209-
getPublishedSubscriptions: async (address: string): Promise<PublishedSubscriptions[]> => {
209+
getPublishedSubscriptions: async (): Promise<SubscriptionsAndServicesDDOs[]> => {
210210
try {
211-
const publishedAssets = await account.getReleases(address)
212-
213-
const subscriptions = await Promise.all(
214-
publishedAssets.map(async (a) => {
215-
try {
216-
const subscriptionDDO = await assets.findOne(a)
217-
218-
if (!subscriptionDDO) {
219-
return undefined
220-
}
221-
222-
const metadata = subscriptionDDO?.findServiceByType('metadata')
223-
const isNFTSales = subscriptionDDO?.findServiceByType('nft-sales')
224-
225-
if(!metadata || !isNFTSales || metadata.attributes.main.nftType !== NeverminedNFT721Type.nft721Subscription) {
226-
return undefined
227-
}
228-
229-
const nftInfo = await sdk.keeper.didRegistry.getNFTInfo(subscriptionDDO.id) as string[]
230-
231-
const services = await Promise.all(publishedAssets.map(async (p) => {
232-
try {
233-
const serviceDDO = await assets.findOne(p)
234-
235-
const metadata = serviceDDO?.findServiceByType('metadata')
236-
const isNFTAccess = serviceDDO?.findServiceByType('nft-access')
237-
238-
const nftServiceInfo = await sdk.keeper.didRegistry.getNFTInfo(serviceDDO.id) as string[]
239-
240-
if(!metadata || !isNFTAccess || metadata.attributes.main.nftType !== NeverminedNFT721Type.nft721Subscription || nftServiceInfo[0] !== nftInfo[0]) {
241-
return undefined
242-
}
243-
244-
return serviceDDO
245-
246-
} catch (_error) {
247-
return undefined
248-
}
249-
}))
250-
251-
return {
252-
subscription: subscriptionDDO,
253-
services: services.filter(service => Boolean(service))
254-
}
255-
} catch (_error) {
256-
return undefined
257-
}
258-
})
259-
)
260-
261-
return subscriptions.filter(ddo => Boolean(ddo)) as PublishedSubscriptions[]
211+
const account = await getCurrentAccount(sdk)
212+
const query = await sdk.search.subscriptionsCreated(account)
213+
return getSubscriptionsAndServices(query.results, sdk)
214+
} catch (error) {
215+
verbose && Logger.error(error)
216+
return []
217+
}
218+
},
262219

220+
getPurchasedSubscriptions: async (): Promise<SubscriptionsAndServicesDDOs[]> => {
221+
try {
222+
const account = await getCurrentAccount(sdk)
223+
const query = await sdk.search.subscriptionsPurchased(account)
224+
return getSubscriptionsAndServices(query.results, sdk)
263225
} catch (error) {
264226
verbose && Logger.error(error)
265227
return []
266228
}
267229
},
230+
268231
isAssetHolder: async (
269232
did: string,
270233
walletAddress: string,

catalog/src/types/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,10 +402,15 @@ export interface AccountModule {
402402
*/
403403
getCollection: (address: string) => Promise<DID[]>
404404
/**
405-
* Get all the published subscription from the wallet address passed
405+
* Get all the published subscriptions and services associated from the wallet address passed
406406
* @param address the address which published the subscription returned
407407
*/
408-
getPublishedSubscriptions: (address: string) => Promise<PublishedSubscriptions[]>
408+
getPublishedSubscriptions: (address: string) => Promise<SubscriptionsAndServicesDDOs[]>
409+
/**
410+
* Get all the purchased subscriptions and services associated from the wallet address passed
411+
* @param address the address which published the subscription returned
412+
*/
413+
getPurchasedSubscriptions: (address: string) => Promise<SubscriptionsAndServicesDDOs[]>
409414
/**
410415
* Generate a token for authentication in the Marketplace API
411416
* @returns The new generated token
@@ -911,7 +916,7 @@ export interface Credentials {
911916
babySig: Babysig
912917
}
913918

914-
export interface PublishedSubscriptions {
919+
export interface SubscriptionsAndServicesDDOs {
915920
subscription: DDO
916921
services: DDO[]
917922
}

catalog/src/utils/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,16 @@ export const handlePostRequest = async (url: string, formData: FormData, retries
166166
throw new ClientError((e as any).message, 'Catalog')
167167
}
168168
}
169+
170+
export const getSubscriptionsAndServices = async (subscriptionsDDOs: DDO[], sdk: Nevermined) => {
171+
return Promise.all(
172+
subscriptionsDDOs.map(async (ddo) => {
173+
const query = await sdk.search.servicesBySubscription(ddo.id)
174+
175+
return {
176+
subscription: ddo,
177+
services: query.results,
178+
}
179+
}),
180+
)
181+
}

catalog/unit-tests/catalog/catalog.account.spec.tsx

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useEffect, useState } from 'react'
22
import { renderHook, waitFor } from '@testing-library/react'
33
import { generateTestingUtils } from 'eth-testing'
44
import { appConfig } from '../config'
5-
import { Catalog, AuthToken, PublishedSubscriptions } from '../../src'
5+
import { Catalog, AuthToken, SubscriptionsAndServicesDDOs } from '../../src'
66
import jwt from 'jsonwebtoken'
77
import { ddo, ddo2, ddo3, walletAddress, nevermined } from '../mockups'
88
import { faker } from '@faker-js/faker'
@@ -545,7 +545,7 @@ describe('Nevermined account', () => {
545545
const { result } = renderHook(
546546
() => {
547547
const { account, isLoadingSDK, updateSDK } = Catalog.useNevermined()
548-
const [ subscriptions, setSubscriptions ] = useState<PublishedSubscriptions[]>([])
548+
const [ subscriptions, setSubscriptions ] = useState<SubscriptionsAndServicesDDOs[]>([])
549549

550550
useEffect(() => {
551551
if (isLoadingSDK) {
@@ -579,4 +579,53 @@ describe('Nevermined account', () => {
579579
}])
580580
})
581581
})
582+
583+
it('should get all the subscriptions purchased by the address passed', async () =>{
584+
const sdk = await jest.requireActual('../mockups').nevermined.getInstance()
585+
jest.spyOn(nevermined, 'getInstance').mockResolvedValue({
586+
...sdk,
587+
assets: {
588+
...sdk.assets,
589+
resolve: (id: string) => [ddo, ddo2, ddo3].find(item => item.id === id)
590+
}
591+
592+
})
593+
594+
const { result } = renderHook(
595+
() => {
596+
const { account, isLoadingSDK, updateSDK } = Catalog.useNevermined()
597+
const [ subscriptions, setSubscriptions ] = useState<SubscriptionsAndServicesDDOs[]>([])
598+
599+
useEffect(() => {
600+
if (isLoadingSDK) {
601+
appConfig.web3Provider = testingUtils.getProvider()
602+
updateSDK(appConfig)
603+
return
604+
}
605+
606+
(async () => {
607+
try {
608+
const result = await account.getPurchasedSubscriptions(walletAddress)
609+
610+
setSubscriptions([...result])
611+
} catch (error: any) {
612+
console.error(error.message)
613+
}
614+
})()
615+
}, [isLoadingSDK])
616+
617+
return subscriptions
618+
},
619+
{
620+
wrapper: wrapperProvider
621+
}
622+
)
623+
624+
await waitFor(() => {
625+
expect(result.current).toStrictEqual([{
626+
subscription: ddo,
627+
services: [ddo2, ddo3]
628+
}])
629+
})
630+
})
582631
})

catalog/unit-tests/mockups.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,26 @@ export const ddo2 = {
346346
name: 'UK Weather information 2011',
347347
price: '1',
348348
type: 'dataset',
349-
nftType: 'nft721-subscription',
349+
nftType: 'nft721',
350+
webService: {
351+
type: 'RESTful',
352+
endpoints: [
353+
{
354+
GET: 'http://127.0.0.1:3000',
355+
},
356+
],
357+
internalAttributes: {
358+
authentication: {
359+
type: 'oauth',
360+
token: '',
361+
},
362+
headers: [
363+
{
364+
Authorization: 'Bearer xxxxxx',
365+
},
366+
],
367+
},
368+
},
350369
},
351370
additionalInformation: {
352371
description: 'Weather information of UK including temperature and humidity',
@@ -756,10 +775,34 @@ export const nevermined = {
756775
createWithRoyalties: async () => ddo,
757776
transferForDelegate: async () => true,
758777
},
778+
search: {
779+
subscriptionsCreated: () => ({ results: [ddo] }),
780+
subscriptionsPurchased: () => ({ results: [ddo] }),
781+
servicesBySubscription: () => ({ results: [ddo2, ddo3] }),
782+
},
759783
keeper: {
760784
conditions: {
761785
transferNftCondition: {
762786
address: '0x610D9314EDF2ced7681BA1633C33fdb8cF365a12',
787+
events: {
788+
getPastEvents: () => [
789+
{
790+
_creator: walletAddress,
791+
_did: ddo.id,
792+
_agreementId: agreementId,
793+
},
794+
{
795+
_creator: walletAddress,
796+
_did: ddo2.id,
797+
_agreementId: agreementId,
798+
},
799+
{
800+
_creator: walletAddress,
801+
_did: ddo3.id,
802+
_agreementId: agreementId,
803+
},
804+
],
805+
},
763806
},
764807
accessCondition: {
765808
events: {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@
105105
]
106106
},
107107
"dependencies": {
108-
"@nevermined-io/sdk": "^1.1.0",
109-
"@nevermined-io/sdk-dtp": "^0.4.0",
108+
"@nevermined-io/sdk": "^1.2.0",
109+
"@nevermined-io/sdk-dtp": "^0.4.1",
110110
"@types/jsonwebtoken": "^8.5.8",
111111
"@wagmi/core": "^0.8.15",
112112
"axios": "^0.27.2",

providers/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@nevermined-io/providers",
33
"private": false,
4-
"version": "1.1.5",
4+
"version": "1.1.6",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",
77
"dependencies": {

yarn.lock

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,23 +2168,23 @@
21682168
"@motionone/dom" "^10.15.5"
21692169
tslib "^2.3.1"
21702170

2171-
"@nevermined-io/sdk-dtp@^0.4.0":
2172-
version "0.4.0"
2173-
resolved "https://registry.yarnpkg.com/@nevermined-io/sdk-dtp/-/sdk-dtp-0.4.0.tgz#1e02e89ef335badcbb3864db491e17b449f51e09"
2174-
integrity sha512-MMWQ9G7PQQZLrqvkoOttieqLMpvdSIJm+Iv5O/SR0tDAoCMxJMHrF3NtkvVs/13qf4VsmT7KwLqBJ6/MQdot3A==
2171+
"@nevermined-io/sdk-dtp@^0.4.1":
2172+
version "0.4.1"
2173+
resolved "https://registry.yarnpkg.com/@nevermined-io/sdk-dtp/-/sdk-dtp-0.4.1.tgz#a04c622f3b5d4cb1a9480adec86f01631157abab"
2174+
integrity sha512-RjEWMx1OWwFYSq5jZXM/3n9WEoyaG1nwrXM/sK+/zpBMOwyScvfWZf3wpJKaVfshMH30FNOqcQrGYbcM8/5o7g==
21752175
dependencies:
2176-
"@nevermined-io/sdk" "1.1.0"
2176+
"@nevermined-io/sdk" "1.2.0"
21772177
circomlibjs "^0.1.1"
21782178
eciesjs "^0.3.15"
21792179
ffjavascript "^0.2.55"
21802180
node-rsa "^1.1.1"
21812181
snarkjs "^0.4.26"
21822182
web3-utils "^1.7.4"
21832183

2184-
"@nevermined-io/sdk@1.1.0", "@nevermined-io/sdk@^1.1.0":
2185-
version "1.1.0"
2186-
resolved "https://registry.yarnpkg.com/@nevermined-io/sdk/-/sdk-1.1.0.tgz#b18d63b2c17ef0508aed363a24750bbcfd1b8e87"
2187-
integrity sha512-UTT1l9VCIHVs7GM/PKnRFUHjT6KQIwH9WZ5fPQEQpeKzP4nDEmsyMOse9q2I4YtPY4O6az41EHWvfFmmljxhLw==
2184+
"@nevermined-io/sdk@1.2.0", "@nevermined-io/sdk@^1.2.0":
2185+
version "1.2.0"
2186+
resolved "https://registry.yarnpkg.com/@nevermined-io/sdk/-/sdk-1.2.0.tgz#5e11927eaf88c113c95abb19c61ca0682c2e584d"
2187+
integrity sha512-RqbBvZD0MEoYyl98x42qQfg85qcNrDQbFONF/cdVuPaPjmTsf64vImA/t5VwlLUMRGRH/OXKNnVuSw9r3C5VxA==
21882188
dependencies:
21892189
"@nevermined-io/subgraphs" "0.5.0"
21902190
assert "^2.0.0"

0 commit comments

Comments
 (0)