|
5 | 5 |
|
6 | 6 | import * as fs from 'fs'; |
7 | 7 | import * as path from 'path'; |
| 8 | +import { execSync } from 'child_process'; |
8 | 9 | import BaseApi from './BaseApi'; |
9 | 10 |
|
10 | 11 | export default class ChannableApi extends BaseApi { |
@@ -191,4 +192,169 @@ export default class ChannableApi extends BaseApi { |
191 | 192 |
|
192 | 193 | this.flushAllCaches(); |
193 | 194 | } |
| 195 | + |
| 196 | + /** |
| 197 | + * Create a product via the Magento REST API. |
| 198 | + */ |
| 199 | + async createProduct(baseURL: string, payload: any): Promise<any> { |
| 200 | + const token = process.env.admin_token; |
| 201 | + const url = `${baseURL}rest/all/V1/products`; |
| 202 | + |
| 203 | + const response = await fetch(url, { |
| 204 | + method: 'POST', |
| 205 | + headers: { |
| 206 | + 'Content-Type': 'application/json', |
| 207 | + 'Authorization': `Bearer ${token}`, |
| 208 | + }, |
| 209 | + body: JSON.stringify({ product: payload }), |
| 210 | + }); |
| 211 | + |
| 212 | + if (!response.ok) { |
| 213 | + const text = await response.text(); |
| 214 | + throw new Error(`Failed to create product ${payload.sku}: ${response.status} - ${text}`); |
| 215 | + } |
| 216 | + |
| 217 | + return response.json(); |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * Delete a product by SKU via the Magento REST API. |
| 222 | + */ |
| 223 | + async deleteProduct(baseURL: string, sku: string): Promise<void> { |
| 224 | + const token = process.env.admin_token; |
| 225 | + const url = `${baseURL}rest/all/V1/products/${encodeURIComponent(sku)}`; |
| 226 | + |
| 227 | + const response = await fetch(url, { |
| 228 | + method: 'DELETE', |
| 229 | + headers: { 'Authorization': `Bearer ${token}` }, |
| 230 | + }); |
| 231 | + |
| 232 | + if (!response.ok && response.status !== 404) { |
| 233 | + const text = await response.text(); |
| 234 | + throw new Error(`Failed to delete product ${sku}: ${response.status} - ${text}`); |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * Set stock status and quantity for a product by SKU. |
| 240 | + */ |
| 241 | + async setStockStatus(baseURL: string, sku: string, qty: number, inStock: boolean): Promise<void> { |
| 242 | + const token = process.env.admin_token; |
| 243 | + const url = `${baseURL}rest/all/V1/products/${encodeURIComponent(sku)}`; |
| 244 | + |
| 245 | + const response = await fetch(url, { |
| 246 | + method: 'PUT', |
| 247 | + headers: { |
| 248 | + 'Content-Type': 'application/json', |
| 249 | + 'Authorization': `Bearer ${token}`, |
| 250 | + }, |
| 251 | + body: JSON.stringify({ |
| 252 | + product: { |
| 253 | + sku, |
| 254 | + extension_attributes: { |
| 255 | + stock_item: { |
| 256 | + qty, |
| 257 | + is_in_stock: inStock, |
| 258 | + }, |
| 259 | + }, |
| 260 | + }, |
| 261 | + }), |
| 262 | + }); |
| 263 | + |
| 264 | + if (!response.ok) { |
| 265 | + const text = await response.text(); |
| 266 | + throw new Error(`Failed to set stock for ${sku}: ${response.status} - ${text}`); |
| 267 | + } |
| 268 | + } |
| 269 | + |
| 270 | + /** |
| 271 | + * Reindex catalog prices via docker exec. |
| 272 | + */ |
| 273 | + reindexPrices(): void { |
| 274 | + if (!this.container) { |
| 275 | + throw new Error('MAGENTO_CONTAINER env var is required for reindexing'); |
| 276 | + } |
| 277 | + |
| 278 | + execSync( |
| 279 | + `docker exec ${this.container} bin/magento indexer:reindex catalog_product_price cataloginventory_stock`, |
| 280 | + { stdio: 'pipe', timeout: 120000 } |
| 281 | + ); |
| 282 | + console.log('Price index rebuilt.'); |
| 283 | + } |
| 284 | + |
| 285 | + /** |
| 286 | + * Full reindex of all indexers + cache flush via docker exec. |
| 287 | + */ |
| 288 | + reindexAll(): void { |
| 289 | + if (!this.container) { |
| 290 | + throw new Error('MAGENTO_CONTAINER env var is required for reindexing'); |
| 291 | + } |
| 292 | + |
| 293 | + execSync( |
| 294 | + `docker exec ${this.container} bin/magento indexer:reindex`, |
| 295 | + { stdio: 'pipe', timeout: 120000 } |
| 296 | + ); |
| 297 | + this.flushAllCaches(); |
| 298 | + console.log('Full reindex + cache flush done.'); |
| 299 | + } |
| 300 | + |
| 301 | + /** |
| 302 | + * Fetch a single product from the Channable feed by product ID. |
| 303 | + */ |
| 304 | + async getFeedProduct(baseURL: string, pid: number, storeId: number = 1): Promise<any> { |
| 305 | + const token = process.env.CHANNABLE_TOKEN || 'e2e-test-token'; |
| 306 | + const url = `${baseURL}channable/feed/json?id=${storeId}&token=${token}&pid=${pid}`; |
| 307 | + |
| 308 | + const response = await fetch(url, { |
| 309 | + headers: { 'Accept': 'application/json' }, |
| 310 | + }); |
| 311 | + |
| 312 | + if (!response.ok) { |
| 313 | + throw new Error(`Feed request failed: ${response.status}`); |
| 314 | + } |
| 315 | + |
| 316 | + const body = await response.json() as any; |
| 317 | + |
| 318 | + // ?pid= returns {"products": {"product": {...}, "feed": {...}}} |
| 319 | + // ?page= returns {"products": [...]} |
| 320 | + const productsNode = body.products; |
| 321 | + |
| 322 | + if (!productsNode) { |
| 323 | + throw new Error(`Product ${pid} not found in feed (no products key)`); |
| 324 | + } |
| 325 | + |
| 326 | + // Single product via ?pid= — return the processed feed object |
| 327 | + if (productsNode.feed) { |
| 328 | + return productsNode.feed; |
| 329 | + } |
| 330 | + |
| 331 | + // Array from ?page= |
| 332 | + if (Array.isArray(productsNode) && productsNode.length > 0) { |
| 333 | + return productsNode[0]; |
| 334 | + } |
| 335 | + |
| 336 | + throw new Error(`Product ${pid} not found in feed`); |
| 337 | + } |
| 338 | + |
| 339 | + /** |
| 340 | + * Get the Magento entity ID for a product by SKU (via REST API). |
| 341 | + */ |
| 342 | + async getProductId(baseURL: string, sku: string): Promise<number> { |
| 343 | + const token = process.env.admin_token; |
| 344 | + const url = `${baseURL}rest/all/V1/products/${encodeURIComponent(sku)}`; |
| 345 | + |
| 346 | + const response = await fetch(url, { |
| 347 | + headers: { |
| 348 | + 'Authorization': `Bearer ${token}`, |
| 349 | + 'Accept': 'application/json', |
| 350 | + }, |
| 351 | + }); |
| 352 | + |
| 353 | + if (!response.ok) { |
| 354 | + throw new Error(`Product ${sku} not found: ${response.status}`); |
| 355 | + } |
| 356 | + |
| 357 | + const data = await response.json() as any; |
| 358 | + return data.id; |
| 359 | + } |
194 | 360 | } |
0 commit comments