This commit is contained in:
@@ -0,0 +1,22 @@
|
|||||||
|
import csvParser from './csvParser.js';
|
||||||
|
import apiClient from './apiClient.js';
|
||||||
|
import config from './config.js';
|
||||||
|
|
||||||
|
export async function processCollections(filePath) {
|
||||||
|
try {
|
||||||
|
const items = await csvParser.parseCsv(filePath);
|
||||||
|
|
||||||
|
for (const item of items) {
|
||||||
|
console.log(`Processing item: ${item.name} (Product ID: ${item.productId})`);
|
||||||
|
// Perform API call to create collection (using apiClient)
|
||||||
|
// ... Create your collection API call here ...
|
||||||
|
// Use item.productId in your API call.
|
||||||
|
|
||||||
|
// Download image (using imageDownloader)
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
console.log("All collections processed successfully!");
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error processing collections:", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
const config = {
|
||||||
|
baseUrl: 'https://koillection.nelson-household.com/api',
|
||||||
|
apiEndpoint: '/api/collections' , // Replace with your API endpoint
|
||||||
|
imageDownloadDirectory: path.join(__dirname, 'master-set-images/perfect-order'), // Local directory for downloaded images
|
||||||
|
maxConcurrentDownloads: 5, // Limit concurrent downloads
|
||||||
|
dataValidationRules: {
|
||||||
|
collectionName: /^[a-zA-Z0-9_]+$/, // Regular expression for collection name
|
||||||
|
itemName: /^[a-zA-Z0-9_ ]+$/ // Regular expression for item name
|
||||||
|
},
|
||||||
|
collectionsMapping: {
|
||||||
|
'Perfect Order': './csv/ME03PerfectOrderProductsAndPrices.csv',
|
||||||
|
}
|
||||||
|
// Add more configuration settings here (e.g., image server URL, etc.)
|
||||||
|
};
|
||||||
|
|
||||||
|
export default config;
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import config from './config.js';
|
||||||
|
|
||||||
|
export async function parseCsv(filePath) {
|
||||||
|
const fs = require('fs').promises; // Use promises version
|
||||||
|
const csv = require('csv-parser');
|
||||||
|
|
||||||
|
const results = [];
|
||||||
|
const stream = fs.createReadStream(filePath).pipe(csv());
|
||||||
|
|
||||||
|
for await (const row of stream) {
|
||||||
|
// Map CSV columns to item properties. Adjust this mapping based on your CSV
|
||||||
|
const item = {
|
||||||
|
productId: row.productId, // Use productId from CSV
|
||||||
|
name: row.name,
|
||||||
|
collectionId: row.collectionId,
|
||||||
|
imageUrl: row.imageUrl
|
||||||
|
// Add other properties as needed
|
||||||
|
};
|
||||||
|
results.push(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
return results;
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
import config from './config.js';
|
||||||
|
|
||||||
|
export function validateData(data) {
|
||||||
|
let isValid = true;
|
||||||
|
|
||||||
|
for (const key in data) {
|
||||||
|
if (config.dataValidationRules[key]) {
|
||||||
|
const regex = config.dataValidationRules[key];
|
||||||
|
if (!regex.test(data[key])) {
|
||||||
|
console.warn(`Invalid value for ${key}: ${data[key]}`);
|
||||||
|
isValid = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return isValid;
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
import config from './config.js';
|
||||||
|
const username = process.env.KOILLECTION_USERNAME;
|
||||||
|
const password = process.env.KOILLECTION_PASSWORD;
|
||||||
|
|
||||||
|
export async function createJWTToken() {
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${config.baseUrl}/authentication_token`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ username, password }),
|
||||||
|
});
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Failed to obtain JWT token: ${response.status} - ${response.statusText}`);
|
||||||
|
}
|
||||||
|
const data = await response.json();
|
||||||
|
console.log(`JWT Token obtained: ${data.token}`);
|
||||||
|
return data.token;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Error obtaining JWT token:`, error);
|
||||||
|
throw error; // Re-throw to be caught in other functions
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user