Skip to content

Commit 578015e

Browse files
feat: add name, accessPrice, remainingAccess and isUserStrict properties to Contact
1 parent b4633fb commit 578015e

File tree

5 files changed

+74
-6
lines changed

5 files changed

+74
-6
lines changed

src/utils/subgraphQuery.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ const checkProtectedDataQuery = gql`
2121
orderDirection: desc
2222
) {
2323
id
24+
name
2425
}
2526
}
2627
`;
2728

2829
export const getValidContact = async (
2930
graphQLClient: GraphQLClient,
30-
contacts: Contact[]
31+
contacts: Omit<Contact, 'name'>[]
3132
): Promise<Contact[]> => {
3233
if (contacts.length === 0) {
3334
return [];
@@ -66,13 +67,17 @@ export const getValidContact = async (
6667
);
6768

6869
// Convert protectedData[] into Contact[] using the map for constant time lookups
69-
return protectedDataList.map(({ id }) => {
70+
return protectedDataList.map(({ id, name }) => {
7071
const contact = contactsMap.get(id);
7172
if (contact) {
7273
return {
7374
address: id,
75+
name: name,
76+
remainingAccess: contact.remainingAccess,
77+
accessPrice: contact.accessPrice,
7478
owner: contact.owner,
7579
accessGrantTimestamp: contact.accessGrantTimestamp,
80+
isUserStrict: contact.isUserStrict,
7681
};
7782
}
7883
});

src/web3mail/fetchUserContacts.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { PublishedDatasetorder } from 'iexec/IExecOrderbookModule';
2+
import { ZeroAddress } from 'ethers';
3+
import { IExec } from 'iexec';
14
import { ANY_DATASET_ADDRESS } from '../config/config.js';
25
import { handleIfProtocolError, WorkflowError } from '../utils/errors.js';
36
import { autoPaginateRequest } from '../utils/paginate.js';
@@ -63,7 +66,7 @@ export const fetchUserContacts = async ({
6366
]);
6467

6568
const orders = dappOrders.concat(whitelistOrders);
66-
const myContacts: Contact[] = [];
69+
const myContacts: Omit<Contact, 'name'>[] = [];
6770
let web3DappResolvedAddress = vDappAddressOrENS;
6871
if (isEnsTest(vDappAddressOrENS)) {
6972
web3DappResolvedAddress = await iexec.ens.resolveName(vDappAddressOrENS);
@@ -78,7 +81,10 @@ export const fetchUserContacts = async ({
7881
const contact = {
7982
address: order.order.dataset.toLowerCase(),
8083
owner: order.signer.toLowerCase(),
84+
remainingAccess: order.remaining,
85+
accessPrice: order.order.datasetprice,
8186
accessGrantTimestamp: order.publicationTimestamp,
87+
isUserStrict: order.order.requesterrestrict !== ZeroAddress,
8288
};
8389
myContacts.push(contact);
8490
}
@@ -102,7 +108,12 @@ async function fetchAllOrdersByApp({
102108
userAddress,
103109
appAddress,
104110
isUserStrict,
105-
}) {
111+
}: {
112+
iexec: IExec;
113+
userAddress: string;
114+
appAddress: string;
115+
isUserStrict: boolean;
116+
}): Promise<PublishedDatasetorder[]> {
106117
const ordersFirstPage = iexec.orderbook.fetchDatasetOrderbook(
107118
ANY_DATASET_ADDRESS,
108119
{

src/web3mail/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ export type Contact = {
2020
address: Address;
2121
owner: Address;
2222
accessGrantTimestamp: TimeStamp;
23+
isUserStrict: boolean;
24+
name: string;
25+
remainingAccess: number;
26+
accessPrice: number;
2327
};
2428

2529
export type SendEmailParams = {
@@ -59,6 +63,7 @@ export type SendEmailResponse = {
5963
*/
6064
export type ProtectedDataQuery = {
6165
id: string;
66+
name: string;
6267
};
6368

6469
export type GraphQLResponse = {

tests/e2e/fetchUserContacts.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,33 @@ describe('web3mail.fetchMyContacts()', () => {
4343
}, 4 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME);
4444

4545
describe('when no access is granted', () => {
46+
it('contacts should contain name, accessPrice, remainingAccess, owner, accessGrantTimestamp, and isUserStrict', async () => {
47+
const userWithAccess = Wallet.createRandom().address;
48+
49+
await web3mail.init();
50+
// eslint-disable-next-line @typescript-eslint/dot-notation
51+
const authorizedApp = web3mail['dappAddressOrENS'];
52+
53+
await dataProtector.grantAccess({
54+
authorizedApp: authorizedApp,
55+
protectedData: protectedData1.address,
56+
authorizedUser: userWithAccess,
57+
});
58+
59+
const contacts = await web3mail.fetchUserContacts({
60+
userAddress: userWithAccess,
61+
isUserStrict: true,
62+
});
63+
expect(contacts.length).toBe(1);
64+
expect(contacts[0].address).toBe(protectedData1.address.toLowerCase());
65+
expect(contacts[0].owner).toBeDefined();
66+
expect(contacts[0].accessPrice).toBe(0);
67+
expect(contacts[0].remainingAccess).toBe(1);
68+
expect(contacts[0].accessGrantTimestamp).toBeDefined();
69+
expect(contacts[0].isUserStrict).toBe(true);
70+
expect(contacts[0].name).toBe('test do not use');
71+
});
72+
4673
it(
4774
'should return an empty contact array',
4875
async () => {

tests/unit/utils/subgraphQuery.test.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { Contact } from '../../../src/web3mail/types.js';
88

99
describe('getValidContact', () => {
1010
// Define the variables in the outermost scope
11-
let contacts: Contact[];
11+
let contacts: Omit<Contact, 'name'>[];
1212
let graphQLClient: GraphQLClient;
1313

1414
beforeAll(() => {
@@ -18,16 +18,25 @@ describe('getValidContact', () => {
1818
address: 'address1',
1919
owner: 'owner1',
2020
accessGrantTimestamp: '2023-06-08T09:32:29.761Z',
21+
remainingAccess: 1,
22+
accessPrice: 0,
23+
isUserStrict: true,
2124
},
2225
{
2326
address: 'address2',
2427
owner: 'owner2',
2528
accessGrantTimestamp: '2023-06-09T14:21:17.231Z',
29+
remainingAccess: 1,
30+
accessPrice: 0,
31+
isUserStrict: true,
2632
},
2733
{
2834
address: 'address3',
2935
owner: 'owner3',
3036
accessGrantTimestamp: '2023-06-10T14:21:17.231Z',
37+
remainingAccess: 1,
38+
accessPrice: 0,
39+
isUserStrict: true,
3140
},
3241
];
3342

@@ -38,7 +47,10 @@ describe('getValidContact', () => {
3847
it('should fetch valid contacts', async () => {
3948
// Mock the request method of the GraphQLClient instance
4049
jest.spyOn(graphQLClient, 'request').mockResolvedValue({
41-
protectedDatas: [{ id: 'address1' }, { id: 'address3' }],
50+
protectedDatas: [
51+
{ id: 'address1', name: 'Contact 1' },
52+
{ id: 'address3', name: 'Contact 3' },
53+
],
4254
});
4355

4456
const validContacts = await getValidContact(graphQLClient, contacts);
@@ -48,11 +60,19 @@ describe('getValidContact', () => {
4860
address: 'address1',
4961
owner: 'owner1',
5062
accessGrantTimestamp: '2023-06-08T09:32:29.761Z',
63+
name: 'Contact 1',
64+
remainingAccess: 1,
65+
accessPrice: 0,
66+
isUserStrict: true,
5167
},
5268
{
5369
address: 'address3',
5470
owner: 'owner3',
5571
accessGrantTimestamp: '2023-06-10T14:21:17.231Z',
72+
name: 'Contact 3',
73+
remainingAccess: 1,
74+
accessPrice: 0,
75+
isUserStrict: true,
5676
},
5777
]);
5878

0 commit comments

Comments
 (0)