Skip to content

Commit

Permalink
Merge pull request #48 from curiosta/feat/add-product-sync
Browse files Browse the repository at this point in the history
Feat/add product sync
  • Loading branch information
Labham-Jain authored Nov 1, 2023
2 parents b37c0a7 + 47fca3f commit ffb2f78
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 9 deletions.
Binary file modified dump.rdb
Binary file not shown.
2 changes: 1 addition & 1 deletion src/api/routes/admin/sheets/helpers/mapSheetProduct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type UpdateProductResponse = UpdateProductInput & { id: string, rowNumber

export const mapSheetProduct = (product: CreateProduct): CreateProductResponse | UpdateProductResponse => {

const options = product.options?.length ? [{ id: product.options[0].id, value: 'one size' }] : undefined
const options = product.options?.length ? [{ option_id: product.options[0].id, value: 'one size' }] : undefined
return ({
id: product.id,
rowNumber: product.rowNumber,
Expand Down
2 changes: 1 addition & 1 deletion src/api/routes/admin/sheets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const SheetsRouter = (router: Router) => {
const promises = sheetData.map(async (entry) => {
const location = await categoryService.getCategoryByName(entry.Location.split('/').pop().trim());
const category = await categoryService.getCategoryByName(entry.Category);
let options;
let options: ProductOption[];
try {
const product = await productService.retrieve(entry["Product ID"], { relations: ['options'] })
options = product.options;
Expand Down
8 changes: 5 additions & 3 deletions src/services/google-sheet-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ class GoogleSheetAPIService extends TransactionBaseService {
const csvDataArray = [];

// Filter and process the data to create the array of objects
const filteredData = response.data.values?.filter(row => row.slice(1).every((cell) => cell !== '' && cell !== null));

filteredData.slice(1).forEach((row, index) => {
response.data.values?.slice(1).forEach((row, index) => {
const itemData = {
rowNumber: index + 2 // 1 for index, 2nd for header. so adding 2.
};

if (!row[1] || !row[2]) {
return;
}

row.forEach((cell, columnIndex) => {
const header = headers[columnIndex];

Expand Down
7 changes: 3 additions & 4 deletions src/services/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class ProductService extends BaseProductService {

async addBulkProducts(products: ((CreateProductInput | UpdateProductInput) & { id: string; rowNumber: number; })[]) {
await this.loginAdmin();

const promises = products.map((product, i) => {
return new Promise<Partial<Product & { rowNumber: number }>>(async (resolve, reject) => {
if (product.id) {
Expand All @@ -79,18 +80,16 @@ class ProductService extends BaseProductService {
try {
if (productExists) {
const { id, rowNumber, ...updateProduct } = product as UpdateProductInput & { id: string; rowNumber: number; };

const { product: updatedProduct } = await this.updateProductWithFetch(id, updateProduct)
resolve({ ...updatedProduct, rowNumber });
} else {

const { id, rowNumber, ...createProduct } = product as CreateProductInput & { id: string; rowNumber: number; }

const { product: createdProduct } = await this.createProductWithFetch(createProduct);

resolve({ ...createdProduct, rowNumber })
}
} catch (error) {
console.log('An error occurred!', error.response.data);
console.log('An error occurred!\n', error.response.data);
}
} else {
const { id, rowNumber, ...createProduct } = product as CreateProductInput & { id: string, rowNumber: number; }
Expand Down

0 comments on commit ffb2f78

Please sign in to comment.