-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
156 lines (125 loc) · 4.08 KB
/
Copy pathmain.js
File metadata and controls
156 lines (125 loc) · 4.08 KB
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
import axios from 'axios'
import pLimit from 'p-limit'
const {
API_KEY = 'API key for site currencyfreaks.com',
DEVELOPER_API_HOST = 'https://your_project.oneentry.cloud',
BASE_CURRENCY_LANG = 'en_US',
BASE_CURRENCY = 'USD',
SYNC_CURRENCY_LANG = 'fr_FR',
SYNC_CURRENCY = 'EUR',
ATTRIBUTE_SET_MARKER = 'boots',
PRICE_ATTRIBUTE_MARKER = 'price_boots',
DEVELOPER_LOGIN = 'developer_admin',
DEVELOPER_PASSWORD = '1-1',
UPDATE_EVERY = 3 * 60 * 60 * 1000,
} = process.env
const API_URL = `https://api.currencyfreaks.com/v2.0/rates/latest?apikey=${API_KEY}`
const api = axios.create({
baseURL: DEVELOPER_API_HOST + '/api/developer',
})
let {
data: { accessToken, refreshToken },
} = await api.post('auth/login', {
login: DEVELOPER_LOGIN,
password: DEVELOPER_PASSWORD,
})
api.interceptors.request.use((config) => {
config.headers.setAuthorization(`Bearer ${accessToken}`)
return config
}, Promise.reject)
api.interceptors.response.use(
(response) => response,
async (error) => {
const config = error?.config
if (error?.response?.status === 401 && !config?.sent) {
config.sent = true
const { data: pair } = await axios.post(
`${DEVELOPER_API_HOST}/auth/refresh`,
{
refreshToken,
}
)
refreshToken = pair.refreshToken
config.headers = {
...config.headers,
authorization: `Bearer ${pair.accessToken}`,
}
return axios(config)
}
return Promise.reject(error)
}
)
const { data: attributeSet } = await api.get(
`attributes-sets/marker/${ATTRIBUTE_SET_MARKER}`
)
const attributeSchema = Object.values(attributeSet.schema).find(
({ identifier }) => identifier === PRICE_ATTRIBUTE_MARKER
)
if (!attributeSchema) {
throw new Error('Unable to find attribute to change')
}
const ATTRIBUTE_INTERNAL_ID = `${attributeSchema.type}_id${attributeSchema.id}`
const limit = pLimit(8)
let offset = 0
let total = 0
let rate = await fetchRate()
let updates = []
while (true) {
if (offset > total) {
await Promise.all(updates)
console.log(`${updates.length} products updated`)
offset = 0
await new Promise((r) => setTimeout(r, UPDATE_EVERY))
rate = await fetchRate()
}
const { data } = await api.post(
`products/all?langCode=${BASE_CURRENCY_LANG}&limit=30&offset=${offset}`,
[]
)
total = data.total
for (const product of data.items) {
if (product.attributeSetId !== attributeSet.id) {
continue
}
const baseCurrencyAttributes =
product.attributesSets[BASE_CURRENCY_LANG] ?? {}
const syncCurrencyAttributes =
structuredClone(product.attributesSets[SYNC_CURRENCY_LANG]) ?? {}
const newPrice =
parseFloat(baseCurrencyAttributes[ATTRIBUTE_INTERNAL_ID] ?? 0) *
rate
console.log('New price: ', newPrice)
const stringPrice = newPrice.toFixed(2)
if (syncCurrencyAttributes[ATTRIBUTE_INTERNAL_ID] === stringPrice) {
continue
}
syncCurrencyAttributes[ATTRIBUTE_INTERNAL_ID] = stringPrice
updates.push(
limit(() =>
api.put(`products/${product.id}`, {
attributesSets: {
...product.attributesSets,
[SYNC_CURRENCY_LANG]: syncCurrencyAttributes,
},
version: product.version + 1,
})
)
)
}
offset += 30
}
async function fetchRate() {
console.log('Updating rates')
try {
const response = await axios.get(API_URL)
const { rates = {} } = response.data
if (!rates) {
console.log('No data available for exchange rates.')
process.exit(1)
}
return rates[BASE_CURRENCY] * rates[SYNC_CURRENCY]
} catch (error) {
console.error('Error fetching exchange rates:', error)
process.exit(1)
}
}