-
Notifications
You must be signed in to change notification settings - Fork 0
/
script2.ts
56 lines (50 loc) · 1.71 KB
/
script2.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
import { request, gql } from 'graphql-request';
import { config } from './config';
import { salesByOrderInterface, total_salesInterface } from './interfaces/salesDataInterfaces';
const salesData = require('./salesdata.json');
const products = salesData.salesByProduct.map((sale: salesByOrderInterface) => ({
id: sale.product_id,
totalSales: sale.quantity * sale.price,
}));
const updateProductMetafields = async (products: total_salesInterface[]) => {
const endpoint = `${config.endPoint2}`;
const headers = {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': `${config.secretCake}`,
};
const mutation = gql`
mutation MetafieldsSet($metafields: [MetafieldsSetInput!]!) {
metafieldsSet(metafields: $metafields) {
metafields {
key
namespace
value
type
}
userErrors {
field
message
code
}
}
}
`;
const variables = {
metafields: products.map((product: total_salesInterface) => ({
key: 'total_sales',
namespace: 'total_sales',
ownerId: `gid://shopify/Product/${product.id}`,
value: product.totalSales.toString(),
type: 'single_line_text_field',
})),
};
try {
const data = await request(endpoint, mutation, variables, headers);
if (data) {
products.forEach((product: total_salesInterface) => { console.log(`Metafields updated for product with ID ${product.id}`) })
}
} catch (error: any) {
console.log('an error occured with your request ', error.response.errors)
}
};
updateProductMetafields(products);