-
Notifications
You must be signed in to change notification settings - Fork 1
/
product.service.ts
169 lines (139 loc) · 3.78 KB
/
product.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { Injectable } from '@nestjs/common';
import { ApolloClient, InMemoryCache, gql } from '@apollo/client/core';
const clients = {
80001: new ApolloClient({
uri: 'https://api.studio.thegraph.com/query/48370/zbay/version/latest',
cache: new InMemoryCache(),
}),
5: new ApolloClient({
uri: 'https://api.studio.thegraph.com/query/48370/zbay_goerli/version/latest',
cache: new InMemoryCache(),
}),
100: new ApolloClient({
uri: 'https://api.studio.thegraph.com/query/48370/zbay_gnosis/version/latest',
cache: new InMemoryCache(),
}),
};
export interface Product {
id: string;
cid: string;
price: string;
seller: string;
timestamp: string;
}
export interface ProductBought {
id: string;
buyer: string;
timestamp: string;
}
@Injectable()
export class ProductService {
async getProductsByMerchant(merchantId: string) {
const products = await this.getProducts(merchantId);
console.log(products);
return products;
}
async getProductById(productId: string) {
const query = gql`
{
productCreateds (where: {ZBay_id: "${productId}"}) {
ZBay_id
cid
price
seller
blockTimestamp
}
}
`;
const response = await clients[80001].query({ query, fetchPolicy: 'no-cache' });
const productData = response.data.productCreateds[0];
console.log(productData);
const product: Product = {
id: productData.ZBay_id,
cid: productData.cid,
price: productData.price,
seller: productData.seller,
timestamp: productData.blockTimestamp,
};
return product;
}
async getAllProducts(chainId: number) {
const products = await this.getProducts(null, chainId);
console.log(products);
return products;
}
async getProductsByBuyer(buyerId: string) {
const products = await this.getProductsBought(buyerId);
console.log(products);
return products;
}
async getAllProductsBought() {
const products = await this.getProductsBought(null);
console.log(products);
return products;
}
async getProducts(merchantId: string, chainId = 80001): Promise<Array<Product>> {
let query = gql`
{
productCreateds {
ZBay_id
cid
price
seller
blockTimestamp
}
}
`;
if (merchantId != null) {
query = gql`{
productCreateds (where: {seller: "${merchantId}"}) {
ZBay_id
cid
price
seller
blockTimestamp
}
}`;
}
const response = await clients[chainId].query({ query, fetchPolicy: 'no-cache' });
const products = response.data.productCreateds.map((product) => {
return {
id: product.ZBay_id,
cid: product.cid,
price: product.price,
seller: product.seller,
timestamp: product.blockTimestamp,
};
});
return products;
}
async getProductsBought(buyerId: string, chainId = 80001): Promise<Array<ProductBought>> {
let query = gql`
{
productPurchaseds {
ZBay_id
buyer
blockTimestamp
}
}
`;
if (buyerId != null) {
query = gql`{
productPurchaseds (where: {buyer: "${buyerId}"}) {
ZBay_id
buyer
blockTimestamp
}
}`;
}
const response = await clients[chainId].query({ query });
const products = response.data.productPurchaseds.map((product) => {
return {
id: product.ZBay_id,
buyer: product.buyer,
timestamp: product.blockTimestamp,
};
});
return products;
}
}