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

Commit f13a3b1

Browse files
authored
Merge pull request #277 from nevermined-io/feature/find-associated-datasets
Find associated datasets
2 parents 8680a5c + 29b03e5 commit f13a3b1

File tree

11 files changed

+508
-22
lines changed

11 files changed

+508
-22
lines changed

.github/workflows/testing.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
with:
3535
node-version: "17.x"
3636
registry-url: https://registry.npmjs.org/
37-
- uses: nevermined-io/nvm-tools-actions@v0.7.1
37+
- uses: nevermined-io/nvm-tools-actions@v0.8.0
3838
with:
3939
token: ${{ secrets.API_TOKEN_GITHUB }}
4040
opengsn: 'true'

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ 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.2.0](https://github.com/nevermined-io/components-catalog/compare/v1.1.10...v1.2.0)
8+
9+
> 20 March 2023
10+
11+
- Feature add subscription funtionalities [`#275`](https://github.com/nevermined-io/components-catalog/pull/275)
12+
- add subscription functionalities [`01f4c28`](https://github.com/nevermined-io/components-catalog/commit/01f4c288a322ba2a0e00ce8486acf4690dce2bb1)
13+
- Adding v1.1.10 Changelog updates [`ddbe5ef`](https://github.com/nevermined-io/components-catalog/commit/ddbe5effd13d2dc73732ab10d1d0eac20af2ffce)
14+
- update React Components version [`29da903`](https://github.com/nevermined-io/components-catalog/commit/29da903598cdd3ae6622d664b1527d2475b9ebb4)
15+
716
#### [v1.1.10](https://github.com/nevermined-io/components-catalog/compare/v1.1.9...v1.1.10)
817

918
> 16 March 2023

catalog/package.json

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

catalog/src/catalog.tsx

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import {
2222
TransferNFTConditionMethod,
2323
BigNumber,
2424
SubscriptionsAndServicesDDOs,
25-
OrderProgressStep
25+
OrderProgressStep,
26+
SubscriptionsAndDatasetsDDOs
2627
} from './types'
2728
import {
2829
conductOrder,
@@ -32,6 +33,7 @@ import {
3233
getAgreementId,
3334
handlePostRequest,
3435
getSubscriptionsAndServices,
36+
getSubscriptionsAndDatasets,
3537
executeWithProgressEvent,
3638
} from './utils'
3739
import { _getCryptoConfig, _getDTPInstance, _grantAccess } from './utils/dtp'
@@ -230,6 +232,17 @@ export const NeverminedProvider = ({ children, config, verbose }: NeverminedProv
230232
}
231233
},
232234

235+
getPublishedSubscriptionsAndDatasets: async (): Promise<SubscriptionsAndDatasetsDDOs[]> => {
236+
try {
237+
const account = await getCurrentAccount(sdk)
238+
const query = await sdk.search.subscriptionsCreated(account)
239+
return getSubscriptionsAndDatasets(query.results, sdk)
240+
} catch (error) {
241+
verbose && Logger.error(error)
242+
return []
243+
}
244+
},
245+
233246
getPurchasedSubscriptions: async (): Promise<DDO[]> => {
234247
try {
235248
const account = await getCurrentAccount(sdk)
@@ -252,6 +265,17 @@ export const NeverminedProvider = ({ children, config, verbose }: NeverminedProv
252265
}
253266
},
254267

268+
getPurchasedSubscriptionsAndDatasets: async (): Promise<SubscriptionsAndDatasetsDDOs[]> => {
269+
try {
270+
const account = await getCurrentAccount(sdk)
271+
const query = await sdk.search.subscriptionsPurchased(account)
272+
return getSubscriptionsAndDatasets(query.results, sdk)
273+
} catch (error) {
274+
verbose && Logger.error(error)
275+
return []
276+
}
277+
},
278+
255279
getAssociatedServices: async (did: string): Promise<DDO[]> => {
256280
try {
257281
const query = await sdk.search.servicesBySubscription(did)
@@ -262,6 +286,16 @@ export const NeverminedProvider = ({ children, config, verbose }: NeverminedProv
262286
}
263287
},
264288

289+
getAssociatedDatasets: async (did: string): Promise<DDO[]> => {
290+
try {
291+
const query = await sdk.search.datasetsBySubscription(did)
292+
return query.results
293+
} catch (error) {
294+
verbose && Logger.error(error)
295+
return []
296+
}
297+
},
298+
265299
isAssetHolder: async (
266300
did: string,
267301
walletAddress: string,

catalog/src/types/index.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,18 +409,29 @@ export interface AccountModule {
409409
*/
410410
getPublishedSubscriptions: (address: string) => Promise<DDO[]>
411411
/**
412-
* Get all the services associated
413-
* @param address the address which published the subscription returned
412+
* Get all the services associated a subscription
413+
* @param address the subscription address
414414
* @returns associated services to subscriptions
415415
*/
416416
getAssociatedServices: (did: string) => Promise<DDO[]>
417+
/**
418+
* Get all the datasets associated to a subscription
419+
* @param address the subscription address
420+
* @returns associated datasets to subscriptions
421+
*/
422+
getAssociatedDatasets: (did: string) => Promise<DDO[]>
417423
/**
418424
* Get all the published subscriptions and services associated from the wallet address passed
419-
* Get only published Subscriptions
420425
* @param did the did of the subscription
421426
* @returns published subscriptions and service
422427
*/
423428
getPublishedSubscriptionsAndServices: (address: string) => Promise<SubscriptionsAndServicesDDOs[]>
429+
/**
430+
* Get all the published subscriptions and datasets associated from the wallet address passed
431+
* @param did the did of the subscription
432+
* @returns published subscriptions and its datasets
433+
*/
434+
getPublishedSubscriptionsAndDatasets: (address: string) => Promise<SubscriptionsAndDatasetsDDOs[]>
424435
/**
425436
* Get only purchased Subscriptions
426437
* @param address the address which purchased the subscription returned
@@ -433,6 +444,12 @@ export interface AccountModule {
433444
* @returns purchased subscriptions and services
434445
*/
435446
getPurchasedSubscriptionsAndServices: (address: string) => Promise<SubscriptionsAndServicesDDOs[]>
447+
/**
448+
* Get all the purchased subscriptions and datasets associated from the wallet address passed
449+
* @param did the did of the subscription
450+
* @returns purchased subscriptions and its datasets
451+
*/
452+
getPurchasedSubscriptionsAndDatasets: (address: string) => Promise<SubscriptionsAndDatasetsDDOs[]>
436453
/**
437454
* Generate a token for authentication in the Marketplace API
438455
* @returns The new generated token
@@ -948,3 +965,8 @@ export interface SubscriptionsAndServicesDDOs {
948965
subscription: DDO
949966
services: DDO[]
950967
}
968+
969+
export interface SubscriptionsAndDatasetsDDOs {
970+
subscription: DDO
971+
datasets: DDO[]
972+
}

catalog/src/utils/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,19 @@ export const getSubscriptionsAndServices = async (subscriptionsDDOs: DDO[], sdk:
186186
)
187187
}
188188

189+
export const getSubscriptionsAndDatasets = async (subscriptionsDDOs: DDO[], sdk: Nevermined) => {
190+
return Promise.all(
191+
subscriptionsDDOs.map(async (ddo) => {
192+
const query = await sdk.search.datasetsBySubscription(ddo.id)
193+
194+
return {
195+
subscription: ddo,
196+
datasets: query.results,
197+
}
198+
}),
199+
)
200+
}
201+
189202
export const executeWithProgressEvent = async <T>(
190203
subscribableAction: () => SubscribablePromise<any, T>,
191204
onEvent?: (next: any) => void,

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

Lines changed: 116 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ 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, SubscriptionsAndServicesDDOs, DDO } from '../../src'
5+
import { Catalog, AuthToken, SubscriptionsAndServicesDDOs, SubscriptionsAndDatasetsDDOs, DDO } from '../../src'
66
import jwt from 'jsonwebtoken'
7-
import { ddo, ddo2, ddo3, walletAddress, nevermined } from '../mockups'
7+
import { ddo, ddo2, ddo3, ddo4, ddo5, walletAddress, nevermined } from '../mockups'
88
import { faker } from '@faker-js/faker'
99

1010
jest.mock('@nevermined-io/sdk', () => ({
@@ -716,4 +716,118 @@ describe('Nevermined account', () => {
716716
expect(result.current).toStrictEqual([ddo2, ddo3])
717717
})
718718
})
719+
720+
it('should get all the subscriptions published and datasets by the address passed', async () =>{
721+
const { result } = renderHook(
722+
() => {
723+
const { account, isLoadingSDK, updateSDK } = Catalog.useNevermined()
724+
const [ subscriptions, setSubscriptions ] = useState<SubscriptionsAndDatasetsDDOs[]>([])
725+
726+
useEffect(() => {
727+
if (isLoadingSDK) {
728+
appConfig.web3Provider = testingUtils.getProvider()
729+
updateSDK(appConfig)
730+
return
731+
}
732+
733+
(async () => {
734+
try {
735+
const result = await account.getPublishedSubscriptionsAndDatasets(walletAddress)
736+
737+
setSubscriptions([...result])
738+
} catch (error: any) {
739+
console.error(error.message)
740+
}
741+
})()
742+
}, [isLoadingSDK])
743+
744+
return subscriptions
745+
},
746+
{
747+
wrapper: wrapperProvider
748+
}
749+
)
750+
751+
await waitFor(() => {
752+
expect(result.current).toStrictEqual([{
753+
subscription: ddo,
754+
datasets: [ddo4, ddo5]
755+
}])
756+
})
757+
})
758+
759+
it('should get all the subscriptions purchased and datasets by the address passed', async () =>{
760+
const { result } = renderHook(
761+
() => {
762+
const { account, isLoadingSDK, updateSDK } = Catalog.useNevermined()
763+
const [ subscriptions, setSubscriptions ] = useState<SubscriptionsAndDatasetsDDOs[]>([])
764+
765+
useEffect(() => {
766+
if (isLoadingSDK) {
767+
appConfig.web3Provider = testingUtils.getProvider()
768+
updateSDK(appConfig)
769+
return
770+
}
771+
772+
(async () => {
773+
try {
774+
const result = await account.getPublishedSubscriptionsAndDatasets(walletAddress)
775+
776+
setSubscriptions([...result])
777+
} catch (error: any) {
778+
console.error(error.message)
779+
}
780+
})()
781+
}, [isLoadingSDK])
782+
783+
return subscriptions
784+
},
785+
{
786+
wrapper: wrapperProvider
787+
}
788+
)
789+
790+
await waitFor(() => {
791+
expect(result.current).toStrictEqual([{
792+
subscription: ddo,
793+
datasets: [ddo4, ddo5]
794+
}])
795+
})
796+
})
797+
798+
it('should get all the datasets associated to the subscription id passed', async () =>{
799+
const { result } = renderHook(
800+
() => {
801+
const { account, isLoadingSDK, updateSDK } = Catalog.useNevermined()
802+
const [ datasets, setDatasets ] = useState<DDO[]>([])
803+
804+
useEffect(() => {
805+
if (isLoadingSDK) {
806+
appConfig.web3Provider = testingUtils.getProvider()
807+
updateSDK(appConfig)
808+
return
809+
}
810+
811+
(async () => {
812+
try {
813+
const result = await account.getAssociatedDatasets(ddo.id)
814+
815+
setDatasets([...result])
816+
} catch (error: any) {
817+
console.error(error.message)
818+
}
819+
})()
820+
}, [isLoadingSDK])
821+
822+
return datasets
823+
},
824+
{
825+
wrapper: wrapperProvider
826+
}
827+
)
828+
829+
await waitFor(() => {
830+
expect(result.current).toStrictEqual([ddo4, ddo5])
831+
})
832+
})
719833
})

0 commit comments

Comments
 (0)