Skip to content

Commit

Permalink
Merge pull request #6 from CrystallizeAPI/new-schema
Browse files Browse the repository at this point in the history
Updated schema
  • Loading branch information
hakonkrogh authored Sep 10, 2020
2 parents 960064d + 51af922 commit d7564fe
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 107 deletions.
18 changes: 0 additions & 18 deletions products-import-from-csv/graph.js

This file was deleted.

29 changes: 13 additions & 16 deletions products-import-from-csv/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ const parseCSV = require('./parse-csv');
const utils = require('./utils');

async function importProducts({
language,
products,
tenantId,
shapeId,
vatTypeId,
treeParentId,
}) {
const chunks = utils.chunkArray(products, 10);

const productImportResult = [];

for (let i = 0; i < chunks.length; i++) {
const data = await utils.graphQLFetcher({
query: `mutation importProducts($products: [CreateProductInput!]!) {
// Import one product at a time
for (let i = 0; i < products.length; i++) {
const product = products[i];
const response = await utils.graphQLFetcher({
query: `mutation importProduct($language: String!, $product: CreateProductInput!) {
product {
createMany(input: $products) {
create(input: $product, language: $language) {
id
tree {
parentId
Expand All @@ -26,25 +27,20 @@ async function importProducts({
}
}`,
variables: {
products: products.map(product => ({
language,
product: {
...product,
tenantId,
shapeId,
vatTypeId,
tree: {
parentId: treeParentId,
},
name: [
{
language: 'en',
translation: product.name,
},
],
})),
},
},
});

productImportResult.push(data);
productImportResult.push(response);
}

return productImportResult;
Expand All @@ -62,7 +58,7 @@ async function importProducts({
} else {
console.log(`Found ${products.length} product(s) for import`);

const tenantId = await utils.getTenantId();
const { tenantId, language } = await utils.getTenantBaseInfo();

const {
shapeId,
Expand All @@ -71,6 +67,7 @@ async function importProducts({
} = await utils.getExtraProductProperties(tenantId);

const importResult = await importProducts({
language,
products,
tenantId,
shapeId,
Expand Down
5 changes: 5 additions & 0 deletions products-import-from-csv/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions products-import-from-csv/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"dependencies": {
"csv-parse": "^4.4.7",
"dotenv": "^8.2.0",
"inquirer": "^6.5.1",
"node-fetch": "2.6.0",
"ora": "^3.4.0",
Expand Down
13 changes: 4 additions & 9 deletions products-import-from-csv/parse-csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function getRows(csv) {
}
});

parser.on('error', error => {
parser.on('error', (error) => {
reject({
message: 'Could not parse CSV',
error,
Expand All @@ -38,7 +38,7 @@ function getRows(csv) {

function parseAttributes(attributesString) {
const parts = attributesString.split(';');
return parts.map(part => {
return parts.map((part) => {
const [attribute, value] = part.split('=');
return { attribute, value };
});
Expand All @@ -59,7 +59,7 @@ function getProductsFromRows(rows) {

const products = [];

rest.forEach(row => {
rest.forEach((row) => {
if (row[keys.type] === 'product') {
handleProduct(row);
} else {
Expand All @@ -78,12 +78,7 @@ function getProductsFromRows(rows) {
const product = products[products.length - 1];

product.variants.push({
name: [
{
language: 'en',
translation: row[keys.name],
},
],
name: row[keys.name],
sku: row[keys.sku],
price: parseFloat(row[keys.price], 10),
stock: parseInt(row[keys.stock], 10),
Expand Down
Loading

0 comments on commit d7564fe

Please sign in to comment.