diff --git a/.eslintignore b/.eslintignore index 5a131029ce..8b429117d2 100644 --- a/.eslintignore +++ b/.eslintignore @@ -10,7 +10,6 @@ /packages/medusa/src/subscribers/notification.js /packages/medusa/src/subscribers/order.js /packages/medusa/src/subscribers/product.js - /packages/medusa/src/loaders/api.js /packages/medusa/src/loaders/database.js /packages/medusa/src/loaders/defaults.js @@ -24,34 +23,21 @@ /packages/medusa/src/loaders/repositories.js /packages/medusa/src/loaders/services.js /packages/medusa/src/loaders/subscribers.js - /packages/medusa/src/api/routes/admin/apps /packages/medusa/src/api/routes/admin/auth /packages/medusa/src/api/routes/admin/collections /packages/medusa/src/api/routes/admin/customers /packages/medusa/src/api/routes/admin/discounts /packages/medusa/src/api/routes/admin/draft-orders - /packages/medusa/src/api/routes/admin/notes -/packages/medusa/src/api/routes/admin/notifications /packages/medusa/src/api/routes/admin/orders -/packages/medusa/src/api/routes/admin/products -/packages/medusa/src/api/routes/admin/regions /packages/medusa/src/api/routes/admin/return-reasons /packages/medusa/src/api/routes/admin/returns -/packages/medusa/src/api/routes/admin/shipping-options /packages/medusa/src/api/routes/admin/shipping-profiles /packages/medusa/src/api/routes/admin/store -/packages/medusa/src/api/routes/admin/swaps - - -/packages/medusa/src/api/routes/store/auth /packages/medusa/src/api/routes/store/carts - -/packages/medusa/src/api/routes/store/regions /packages/medusa/src/api/routes/store/return-reasons /packages/medusa/src/api/routes/store/returns - /packages/medusa/src/api/routes/store/swaps # END OF FILES TODO @@ -62,7 +48,6 @@ /packages/medusa/src/helpers /packages/medusa/src/migrations /packages/medusa/src/utils - /integration-tests /docs /docs-util diff --git a/.github/actions/cache-bootstrap/action.yml b/.github/actions/cache-bootstrap/action.yml new file mode 100644 index 0000000000..238acdceeb --- /dev/null +++ b/.github/actions/cache-bootstrap/action.yml @@ -0,0 +1,33 @@ +name: cache-bootstrap +description: Creates a cache with the given extension for lerna packages +inputs: + extension: + description: Extension for cache name + partial: + description: Boolean flag to describe whether or not to run a partial bootstrap when finding cache + default: false +runs: + using: composite + steps: + # for always overriding cache, use: pat-s/always-upload-cache@v2.1.5 + - uses: actions/cache@v2 + id: cache + with: + path: | + node_modules + */*/node_modules + key: ${{ runner.os }}-yarn-${{inputs.extension}}-v8-${{ hashFiles('**/yarn.lock') }} + # We want to only bootstrap and install if no cache is found. + # Futhermore, we might want to do a partial, hoisted, bootstrap towards + # the base branch if it exists, otherwise we choose develop for this. + + # yarn install --frozen-lockfile + - run: | + if [[ "${{steps.cache.outputs.cache-hit}}" != "true" || "${{inputs.partial}}" != "true" ]]; then + yarn install --frozen-lockfile + yarn bootstrap --concurrency=2 + elif [[ "${{inputs.partial}}" = "true" ]]; then + [[ ! -z "${GITHUB_BASE_REF}" ]] && ref="${GITHUB_BASE_REF#refs/heads/}" || ref="develop" + yarn bootstrap --npm-client=npm --hoist --since "origin/${ref}...HEAD" --concurrency=2 + fi + shell: bash diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml new file mode 100644 index 0000000000..c1c33c4ad7 --- /dev/null +++ b/.github/workflows/action.yml @@ -0,0 +1,82 @@ +name: Medusa Pipeline +on: [push, pull_request] + +jobs: + unit-tests: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2.3.5 + with: + fetch-depth: 0 + + - name: Setup Node.js environment + uses: actions/setup-node@v2.4.1 + with: + node-version: '14' + cache: 'yarn' + + - name: Assert changed + run: ./scripts/assert-changed-files-actions.sh "packages" + + - name: Bootstrap packages + uses: ./.github/actions/cache-bootstrap + with: + extension: unit-tests + + - name: Run unit tests + run: node --max-old-space-size=2048 ./node_modules/.bin/jest -w 1 + + integration-tests: + runs-on: ubuntu-latest + + services: + postgres: + image: postgres + env: + POSTGRES_PASSWORD: postgres + POSTGRES_USER: postgres + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + ports: + - 5432:5432 + + steps: + - name: Checkout + uses: actions/checkout@v2.3.5 + with: + fetch-depth: 0 + + - name: Setup Node.js environment + uses: actions/setup-node@v2.4.1 + with: + node-version: '14' + cache: 'yarn' + + - name: Bootstrap packages + uses: ./.github/actions/cache-bootstrap + with: + extension: integration-tests + + - name: Install dev cli + run: sudo npm i -g medusa-dev-cli + + - name: Set path to medusa repo + run: medusa-dev --set-path-to-repo `pwd` + + - name: Force install + run: medusa-dev --force-install + working-directory: integration-tests/api + + - name: Build integration tests + run: yarn build + working-directory: integration-tests/api + + - name: Run integration tests + run: yarn test + working-directory: integration-tests/api + env: + DB_PASSWORD: postgres diff --git a/packages/medusa/src/api/routes/admin/notifications/index.js b/packages/medusa/src/api/routes/admin/notifications/index.js index a442e895ba..b6549833f4 100644 --- a/packages/medusa/src/api/routes/admin/notifications/index.js +++ b/packages/medusa/src/api/routes/admin/notifications/index.js @@ -3,7 +3,7 @@ import middlewares from "../../../middlewares" const route = Router() -export default app => { +export default (app) => { app.use("/notifications", route) /** diff --git a/packages/medusa/src/api/routes/admin/notifications/list-notifications.js b/packages/medusa/src/api/routes/admin/notifications/list-notifications.js index 513f0e6b88..8b9799656a 100644 --- a/packages/medusa/src/api/routes/admin/notifications/list-notifications.js +++ b/packages/medusa/src/api/routes/admin/notifications/list-notifications.js @@ -21,63 +21,59 @@ import { defaultRelations, defaultFields } from "./" * $ref: "#/components/schemas/notification" */ export default async (req, res) => { - try { - const notificationService = req.scope.resolve("notificationService") + const notificationService = req.scope.resolve("notificationService") - const limit = parseInt(req.query.limit) || 50 - const offset = parseInt(req.query.offset) || 0 + const limit = parseInt(req.query.limit) || 50 + const offset = parseInt(req.query.offset) || 0 - let selector = {} + const selector = {} - let includeFields = [] - if ("fields" in req.query) { - includeFields = req.query.fields.split(",") - } - - let expandFields = [] - if ("expand" in req.query) { - expandFields = req.query.expand.split(",") - } - - if ("event_name" in req.query) { - const values = req.query.event_name.split(",") - selector.event_name = values.length > 1 ? values : values[0] - } - - if ("resource_type" in req.query) { - const values = req.query.resource_type.split(",") - selector.resource_type = values.length > 1 ? values : values[0] - } - - if ("resource_id" in req.query) { - const values = req.query.resource_id.split(",") - selector.resource_id = values.length > 1 ? values : values[0] - } - - if ("to" in req.query) { - const values = req.query.to.split(",") - selector.to = values.length > 1 ? values : values[0] - } - - if (!("include_resends" in req.query)) { - selector.parent_id = null - } - - const listConfig = { - select: includeFields.length ? includeFields : defaultFields, - relations: expandFields.length ? expandFields : defaultRelations, - skip: offset, - take: limit, - order: { created_at: "DESC" }, - } - - const notifications = await notificationService.list(selector, listConfig) - - const fields = [...listConfig.select, ...listConfig.relations] - const data = notifications.map(o => _.pick(o, fields)) - - res.json({ notifications: data, offset, limit }) - } catch (error) { - throw error + let includeFields = [] + if ("fields" in req.query) { + includeFields = req.query.fields.split(",") } + + let expandFields = [] + if ("expand" in req.query) { + expandFields = req.query.expand.split(",") + } + + if ("event_name" in req.query) { + const values = req.query.event_name.split(",") + selector.event_name = values.length > 1 ? values : values[0] + } + + if ("resource_type" in req.query) { + const values = req.query.resource_type.split(",") + selector.resource_type = values.length > 1 ? values : values[0] + } + + if ("resource_id" in req.query) { + const values = req.query.resource_id.split(",") + selector.resource_id = values.length > 1 ? values : values[0] + } + + if ("to" in req.query) { + const values = req.query.to.split(",") + selector.to = values.length > 1 ? values : values[0] + } + + if (!("include_resends" in req.query)) { + selector.parent_id = null + } + + const listConfig = { + select: includeFields.length ? includeFields : defaultFields, + relations: expandFields.length ? expandFields : defaultRelations, + skip: offset, + take: limit, + order: { created_at: "DESC" }, + } + + const notifications = await notificationService.list(selector, listConfig) + + const fields = [...listConfig.select, ...listConfig.relations] + const data = notifications.map((o) => _.pick(o, fields)) + + res.json({ notifications: data, offset, limit }) } diff --git a/packages/medusa/src/api/routes/admin/notifications/resend-notification.js b/packages/medusa/src/api/routes/admin/notifications/resend-notification.js index 92f94220c6..a701ae5725 100644 --- a/packages/medusa/src/api/routes/admin/notifications/resend-notification.js +++ b/packages/medusa/src/api/routes/admin/notifications/resend-notification.js @@ -32,24 +32,20 @@ export default async (req, res) => { throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details) } - try { - const notificationService = req.scope.resolve("notificationService") + const notificationService = req.scope.resolve("notificationService") - const config = {} + const config = {} - if (value.to) { - config.to = value.to - } - - await notificationService.resend(id, config) - - const notification = await notificationService.retrieve(id, { - select: defaultFields, - relations: defaultRelations, - }) - - res.json({ notification }) - } catch (error) { - throw error + if (value.to) { + config.to = value.to } + + await notificationService.resend(id, config) + + const notification = await notificationService.retrieve(id, { + select: defaultFields, + relations: defaultRelations, + }) + + res.json({ notification }) } diff --git a/packages/medusa/src/api/routes/admin/products/add-option.js b/packages/medusa/src/api/routes/admin/products/add-option.js index c7f9e5291c..cfe4ba273a 100644 --- a/packages/medusa/src/api/routes/admin/products/add-option.js +++ b/packages/medusa/src/api/routes/admin/products/add-option.js @@ -39,17 +39,13 @@ export default async (req, res) => { throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details) } - try { - const productService = req.scope.resolve("productService") + const productService = req.scope.resolve("productService") - await productService.addOption(id, value.title) - const product = await productService.retrieve(id, { - select: defaultFields, - relations: defaultRelations, - }) + await productService.addOption(id, value.title) + const product = await productService.retrieve(id, { + select: defaultFields, + relations: defaultRelations, + }) - res.json({ product }) - } catch (err) { - throw err - } + res.json({ product }) } diff --git a/packages/medusa/src/api/routes/admin/products/create-product.js b/packages/medusa/src/api/routes/admin/products/create-product.js index d02ac8fb7d..86c6004ff1 100644 --- a/packages/medusa/src/api/routes/admin/products/create-product.js +++ b/packages/medusa/src/api/routes/admin/products/create-product.js @@ -188,9 +188,7 @@ export default async (req, res) => { description: Validator.string().allow(""), is_giftcard: Validator.boolean().default(false), discountable: Validator.boolean().default(true), - images: Validator.array() - .items(Validator.string()) - .optional(), + images: Validator.array().items(Validator.string()).optional(), thumbnail: Validator.string().optional(), handle: Validator.string().optional(), status: Validator.string() @@ -203,9 +201,7 @@ export default async (req, res) => { }) .allow(null) .optional(), - collection_id: Validator.string() - .allow(null) - .optional(), + collection_id: Validator.string().allow(null).optional(), tags: Validator.array() .items({ id: Validator.string().optional(), @@ -225,30 +221,13 @@ export default async (req, res) => { inventory_quantity: Validator.number().default(0), allow_backorder: Validator.boolean().optional(), manage_inventory: Validator.boolean().optional(), - weight: Validator.number() - .allow(null) - .optional(), - length: Validator.number() - .allow(null) - .optional(), - height: Validator.number() - .allow(null) - .optional(), - width: Validator.number() - .allow(null) - .optional(), - origin_country: Validator.string() - .optional() - .allow("") - .allow(null), - mid_code: Validator.string() - .optional() - .allow("") - .allow(null), - material: Validator.string() - .optional() - .allow("") - .allow(null), + weight: Validator.number().allow(null).optional(), + length: Validator.number().allow(null).optional(), + height: Validator.number().allow(null).optional(), + width: Validator.number().allow(null).optional(), + origin_country: Validator.string().optional().allow("").allow(null), + mid_code: Validator.string().optional().allow("").allow(null), + material: Validator.string().optional().allow("").allow(null), metadata: Validator.object().optional(), prices: Validator.array() .items( @@ -256,9 +235,7 @@ export default async (req, res) => { .keys({ region_id: Validator.string(), currency_code: Validator.string(), - amount: Validator.number() - .integer() - .required(), + amount: Validator.number().integer().required(), sale_amount: Validator.number().optional(), }) .xor("region_id", "currency_code") @@ -270,30 +247,14 @@ export default async (req, res) => { }) .default([]), }), - weight: Validator.number() - .allow(null) - .optional(), - length: Validator.number() - .allow(null) - .optional(), - height: Validator.number() - .allow(null) - .optional(), - width: Validator.number() - .allow(null) - .optional(), - hs_code: Validator.string() - .optional() - .allow(""), - origin_country: Validator.string() - .optional() - .allow(""), - mid_code: Validator.string() - .optional() - .allow(""), - material: Validator.string() - .optional() - .allow(""), + weight: Validator.number().allow(null).optional(), + length: Validator.number().allow(null).optional(), + height: Validator.number().allow(null).optional(), + width: Validator.number().allow(null).optional(), + hs_code: Validator.string().optional().allow(""), + origin_country: Validator.string().optional().allow(""), + mid_code: Validator.string().optional().allow(""), + material: Validator.string().optional().allow(""), metadata: Validator.object().optional(), }) @@ -302,66 +263,64 @@ export default async (req, res) => { throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details) } - try { - const productService = req.scope.resolve("productService") - const productVariantService = req.scope.resolve("productVariantService") - const shippingProfileService = req.scope.resolve("shippingProfileService") + const productService = req.scope.resolve("productService") + const productVariantService = req.scope.resolve("productVariantService") + const shippingProfileService = req.scope.resolve("shippingProfileService") - const entityManager = req.scope.resolve("manager") + const entityManager = req.scope.resolve("manager") - let newProduct - await entityManager.transaction(async manager => { - const { variants } = value - delete value.variants + let newProduct + await entityManager.transaction(async (manager) => { + const { variants } = value + delete value.variants - if (!value.thumbnail && value.images && value.images.length) { - value.thumbnail = value.images[0] + if (!value.thumbnail && value.images && value.images.length) { + value.thumbnail = value.images[0] + } + + let shippingProfile + // Get default shipping profile + if (value.is_giftcard) { + shippingProfile = await shippingProfileService.retrieveGiftCardDefault() + } else { + shippingProfile = await shippingProfileService.retrieveDefault() + } + + newProduct = await productService + .withTransaction(manager) + .create({ ...value, profile_id: shippingProfile.id }) + + if (variants) { + for (const [i, variant] of variants.entries()) { + variant.variant_rank = i } - let shippingProfile - // Get default shipping profile - if (value.is_giftcard) { - shippingProfile = await shippingProfileService.retrieveGiftCardDefault() - } else { - shippingProfile = await shippingProfileService.retrieveDefault() - } + const optionIds = value.options.map( + (o) => newProduct.options.find((newO) => newO.title === o.title).id + ) - newProduct = await productService - .withTransaction(manager) - .create({ ...value, profile_id: shippingProfile.id }) + await Promise.all( + variants.map(async (v) => { + const variant = { + ...v, + options: v.options.map((o, index) => ({ + ...o, + option_id: optionIds[index], + })), + } - if (variants) { - for (const [i, variant] of variants.entries()) variant.variant_rank = i + await productVariantService + .withTransaction(manager) + .create(newProduct.id, variant) + }) + ) + } + }) - const optionIds = value.options.map( - o => newProduct.options.find(newO => newO.title === o.title).id - ) + const product = await productService.retrieve(newProduct.id, { + select: defaultFields, + relations: defaultRelations, + }) - await Promise.all( - variants.map(async v => { - const variant = { - ...v, - options: v.options.map((o, index) => ({ - ...o, - option_id: optionIds[index], - })), - } - - await productVariantService - .withTransaction(manager) - .create(newProduct.id, variant) - }) - ) - } - }) - - const product = await productService.retrieve(newProduct.id, { - select: defaultFields, - relations: defaultRelations, - }) - - res.json({ product }) - } catch (err) { - throw err - } + res.json({ product }) } diff --git a/packages/medusa/src/api/routes/admin/products/create-variant.js b/packages/medusa/src/api/routes/admin/products/create-variant.js index a45722cd0c..d6ad39e100 100644 --- a/packages/medusa/src/api/routes/admin/products/create-variant.js +++ b/packages/medusa/src/api/routes/admin/products/create-variant.js @@ -115,18 +115,10 @@ export default async (req, res) => { inventory_quantity: Validator.number().default(0), allow_backorder: Validator.boolean().optional(), manage_inventory: Validator.boolean().optional(), - weight: Validator.number() - .allow(null) - .optional(), - length: Validator.number() - .allow(null) - .optional(), - height: Validator.number() - .allow(null) - .optional(), - width: Validator.number() - .allow(null) - .optional(), + weight: Validator.number().allow(null).optional(), + length: Validator.number().allow(null).optional(), + height: Validator.number().allow(null).optional(), + width: Validator.number().allow(null).optional(), origin_country: Validator.string().allow(""), mid_code: Validator.string().allow(""), material: Validator.string().allow(""), @@ -137,9 +129,7 @@ export default async (req, res) => { .keys({ region_id: Validator.string().empty(null), currency_code: Validator.string().required(), - amount: Validator.number() - .integer() - .required(), + amount: Validator.number().integer().required(), sale_amount: Validator.number().optional(), }) .xor("region_id", "currency_code") @@ -158,19 +148,15 @@ export default async (req, res) => { throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details) } - try { - const productVariantService = req.scope.resolve("productVariantService") - const productService = req.scope.resolve("productService") + const productVariantService = req.scope.resolve("productVariantService") + const productService = req.scope.resolve("productService") - await productVariantService.create(id, value) + await productVariantService.create(id, value) - const product = await productService.retrieve(id, { - select: defaultFields, - relations: defaultRelations, - }) + const product = await productService.retrieve(id, { + select: defaultFields, + relations: defaultRelations, + }) - res.json({ product }) - } catch (err) { - throw err - } + res.json({ product }) } diff --git a/packages/medusa/src/api/routes/admin/products/delete-option.js b/packages/medusa/src/api/routes/admin/products/delete-option.js index 8b8911713e..2e5e4ce25e 100644 --- a/packages/medusa/src/api/routes/admin/products/delete-option.js +++ b/packages/medusa/src/api/routes/admin/products/delete-option.js @@ -31,21 +31,17 @@ import { defaultRelations, defaultFields } from "." export default async (req, res) => { const { id, option_id } = req.params - try { - const productService = req.scope.resolve("productService") - await productService.deleteOption(id, option_id) - const data = await productService.retrieve(id, { - select: defaultFields, - relations: defaultRelations, - }) + const productService = req.scope.resolve("productService") + await productService.deleteOption(id, option_id) + const data = await productService.retrieve(id, { + select: defaultFields, + relations: defaultRelations, + }) - res.json({ - option_id, - object: "option", - deleted: true, - product: data, - }) - } catch (err) { - throw err - } + res.json({ + option_id, + object: "option", + deleted: true, + product: data, + }) } diff --git a/packages/medusa/src/api/routes/admin/products/delete-product.js b/packages/medusa/src/api/routes/admin/products/delete-product.js index a09b99273d..5b87f1d90b 100644 --- a/packages/medusa/src/api/routes/admin/products/delete-product.js +++ b/packages/medusa/src/api/routes/admin/products/delete-product.js @@ -26,15 +26,11 @@ export default async (req, res) => { const { id } = req.params - try { - const productService = req.scope.resolve("productService") - await productService.delete(id) - res.json({ - id, - object: "product", - deleted: true, - }) - } catch (err) { - throw err - } + const productService = req.scope.resolve("productService") + await productService.delete(id) + res.json({ + id, + object: "product", + deleted: true, + }) } diff --git a/packages/medusa/src/api/routes/admin/products/delete-variant.js b/packages/medusa/src/api/routes/admin/products/delete-variant.js index 0381c85f5a..dec7a36628 100644 --- a/packages/medusa/src/api/routes/admin/products/delete-variant.js +++ b/packages/medusa/src/api/routes/admin/products/delete-variant.js @@ -29,24 +29,20 @@ import { defaultRelations, defaultFields } from "." export default async (req, res) => { const { id, variant_id } = req.params - try { - const productVariantService = req.scope.resolve("productVariantService") - const productService = req.scope.resolve("productService") + const productVariantService = req.scope.resolve("productVariantService") + const productService = req.scope.resolve("productService") - await productVariantService.delete(variant_id) + await productVariantService.delete(variant_id) - const data = await productService.retrieve(id, { - select: defaultFields, - relations: defaultRelations, - }) + const data = await productService.retrieve(id, { + select: defaultFields, + relations: defaultRelations, + }) - res.json({ - variant_id, - object: "product-variant", - deleted: true, - product: data, - }) - } catch (err) { - throw err - } + res.json({ + variant_id, + object: "product-variant", + deleted: true, + product: data, + }) } diff --git a/packages/medusa/src/api/routes/admin/products/index.js b/packages/medusa/src/api/routes/admin/products/index.js index e32e13d168..54b1deb17a 100644 --- a/packages/medusa/src/api/routes/admin/products/index.js +++ b/packages/medusa/src/api/routes/admin/products/index.js @@ -3,7 +3,7 @@ import middlewares from "../../../middlewares" const route = Router() -export default app => { +export default (app) => { app.use("/products", route) route.post("/", middlewares.wrap(require("./create-product").default)) diff --git a/packages/medusa/src/api/routes/admin/products/list-products.js b/packages/medusa/src/api/routes/admin/products/list-products.js index ea309a43ab..25d1595a3e 100644 --- a/packages/medusa/src/api/routes/admin/products/list-products.js +++ b/packages/medusa/src/api/routes/admin/products/list-products.js @@ -1,4 +1,3 @@ -import _ from "lodash" import { MedusaError, Validator } from "medusa-core-utils" import { defaultFields, defaultRelations, filterableFields } from "./" @@ -42,52 +41,48 @@ export default async (req, res) => { ) } - try { - const productService = req.scope.resolve("productService") + const productService = req.scope.resolve("productService") - const limit = parseInt(req.query.limit) || 50 - const offset = parseInt(req.query.offset) || 0 + const limit = parseInt(req.query.limit) || 50 + const offset = parseInt(req.query.offset) || 0 - const selector = {} + const selector = {} - if ("q" in req.query) { - selector.q = req.query.q - } - - let includeFields = [] - if ("fields" in req.query) { - includeFields = req.query.fields.split(",") - } - - let expandFields = [] - if ("expand" in req.query) { - expandFields = req.query.expand.split(",") - } - - for (const k of filterableFields) { - if (k in value) { - selector[k] = value[k] - } - } - - if (selector.status?.indexOf("null") > -1) { - selector.status.splice(selector.status.indexOf("null"), 1) - if (selector.status.length === 0) { - delete selector.status - } - } - - const listConfig = { - select: includeFields.length ? includeFields : defaultFields, - relations: expandFields.length ? expandFields : defaultRelations, - skip: offset, - take: limit, - } - - let products = await productService.list(selector, listConfig) - - res.json({ products, count: products.length, offset, limit }) - } catch (error) { - throw error + if ("q" in req.query) { + selector.q = req.query.q } + + let includeFields = [] + if ("fields" in req.query) { + includeFields = req.query.fields.split(",") + } + + let expandFields = [] + if ("expand" in req.query) { + expandFields = req.query.expand.split(",") + } + + for (const k of filterableFields) { + if (k in value) { + selector[k] = value[k] + } + } + + if (selector.status?.indexOf("null") > -1) { + selector.status.splice(selector.status.indexOf("null"), 1) + if (selector.status.length === 0) { + delete selector.status + } + } + + const listConfig = { + select: includeFields.length ? includeFields : defaultFields, + relations: expandFields.length ? expandFields : defaultRelations, + skip: offset, + take: limit, + } + + const products = await productService.list(selector, listConfig) + + res.json({ products, count: products.length, offset, limit }) } diff --git a/packages/medusa/src/api/routes/admin/products/list-tag-usage-count.js b/packages/medusa/src/api/routes/admin/products/list-tag-usage-count.js index 9bd36ac04d..bb8ed39896 100644 --- a/packages/medusa/src/api/routes/admin/products/list-tag-usage-count.js +++ b/packages/medusa/src/api/routes/admin/products/list-tag-usage-count.js @@ -1,11 +1,7 @@ export default async (req, res) => { - try { - const productService = req.scope.resolve("productService") + const productService = req.scope.resolve("productService") - const tags = await productService.listTagsByUsage() + const tags = await productService.listTagsByUsage() - res.json({ tags }) - } catch (error) { - throw error - } + res.json({ tags }) } diff --git a/packages/medusa/src/api/routes/admin/products/list-types.js b/packages/medusa/src/api/routes/admin/products/list-types.js index d1d3b78f59..1c0cc5d603 100644 --- a/packages/medusa/src/api/routes/admin/products/list-types.js +++ b/packages/medusa/src/api/routes/admin/products/list-types.js @@ -18,13 +18,9 @@ * $ref: "#/components/schemas/product_type" */ export default async (req, res) => { - try { - const productService = req.scope.resolve("productService") + const productService = req.scope.resolve("productService") - const types = await productService.listTypes() + const types = await productService.listTypes() - res.json({ types }) - } catch (error) { - throw error - } + res.json({ types }) } diff --git a/packages/medusa/src/api/routes/admin/products/set-metadata.js b/packages/medusa/src/api/routes/admin/products/set-metadata.js index fbd509e1e7..a25ae4562b 100644 --- a/packages/medusa/src/api/routes/admin/products/set-metadata.js +++ b/packages/medusa/src/api/routes/admin/products/set-metadata.js @@ -14,19 +14,15 @@ export default async (req, res) => { throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details) } - try { - const productService = req.scope.resolve("productService") - await productService.update(id, { - metadata: { [value.key]: value.value }, - }) + const productService = req.scope.resolve("productService") + await productService.update(id, { + metadata: { [value.key]: value.value }, + }) - const product = await productService.retrieve(id, { - select: defaultFields, - relations: defaultRelations, - }) + const product = await productService.retrieve(id, { + select: defaultFields, + relations: defaultRelations, + }) - res.status(200).json({ product }) - } catch (err) { - throw err - } + res.status(200).json({ product }) } diff --git a/packages/medusa/src/api/routes/admin/products/update-option.js b/packages/medusa/src/api/routes/admin/products/update-option.js index ecd7e3aa2b..c1c40e53e1 100644 --- a/packages/medusa/src/api/routes/admin/products/update-option.js +++ b/packages/medusa/src/api/routes/admin/products/update-option.js @@ -41,18 +41,14 @@ export default async (req, res) => { throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details) } - try { - const productService = req.scope.resolve("productService") + const productService = req.scope.resolve("productService") - await productService.updateOption(id, option_id, value) + await productService.updateOption(id, option_id, value) - const product = await productService.retrieve(id, { - select: defaultFields, - relations: defaultRelations, - }) + const product = await productService.retrieve(id, { + select: defaultFields, + relations: defaultRelations, + }) - res.json({ product }) - } catch (err) { - throw err - } + res.json({ product }) } diff --git a/packages/medusa/src/api/routes/admin/products/update-product.js b/packages/medusa/src/api/routes/admin/products/update-product.js index 85b8f72559..f2022bf5b7 100644 --- a/packages/medusa/src/api/routes/admin/products/update-product.js +++ b/packages/medusa/src/api/routes/admin/products/update-product.js @@ -188,9 +188,7 @@ export default async (req, res) => { const schema = Validator.object().keys({ title: Validator.string().optional(), - subtitle: Validator.string() - .optional() - .allow(null, ""), + subtitle: Validator.string().optional().allow(null, ""), description: Validator.string().optional(), discountable: Validator.boolean().optional(), status: Validator.string().valid( @@ -206,9 +204,7 @@ export default async (req, res) => { }) .allow(null) .optional(), - collection_id: Validator.string() - .allow(null) - .optional(), + collection_id: Validator.string().allow(null).optional(), tags: Validator.array() .items({ id: Validator.string().optional(), @@ -216,26 +212,15 @@ export default async (req, res) => { }) .optional(), handle: Validator.string().optional(), - weight: Validator.number() - .allow(null) - .optional(), - length: Validator.number() - .allow(null) - .optional(), - height: Validator.number() - .allow(null) - .optional(), - width: Validator.number() - .allow(null) - .optional(), + weight: Validator.number().allow(null).optional(), + length: Validator.number().allow(null).optional(), + height: Validator.number().allow(null).optional(), + width: Validator.number().allow(null).optional(), origin_country: Validator.string().allow(null, ""), hs_code: Validator.string().allow(null, ""), mid_code: Validator.string().allow(null, ""), material: Validator.string().allow(null, ""), - images: Validator.array() - .items(Validator.string()) - .optional() - .optional(), + images: Validator.array().items(Validator.string()).optional().optional(), thumbnail: Validator.string().optional(), variants: Validator.array() .items({ @@ -249,9 +234,7 @@ export default async (req, res) => { .keys({ region_id: Validator.string(), currency_code: Validator.string(), - amount: Validator.number() - .integer() - .required(), + amount: Validator.number().integer().required(), sale_amount: Validator.number().optional(), }) .xor("region_id", "currency_code") @@ -266,21 +249,11 @@ export default async (req, res) => { inventory_quantity: Validator.number().allow(null), allow_backorder: Validator.boolean().allow(null), manage_inventory: Validator.boolean().allow(null), - weight: Validator.number() - .allow(null) - .optional(), - length: Validator.number() - .allow(null) - .optional(), - height: Validator.number() - .allow(null) - .optional(), - width: Validator.number() - .allow(null) - .optional(), - hs_code: Validator.string() - .optional() - .allow(null, ""), + weight: Validator.number().allow(null).optional(), + length: Validator.number().allow(null).optional(), + height: Validator.number().allow(null).optional(), + width: Validator.number().allow(null).optional(), + hs_code: Validator.string().optional().allow(null, ""), origin_country: Validator.string().allow(null, ""), mid_code: Validator.string().allow(null, ""), material: Validator.string().allow(null, ""), @@ -295,21 +268,17 @@ export default async (req, res) => { throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details) } - try { - const productService = req.scope.resolve("productService") - const entityManager = req.scope.resolve("manager") + const productService = req.scope.resolve("productService") + const entityManager = req.scope.resolve("manager") - await entityManager.transaction(async manager => { - await productService.withTransaction(manager).update(id, value) - }) + await entityManager.transaction(async (manager) => { + await productService.withTransaction(manager).update(id, value) + }) - const product = await productService.retrieve(id, { - select: defaultFields, - relations: defaultRelations, - }) + const product = await productService.retrieve(id, { + select: defaultFields, + relations: defaultRelations, + }) - res.json({ product }) - } catch (err) { - throw err - } + res.json({ product }) } diff --git a/packages/medusa/src/api/routes/admin/products/update-variant.js b/packages/medusa/src/api/routes/admin/products/update-variant.js index 1782ab17be..b70a8a455e 100644 --- a/packages/medusa/src/api/routes/admin/products/update-variant.js +++ b/packages/medusa/src/api/routes/admin/products/update-variant.js @@ -1,4 +1,3 @@ -import _ from "lodash" import { MedusaError, Validator } from "medusa-core-utils" import { defaultFields, defaultRelations } from "./" @@ -116,9 +115,7 @@ export default async (req, res) => { .keys({ region_id: Validator.string().empty(null), currency_code: Validator.string(), - amount: Validator.number() - .integer() - .required(), + amount: Validator.number().integer().required(), sale_amount: Validator.number().optional(), }) .xor("region_id", "currency_code") @@ -133,30 +130,14 @@ export default async (req, res) => { inventory_quantity: Validator.number().optional(), allow_backorder: Validator.boolean().optional(), manage_inventory: Validator.boolean().optional(), - weight: Validator.number() - .allow(null) - .optional(), - length: Validator.number() - .allow(null) - .optional(), - height: Validator.number() - .allow(null) - .optional(), - width: Validator.number() - .allow(null) - .optional(), - hs_code: Validator.string() - .optional() - .allow(null, ""), - origin_country: Validator.string() - .optional() - .allow(null, ""), - mid_code: Validator.string() - .optional() - .allow(null, ""), - material: Validator.string() - .optional() - .allow(null, ""), + weight: Validator.number().allow(null).optional(), + length: Validator.number().allow(null).optional(), + height: Validator.number().allow(null).optional(), + width: Validator.number().allow(null).optional(), + hs_code: Validator.string().optional().allow(null, ""), + origin_country: Validator.string().optional().allow(null, ""), + mid_code: Validator.string().optional().allow(null, ""), + material: Validator.string().optional().allow(null, ""), metadata: Validator.object().optional(), }) @@ -165,18 +146,15 @@ export default async (req, res) => { throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details) } - try { - const productService = req.scope.resolve("productService") - const productVariantService = req.scope.resolve("productVariantService") + const productService = req.scope.resolve("productService") + const productVariantService = req.scope.resolve("productVariantService") - await productVariantService.update(variant_id, value) + await productVariantService.update(variant_id, value) - const product = await productService.retrieve(id, { - select: defaultFields, - relations: defaultRelations, - }) - res.json({ product }) - } catch (err) { - throw err - } + const product = await productService.retrieve(id, { + select: defaultFields, + relations: defaultRelations, + }) + + res.json({ product }) } diff --git a/packages/medusa/src/api/routes/admin/regions/add-country.js b/packages/medusa/src/api/routes/admin/regions/add-country.js index ad0e8a15e1..62c9b45740 100644 --- a/packages/medusa/src/api/routes/admin/regions/add-country.js +++ b/packages/medusa/src/api/routes/admin/regions/add-country.js @@ -39,17 +39,13 @@ export default async (req, res) => { throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details) } - try { - const regionService = req.scope.resolve("regionService") - await regionService.addCountry(region_id, value.country_code) + const regionService = req.scope.resolve("regionService") + await regionService.addCountry(region_id, value.country_code) - const region = await regionService.retrieve(region_id, { - select: defaultFields, - relations: defaultRelations, - }) + const region = await regionService.retrieve(region_id, { + select: defaultFields, + relations: defaultRelations, + }) - res.status(200).json({ region }) - } catch (err) { - throw err - } + res.status(200).json({ region }) } diff --git a/packages/medusa/src/api/routes/admin/regions/add-fulfillment-provider.js b/packages/medusa/src/api/routes/admin/regions/add-fulfillment-provider.js index 09f618e9a1..59ce3a4f7c 100644 --- a/packages/medusa/src/api/routes/admin/regions/add-fulfillment-provider.js +++ b/packages/medusa/src/api/routes/admin/regions/add-fulfillment-provider.js @@ -39,16 +39,12 @@ export default async (req, res) => { throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details) } - try { - const regionService = req.scope.resolve("regionService") - await regionService.addFulfillmentProvider(region_id, value.provider_id) + const regionService = req.scope.resolve("regionService") + await regionService.addFulfillmentProvider(region_id, value.provider_id) - const data = await regionService.retrieve(region_id, { - select: defaultFields, - relations: defaultRelations, - }) - res.status(200).json({ region: data }) - } catch (err) { - throw err - } + const data = await regionService.retrieve(region_id, { + select: defaultFields, + relations: defaultRelations, + }) + res.status(200).json({ region: data }) } diff --git a/packages/medusa/src/api/routes/admin/regions/add-payment-provider.js b/packages/medusa/src/api/routes/admin/regions/add-payment-provider.js index 424ec645b2..7c20afc41f 100644 --- a/packages/medusa/src/api/routes/admin/regions/add-payment-provider.js +++ b/packages/medusa/src/api/routes/admin/regions/add-payment-provider.js @@ -39,16 +39,12 @@ export default async (req, res) => { throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details) } - try { - const regionService = req.scope.resolve("regionService") - await regionService.addPaymentProvider(region_id, value.provider_id) + const regionService = req.scope.resolve("regionService") + await regionService.addPaymentProvider(region_id, value.provider_id) - const data = await regionService.retrieve(region_id, { - select: defaultFields, - relations: defaultRelations, - }) - res.status(200).json({ region: data }) - } catch (err) { - throw err - } + const data = await regionService.retrieve(region_id, { + select: defaultFields, + relations: defaultRelations, + }) + res.status(200).json({ region: data }) } diff --git a/packages/medusa/src/api/routes/admin/regions/create-region.js b/packages/medusa/src/api/routes/admin/regions/create-region.js index 4c29350434..0071280301 100644 --- a/packages/medusa/src/api/routes/admin/regions/create-region.js +++ b/packages/medusa/src/api/routes/admin/regions/create-region.js @@ -66,17 +66,13 @@ export default async (req, res) => { throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details) } - try { - const regionService = req.scope.resolve("regionService") - const result = await regionService.create(value) + const regionService = req.scope.resolve("regionService") + const result = await regionService.create(value) - const region = await regionService.retrieve(result.id, { - select: defaultFields, - relations: defaultRelations, - }) + const region = await regionService.retrieve(result.id, { + select: defaultFields, + relations: defaultRelations, + }) - res.status(200).json({ region }) - } catch (err) { - throw err - } + res.status(200).json({ region }) } diff --git a/packages/medusa/src/api/routes/admin/regions/delete-metadata.js b/packages/medusa/src/api/routes/admin/regions/delete-metadata.js index b029c6820b..a8f17f1c01 100644 --- a/packages/medusa/src/api/routes/admin/regions/delete-metadata.js +++ b/packages/medusa/src/api/routes/admin/regions/delete-metadata.js @@ -23,17 +23,13 @@ import { defaultRelations, defaultFields } from "./" export default async (req, res) => { const { id, key } = req.params - try { - const regionService = req.scope.resolve("regionService") - await regionService.deleteMetadata(id, key) + const regionService = req.scope.resolve("regionService") + await regionService.deleteMetadata(id, key) - const region = await regionService.retrieve(id, { - select: defaultFields, - relations: defaultRelations, - }) + const region = await regionService.retrieve(id, { + select: defaultFields, + relations: defaultRelations, + }) - res.status(200).json({ region }) - } catch (err) { - throw err - } + res.status(200).json({ region }) } diff --git a/packages/medusa/src/api/routes/admin/regions/delete-region.js b/packages/medusa/src/api/routes/admin/regions/delete-region.js index d15f01d823..ab8a5246af 100644 --- a/packages/medusa/src/api/routes/admin/regions/delete-region.js +++ b/packages/medusa/src/api/routes/admin/regions/delete-region.js @@ -1,5 +1,3 @@ -import { MedusaError, Validator } from "medusa-core-utils" - /** * @oas [delete] /regions/{id} * operationId: "DeleteRegionsRegion" @@ -27,16 +25,12 @@ import { MedusaError, Validator } from "medusa-core-utils" */ export default async (req, res) => { const { region_id } = req.params - try { - const regionService = req.scope.resolve("regionService") - await regionService.delete(region_id) + const regionService = req.scope.resolve("regionService") + await regionService.delete(region_id) - res.status(200).json({ - id: region_id, - object: "region", - deleted: true, - }) - } catch (err) { - throw err - } + res.status(200).json({ + id: region_id, + object: "region", + deleted: true, + }) } diff --git a/packages/medusa/src/api/routes/admin/regions/get-fulfillment-options.js b/packages/medusa/src/api/routes/admin/regions/get-fulfillment-options.js index ea0aeac4e1..63525abea6 100644 --- a/packages/medusa/src/api/routes/admin/regions/get-fulfillment-options.js +++ b/packages/medusa/src/api/routes/admin/regions/get-fulfillment-options.js @@ -22,25 +22,21 @@ export default async (req, res) => { const { region_id } = req.params - try { - const fulfillmentProviderService = req.scope.resolve( - "fulfillmentProviderService" - ) - const regionService = req.scope.resolve("regionService") - const region = await regionService.retrieve(region_id, { - relations: ["fulfillment_providers"], - }) + const fulfillmentProviderService = req.scope.resolve( + "fulfillmentProviderService" + ) + const regionService = req.scope.resolve("regionService") + const region = await regionService.retrieve(region_id, { + relations: ["fulfillment_providers"], + }) - const fpsIds = region.fulfillment_providers.map(fp => fp.id) || [] + const fpsIds = region.fulfillment_providers.map((fp) => fp.id) || [] - const options = await fulfillmentProviderService.listFulfillmentOptions( - fpsIds - ) + const options = await fulfillmentProviderService.listFulfillmentOptions( + fpsIds + ) - res.status(200).json({ - fulfillment_options: options, - }) - } catch (err) { - throw err - } + res.status(200).json({ + fulfillment_options: options, + }) } diff --git a/packages/medusa/src/api/routes/admin/regions/get-region.js b/packages/medusa/src/api/routes/admin/regions/get-region.js index 14b78ccb79..094c06afa8 100644 --- a/packages/medusa/src/api/routes/admin/regions/get-region.js +++ b/packages/medusa/src/api/routes/admin/regions/get-region.js @@ -1,4 +1,3 @@ -import { MedusaError, Validator } from "medusa-core-utils" import { defaultRelations, defaultFields } from "./" /** @@ -22,15 +21,11 @@ import { defaultRelations, defaultFields } from "./" */ export default async (req, res) => { const { region_id } = req.params - try { - const regionService = req.scope.resolve("regionService") - const data = await regionService.retrieve(region_id, { - select: defaultFields, - relations: defaultRelations, - }) + const regionService = req.scope.resolve("regionService") + const data = await regionService.retrieve(region_id, { + select: defaultFields, + relations: defaultRelations, + }) - res.status(200).json({ region: data }) - } catch (err) { - throw err - } + res.status(200).json({ region: data }) } diff --git a/packages/medusa/src/api/routes/admin/regions/index.js b/packages/medusa/src/api/routes/admin/regions/index.js index 0aab12bacf..c38ba24357 100644 --- a/packages/medusa/src/api/routes/admin/regions/index.js +++ b/packages/medusa/src/api/routes/admin/regions/index.js @@ -3,7 +3,7 @@ import middlewares from "../../../middlewares" const route = Router() -export default app => { +export default (app) => { app.use("/regions", route) route.get("/", middlewares.wrap(require("./list-regions").default)) diff --git a/packages/medusa/src/api/routes/admin/regions/list-regions.js b/packages/medusa/src/api/routes/admin/regions/list-regions.js index c1369f6aca..b4aae7a181 100644 --- a/packages/medusa/src/api/routes/admin/regions/list-regions.js +++ b/packages/medusa/src/api/routes/admin/regions/list-regions.js @@ -20,25 +20,21 @@ import { defaultFields, defaultRelations } from "./" * $ref: "#/components/schemas/region" */ export default async (req, res) => { - try { - const regionService = req.scope.resolve("regionService") + const regionService = req.scope.resolve("regionService") - const limit = parseInt(req.query.limit) || 50 - const offset = parseInt(req.query.offset) || 0 + const limit = parseInt(req.query.limit) || 50 + const offset = parseInt(req.query.offset) || 0 - const selector = {} + const selector = {} - const listConfig = { - select: defaultFields, - relations: defaultRelations, - skip: offset, - take: limit, - } - - let regions = await regionService.list(selector, listConfig) - - res.json({ regions, count: regions.length, offset, limit }) - } catch (err) { - throw err + const listConfig = { + select: defaultFields, + relations: defaultRelations, + skip: offset, + take: limit, } + + const regions = await regionService.list(selector, listConfig) + + res.json({ regions, count: regions.length, offset, limit }) } diff --git a/packages/medusa/src/api/routes/admin/regions/remove-country.js b/packages/medusa/src/api/routes/admin/regions/remove-country.js index 9c132666a5..944f41a118 100644 --- a/packages/medusa/src/api/routes/admin/regions/remove-country.js +++ b/packages/medusa/src/api/routes/admin/regions/remove-country.js @@ -1,4 +1,3 @@ -import { MedusaError, Validator } from "medusa-core-utils" import { defaultRelations, defaultFields } from "./" /** @@ -23,17 +22,13 @@ import { defaultRelations, defaultFields } from "./" */ export default async (req, res) => { const { region_id, country_code } = req.params - try { - const regionService = req.scope.resolve("regionService") - await regionService.removeCountry(region_id, country_code) + const regionService = req.scope.resolve("regionService") + await regionService.removeCountry(region_id, country_code) - const region = await regionService.retrieve(region_id, { - select: defaultFields, - relations: defaultRelations, - }) + const region = await regionService.retrieve(region_id, { + select: defaultFields, + relations: defaultRelations, + }) - res.json({ region }) - } catch (err) { - throw err - } + res.json({ region }) } diff --git a/packages/medusa/src/api/routes/admin/regions/remove-fulfillment-provider.js b/packages/medusa/src/api/routes/admin/regions/remove-fulfillment-provider.js index 9e28f89a09..596617cad6 100644 --- a/packages/medusa/src/api/routes/admin/regions/remove-fulfillment-provider.js +++ b/packages/medusa/src/api/routes/admin/regions/remove-fulfillment-provider.js @@ -1,4 +1,3 @@ -import { MedusaError, Validator } from "medusa-core-utils" import { defaultRelations, defaultFields } from "./" /** @@ -23,18 +22,14 @@ import { defaultRelations, defaultFields } from "./" */ export default async (req, res) => { const { region_id, provider_id } = req.params - try { - const regionService = req.scope.resolve("regionService") + const regionService = req.scope.resolve("regionService") - await regionService.removeFulfillmentProvider(region_id, provider_id) + await regionService.removeFulfillmentProvider(region_id, provider_id) - const region = await regionService.retrieve(region_id, { - select: defaultFields, - relations: defaultRelations, - }) + const region = await regionService.retrieve(region_id, { + select: defaultFields, + relations: defaultRelations, + }) - res.json({ region }) - } catch (err) { - throw err - } + res.json({ region }) } diff --git a/packages/medusa/src/api/routes/admin/regions/remove-payment-provider.js b/packages/medusa/src/api/routes/admin/regions/remove-payment-provider.js index 4c029e8965..09da196323 100644 --- a/packages/medusa/src/api/routes/admin/regions/remove-payment-provider.js +++ b/packages/medusa/src/api/routes/admin/regions/remove-payment-provider.js @@ -1,4 +1,3 @@ -import { MedusaError, Validator } from "medusa-core-utils" import { defaultRelations, defaultFields } from "./" /** @@ -23,17 +22,13 @@ import { defaultRelations, defaultFields } from "./" */ export default async (req, res) => { const { region_id, provider_id } = req.params - try { - const regionService = req.scope.resolve("regionService") - await regionService.removePaymentProvider(region_id, provider_id) + const regionService = req.scope.resolve("regionService") + await regionService.removePaymentProvider(region_id, provider_id) - const region = await regionService.retrieve(region_id, { - select: defaultFields, - relations: defaultRelations, - }) + const region = await regionService.retrieve(region_id, { + select: defaultFields, + relations: defaultRelations, + }) - res.json({ region }) - } catch (err) { - throw err - } + res.json({ region }) } diff --git a/packages/medusa/src/api/routes/admin/regions/set-metadata.js b/packages/medusa/src/api/routes/admin/regions/set-metadata.js index 36445b2dd5..42ad25be45 100644 --- a/packages/medusa/src/api/routes/admin/regions/set-metadata.js +++ b/packages/medusa/src/api/routes/admin/regions/set-metadata.js @@ -1,4 +1,5 @@ import { MedusaError, Validator } from "medusa-core-utils" +import { defaultFields, defaultRelations } from "." export default async (req, res) => { const { id } = req.params @@ -13,17 +14,13 @@ export default async (req, res) => { throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details) } - try { - const regionService = req.scope.resolve("regionService") - await regionService.setMetadata(id, value.key, value.value) + const regionService = req.scope.resolve("regionService") + await regionService.setMetadata(id, value.key, value.value) - const region = await regionService.retrieve(region_id, { - select: defaultFields, - relations: defaultRelations, - }) + const region = await regionService.retrieve(id, { + select: defaultFields, + relations: defaultRelations, + }) - res.status(200).json({ region }) - } catch (err) { - throw err - } + res.status(200).json({ region }) } diff --git a/packages/medusa/src/api/routes/admin/regions/update-region.js b/packages/medusa/src/api/routes/admin/regions/update-region.js index f0d39dfd0d..ff6e4c317c 100644 --- a/packages/medusa/src/api/routes/admin/regions/update-region.js +++ b/packages/medusa/src/api/routes/admin/regions/update-region.js @@ -70,16 +70,12 @@ export default async (req, res) => { throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details) } - try { - const regionService = req.scope.resolve("regionService") - await regionService.update(region_id, value) - const region = await regionService.retrieve(region_id, { - select: defaultFields, - relations: defaultRelations, - }) + const regionService = req.scope.resolve("regionService") + await regionService.update(region_id, value) + const region = await regionService.retrieve(region_id, { + select: defaultFields, + relations: defaultRelations, + }) - res.status(200).json({ region }) - } catch (err) { - throw err - } + res.status(200).json({ region }) } diff --git a/packages/medusa/src/api/routes/admin/shipping-options/create-shipping-option.js b/packages/medusa/src/api/routes/admin/shipping-options/create-shipping-option.js index 5422db7ce0..8e44305392 100644 --- a/packages/medusa/src/api/routes/admin/shipping-options/create-shipping-option.js +++ b/packages/medusa/src/api/routes/admin/shipping-options/create-shipping-option.js @@ -72,16 +72,12 @@ export default async (req, res) => { profile_id: Validator.string(), data: Validator.object().required(), price_type: Validator.string().required(), - amount: Validator.number() - .integer() - .optional(), + amount: Validator.number().integer().optional(), requirements: Validator.array() .items( Validator.object({ type: Validator.string().required(), - amount: Validator.number() - .integer() - .required(), + amount: Validator.number().integer().required(), }) ) .optional(), @@ -94,24 +90,20 @@ export default async (req, res) => { throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details) } - try { - const optionService = req.scope.resolve("shippingOptionService") - const shippingProfileService = req.scope.resolve("shippingProfileService") + const optionService = req.scope.resolve("shippingOptionService") + const shippingProfileService = req.scope.resolve("shippingProfileService") - // Add to default shipping profile - if (!value.profile_id) { - const { id } = await shippingProfileService.retrieveDefault() - value.profile_id = id - } - - const result = await optionService.create(value) - const data = await optionService.retrieve(result.id, { - select: defaultFields, - relations: defaultRelations, - }) - - res.status(200).json({ shipping_option: data }) - } catch (err) { - throw err + // Add to default shipping profile + if (!value.profile_id) { + const { id } = await shippingProfileService.retrieveDefault() + value.profile_id = id } + + const result = await optionService.create(value) + const data = await optionService.retrieve(result.id, { + select: defaultFields, + relations: defaultRelations, + }) + + res.status(200).json({ shipping_option: data }) } diff --git a/packages/medusa/src/api/routes/admin/shipping-options/delete-shipping-option.js b/packages/medusa/src/api/routes/admin/shipping-options/delete-shipping-option.js index 9e3ae7d89b..f4b32caf43 100644 --- a/packages/medusa/src/api/routes/admin/shipping-options/delete-shipping-option.js +++ b/packages/medusa/src/api/routes/admin/shipping-options/delete-shipping-option.js @@ -25,17 +25,13 @@ */ export default async (req, res) => { const { option_id } = req.params - try { - const optionService = req.scope.resolve("shippingOptionService") + const optionService = req.scope.resolve("shippingOptionService") - await optionService.delete(option_id) + await optionService.delete(option_id) - res.json({ - id: option_id, - object: "shipping-option", - deleted: true, - }) - } catch (err) { - throw err - } + res.json({ + id: option_id, + object: "shipping-option", + deleted: true, + }) } diff --git a/packages/medusa/src/api/routes/admin/shipping-options/get-shipping-option.js b/packages/medusa/src/api/routes/admin/shipping-options/get-shipping-option.js index 44bdddd993..20508c9fe2 100644 --- a/packages/medusa/src/api/routes/admin/shipping-options/get-shipping-option.js +++ b/packages/medusa/src/api/routes/admin/shipping-options/get-shipping-option.js @@ -19,12 +19,8 @@ */ export default async (req, res) => { const { option_id } = req.params - try { - const optionService = req.scope.resolve("shippingOptionService") - const data = await optionService.retrieve(option_id) + const optionService = req.scope.resolve("shippingOptionService") + const data = await optionService.retrieve(option_id) - res.status(200).json({ shipping_option: data }) - } catch (err) { - throw err - } + res.status(200).json({ shipping_option: data }) } diff --git a/packages/medusa/src/api/routes/admin/shipping-options/index.js b/packages/medusa/src/api/routes/admin/shipping-options/index.js index 84dbfcde6d..14f79cb287 100644 --- a/packages/medusa/src/api/routes/admin/shipping-options/index.js +++ b/packages/medusa/src/api/routes/admin/shipping-options/index.js @@ -3,7 +3,7 @@ import middlewares from "../../../middlewares" const route = Router() -export default app => { +export default (app) => { app.use("/shipping-options", route) route.get("/", middlewares.wrap(require("./list-shipping-options").default)) diff --git a/packages/medusa/src/api/routes/admin/shipping-options/list-shipping-options.js b/packages/medusa/src/api/routes/admin/shipping-options/list-shipping-options.js index 5caf463c88..406b447c52 100644 --- a/packages/medusa/src/api/routes/admin/shipping-options/list-shipping-options.js +++ b/packages/medusa/src/api/routes/admin/shipping-options/list-shipping-options.js @@ -21,17 +21,13 @@ import { defaultFields, defaultRelations } from "./" * $ref: "#/components/schemas/shipping_option" */ export default async (req, res) => { - try { - const query = _.pick(req.query, ["region_id", "is_return", "admin_only"]) + const query = _.pick(req.query, ["region_id", "is_return", "admin_only"]) - const optionService = req.scope.resolve("shippingOptionService") - const data = await optionService.list(query, { - select: defaultFields, - relations: defaultRelations, - }) + const optionService = req.scope.resolve("shippingOptionService") + const data = await optionService.list(query, { + select: defaultFields, + relations: defaultRelations, + }) - res.status(200).json({ shipping_options: data }) - } catch (err) { - throw err - } + res.status(200).json({ shipping_options: data }) } diff --git a/packages/medusa/src/api/routes/admin/shipping-options/update-shipping-option.js b/packages/medusa/src/api/routes/admin/shipping-options/update-shipping-option.js index 0bca080323..185444182d 100644 --- a/packages/medusa/src/api/routes/admin/shipping-options/update-shipping-option.js +++ b/packages/medusa/src/api/routes/admin/shipping-options/update-shipping-option.js @@ -1,4 +1,3 @@ -import _ from "lodash" import { MedusaError, Validator } from "medusa-core-utils" import { defaultFields, defaultRelations } from "./" @@ -50,17 +49,13 @@ export default async (req, res) => { const { option_id } = req.params const schema = Validator.object().keys({ name: Validator.string().optional(), - amount: Validator.number() - .integer() - .optional(), + amount: Validator.number().integer().optional(), requirements: Validator.array() .items( Validator.object({ id: Validator.string().optional(), type: Validator.string().required(), - amount: Validator.number() - .integer() - .required(), + amount: Validator.number().integer().required(), }) ) .optional(), @@ -73,18 +68,14 @@ export default async (req, res) => { throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details) } - try { - const optionService = req.scope.resolve("shippingOptionService") + const optionService = req.scope.resolve("shippingOptionService") - await optionService.update(option_id, value) + await optionService.update(option_id, value) - const data = await optionService.retrieve(option_id, { - select: defaultFields, - relations: defaultRelations, - }) + const data = await optionService.retrieve(option_id, { + select: defaultFields, + relations: defaultRelations, + }) - res.status(200).json({ shipping_option: data }) - } catch (err) { - throw err - } + res.status(200).json({ shipping_option: data }) } diff --git a/packages/medusa/src/api/routes/admin/swaps/get-swap.js b/packages/medusa/src/api/routes/admin/swaps/get-swap.js index 1f1b3cefe2..400867a0d9 100644 --- a/packages/medusa/src/api/routes/admin/swaps/get-swap.js +++ b/packages/medusa/src/api/routes/admin/swaps/get-swap.js @@ -22,16 +22,12 @@ import { defaultFields, defaultRelations } from "./" export default async (req, res) => { const { id } = req.params - try { - const swapService = req.scope.resolve("swapService") + const swapService = req.scope.resolve("swapService") - const swap = await swapService.retrieve(id, { - select: defaultFields, - relations: defaultRelations, - }) + const swap = await swapService.retrieve(id, { + select: defaultFields, + relations: defaultRelations, + }) - res.json({ swap }) - } catch (error) { - throw error - } + res.json({ swap }) } diff --git a/packages/medusa/src/api/routes/admin/swaps/index.js b/packages/medusa/src/api/routes/admin/swaps/index.js index 00bc25a0db..9800546bbd 100644 --- a/packages/medusa/src/api/routes/admin/swaps/index.js +++ b/packages/medusa/src/api/routes/admin/swaps/index.js @@ -3,7 +3,7 @@ import middlewares from "../../../middlewares" const route = Router() -export default app => { +export default (app) => { app.use("/swaps", route) /** diff --git a/packages/medusa/src/api/routes/admin/swaps/list-swaps.js b/packages/medusa/src/api/routes/admin/swaps/list-swaps.js index f9e9e47966..be894e47fe 100644 --- a/packages/medusa/src/api/routes/admin/swaps/list-swaps.js +++ b/packages/medusa/src/api/routes/admin/swaps/list-swaps.js @@ -1,5 +1,3 @@ -import _ from "lodash" - /** * @oas [get] /swaps * operationId: "GetSwaps" @@ -20,24 +18,20 @@ import _ from "lodash" * $ref: "#/components/schemas/swap" */ export default async (req, res) => { - try { - const swapService = req.scope.resolve("swapService") + const swapService = req.scope.resolve("swapService") - const limit = parseInt(req.query.limit) || 50 - const offset = parseInt(req.query.offset) || 0 + const limit = parseInt(req.query.limit) || 50 + const offset = parseInt(req.query.offset) || 0 - const selector = {} + const selector = {} - const listConfig = { - skip: offset, - take: limit, - order: { created_at: "DESC" }, - } - - const swaps = await swapService.list(selector, { ...listConfig }) - - res.json({ swaps, count: swaps.length, offset, limit }) - } catch (error) { - throw error + const listConfig = { + skip: offset, + take: limit, + order: { created_at: "DESC" }, } + + const swaps = await swapService.list(selector, { ...listConfig }) + + res.json({ swaps, count: swaps.length, offset, limit }) } diff --git a/packages/medusa/src/api/routes/store/auth/exists.js b/packages/medusa/src/api/routes/store/auth/exists.js index 6bf8c5f921..4d6e00622f 100644 --- a/packages/medusa/src/api/routes/store/auth/exists.js +++ b/packages/medusa/src/api/routes/store/auth/exists.js @@ -1,5 +1,3 @@ -import { Validator, MedusaError } from "medusa-core-utils" - /** * @oas [get] /auth/{email} * operationId: "GetAuthEmail" diff --git a/packages/medusa/src/api/routes/store/auth/get-session.js b/packages/medusa/src/api/routes/store/auth/get-session.js index da125cf822..e5cf33f09d 100644 --- a/packages/medusa/src/api/routes/store/auth/get-session.js +++ b/packages/medusa/src/api/routes/store/auth/get-session.js @@ -16,17 +16,13 @@ * $ref: "#/components/schemas/customer" */ export default async (req, res) => { - try { - if (req.user && req.user.customer_id) { - const customerService = req.scope.resolve("customerService") - const customer = await customerService.retrieve(req.user.customer_id, { - relations: ["shipping_addresses", "orders", "orders.items"], - }) - res.json({ customer }) - } else { - res.sendStatus(401) - } - } catch (err) { - throw err + if (req.user && req.user.customer_id) { + const customerService = req.scope.resolve("customerService") + const customer = await customerService.retrieve(req.user.customer_id, { + relations: ["shipping_addresses", "orders", "orders.items"], + }) + res.json({ customer }) + } else { + res.sendStatus(401) } } diff --git a/packages/medusa/src/api/routes/store/variants/__tests__/get-variant.js b/packages/medusa/src/api/routes/store/variants/__tests__/get-variant.js index eb16ea70d1..5090457c54 100644 --- a/packages/medusa/src/api/routes/store/variants/__tests__/get-variant.js +++ b/packages/medusa/src/api/routes/store/variants/__tests__/get-variant.js @@ -1,4 +1,3 @@ -import { IdMap } from "../../../../../../../medusa-test-utils/dist" import { request } from "../../../../../helpers/test-request" import { ProductVariantServiceMock } from "../../../../../services/__mocks__/product-variant" diff --git a/scripts/assert-changed-files-actions.sh b/scripts/assert-changed-files-actions.sh new file mode 100755 index 0000000000..c7a1851821 --- /dev/null +++ b/scripts/assert-changed-files-actions.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +IS_CI="${CI:-false}" +GREP_PATTERN=$1 + +if [ "$IS_CI" = true ]; then + git config --local url."https://github.com/".insteadOf git@github.com: + git config --local user.name "Medusajs Bot" + git config --local user.email "core@medusa-commerce.com" + + git fetch origin + git merge --no-edit origin/master + + if [ $? -ne 0 ]; then + echo "Branch has conflicts with master, rolling back test." + git merge --abort + fi + + git config --local --unset user.name + git config --local --unset user.email + git config --local --unset url."https://github.com/".insteadOf +fi + +# Make sure that we are diffing towards the right branch, in github actions this is different +# depending on whether or not we are creating a pull request or not. +[ ! -z ${GITHUB_BASE_REF} ] && HAS_BASE=true || HAS_BASE=false +[ HAS_BASE = true ] && COMPARE="${GITHUB_BASE_REF#refs/heads/}" || COMPARE="develop" +FILES_COUNT="$(git diff-tree --no-commit-id --name-only -r origin/"$COMPARE" | grep -E "$GREP_PATTERN" -c)" + +if [ "$IS_CI" = true ]; then + # reset to previous state + git reset --hard $GITHUB_SHA +fi + +if [ "$FILES_COUNT" -eq 0 ]; then + echo "0 files matching '$GREP_PATTERN'; exiting and marking successful." + exit 0 +else + echo "$FILES_COUNT file(s) matching '$GREP_PATTERN'; continuing." +fi