From b39fc6e7dbbb2f3b8e78e560c63bb15405e467e6 Mon Sep 17 00:00:00 2001 From: Joseph Nelson Date: Sat, 2 May 2026 23:29:32 -0700 Subject: [PATCH] working on code breakup --- collectionProcessor.js | 22 ++++++++++++++++++++++ config.js | 18 ++++++++++++++++++ csvParser.js | 23 +++++++++++++++++++++++ dataValidator.js | 16 ++++++++++++++++ index.js | 0 token.js | 24 ++++++++++++++++++++++++ 6 files changed, 103 insertions(+) create mode 100644 collectionProcessor.js create mode 100644 config.js create mode 100644 csvParser.js create mode 100644 dataValidator.js create mode 100644 index.js create mode 100644 token.js diff --git a/collectionProcessor.js b/collectionProcessor.js new file mode 100644 index 0000000..b2177f7 --- /dev/null +++ b/collectionProcessor.js @@ -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); + } +} diff --git a/config.js b/config.js new file mode 100644 index 0000000..b4eff7c --- /dev/null +++ b/config.js @@ -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; diff --git a/csvParser.js b/csvParser.js new file mode 100644 index 0000000..9f154ed --- /dev/null +++ b/csvParser.js @@ -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; +} diff --git a/dataValidator.js b/dataValidator.js new file mode 100644 index 0000000..95352c8 --- /dev/null +++ b/dataValidator.js @@ -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; +} diff --git a/index.js b/index.js new file mode 100644 index 0000000..e69de29 diff --git a/token.js b/token.js new file mode 100644 index 0000000..68aa258 --- /dev/null +++ b/token.js @@ -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 + } +}