/
shopify.js
133 lines (110 loc) · 3.29 KB
/
shopify.js
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
import { BaseService } from "medusa-interfaces"
import { INCLUDE_PRESENTMENT_PRICES } from "../utils/const"
class ShopifyService extends BaseService {
constructor(
{
manager,
shippingProfileService,
storeService,
shopifyProductService,
shopifyCollectionService,
shopifyClientService,
},
options
) {
super()
this.options = options
/** @private @const {EntityManager} */
this.manager_ = manager
/** @private @const {ShippingProfileService} */
this.shippingProfileService_ = shippingProfileService
/** @private @const {ShopifyProductService} */
this.productService_ = shopifyProductService
/** @private @const {ShopifyCollectionService} */
this.collectionService_ = shopifyCollectionService
/** @private @const {ShopifyRestClient} */
this.client_ = shopifyClientService
/** @private @const {StoreService} */
this.store_ = storeService
}
withTransaction(transactionManager) {
if (!transactionManager) {
return this
}
const cloned = new ShopifyService({
manager: transactionManager,
options: this.options,
shippingProfileService: this.shippingProfileService_,
shopifyClientService: this.client_,
shopifyProductService: this.productService_,
shopifyCollectionService: this.collectionService_,
shopifyBuildService: this.buildService_,
storeService: this.store_,
})
cloned.transactionManager_ = transactionManager
return cloned
}
async importShopify() {
return this.atomicPhase_(async (manager) => {
const updatedSinceQuery = await this.getAndUpdateBuildTime_()
await this.shippingProfileService_.createDefault()
await this.shippingProfileService_.createGiftCardDefault()
const products = await this.client_.list(
"products",
INCLUDE_PRESENTMENT_PRICES,
updatedSinceQuery
)
const customCollections = await this.client_.list(
"custom_collections",
null,
updatedSinceQuery
)
const smartCollections = await this.client_.list(
"smart_collections",
null,
updatedSinceQuery
)
const collects = await this.client_.list(
"collects",
null,
updatedSinceQuery
)
const resolvedProducts = await Promise.all(
products.map(async (product) => {
return await this.productService_
.withTransaction(manager)
.create(product)
})
)
await this.collectionService_
.withTransaction(manager)
.createCustomCollections(collects, customCollections, resolvedProducts)
await this.collectionService_
.withTransaction(manager)
.createSmartCollections(smartCollections, resolvedProducts)
})
}
async getAndUpdateBuildTime_() {
let buildtime = null
const store = await this.store_.retrieve()
if (!store) {
return {}
}
if (store.metadata?.source_shopify_bt) {
buildtime = store.metadata.source_shopify_bt
}
const payload = {
metadata: {
source_shopify_bt: new Date().toISOString(),
},
}
await this.store_.update(payload)
if (!buildtime) {
return {}
}
return {
updated_at_min: buildtime,
}
}
}
export default ShopifyService