From 90ea88882978859595f4980efb71bfe54a8b4d33 Mon Sep 17 00:00:00 2001 From: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com> Date: Thu, 18 Feb 2021 13:46:34 +0100 Subject: [PATCH 1/4] hotfix(medusa-fulfillment-webshipper): Cancel fulfillment idempotently (#175) --- .../src/services/webshipper-fulfillment.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/medusa-fulfillment-webshipper/src/services/webshipper-fulfillment.js b/packages/medusa-fulfillment-webshipper/src/services/webshipper-fulfillment.js index 89142f3bb6..43d96fd573 100644 --- a/packages/medusa-fulfillment-webshipper/src/services/webshipper-fulfillment.js +++ b/packages/medusa-fulfillment-webshipper/src/services/webshipper-fulfillment.js @@ -523,6 +523,11 @@ class WebshipperFulfillmentService extends FulfillmentService { .retrieve(data.id) .catch(() => undefined) + // if order does not exist, we resolve gracefully + if (!order) { + return Promise.resolve() + } + if (order) { if (order.data.attributes.status !== "pending") { if (order.data.attributes.status === "cancelled") { From 587a464e83576833ff616bde7bb26b1bb48472fe Mon Sep 17 00:00:00 2001 From: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com> Date: Tue, 23 Feb 2021 07:49:01 +0100 Subject: [PATCH 2/4] fix(medusa-plugin-contentful): Allow custom fields in plugin options (#180) --- .../src/loaders/check-types.js | 24 ++- .../src/services/contentful.js | 174 +++++++++++++++--- 2 files changed, 164 insertions(+), 34 deletions(-) diff --git a/packages/medusa-plugin-contentful/src/loaders/check-types.js b/packages/medusa-plugin-contentful/src/loaders/check-types.js index 88f6ea78ef..9c224621dd 100644 --- a/packages/medusa-plugin-contentful/src/loaders/check-types.js +++ b/packages/medusa-plugin-contentful/src/loaders/check-types.js @@ -19,8 +19,16 @@ const checkContentTypes = async (container) => { if (product && product.fields) { const productFields = product.fields + const customProductFields = Object.keys( + contentfulService.options_.custom_product_fields || {} + ) const keys = Object.values(productFields).map((f) => f.id) - if (!requiredProductFields.every((f) => keys.includes(f))) { + + const missingKeys = requiredProductFields.filter( + (rpf) => !keys.includes(rpf) && !customProductFields.includes(rpf) + ) + + if (missingKeys.length) { throw Error( `Contentful: Content type ${`product`} is missing some required key(s). Required: ${requiredProductFields.join( ", " @@ -32,8 +40,16 @@ const checkContentTypes = async (container) => { if (variant && variant.fields) { const variantFields = variant.fields + const customVariantFields = Object.keys( + contentfulService.options_.custom_variant_fields || {} + ) const keys = Object.values(variantFields).map((f) => f.id) - if (!requiredVariantFields.every((f) => keys.includes(f))) { + + const missingKeys = requiredVariantFields.filter( + (rpf) => !keys.includes(rpf) && !customVariantFields.includes(rpf) + ) + + if (missingKeys.length) { throw Error( `Contentful: Content type ${`productVariant`} is missing some required key(s). Required: ${requiredVariantFields.join( ", " @@ -47,13 +63,13 @@ const requiredProductFields = [ "title", "variants", "options", - "objectId", + "medusaId", "type", "collection", "tags", "handle", ] -const requiredVariantFields = ["title", "sku", "prices", "options", "objectId"] +const requiredVariantFields = ["title", "sku", "prices", "options", "medusaId"] export default checkContentTypes diff --git a/packages/medusa-plugin-contentful/src/services/contentful.js b/packages/medusa-plugin-contentful/src/services/contentful.js index 33f45acd32..b0ec7ce842 100644 --- a/packages/medusa-plugin-contentful/src/services/contentful.js +++ b/packages/medusa-plugin-contentful/src/services/contentful.js @@ -111,10 +111,27 @@ class ContentfulService extends BaseService { return assets } + getCustomField(field, type) { + const customOptions = this.options_[`custom_${type}_fields`] + + if (customOptions) { + return customOptions[field] || field + } else { + return field + } + } + async createProductInContentful(product) { try { const p = await this.productService_.retrieve(product.id, { - relations: ["variants", "options", "tags", "type", "collection"], + relations: [ + "variants", + "options", + "tags", + "type", + "collection", + "images", + ], }) const environment = await this.getContentfulEnvironment_() @@ -122,46 +139,92 @@ class ContentfulService extends BaseService { const variantLinks = this.getVariantLinks_(variantEntries) const fields = { - title: { + [this.getCustomField("title", "product")]: { "en-US": p.title, }, - variants: { + [this.getCustomField("variants", "product")]: { "en-US": variantLinks, }, - options: { + [this.getCustomField("options", "product")]: { "en-US": p.options, }, - objectId: { + [this.getCustomField("medusaId", "product")]: { "en-US": p.id, }, } + if (p.images.length > 0) { + const imageLinks = await this.createImageAssets(product) + + const thumbnailAsset = await environment.createAsset({ + fields: { + title: { + "en-US": `${p.title}`, + }, + description: { + "en-US": "", + }, + file: { + "en-US": { + contentType: "image/xyz", + fileName: p.thumbnail, + upload: p.thumbnail, + }, + }, + }, + }) + + await thumbnailAsset.processForAllLocales() + + const thumbnailLink = { + sys: { + type: "Link", + linkType: "Asset", + id: thumbnailAsset.sys.id, + }, + } + + fields.thumbnail = { + "en-US": thumbnailLink, + } + + if (imageLinks) { + fields.images = { + "en-US": imageLinks, + } + } + } + if (p.type) { const type = { "en-US": p.type.value, } - fields.type = type + + fields[this.getCustomField("type", "product")] = type } if (p.collection) { const collection = { "en-US": p.collection.title, } - fields.collection = collection + + fields[this.getCustomField("collection", "product")] = collection } if (p.tags) { const tags = { "en-US": p.tags, } - fields.tags = tags + + fields[this.getCustomField("tags", "product")] = tags } if (p.handle) { const handle = { "en-US": p.handle, } - fields.handle = handle + + fields[this.getCustomField("handle", "product")] = handle } const result = await environment.createEntryWithId("product", p.id, { @@ -189,19 +252,19 @@ class ContentfulService extends BaseService { v.id, { fields: { - title: { + [this.getCustomField("title", "variant")]: { "en-US": v.title, }, - sku: { + [this.getCustomField("sku", "variant")]: { "en-US": v.sku, }, - prices: { + [this.getCustomField("prices", "variant")]: { "en-US": v.prices, }, - options: { + [this.getCustomField("options", "variant")]: { "en-US": v.options, }, - objectId: { + [this.getCustomField("medusaId", "variant")]: { "en-US": v.id, }, }, @@ -240,7 +303,14 @@ class ContentfulService extends BaseService { } const p = await this.productService_.retrieve(product.id, { - relations: ["options", "variants", "type", "collection", "tags"], + relations: [ + "options", + "variants", + "type", + "collection", + "tags", + "images", + ], }) const variantEntries = await this.getVariantEntries_(p.variants) @@ -248,46 +318,86 @@ class ContentfulService extends BaseService { const productEntryFields = { ...productEntry.fields, - title: { + [this.getCustomField("title", "product")]: { "en-US": p.title, }, - options: { + [this.getCustomField("options", "product")]: { "en-US": p.options, }, - variants: { + [this.getCustomField("variants", "product")]: { "en-US": variantLinks, }, - objectId: { + [this.getCustomField("medusaId", "product")]: { "en-US": p.id, }, } + if (p.thumbnail) { + const thumbnailAsset = await environment.createAsset({ + fields: { + title: { + "en-US": `${p.title}`, + }, + description: { + "en-US": "", + }, + file: { + "en-US": { + contentType: "image/xyz", + fileName: p.thumbnail, + upload: p.thumbnail, + }, + }, + }, + }) + + await thumbnailAsset.processForAllLocales() + + const thumbnailLink = { + sys: { + type: "Link", + linkType: "Asset", + id: thumbnailAsset.sys.id, + }, + } + + productEntryFields.thumbnail = { + "en-US": thumbnailLink, + } + } + if (p.type) { const type = { "en-US": p.type.value, } - productEntryFields.type = type + + productEntryFields[this.getCustomField("type", "product")] = type } if (p.collection) { const collection = { "en-US": p.collection.title, } - productEntryFields.collection = collection + + productEntryFields[ + this.getCustomField("collection", "product") + ] = collection } if (p.tags) { const tags = { "en-US": p.tags, } - productEntryFields.tags = tags + + productEntryFields[this.getCustomField("tags", "product")] = tags } if (p.handle) { const handle = { "en-US": p.handle, } - productEntryFields.handle = handle + + productEntryFields[this.getCustomField("handle", "product")] = handle } productEntry.fields = productEntryFields @@ -333,19 +443,19 @@ class ContentfulService extends BaseService { const variantEntryFields = { ...variantEntry.fields, - title: { + [this.getCustomField("title", "variant")]: { "en-US": v.title, }, - sku: { + [this.getCustomField("sku", "variant")]: { "en-US": v.sku, }, - options: { + [this.getCustomField("options", "variant")]: { "en-US": v.options, }, - prices: { + [this.getCustomField("prices", "variant")]: { "en-US": v.prices, }, - objectId: { + [this.getCustomField("medusaId", "variant")]: { "en-US": v.id, }, } @@ -377,7 +487,8 @@ class ContentfulService extends BaseService { } let update = { - title: productEntry.fields.title["en-US"], + title: + productEntry.fields[this.getCustomField("title", "product")]["en-US"], } // Get the thumbnail, if present @@ -421,7 +532,10 @@ class ContentfulService extends BaseService { const updatedVariant = await this.productVariantService_.update( variantId, { - title: variantEntry.fields.title["en-US"], + title: + variantEntry.fields[this.getCustomField("title", "variant")][ + "en-US" + ], } ) From 22be418ec132944afe469106ba4b3b92f634d240 Mon Sep 17 00:00:00 2001 From: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com> Date: Tue, 23 Feb 2021 13:40:49 +0100 Subject: [PATCH 3/4] fix(medusa): Add querying func. on customer retrievals (#181) --- .../api/__tests__/admin/customer.js | 135 ++++++++++++++++++ .../api/helpers/customer-seeder.js | 27 ++++ .../routes/admin/customers/list-customers.js | 22 ++- packages/medusa/src/services/customer.js | 45 ++++++ 4 files changed, 225 insertions(+), 4 deletions(-) create mode 100644 integration-tests/api/__tests__/admin/customer.js create mode 100644 integration-tests/api/helpers/customer-seeder.js diff --git a/integration-tests/api/__tests__/admin/customer.js b/integration-tests/api/__tests__/admin/customer.js new file mode 100644 index 0000000000..14c4222977 --- /dev/null +++ b/integration-tests/api/__tests__/admin/customer.js @@ -0,0 +1,135 @@ +const { dropDatabase } = require("pg-god"); +const path = require("path"); + +const setupServer = require("../../../helpers/setup-server"); +const { useApi } = require("../../../helpers/use-api"); +const { initDb } = require("../../../helpers/use-db"); + +const customerSeeder = require("../../helpers/customer-seeder"); +const adminSeeder = require("../../helpers/admin-seeder"); + +jest.setTimeout(30000); + +describe("/admin/customers", () => { + let medusaProcess; + let dbConnection; + + beforeAll(async () => { + const cwd = path.resolve(path.join(__dirname, "..", "..")); + dbConnection = await initDb({ cwd }); + medusaProcess = await setupServer({ cwd }); + }); + + afterAll(async () => { + await dbConnection.close(); + await dropDatabase({ databaseName: "medusa-integration" }); + + medusaProcess.kill(); + }); + + describe("GET /admin/customers", () => { + beforeEach(async () => { + try { + await adminSeeder(dbConnection); + await customerSeeder(dbConnection); + } catch (err) { + console.log(err); + throw err; + } + }); + + afterEach(async () => { + const manager = dbConnection.manager; + await manager.query(`DELETE FROM "address"`); + await manager.query(`DELETE FROM "customer"`); + await manager.query(`DELETE FROM "user"`); + }); + + it("lists customers and query count", async () => { + const api = useApi(); + + const response = await api + .get("/admin/customers", { + headers: { + Authorization: "Bearer test_token", + }, + }) + .catch((err) => { + console.log(err); + }); + + expect(response.status).toEqual(200); + expect(response.data.count).toEqual(3); + expect(response.data.customers).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + id: "test-customer-1", + }), + expect.objectContaining({ + id: "test-customer-2", + }), + expect.objectContaining({ + id: "test-customer-3", + }), + ]) + ); + }); + + it("lists customers with specific query", async () => { + const api = useApi(); + + const response = await api + .get("/admin/customers?q=test2@email.com", { + headers: { + Authorization: "Bearer test_token", + }, + }) + .catch((err) => { + console.log(err); + }); + + expect(response.status).toEqual(200); + expect(response.data.count).toEqual(1); + expect(response.data.customers).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + id: "test-customer-2", + email: "test2@email.com", + }), + ]) + ); + }); + + it("lists customers with expand query", async () => { + const api = useApi(); + + const response = await api + .get("/admin/customers?q=test1@email.com&expand=shipping_addresses", { + headers: { + Authorization: "Bearer test_token", + }, + }) + .catch((err) => { + console.log(err); + }); + + expect(response.status).toEqual(200); + expect(response.data.count).toEqual(1); + console.log(response.data.customers); + expect(response.data.customers).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + id: "test-customer-1", + shipping_addresses: expect.arrayContaining([ + expect.objectContaining({ + id: "test-address", + first_name: "Lebron", + last_name: "James", + }), + ]), + }), + ]) + ); + }); + }); +}); diff --git a/integration-tests/api/helpers/customer-seeder.js b/integration-tests/api/helpers/customer-seeder.js new file mode 100644 index 0000000000..78b3e22ec5 --- /dev/null +++ b/integration-tests/api/helpers/customer-seeder.js @@ -0,0 +1,27 @@ +const { Customer, Address } = require("@medusajs/medusa"); + +module.exports = async (connection, data = {}) => { + const manager = connection.manager; + + await manager.insert(Customer, { + id: "test-customer-1", + email: "test1@email.com", + }); + + await manager.insert(Customer, { + id: "test-customer-2", + email: "test2@email.com", + }); + + await manager.insert(Customer, { + id: "test-customer-3", + email: "test3@email.com", + }); + + await manager.insert(Address, { + id: "test-address", + first_name: "Lebron", + last_name: "James", + customer_id: "test-customer-1", + }); +}; diff --git a/packages/medusa/src/api/routes/admin/customers/list-customers.js b/packages/medusa/src/api/routes/admin/customers/list-customers.js index 6683f80e06..e0bcb9b9c8 100644 --- a/packages/medusa/src/api/routes/admin/customers/list-customers.js +++ b/packages/medusa/src/api/routes/admin/customers/list-customers.js @@ -2,18 +2,32 @@ export default async (req, res) => { try { const customerService = req.scope.resolve("customerService") - const limit = parseInt(req.query.limit) || 10 + const limit = parseInt(req.query.limit) || 50 const offset = parseInt(req.query.offset) || 0 + const selector = {} + + if ("q" in req.query) { + selector.q = req.query.q + } + + let expandFields = [] + if ("expand" in req.query) { + expandFields = req.query.expand.split(",") + } + const listConfig = { - relations: [], + relations: expandFields.length ? expandFields : [], skip: offset, take: limit, } - const customers = await customerService.list({}, listConfig) + const [customers, count] = await customerService.listAndCount( + selector, + listConfig + ) - res.json({ customers, count: customers.length, offset, limit }) + res.json({ customers, count, offset, limit }) } catch (error) { throw error } diff --git a/packages/medusa/src/services/customer.js b/packages/medusa/src/services/customer.js index 6db5092f03..486e89c210 100644 --- a/packages/medusa/src/services/customer.js +++ b/packages/medusa/src/services/customer.js @@ -3,6 +3,7 @@ import Scrypt from "scrypt-kdf" import _ from "lodash" import { Validator, MedusaError } from "medusa-core-utils" import { BaseService } from "medusa-interfaces" +import { Brackets } from "typeorm" /** * Provides layer to manipulate customers. @@ -132,6 +133,50 @@ class CustomerService extends BaseService { return customerRepo.find(query) } + async listAndCount( + selector, + config = { relations: [], skip: 0, take: 50, order: { created_at: "DESC" } } + ) { + const customerRepo = this.manager_.getCustomRepository( + this.customerRepository_ + ) + + let q + if ("q" in selector) { + q = selector.q + delete selector.q + } + + const query = this.buildQuery_(selector, config) + + if (q) { + const where = query.where + + delete where.email + delete where.first_name + delete where.last_name + + query.join = { + alias: "customer", + } + + query.where = qb => { + qb.where(where) + + qb.andWhere( + new Brackets(qb => { + qb.where(`customer.first_name ILIKE :q`, { q: `%${q}%` }) + .orWhere(`customer.last_name ILIKE :q`, { q: `%${q}%` }) + .orWhere(`customer.email ILIKE :q`, { q: `%${q}%` }) + }) + ) + } + } + + const [customers, count] = await customerRepo.findAndCount(query) + return [customers, count] + } + /** * Return the total number of documents in database * @return {Promise} the result of the count operation From ba39cb16db88e24b1ebf9d5d62ca0c43609db4dd Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Wed, 24 Feb 2021 08:52:39 +0100 Subject: [PATCH 4/4] hotfix(brightpearl): wrap inventory update in transaction --- .../src/services/brightpearl.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/medusa-plugin-brightpearl/src/services/brightpearl.js b/packages/medusa-plugin-brightpearl/src/services/brightpearl.js index a114000e52..2fd27359e8 100644 --- a/packages/medusa-plugin-brightpearl/src/services/brightpearl.js +++ b/packages/medusa-plugin-brightpearl/src/services/brightpearl.js @@ -5,6 +5,7 @@ import Brightpearl from "../utils/brightpearl" class BrightpearlService extends BaseService { constructor( { + manager, oauthService, totalsService, productVariantService, @@ -18,6 +19,7 @@ class BrightpearlService extends BaseService { ) { super() + this.manager_ = manager this.options = options this.productVariantService_ = productVariantService this.regionService_ = regionService @@ -189,8 +191,12 @@ class BrightpearlService extends BaseService { .retrieveBySKU(sku) .catch((_) => undefined) if (variant && variant.manage_inventory) { - await this.productVariantService_.update(variant.id, { - inventory_quantity: onHand, + await this.manager_.transaction((m) => { + return this.productVariantService_ + .withTransaction(m) + .update(variant.id, { + inventory_quantity: onHand, + }) }) } }