-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from curiosta/feat/add-product-sync
Feat/add product sync
- Loading branch information
Showing
18 changed files
with
740 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,9 @@ package-lock.json | |
yarn.lock | ||
medusa-db.sql | ||
build | ||
.cache | ||
|
||
*.json | ||
.cache | ||
|
||
.env.* | ||
!.env.template |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Product, ProductStatus } from "@medusajs/medusa"; | ||
import { CreateProductInput } from "@medusajs/medusa/dist/types/product"; | ||
import { ulid } from "ulid"; | ||
|
||
|
||
type ExtraProps = { | ||
stock: number, | ||
categories: { id: string }[], | ||
rowNumber: number; | ||
} | ||
|
||
export type CreateProduct = Omit<Partial<Product>, 'variants' | 'categories'> & ExtraProps | ||
|
||
export type CreateProductResponse = CreateProductInput & { id: string, rowNumber: number } | ||
|
||
export const mapSheetProduct = (product: CreateProduct): CreateProductResponse => { | ||
return ({ | ||
id: product.id, | ||
rowNumber: product.rowNumber, | ||
title: product.title, | ||
handle: `${product.title.toLowerCase().replace(/-/g, '').replace(/ /g, '-')}_${ulid()}`, | ||
description: product.description, | ||
status: !product.stock ? ProductStatus.DRAFT : product.status, | ||
categories: product.categories, | ||
variants: [ | ||
{ | ||
title: 'one size', | ||
inventory_quantity: product.stock || 0, | ||
prices: [ | ||
{ | ||
amount: 10000, | ||
currency_code: 'usd' | ||
} | ||
] | ||
} | ||
], | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { ProductStatus, authenticate } from "@medusajs/medusa" | ||
import express, { Router } from "express"; | ||
import CategoryService from "../../../../services/category"; | ||
import GoogleSheetAPIService, { ProductData } from "../../../../services/google-sheet-api"; | ||
import ProductService from "../../../../services/product"; | ||
import { mapSheetProduct } from "./helpers/mapSheetProduct"; | ||
|
||
type CategoriesID = { id: string }[]; | ||
|
||
type SheetProductWithCategories = ProductData & { categories: CategoriesID }; | ||
|
||
export const SheetsRouter = (router: Router) => { | ||
router.use('/admin/sheets', express.json(), authenticate()); | ||
|
||
router.get('/admin/sheets', async (req, res) => { | ||
const googleSheetService = req.scope.resolve('googleSheetApiService') as GoogleSheetAPIService; | ||
const categoryService = req.scope.resolve('categoryService') as CategoryService; | ||
const productService = req.scope.resolve('productService') as ProductService | ||
|
||
googleSheetService.sheetId = (req.query.sheetId as string) || '1TaiFMTqYGirhLrjUkEfCGbV3hCrX9po_tduFw_sETUg'; | ||
|
||
try { | ||
const sheetData = await googleSheetService.getProductDataBySheetId(); | ||
|
||
const promises = sheetData.map(async (entry) => { | ||
const location = await categoryService.getCategoryByName(entry.Location); | ||
const category = await categoryService.getCategoryByName(entry.Category); | ||
|
||
const categories: { id: string }[] = [] | ||
|
||
if (location) { | ||
categories.push({ id: location.id }) | ||
} | ||
if (category) { | ||
categories.push({ id: category.id }) | ||
} | ||
|
||
return { ...entry, categories } | ||
}); | ||
|
||
let results = (await Promise.allSettled(promises)).filter(r => r.status === 'fulfilled').map((r: any) => r.value) | ||
|
||
results = results.map((product: SheetProductWithCategories) => { | ||
return mapSheetProduct({ | ||
id: product['Product ID'], | ||
title: product['Product title'], | ||
description: product.Description, | ||
stock: product.Stocks, | ||
categories: product.categories, | ||
rowNumber: product.rowNumber, | ||
status: product.Draft ? ProductStatus.DRAFT : ProductStatus.PUBLISHED | ||
}) | ||
}); | ||
|
||
const bulkAddResult = await productService.addBulkProducts(results) | ||
const savedProductIdAndRowNumber = bulkAddResult.saved.map((p) => ({ id: p.id, rowNumber: p.rowNumber })) | ||
|
||
const affectedCellsCount = await googleSheetService.updateProductId(savedProductIdAndRowNumber) | ||
|
||
return res.json({ status: 'ok', affectedCellsCount }) | ||
} catch (error) { | ||
return res.status(500).json({ status: 500, message: 'An error occurred!', error: error instanceof Error ? error.message : error }) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { authenticate } from "@medusajs/medusa" | ||
import express, { Router } from "express"; | ||
import CategoryService from "../../../../services/category"; | ||
import GoogleSheetAPIService from "../../../../services/google-sheet-api"; | ||
|
||
|
||
export const SheetsSyncCategoriesRouter = (router: Router) => { | ||
router.use('/admin/sheets/sync-categories', express.json(), authenticate()); | ||
|
||
router.get('/admin/sheets/sync-categories', async (req, res) => { | ||
const googleSheetService = req.scope.resolve('googleSheetApiService') as GoogleSheetAPIService; | ||
const categoryService = req.scope.resolve('categoryService') as CategoryService; | ||
|
||
googleSheetService.sheetId = (req.query.sheetId as string) || '1TaiFMTqYGirhLrjUkEfCGbV3hCrX9po_tduFw_sETUg' | ||
|
||
try { | ||
|
||
const categories = await categoryService.retrieveAllCategoriesName() | ||
|
||
if (categories.length) { | ||
await googleSheetService.syncCategories(categories) | ||
} | ||
|
||
return res.json({ status: categories.length ? 'ok' : 'failed, no categories available!' }) | ||
} catch (error) { | ||
return res.status(500).json({ status: 500, message: 'An error occurred!', error: error instanceof Error ? error.message : error }) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { authenticate } from "@medusajs/medusa"; | ||
import express, { Router } from "express"; | ||
import CategoryService from "../../../../services/category"; | ||
import GoogleSheetAPIService from "../../../../services/google-sheet-api"; | ||
|
||
|
||
export const SheetsSyncLocationsRouter = (router: Router) => { | ||
router.use('/admin/sheets/sync-locations', express.json(), authenticate()); | ||
|
||
router.get('/admin/sheets/sync-locations', async (req, res) => { | ||
const googleSheetService = req.scope.resolve('googleSheetApiService') as GoogleSheetAPIService; | ||
const categoryService = req.scope.resolve('categoryService') as CategoryService; | ||
|
||
googleSheetService.sheetId = (req.query.sheetId as string) || '1TaiFMTqYGirhLrjUkEfCGbV3hCrX9po_tduFw_sETUg' | ||
|
||
try { | ||
|
||
const lastLocations = await categoryService.retrieveAllLastLocationsName() | ||
|
||
|
||
if (lastLocations.length) { | ||
await googleSheetService.syncLocations(lastLocations) | ||
} | ||
|
||
return res.json({ status: lastLocations.length ? 'ok' : 'failed, no locations available!' }) | ||
} catch (error) { | ||
return res.status(500).json({ status: 500, message: 'An error occurred!', error: error instanceof Error ? error.message : error }) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { authenticate } from "@medusajs/medusa" | ||
import express, { Router } from "express"; | ||
import GoogleSheetAPIService from "../../../../services/google-sheet-api"; | ||
import ProductService from "../../../../services/product"; | ||
|
||
|
||
export const SheetsSyncProductsRouter = (router: Router) => { | ||
router.use('/admin/sheets/sync-products', express.json(), authenticate()); | ||
|
||
router.get('/admin/sheets/sync-products', async (req, res) => { | ||
const googleSheetService = req.scope.resolve('googleSheetApiService') as GoogleSheetAPIService; | ||
const productService = req.scope.resolve('productService') as ProductService; | ||
|
||
googleSheetService.sheetId = (req.query.sheetId as string) || '1TaiFMTqYGirhLrjUkEfCGbV3hCrX9po_tduFw_sETUg' | ||
|
||
try { | ||
const products = await productService.list({}, { relations: ['categories'] }) | ||
|
||
googleSheetService.syncProducts(products) | ||
|
||
return res.json({ status: products.length ? 'ok' : 'failed, no categories available!' }) | ||
} catch (error) { | ||
return res.status(500).json({ status: 500, message: 'An error occurred!', error: error instanceof Error ? error.message : error }) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.