Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add product sync #48

Merged
merged 3 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading