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

Commit 01f4c28

Browse files
committed
add subscription functionalities
1 parent ddbe5ef commit 01f4c28

File tree

3 files changed

+163
-22
lines changed

3 files changed

+163
-22
lines changed

catalog/src/catalog.tsx

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,18 @@ export const NeverminedProvider = ({ children, config, verbose }: NeverminedProv
208208
}
209209
},
210210

211-
getPublishedSubscriptions: async (): Promise<SubscriptionsAndServicesDDOs[]> => {
211+
getPublishedSubscriptions: async (): Promise<DDO[]> => {
212+
try {
213+
const account = await getCurrentAccount(sdk)
214+
const query = await sdk.search.subscriptionsCreated(account)
215+
return query.results
216+
} catch {
217+
verbose && Logger.error(error)
218+
return []
219+
}
220+
},
221+
222+
getPublishedSubscriptionsAndServices: async (): Promise<SubscriptionsAndServicesDDOs[]> => {
212223
try {
213224
const account = await getCurrentAccount(sdk)
214225
const query = await sdk.search.subscriptionsCreated(account)
@@ -219,7 +230,18 @@ export const NeverminedProvider = ({ children, config, verbose }: NeverminedProv
219230
}
220231
},
221232

222-
getPurchasedSubscriptions: async (): Promise<SubscriptionsAndServicesDDOs[]> => {
233+
getPurchasedSubscriptions: async (): Promise<DDO[]> => {
234+
try {
235+
const account = await getCurrentAccount(sdk)
236+
const query = await sdk.search.subscriptionsPurchased(account)
237+
return query.results
238+
} catch (error) {
239+
verbose && Logger.error(error)
240+
return []
241+
}
242+
},
243+
244+
getPurchasedSubscriptionsAndServices: async (): Promise<SubscriptionsAndServicesDDOs[]> => {
223245
try {
224246
const account = await getCurrentAccount(sdk)
225247
const query = await sdk.search.subscriptionsPurchased(account)
@@ -230,6 +252,16 @@ export const NeverminedProvider = ({ children, config, verbose }: NeverminedProv
230252
}
231253
},
232254

255+
getAssociatedServices: async (did: string): Promise<DDO[]> => {
256+
try {
257+
const query = await sdk.search.servicesBySubscription(did)
258+
return query.results
259+
} catch (error) {
260+
verbose && Logger.error(error)
261+
return []
262+
}
263+
},
264+
233265
isAssetHolder: async (
234266
did: string,
235267
walletAddress: string,

catalog/src/types/index.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,15 +403,36 @@ export interface AccountModule {
403403
*/
404404
getCollection: (address: string) => Promise<DID[]>
405405
/**
406-
* Get all the published subscriptions and services associated from the wallet address passed
406+
* Get only published Subscriptions
407407
* @param address the address which published the subscription returned
408+
* @returns published subscriptions
409+
*/
410+
getPublishedSubscriptions: (address: string) => Promise<DDO[]>
411+
/**
412+
* Get all the services associated
413+
* @param address the address which published the subscription returned
414+
* @returns associated services to subscriptions
415+
*/
416+
getAssociatedServices: (did: string) => Promise<DDO[]>
417+
/**
418+
* Get all the published subscriptions and services associated from the wallet address passed
419+
* Get only published Subscriptions
420+
* @param did the did of the subscription
421+
* @returns published subscriptions and service
422+
*/
423+
getPublishedSubscriptionsAndServices: (address: string) => Promise<SubscriptionsAndServicesDDOs[]>
424+
/**
425+
* Get only purchased Subscriptions
426+
* @param address the address which purchased the subscription returned
427+
* @returns purchased subscriptions
408428
*/
409-
getPublishedSubscriptions: (address: string) => Promise<SubscriptionsAndServicesDDOs[]>
429+
getPurchasedSubscriptions: (address: string) => Promise<DDO[]>
410430
/**
411431
* Get all the purchased subscriptions and services associated from the wallet address passed
412432
* @param address the address which published the subscription returned
433+
* @returns purchased subscriptions and services
413434
*/
414-
getPurchasedSubscriptions: (address: string) => Promise<SubscriptionsAndServicesDDOs[]>
435+
getPurchasedSubscriptionsAndServices: (address: string) => Promise<SubscriptionsAndServicesDDOs[]>
415436
/**
416437
* Generate a token for authentication in the Marketplace API
417438
* @returns The new generated token

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

Lines changed: 105 additions & 17 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, SubscriptionsAndServicesDDOs } from '../../src'
5+
import { Catalog, AuthToken, SubscriptionsAndServicesDDOs, DDO } from '../../src'
66
import jwt from 'jsonwebtoken'
77
import { ddo, ddo2, ddo3, walletAddress, nevermined } from '../mockups'
88
import { faker } from '@faker-js/faker'
@@ -532,16 +532,42 @@ describe('Nevermined account', () => {
532532
})
533533

534534
it('should get all the subscriptions published by the address passed', async () =>{
535-
const sdk = await jest.requireActual('../mockups').nevermined.getInstance()
536-
jest.spyOn(nevermined, 'getInstance').mockResolvedValue({
537-
...sdk,
538-
assets: {
539-
...sdk.assets,
540-
resolve: (id: string) => [ddo, ddo2, ddo3].find(item => item.id === id)
535+
const { result } = renderHook(
536+
() => {
537+
const { account, isLoadingSDK, updateSDK } = Catalog.useNevermined()
538+
const [ subscriptions, setSubscriptions ] = useState<DDO[]>([])
539+
540+
useEffect(() => {
541+
if (isLoadingSDK) {
542+
appConfig.web3Provider = testingUtils.getProvider()
543+
updateSDK(appConfig)
544+
return
545+
}
546+
547+
(async () => {
548+
try {
549+
const result = await account.getPublishedSubscriptions(walletAddress)
550+
551+
setSubscriptions([...result])
552+
} catch (error: any) {
553+
console.error(error.message)
554+
}
555+
})()
556+
}, [isLoadingSDK])
557+
558+
return subscriptions
559+
},
560+
{
561+
wrapper: wrapperProvider
541562
}
542-
563+
)
564+
565+
await waitFor(() => {
566+
expect(result.current).toStrictEqual([ddo])
543567
})
568+
})
544569

570+
it('should get all the subscriptions published and services by the address passed', async () =>{
545571
const { result } = renderHook(
546572
() => {
547573
const { account, isLoadingSDK, updateSDK } = Catalog.useNevermined()
@@ -556,7 +582,7 @@ describe('Nevermined account', () => {
556582

557583
(async () => {
558584
try {
559-
const result = await account.getPublishedSubscriptions(walletAddress)
585+
const result = await account.getPublishedSubscriptionsAndServices(walletAddress)
560586

561587
setSubscriptions([...result])
562588
} catch (error: any) {
@@ -581,16 +607,42 @@ describe('Nevermined account', () => {
581607
})
582608

583609
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)
610+
const { result } = renderHook(
611+
() => {
612+
const { account, isLoadingSDK, updateSDK } = Catalog.useNevermined()
613+
const [ subscriptions, setSubscriptions ] = useState<DDO[]>([])
614+
615+
useEffect(() => {
616+
if (isLoadingSDK) {
617+
appConfig.web3Provider = testingUtils.getProvider()
618+
updateSDK(appConfig)
619+
return
620+
}
621+
622+
(async () => {
623+
try {
624+
const result = await account.getPurchasedSubscriptions(walletAddress)
625+
626+
setSubscriptions([...result])
627+
} catch (error: any) {
628+
console.error(error.message)
629+
}
630+
})()
631+
}, [isLoadingSDK])
632+
633+
return subscriptions
634+
},
635+
{
636+
wrapper: wrapperProvider
590637
}
591-
638+
)
639+
640+
await waitFor(() => {
641+
expect(result.current).toStrictEqual([ddo])
592642
})
643+
})
593644

645+
it('should get all the subscriptions purchased and services by the address passed', async () =>{
594646
const { result } = renderHook(
595647
() => {
596648
const { account, isLoadingSDK, updateSDK } = Catalog.useNevermined()
@@ -605,7 +657,7 @@ describe('Nevermined account', () => {
605657

606658
(async () => {
607659
try {
608-
const result = await account.getPurchasedSubscriptions(walletAddress)
660+
const result = await account.getPurchasedSubscriptionsAndServices(walletAddress)
609661

610662
setSubscriptions([...result])
611663
} catch (error: any) {
@@ -628,4 +680,40 @@ describe('Nevermined account', () => {
628680
}])
629681
})
630682
})
683+
684+
it('should get all the services associated to the subscription id passed', async () =>{
685+
const { result } = renderHook(
686+
() => {
687+
const { account, isLoadingSDK, updateSDK } = Catalog.useNevermined()
688+
const [ services, setServices ] = useState<DDO[]>([])
689+
690+
useEffect(() => {
691+
if (isLoadingSDK) {
692+
appConfig.web3Provider = testingUtils.getProvider()
693+
updateSDK(appConfig)
694+
return
695+
}
696+
697+
(async () => {
698+
try {
699+
const result = await account.getAssociatedServices(ddo.id)
700+
701+
setServices([...result])
702+
} catch (error: any) {
703+
console.error(error.message)
704+
}
705+
})()
706+
}, [isLoadingSDK])
707+
708+
return services
709+
},
710+
{
711+
wrapper: wrapperProvider
712+
}
713+
)
714+
715+
await waitFor(() => {
716+
expect(result.current).toStrictEqual([ddo2, ddo3])
717+
})
718+
})
631719
})

0 commit comments

Comments
 (0)