Skip to content

Commit 61b4bb7

Browse files
authored
Merge pull request #12 from marsian83/patch-client-queries
Add queries to Client which are required in React sdk
2 parents 4ac9162 + 34b048e commit 61b4bb7

13 files changed

Lines changed: 426 additions & 54 deletions

File tree

packages/client/src/escrow/graphql/queries.ts

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,39 @@ export const getProtocolAndPlatformsFees = (
22
originServicePlatformId: string,
33
originValidatedProposalPlatformId: string,
44
): string => `
5-
{
6-
protocols {
7-
protocolEscrowFeeRate
8-
}
9-
servicePlatform: platform(id:${originServicePlatformId}){
10-
originServiceFeeRate
11-
}
12-
proposalPlatform: platform(id:${originValidatedProposalPlatformId}){
13-
originValidatedProposalFeeRate
14-
}
5+
{
6+
protocols {
7+
protocolEscrowFeeRate
8+
}
9+
servicePlatform: platform(id:${originServicePlatformId}){
10+
originServiceFeeRate
11+
}
12+
proposalPlatform: platform(id:${originValidatedProposalPlatformId}){
13+
originValidatedProposalFeeRate
14+
}
15+
}
16+
`;
17+
18+
export const getPaymentsByService = (serviceId: string, paymentType?: string) => {
19+
let condition = `where: {service: "${serviceId}"`;
20+
paymentType ? (condition += `, paymentType: "${paymentType}"`) : '';
21+
condition += '}, orderBy: id, orderDirection: asc';
22+
const query = `
23+
{
24+
payments(${condition}) {
25+
id
26+
amount
27+
rateToken {
28+
address
29+
decimals
30+
name
31+
symbol
1532
}
16-
`;
33+
paymentType
34+
transactionHash
35+
createdAt
36+
}
37+
}
38+
`;
39+
return query;
40+
};

packages/client/src/escrow/index.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Service } from '../services';
66
import { ClientTransactionResponse, NetworkEnum, RateToken } from '../types';
77
import { calculateApprovalAmount } from '../utils/fees';
88
import { ViemClient } from '../viem';
9-
import { getProtocolAndPlatformsFees } from './graphql/queries';
9+
import { getPaymentsByService, getProtocolAndPlatformsFees } from './graphql/queries';
1010

1111
export class Escrow {
1212
graphQlClient: GraphQLClient;
@@ -59,21 +59,21 @@ export class Escrow {
5959
let tx,
6060
cid = proposal.cid;
6161

62-
const protocolAndPlatformsFeesResponse = await this.graphQlClient.get(
63-
getProtocolAndPlatformsFees(proposal.service.platform.id, proposal.platform.id),
62+
const protocolAndPlatformsFees = await this.getProtocolAndPlatformsFees(
63+
proposal.service.platform.id, proposal.platform.id
6464
);
6565

66-
console.log('SDK: fees', protocolAndPlatformsFeesResponse);
66+
console.log('SDK: fees', protocolAndPlatformsFees);
6767

68-
if (!protocolAndPlatformsFeesResponse.data) {
68+
if (!protocolAndPlatformsFees.data) {
6969
throw Error('Unable to fetch fees');
7070
}
7171

7272
const approvalAmount = calculateApprovalAmount(
7373
proposal.rateAmount,
74-
protocolAndPlatformsFeesResponse.data.servicePlatform.originServiceFeeRate,
75-
protocolAndPlatformsFeesResponse.data.proposalPlatform.originValidatedProposalFeeRate,
76-
protocolAndPlatformsFeesResponse.data.protocols[0].protocolEscrowFeeRate,
74+
protocolAndPlatformsFees.servicePlatform.originServiceFeeRate,
75+
protocolAndPlatformsFees.proposalPlatform.originValidatedProposalFeeRate,
76+
protocolAndPlatformsFees.protocols[0].protocolEscrowFeeRate,
7777
);
7878

7979
console.log('SDK: escrow seeking approval for amount: ', approvalAmount);
@@ -187,4 +187,24 @@ export class Escrow {
187187

188188
return tx;
189189
}
190+
191+
public async getProtocolAndPlatformsFees(
192+
originServicePlatformId: string,
193+
originValidatedProposalPlatformId: string,
194+
): Promise<any> {
195+
const query = getProtocolAndPlatformsFees(originServicePlatformId, originValidatedProposalPlatformId);
196+
197+
const response = await this.graphQlClient.get(query);
198+
199+
return response?.data || null;
200+
}
201+
202+
public async getByService(serviceId: string, paymentType?: string): Promise<any> {
203+
const query = getPaymentsByService(serviceId, paymentType);
204+
205+
const response = await this.graphQlClient.get(query);
206+
207+
return response?.data?.payments || null
208+
}
209+
190210
}

packages/client/src/escrow/types/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ export interface IEscrow {
88
): Promise<ClientTransactionResponse>;
99
release(serviceId: string, amount: bigint, userId: number): Promise<any>;
1010
reimburse(serviceId: string, amount: bigint, userId: number): Promise<any>;
11+
getProtocolAndPlatformsFees(
12+
originServicePlatformId: string,
13+
originValidatedProposalPlatformId: string,
14+
): Promise<any>;
15+
getByService(serviceId: string, paymentType?: string): Promise<any>;
1116
}

packages/client/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import { IProfile } from './profile/types';
1616
import { Escrow } from './escrow';
1717
import { IEscrow } from './escrow/types';
1818
import { IService, Service } from './services';
19-
import { IReview } from './review/types';
20-
import { Review } from './review';
19+
import { IReview } from './reviews/types';
20+
import { Review } from './reviews';
2121

2222
/**
2323
* Main client for interacting with the TalentLayer protocol.
@@ -93,7 +93,7 @@ export class TalentLayerClient {
9393

9494
/**
9595
* Provides access to service functionalities.
96-
* @type {IDispute}
96+
* @type {IService}
9797
*/
9898

9999
// @ts-ignore

packages/client/src/profile/graphql/index.ts

Lines changed: 145 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,149 @@ import { Hash } from 'viem';
22

33
export const getProfileByAddress = (address: Hash) =>
44
`
5-
{
6-
users(where: {address: "${address.toLocaleLowerCase()}"}, first: 1) {
7-
id
8-
address
9-
handle
10-
rating
11-
delegates
12-
userStats {
13-
numReceivedReviews
14-
}
15-
updatedAt
16-
createdAt
17-
description {
18-
about
19-
role
20-
name
21-
country
22-
headline
23-
id
24-
image_url
25-
video_url
26-
title
27-
timezone
28-
skills_raw
29-
}
5+
{
6+
users(where: {address: "${address.toLocaleLowerCase()}"}, first: 1) {
7+
id
8+
address
9+
handle
10+
rating
11+
delegates
12+
userStats {
13+
numReceivedReviews
14+
}
15+
updatedAt
16+
createdAt
17+
description {
18+
about
19+
role
20+
name
21+
country
22+
headline
23+
id
24+
image_url
25+
video_url
26+
title
27+
timezone
28+
skills_raw
29+
}
30+
}
31+
}
32+
`;
33+
34+
export const getProfiles = (numberPerPage?: number, offset?: number, searchQuery?: string) => {
35+
const pagination = numberPerPage ? 'first: ' + numberPerPage + ', skip: ' + offset : '';
36+
let condition = ', where: {';
37+
condition += searchQuery ? `, handle_contains_nocase: "${searchQuery}"` : '';
38+
condition += '}';
39+
40+
return `
41+
{
42+
users(orderBy: rating, orderDirection: desc ${pagination} ${condition}) {
43+
id
44+
address
45+
handle
46+
userStats {
47+
numReceivedReviews
48+
}
49+
rating
50+
}
51+
}
52+
`;
53+
};
54+
55+
export const getProfileById = (id: string) => `
56+
{
57+
user(id: "${id}") {
58+
id
59+
address
60+
handle
61+
rating
62+
delegates
63+
userStats {
64+
numReceivedReviews
65+
}
66+
updatedAt
67+
createdAt
68+
description {
69+
about
70+
role
71+
name
72+
country
73+
headline
74+
id
75+
image_url
76+
video_url
77+
title
78+
timezone
79+
skills_raw
80+
web3mailPreferences{
81+
activeOnNewService
82+
activeOnNewProposal
83+
activeOnProposalValidated
84+
activeOnFundRelease
85+
activeOnReview
86+
activeOnPlatformMarketing
87+
activeOnProtocolMarketing
88+
}
89+
}
90+
}
91+
}
92+
`;
93+
94+
export const getUserTotalGains = (id: string) => `
95+
{
96+
user(id: "${id}") {
97+
totalGains{
98+
id
99+
totalGain
100+
token {
101+
id
102+
name
103+
symbol
104+
decimals
30105
}
31-
}
32-
`;
106+
}
107+
}
108+
}
109+
`;
110+
111+
export const getPaymentsForUser = (
112+
userId: string,
113+
numberPerPage?: number,
114+
offset?: number,
115+
startDate?: string,
116+
endDate?: string,
117+
) => {
118+
const pagination = numberPerPage ? 'first: ' + numberPerPage + ', skip: ' + offset : '';
119+
120+
const startDataCondition = startDate ? `, createdAt_gte: "${startDate}"` : '';
121+
const endDateCondition = endDate ? `, createdAt_lte: "${endDate}"` : '';
122+
123+
const query = `
124+
{
125+
payments(where: {
126+
service_: {seller: "${userId}"}
127+
${startDataCondition}
128+
${endDateCondition}
129+
},
130+
orderBy: createdAt orderDirection: desc ${pagination} ) {
131+
id,
132+
rateToken {
133+
address
134+
decimals
135+
name
136+
symbol
137+
}
138+
amount
139+
transactionHash
140+
paymentType
141+
createdAt
142+
service {
143+
id,
144+
cid
145+
}
146+
}
147+
}
148+
`;
149+
return query;
150+
};

packages/client/src/profile/index.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import IPFSClient from '../ipfs';
44
import { getProtocolById } from '../platform/graphql/queries';
55
import { ClientTransactionResponse } from '../types';
66
import { ViemClient } from '../viem';
7-
import { getProfileByAddress } from './graphql';
7+
import { getPaymentsForUser, getProfileByAddress, getProfileById, getProfiles, getUserTotalGains } from './graphql';
88
import { TalentLayerProfile } from './types';
99

1010
export class Profile {
@@ -37,6 +37,14 @@ export class Profile {
3737

3838
return null;
3939
}
40+
41+
public async getById(userId: string): Promise<any> {
42+
const query = getProfileById(userId);
43+
44+
const response = await this.graphQlClient.get(query);
45+
46+
return response?.data?.user || null;
47+
}
4048

4149
public async upload(profileData: TalentLayerProfile): Promise<string> {
4250
return this.ipfsClient.post(JSON.stringify(profileData));
@@ -84,4 +92,51 @@ export class Profile {
8492
BigInt(fee),
8593
);
8694
}
95+
96+
public async getBy(params: {
97+
numberPerPage?: number;
98+
offset?: number;
99+
searchQuery?: string;
100+
}): Promise<any> {
101+
const query = getProfiles(params.numberPerPage, params.offset, params.searchQuery);
102+
103+
return this.graphQlClient.get(query);
104+
}
105+
106+
public async getTotalGains(userId: string): Promise<any> {
107+
const query = getUserTotalGains(userId);
108+
109+
const response = await this.graphQlClient.get(query);
110+
111+
return response?.data?.user?.totalGains || null;
112+
}
113+
114+
public async getPayments(
115+
userId: string,
116+
numberPerPage?: number,
117+
offset?: number,
118+
startDate?: string,
119+
endDate?: string,
120+
): Promise<any> {
121+
const query = getPaymentsForUser(userId, numberPerPage, offset, startDate, endDate)
122+
123+
const response = await this.graphQlClient.get(query);
124+
125+
return response?.data?.payments || null
126+
}
127+
128+
public async getMintFees(): Promise<any> {
129+
const query = `
130+
{
131+
protocols {
132+
userMintFee,
133+
shortHandlesMaxPrice
134+
}
135+
}
136+
`;
137+
138+
const response = await this.graphQlClient.get(query);
139+
140+
return response?.data || null;
141+
}
87142
}

0 commit comments

Comments
 (0)