From f77fd38369a913af859b0d70d9831305df126536 Mon Sep 17 00:00:00 2001 From: Vilfred Sikker Date: Tue, 24 Aug 2021 12:45:03 +0200 Subject: [PATCH 1/8] extended add to wishlist with metadata --- .../medusa-plugin-wishlist/src/api/store/customers.js | 4 +++- packages/medusa/src/services/line-item.js | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/medusa-plugin-wishlist/src/api/store/customers.js b/packages/medusa-plugin-wishlist/src/api/store/customers.js index 342799f456..a7000e6895 100644 --- a/packages/medusa-plugin-wishlist/src/api/store/customers.js +++ b/packages/medusa-plugin-wishlist/src/api/store/customers.js @@ -38,6 +38,7 @@ export default () => { const schema = Validator.object().keys({ variant_id: Validator.string().required(), quantity: Validator.number().required(), + metadata: Validator.object().optional(), }) const { value, error } = schema.validate(req.body) @@ -57,7 +58,8 @@ export default () => { const lineItem = await lineItemService.generate( value.variant_id, regions[0].id, - value.quantity + value.quantity, + { metadata: value.metadata } ) const wishlist = (customer.metadata && customer.metadata.wishlist) || [] diff --git a/packages/medusa/src/services/line-item.js b/packages/medusa/src/services/line-item.js index 759ae2d63a..d2ce87bd80 100644 --- a/packages/medusa/src/services/line-item.js +++ b/packages/medusa/src/services/line-item.js @@ -90,7 +90,7 @@ class LineItemService extends BaseService { } async generate(variantId, regionId, quantity, config = {}) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const variant = await this.productVariantService_ .withTransaction(manager) .retrieve(variantId, { @@ -142,7 +142,7 @@ class LineItemService extends BaseService { * @return {LineItem} the created line item */ async create(lineItem) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const lineItemRepository = manager.getCustomRepository( this.lineItemRepository_ ) @@ -160,7 +160,7 @@ class LineItemService extends BaseService { * @return {LineItem} the update line item */ async update(id, update) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const lineItemRepository = manager.getCustomRepository( this.lineItemRepository_ ) @@ -188,7 +188,7 @@ class LineItemService extends BaseService { * @return {Promise} the result of the delete operation */ async delete(id) { - return this.atomicPhase_(async manager => { + return this.atomicPhase_(async (manager) => { const lineItemRepository = manager.getCustomRepository( this.lineItemRepository_ ) From ae192d844b094d79a042d79b1cebcda722cf049c Mon Sep 17 00:00:00 2001 From: Vilfred Sikker Date: Thu, 26 Aug 2021 13:00:45 +0200 Subject: [PATCH 2/8] wip --- packages/medusa-plugin-wishlist/package.json | 1 + .../medusa-plugin-wishlist/src/api/index.js | 0 .../src/api/store/customers.js | 32 +++++++ .../src/api/store/index.js | 36 +++++++ packages/medusa-plugin-wishlist/yarn.lock | 96 ++++++++++++++++++- 5 files changed, 164 insertions(+), 1 deletion(-) delete mode 100644 packages/medusa-plugin-wishlist/src/api/index.js create mode 100644 packages/medusa-plugin-wishlist/src/api/store/index.js diff --git a/packages/medusa-plugin-wishlist/package.json b/packages/medusa-plugin-wishlist/package.json index f3dbe8f531..88d1e569a2 100644 --- a/packages/medusa-plugin-wishlist/package.json +++ b/packages/medusa-plugin-wishlist/package.json @@ -36,6 +36,7 @@ }, "dependencies": { "body-parser": "^1.19.0", + "cors": "^2.8.5", "express": "^4.17.1", "medusa-core-utils": "^1.1.20", "medusa-test-utils": "^1.1.23" diff --git a/packages/medusa-plugin-wishlist/src/api/index.js b/packages/medusa-plugin-wishlist/src/api/index.js deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/packages/medusa-plugin-wishlist/src/api/store/customers.js b/packages/medusa-plugin-wishlist/src/api/store/customers.js index a7000e6895..ee44fb6b36 100644 --- a/packages/medusa-plugin-wishlist/src/api/store/customers.js +++ b/packages/medusa-plugin-wishlist/src/api/store/customers.js @@ -1,6 +1,9 @@ import { Router } from "express" import bodyParser from "body-parser" import { Validator, MedusaError } from "medusa-core-utils" +import jwt from "jsonwebtoken" + +const JWT_SECRET = process.env.JWT_SECRET || "" export default () => { const app = Router() @@ -74,5 +77,34 @@ export default () => { } }) + app.post("/:id/wishlist/share-token", bodyParser.json(), async (req, res) => { + try { + const customerService = req.scope.resolve("customerService") + + let customer = await customerService.retrieve(req.params.id) + + // check customer exists else throw 404 + if (!customer?.id) { + throw new MedusaError(Medusa.Types.NOT_FOUND, "not found", 404) + } + + // check customer has wishlist else throw 400 bad request + if (!customer?.metadata?.wishlist) { + throw new MedusaError(Medusa.Types.INVALID_DATA, "invalid data", 400) + } + + const token = jwt.sign( + { + customer_id: customer.id, + }, + JWT_SECRET + ) + + res.json({ share_token: token }) + } catch (err) { + throw err + } + }) + return app } diff --git a/packages/medusa-plugin-wishlist/src/api/store/index.js b/packages/medusa-plugin-wishlist/src/api/store/index.js new file mode 100644 index 0000000000..ef5a12687d --- /dev/null +++ b/packages/medusa-plugin-wishlist/src/api/store/index.js @@ -0,0 +1,36 @@ +import { Router } from "express" +import jwt from "jsonwebtoken" +import cors from "cors" +import { getConfigFile } from "medusa-core-utils" + +export default () => { + const app = Router() + + const JWT_SECRET = process.env.JWT_SECRET || "" + + // const { configModule } = getConfigFile(rootDirectory, "medusa-config") + // const { projectConfig } = configModule + + // const corsOptions = { + // origin: projectConfig.store_cors.split(","), + // credentials: true, + // } + // console.log(corsOptions) + // app.options("/wishlists/:token", cors(corsOptions)) + app.get("/wishlists/:token", async (req, res) => { + const { token } = req.params + + // decorde token with decode = jwt.decode(token, secret) + const decode = jwt.decode(token, JWT_SECRET) + console.log(decode) + // fetch customer.retrieve(decode.customer_id) + // get customer.metadata.wishlist + + // respond with + // wishlist + // first_name + res.json("200") + }) + + return app +} diff --git a/packages/medusa-plugin-wishlist/yarn.lock b/packages/medusa-plugin-wishlist/yarn.lock index 87cddac33e..9511ed85a1 100644 --- a/packages/medusa-plugin-wishlist/yarn.lock +++ b/packages/medusa-plugin-wishlist/yarn.lock @@ -879,6 +879,18 @@ exec-sh "^0.3.2" minimist "^1.2.0" +"@hapi/hoek@^9.0.0": + version "9.2.0" + resolved "https://registry.yarnpkg.com/@hapi/hoek/-/hoek-9.2.0.tgz#f3933a44e365864f4dad5db94158106d511e8131" + integrity sha512-sqKVVVOe5ivCaXDWivIJYVSaEgdQK9ul7a4Kity5Iw7u9+wBAPbX1RMSnLLmp7O4Vzj0WOWwMAJsTL00xwaNug== + +"@hapi/topo@^5.0.0": + version "5.1.0" + resolved "https://registry.yarnpkg.com/@hapi/topo/-/topo-5.1.0.tgz#dc448e332c6c6e37a4dc02fd84ba8d44b9afb012" + integrity sha512-foQZKJig7Ob0BMAYBfcJk8d77QtOe7Wo4ox7ff1lQYoNNAb6jwcY1ncdoy2e9wQZzvNy7ODZCYJkK8kzmcAnAg== + dependencies: + "@hapi/hoek" "^9.0.0" + "@istanbuljs/load-nyc-config@^1.0.0": version "1.1.0" resolved "https://registry.yarnpkg.com/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz#fd3db1d59ecf7cf121e80650bb86712f9b55eced" @@ -1063,6 +1075,23 @@ "@types/yargs" "^15.0.0" chalk "^3.0.0" +"@sideway/address@^4.1.0": + version "4.1.2" + resolved "https://registry.yarnpkg.com/@sideway/address/-/address-4.1.2.tgz#811b84333a335739d3969cfc434736268170cad1" + integrity sha512-idTz8ibqWFrPU8kMirL0CoPH/A29XOzzAzpyN3zQ4kAWnzmNfFmRaoMNN6VI8ske5M73HZyhIaW4OuSFIdM4oA== + dependencies: + "@hapi/hoek" "^9.0.0" + +"@sideway/formula@^3.0.0": + version "3.0.0" + resolved "https://registry.yarnpkg.com/@sideway/formula/-/formula-3.0.0.tgz#fe158aee32e6bd5de85044be615bc08478a0a13c" + integrity sha512-vHe7wZ4NOXVfkoRb8T5otiENVlT7a3IAiw7H5M2+GO+9CDgcVUUsX1zalAztCmwyOr2RUTGJdgB+ZvSVqmdHmg== + +"@sideway/pinpoint@^2.0.0": + version "2.0.0" + resolved "https://registry.yarnpkg.com/@sideway/pinpoint/-/pinpoint-2.0.0.tgz#cff8ffadc372ad29fd3f78277aeb29e632cc70df" + integrity sha512-RNiOoTPkptFtSVzQevY/yWtZwf/RxyVnPy/OcA9HBM3MlGDnBEYL5B41H0MTn0Uec8Hi+2qUtTfG2WWZBmMejQ== + "@sinonjs/commons@^1.7.0": version "1.8.1" resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-1.8.1.tgz#e7df00f98a203324f6dc7cc606cad9d4a8ab2217" @@ -1787,6 +1816,14 @@ core-util-is@1.0.2, core-util-is@~1.0.0: resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" integrity sha1-tf1UIgqivFq1eqtxQMlAdUUDwac= +cors@^2.8.5: + version "2.8.5" + resolved "https://registry.yarnpkg.com/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29" + integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g== + dependencies: + object-assign "^4" + vary "^1" + cross-env@^5.2.1: version "5.2.1" resolved "https://registry.yarnpkg.com/cross-env/-/cross-env-5.2.1.tgz#b2c76c1ca7add66dc874d11798466094f551b34d" @@ -2932,6 +2969,11 @@ is-number@^3.0.0: dependencies: kind-of "^3.0.2" +is-number@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/is-number/-/is-number-4.0.0.tgz#0026e37f5454d73e356dfe6564699867c6a7f0ff" + integrity sha512-rSklcAIlf1OmFdyAqbnWTLVelsQ58uvZ66S/ZyawjWqIviTWCjg2PzVGw8WUA+nNuPTqb4wgA+NszrJ+08LlgQ== + is-number@^7.0.0: version "7.0.0" resolved "https://registry.yarnpkg.com/is-number/-/is-number-7.0.0.tgz#7535345b896734d5f80c4d06c50955527a14f12b" @@ -3417,6 +3459,22 @@ jest@^25.5.2: import-local "^3.0.2" jest-cli "^25.5.4" +joi-objectid@^3.0.1: + version "3.0.1" + resolved "https://registry.yarnpkg.com/joi-objectid/-/joi-objectid-3.0.1.tgz#63ace7860f8e1a993a28d40c40ffd8eff01a3668" + integrity sha512-V/3hbTlGpvJ03Me6DJbdBI08hBTasFOmipsauOsxOSnsF1blxV537WTl1zPwbfcKle4AK0Ma4OPnzMH4LlvTpQ== + +joi@^17.3.0: + version "17.4.2" + resolved "https://registry.yarnpkg.com/joi/-/joi-17.4.2.tgz#02f4eb5cf88e515e614830239379dcbbe28ce7f7" + integrity sha512-Lm56PP+n0+Z2A2rfRvsfWVDXGEWjXxatPopkQ8qQ5mxCEhwHG+Ettgg5o98FFaxilOxozoa14cFhrE/hOzh/Nw== + dependencies: + "@hapi/hoek" "^9.0.0" + "@hapi/topo" "^5.0.0" + "@sideway/address" "^4.1.0" + "@sideway/formula" "^3.0.0" + "@sideway/pinpoint" "^2.0.0" + "js-tokens@^3.0.0 || ^4.0.0", js-tokens@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499" @@ -3646,11 +3704,33 @@ map-visit@^1.0.0: dependencies: object-visit "^1.0.0" +math-random@^1.0.1: + version "1.0.4" + resolved "https://registry.yarnpkg.com/math-random/-/math-random-1.0.4.tgz#5dd6943c938548267016d4e34f057583080c514c" + integrity sha512-rUxjysqif/BZQH2yhd5Aaq7vXMSx9NdEsQcyA07uEzIvxgI7zIr33gGsh+RU0/XjmQpCW7RsVof1vlkvQVCK5A== + media-typer@0.3.0: version "0.3.0" resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748" integrity sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g= +medusa-core-utils@^1.1.20: + version "1.1.20" + resolved "https://registry.yarnpkg.com/medusa-core-utils/-/medusa-core-utils-1.1.20.tgz#676c0dc863a206b80cc53299a984c532d07df65f" + integrity sha512-gf+/L5eeqHea3xgjwD7YZEzfUGlxbjfvaeiiGWi3Wfu0dLa+G1B4S0TsX+upR+oVeWPmk66VMqWC80h3e4csqw== + dependencies: + joi "^17.3.0" + joi-objectid "^3.0.1" + +medusa-test-utils@^1.1.23: + version "1.1.23" + resolved "https://registry.yarnpkg.com/medusa-test-utils/-/medusa-test-utils-1.1.23.tgz#e8380df499979cd0b97a5bb87779662f4da9d722" + integrity sha512-okyUgB4t7bqDieE0XO+HkbVVemn6hE1tTAtF9PXRi2igmKmcnyW/Ljk3lqrKYVhjei4z3Z/b+K2b0oNwhopbGQ== + dependencies: + "@babel/plugin-transform-classes" "^7.9.5" + medusa-core-utils "^1.1.20" + randomatic "^3.1.1" + merge-descriptors@1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/merge-descriptors/-/merge-descriptors-1.0.1.tgz#b00aaa556dd8b44568150ec9d1b953f3f90cbb61" @@ -3879,6 +3959,11 @@ oauth-sign@~0.9.0: resolved "https://registry.yarnpkg.com/oauth-sign/-/oauth-sign-0.9.0.tgz#47a7b016baa68b5fa0ecf3dee08a85c679ac6455" integrity sha512-fexhUFFPTGV8ybAtSIGbV6gOkSv8UtRbDBnAyLQw4QPKkgNlsH2ByPGtMUqdWkos6YCRmAqViwgZrJc/mRDzZQ== +object-assign@^4: + version "4.1.1" + resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863" + integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM= + object-copy@^0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/object-copy/-/object-copy-0.1.0.tgz#7e7d858b781bd7c991a41ba975ed3812754e998c" @@ -4201,6 +4286,15 @@ qs@~6.5.2: resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.2.tgz#cb3ae806e8740444584ef154ce8ee98d403f3e36" integrity sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA== +randomatic@^3.1.1: + version "3.1.1" + resolved "https://registry.yarnpkg.com/randomatic/-/randomatic-3.1.1.tgz#b776efc59375984e36c537b2f51a1f0aff0da1ed" + integrity sha512-TuDE5KxZ0J461RVjrJZCJc+J+zCkTb1MbH9AQUq68sMhOMcy9jLcb3BrZKgp9q9Ncltdg4QVqWrH02W2EFFVYw== + dependencies: + is-number "^4.0.0" + kind-of "^6.0.0" + math-random "^1.0.1" + range-parser@~1.2.1: version "1.2.1" resolved "https://registry.yarnpkg.com/range-parser/-/range-parser-1.2.1.tgz#3cf37023d199e1c24d1a55b84800c2f3e6468031" @@ -5185,7 +5279,7 @@ validate-npm-package-license@^3.0.1: spdx-correct "^3.0.0" spdx-expression-parse "^3.0.0" -vary@~1.1.2: +vary@^1, vary@~1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc" integrity sha1-IpnwLG3tMNSllhsLn3RSShj2NPw= From 2c72227244e39147f5baac531e1bb14fede579dc Mon Sep 17 00:00:00 2001 From: olivermrbl Date: Thu, 26 Aug 2021 13:12:21 +0200 Subject: [PATCH 3/8] chore: update --- packages/medusa-plugin-wishlist/yarn.lock | 81 +++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/packages/medusa-plugin-wishlist/yarn.lock b/packages/medusa-plugin-wishlist/yarn.lock index 87cddac33e..f00a7f9a14 100644 --- a/packages/medusa-plugin-wishlist/yarn.lock +++ b/packages/medusa-plugin-wishlist/yarn.lock @@ -879,6 +879,18 @@ exec-sh "^0.3.2" minimist "^1.2.0" +"@hapi/hoek@^9.0.0": + version "9.2.0" + resolved "https://registry.yarnpkg.com/@hapi/hoek/-/hoek-9.2.0.tgz#f3933a44e365864f4dad5db94158106d511e8131" + integrity sha512-sqKVVVOe5ivCaXDWivIJYVSaEgdQK9ul7a4Kity5Iw7u9+wBAPbX1RMSnLLmp7O4Vzj0WOWwMAJsTL00xwaNug== + +"@hapi/topo@^5.0.0": + version "5.1.0" + resolved "https://registry.yarnpkg.com/@hapi/topo/-/topo-5.1.0.tgz#dc448e332c6c6e37a4dc02fd84ba8d44b9afb012" + integrity sha512-foQZKJig7Ob0BMAYBfcJk8d77QtOe7Wo4ox7ff1lQYoNNAb6jwcY1ncdoy2e9wQZzvNy7ODZCYJkK8kzmcAnAg== + dependencies: + "@hapi/hoek" "^9.0.0" + "@istanbuljs/load-nyc-config@^1.0.0": version "1.1.0" resolved "https://registry.yarnpkg.com/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz#fd3db1d59ecf7cf121e80650bb86712f9b55eced" @@ -1063,6 +1075,23 @@ "@types/yargs" "^15.0.0" chalk "^3.0.0" +"@sideway/address@^4.1.0": + version "4.1.2" + resolved "https://registry.yarnpkg.com/@sideway/address/-/address-4.1.2.tgz#811b84333a335739d3969cfc434736268170cad1" + integrity sha512-idTz8ibqWFrPU8kMirL0CoPH/A29XOzzAzpyN3zQ4kAWnzmNfFmRaoMNN6VI8ske5M73HZyhIaW4OuSFIdM4oA== + dependencies: + "@hapi/hoek" "^9.0.0" + +"@sideway/formula@^3.0.0": + version "3.0.0" + resolved "https://registry.yarnpkg.com/@sideway/formula/-/formula-3.0.0.tgz#fe158aee32e6bd5de85044be615bc08478a0a13c" + integrity sha512-vHe7wZ4NOXVfkoRb8T5otiENVlT7a3IAiw7H5M2+GO+9CDgcVUUsX1zalAztCmwyOr2RUTGJdgB+ZvSVqmdHmg== + +"@sideway/pinpoint@^2.0.0": + version "2.0.0" + resolved "https://registry.yarnpkg.com/@sideway/pinpoint/-/pinpoint-2.0.0.tgz#cff8ffadc372ad29fd3f78277aeb29e632cc70df" + integrity sha512-RNiOoTPkptFtSVzQevY/yWtZwf/RxyVnPy/OcA9HBM3MlGDnBEYL5B41H0MTn0Uec8Hi+2qUtTfG2WWZBmMejQ== + "@sinonjs/commons@^1.7.0": version "1.8.1" resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-1.8.1.tgz#e7df00f98a203324f6dc7cc606cad9d4a8ab2217" @@ -2932,6 +2961,11 @@ is-number@^3.0.0: dependencies: kind-of "^3.0.2" +is-number@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/is-number/-/is-number-4.0.0.tgz#0026e37f5454d73e356dfe6564699867c6a7f0ff" + integrity sha512-rSklcAIlf1OmFdyAqbnWTLVelsQ58uvZ66S/ZyawjWqIviTWCjg2PzVGw8WUA+nNuPTqb4wgA+NszrJ+08LlgQ== + is-number@^7.0.0: version "7.0.0" resolved "https://registry.yarnpkg.com/is-number/-/is-number-7.0.0.tgz#7535345b896734d5f80c4d06c50955527a14f12b" @@ -3417,6 +3451,22 @@ jest@^25.5.2: import-local "^3.0.2" jest-cli "^25.5.4" +joi-objectid@^3.0.1: + version "3.0.1" + resolved "https://registry.yarnpkg.com/joi-objectid/-/joi-objectid-3.0.1.tgz#63ace7860f8e1a993a28d40c40ffd8eff01a3668" + integrity sha512-V/3hbTlGpvJ03Me6DJbdBI08hBTasFOmipsauOsxOSnsF1blxV537WTl1zPwbfcKle4AK0Ma4OPnzMH4LlvTpQ== + +joi@^17.3.0: + version "17.4.2" + resolved "https://registry.yarnpkg.com/joi/-/joi-17.4.2.tgz#02f4eb5cf88e515e614830239379dcbbe28ce7f7" + integrity sha512-Lm56PP+n0+Z2A2rfRvsfWVDXGEWjXxatPopkQ8qQ5mxCEhwHG+Ettgg5o98FFaxilOxozoa14cFhrE/hOzh/Nw== + dependencies: + "@hapi/hoek" "^9.0.0" + "@hapi/topo" "^5.0.0" + "@sideway/address" "^4.1.0" + "@sideway/formula" "^3.0.0" + "@sideway/pinpoint" "^2.0.0" + "js-tokens@^3.0.0 || ^4.0.0", js-tokens@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499" @@ -3646,11 +3696,33 @@ map-visit@^1.0.0: dependencies: object-visit "^1.0.0" +math-random@^1.0.1: + version "1.0.4" + resolved "https://registry.yarnpkg.com/math-random/-/math-random-1.0.4.tgz#5dd6943c938548267016d4e34f057583080c514c" + integrity sha512-rUxjysqif/BZQH2yhd5Aaq7vXMSx9NdEsQcyA07uEzIvxgI7zIr33gGsh+RU0/XjmQpCW7RsVof1vlkvQVCK5A== + media-typer@0.3.0: version "0.3.0" resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748" integrity sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g= +medusa-core-utils@^1.1.20: + version "1.1.20" + resolved "https://registry.yarnpkg.com/medusa-core-utils/-/medusa-core-utils-1.1.20.tgz#676c0dc863a206b80cc53299a984c532d07df65f" + integrity sha512-gf+/L5eeqHea3xgjwD7YZEzfUGlxbjfvaeiiGWi3Wfu0dLa+G1B4S0TsX+upR+oVeWPmk66VMqWC80h3e4csqw== + dependencies: + joi "^17.3.0" + joi-objectid "^3.0.1" + +medusa-test-utils@^1.1.23: + version "1.1.23" + resolved "https://registry.yarnpkg.com/medusa-test-utils/-/medusa-test-utils-1.1.23.tgz#e8380df499979cd0b97a5bb87779662f4da9d722" + integrity sha512-okyUgB4t7bqDieE0XO+HkbVVemn6hE1tTAtF9PXRi2igmKmcnyW/Ljk3lqrKYVhjei4z3Z/b+K2b0oNwhopbGQ== + dependencies: + "@babel/plugin-transform-classes" "^7.9.5" + medusa-core-utils "^1.1.20" + randomatic "^3.1.1" + merge-descriptors@1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/merge-descriptors/-/merge-descriptors-1.0.1.tgz#b00aaa556dd8b44568150ec9d1b953f3f90cbb61" @@ -4201,6 +4273,15 @@ qs@~6.5.2: resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.2.tgz#cb3ae806e8740444584ef154ce8ee98d403f3e36" integrity sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA== +randomatic@^3.1.1: + version "3.1.1" + resolved "https://registry.yarnpkg.com/randomatic/-/randomatic-3.1.1.tgz#b776efc59375984e36c537b2f51a1f0aff0da1ed" + integrity sha512-TuDE5KxZ0J461RVjrJZCJc+J+zCkTb1MbH9AQUq68sMhOMcy9jLcb3BrZKgp9q9Ncltdg4QVqWrH02W2EFFVYw== + dependencies: + is-number "^4.0.0" + kind-of "^6.0.0" + math-random "^1.0.1" + range-parser@~1.2.1: version "1.2.1" resolved "https://registry.yarnpkg.com/range-parser/-/range-parser-1.2.1.tgz#3cf37023d199e1c24d1a55b84800c2f3e6468031" From b1132eec23514a6e7a22fe6653f4a4b4fde0e83f Mon Sep 17 00:00:00 2001 From: olivermrbl Date: Thu, 26 Aug 2021 13:57:30 +0200 Subject: [PATCH 4/8] fix: 404 --- .../medusa-plugin-wishlist/src/api/index.js | 25 +++++++++++++ .../src/api/store/index.js | 36 ------------------- 2 files changed, 25 insertions(+), 36 deletions(-) create mode 100644 packages/medusa-plugin-wishlist/src/api/index.js delete mode 100644 packages/medusa-plugin-wishlist/src/api/store/index.js diff --git a/packages/medusa-plugin-wishlist/src/api/index.js b/packages/medusa-plugin-wishlist/src/api/index.js new file mode 100644 index 0000000000..413cac01e5 --- /dev/null +++ b/packages/medusa-plugin-wishlist/src/api/index.js @@ -0,0 +1,25 @@ +import { Router } from "express" +import bodyParser from "body-parser" +import jwt from "jsonwebtoken" + +const app = Router() +export default () => { + app.get("/wishlists/:token", bodyParser.json(), async (req, res) => { + const { token } = req.params + + const JWT_SECRET = process.env.JWT_SECRET || "" + + // decorde token with decode = jwt.decode(token, secret) + const decode = jwt.decode(token, JWT_SECRET) + console.log(decode) + // fetch customer.retrieve(decode.customer_id) + // get customer.metadata.wishlist + + // respond with + // wishlist + // first_name + res.sendStatus(200) + }) + + return app +} diff --git a/packages/medusa-plugin-wishlist/src/api/store/index.js b/packages/medusa-plugin-wishlist/src/api/store/index.js deleted file mode 100644 index ef5a12687d..0000000000 --- a/packages/medusa-plugin-wishlist/src/api/store/index.js +++ /dev/null @@ -1,36 +0,0 @@ -import { Router } from "express" -import jwt from "jsonwebtoken" -import cors from "cors" -import { getConfigFile } from "medusa-core-utils" - -export default () => { - const app = Router() - - const JWT_SECRET = process.env.JWT_SECRET || "" - - // const { configModule } = getConfigFile(rootDirectory, "medusa-config") - // const { projectConfig } = configModule - - // const corsOptions = { - // origin: projectConfig.store_cors.split(","), - // credentials: true, - // } - // console.log(corsOptions) - // app.options("/wishlists/:token", cors(corsOptions)) - app.get("/wishlists/:token", async (req, res) => { - const { token } = req.params - - // decorde token with decode = jwt.decode(token, secret) - const decode = jwt.decode(token, JWT_SECRET) - console.log(decode) - // fetch customer.retrieve(decode.customer_id) - // get customer.metadata.wishlist - - // respond with - // wishlist - // first_name - res.json("200") - }) - - return app -} From af59095d851708be19964d47105c21860a2290f1 Mon Sep 17 00:00:00 2001 From: Vilfred Sikker Date: Fri, 27 Aug 2021 11:10:59 +0200 Subject: [PATCH 5/8] add error catching and response for customer --- .../medusa-plugin-wishlist/src/api/index.js | 60 ++++++++++++++----- .../src/api/store/customers.js | 2 +- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/packages/medusa-plugin-wishlist/src/api/index.js b/packages/medusa-plugin-wishlist/src/api/index.js index 413cac01e5..28da269189 100644 --- a/packages/medusa-plugin-wishlist/src/api/index.js +++ b/packages/medusa-plugin-wishlist/src/api/index.js @@ -1,25 +1,55 @@ import { Router } from "express" -import bodyParser from "body-parser" import jwt from "jsonwebtoken" +import cors from "cors" +import express from "express" +import { getConfigFile, MedusaError } from "medusa-core-utils" const app = Router() -export default () => { - app.get("/wishlists/:token", bodyParser.json(), async (req, res) => { - const { token } = req.params +export default (rootDirectory) => { + const { configModule } = getConfigFile(rootDirectory, "medusa-config") + const { projectConfig } = configModule + const corsOptions = { + origin: projectConfig.store_cors.split(","), + credentials: true, + } + const JWT_SECRET = process.env.JWT_SECRET || "" - const JWT_SECRET = process.env.JWT_SECRET || "" + app.use("/wishlists/:token", cors(corsOptions)) + app.get( + "/wishlists/:token", + cors(corsOptions), + express.json(), + async (req, res) => { + const customerService = req.scope.resolve("customerService") + const { token } = req.params + let decode - // decorde token with decode = jwt.decode(token, secret) - const decode = jwt.decode(token, JWT_SECRET) - console.log(decode) - // fetch customer.retrieve(decode.customer_id) - // get customer.metadata.wishlist + try { + decode = jwt.decode(token, JWT_SECRET) - // respond with - // wishlist - // first_name - res.sendStatus(200) - }) + if (!decode || !decode.customer_id) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + "Invalid token", + 400 + ) + } + } catch (err) { + res.status(400).json(err) + } + try { + const customer = await customerService.retrieve(decode.customer_id) + const response = { + wishlist: customer.metadata.wishlist, + first_name: customer.first_name, + } + + res.status(200).json({ response }) + } catch (err) { + res.status(400).json(err) + } + } + ) return app } diff --git a/packages/medusa-plugin-wishlist/src/api/store/customers.js b/packages/medusa-plugin-wishlist/src/api/store/customers.js index ee44fb6b36..2d33539ad9 100644 --- a/packages/medusa-plugin-wishlist/src/api/store/customers.js +++ b/packages/medusa-plugin-wishlist/src/api/store/customers.js @@ -93,7 +93,7 @@ export default () => { throw new MedusaError(Medusa.Types.INVALID_DATA, "invalid data", 400) } - const token = jwt.sign( + const token = await jwt.sign( { customer_id: customer.id, }, From ab5704208bbf1f8a5229f27c21d6bcee050f92fa Mon Sep 17 00:00:00 2001 From: Vilfred Sikker Date: Fri, 27 Aug 2021 11:19:04 +0200 Subject: [PATCH 6/8] use develop line-item service --- packages/medusa/src/services/line-item.js | 87 +++++++++++------------ 1 file changed, 41 insertions(+), 46 deletions(-) diff --git a/packages/medusa/src/services/line-item.js b/packages/medusa/src/services/line-item.js index d2ce87bd80..843faae709 100644 --- a/packages/medusa/src/services/line-item.js +++ b/packages/medusa/src/services/line-item.js @@ -90,50 +90,45 @@ class LineItemService extends BaseService { } async generate(variantId, regionId, quantity, config = {}) { - return this.atomicPhase_(async (manager) => { - const variant = await this.productVariantService_ - .withTransaction(manager) - .retrieve(variantId, { - relations: ["product"], - }) - - const region = await this.regionService_ - .withTransaction(manager) - .retrieve(regionId) - - let price - let shouldMerge = true - - if (config.unit_price && typeof config.unit_price !== `undefined`) { - // if custom unit_price, we ensure positive values - // and we choose to not merge the items - shouldMerge = false - if (config.unit_price < 0) { - price = 0 - } else { - price = config.unit_price - } - } else { - price = await this.productVariantService_ - .withTransaction(manager) - .getRegionPrice(variant.id, region.id) - } - - const toCreate = { - unit_price: price, - title: variant.product.title, - description: variant.title, - thumbnail: variant.product.thumbnail, - variant_id: variant.id, - quantity: quantity || 1, - allow_discounts: variant.product.discountable, - is_giftcard: variant.product.is_giftcard, - metadata: config?.metadata || {}, - should_merge: shouldMerge, - } - - return toCreate + const variant = await this.productVariantService_.retrieve(variantId, { + relations: ["product"], }) + + const region = await this.regionService_.retrieve(regionId) + + let price + let shouldMerge = true + + if (config.unit_price && typeof config.unit_price !== `undefined`) { + // if custom unit_price, we ensure positive values + // and we choose to not merge the items + shouldMerge = false + if (config.unit_price < 0) { + price = 0 + } else { + price = config.unit_price + } + } else { + price = await this.productVariantService_.getRegionPrice( + variant.id, + region.id + ) + } + + const toCreate = { + unit_price: price, + title: variant.product.title, + description: variant.title, + thumbnail: variant.product.thumbnail, + variant_id: variant.id, + quantity: quantity || 1, + allow_discounts: !variant.product.is_giftcard, + is_giftcard: variant.product.is_giftcard, + metadata: config?.metadata || {}, + should_merge: shouldMerge, + } + + return toCreate } /** @@ -142,7 +137,7 @@ class LineItemService extends BaseService { * @return {LineItem} the created line item */ async create(lineItem) { - return this.atomicPhase_(async (manager) => { + return this.atomicPhase_(async manager => { const lineItemRepository = manager.getCustomRepository( this.lineItemRepository_ ) @@ -160,7 +155,7 @@ class LineItemService extends BaseService { * @return {LineItem} the update line item */ async update(id, update) { - return this.atomicPhase_(async (manager) => { + return this.atomicPhase_(async manager => { const lineItemRepository = manager.getCustomRepository( this.lineItemRepository_ ) @@ -188,7 +183,7 @@ class LineItemService extends BaseService { * @return {Promise} the result of the delete operation */ async delete(id) { - return this.atomicPhase_(async (manager) => { + return this.atomicPhase_(async manager => { const lineItemRepository = manager.getCustomRepository( this.lineItemRepository_ ) From 5261999d129474c9599e75323c6284d0b86d6539 Mon Sep 17 00:00:00 2001 From: Vilfred Sikker Date: Fri, 27 Aug 2021 11:22:14 +0200 Subject: [PATCH 7/8] grap line item from develop --- packages/medusa/src/services/line-item.js | 69 ++++++++++++----------- 1 file changed, 37 insertions(+), 32 deletions(-) diff --git a/packages/medusa/src/services/line-item.js b/packages/medusa/src/services/line-item.js index 843faae709..759ae2d63a 100644 --- a/packages/medusa/src/services/line-item.js +++ b/packages/medusa/src/services/line-item.js @@ -90,45 +90,50 @@ class LineItemService extends BaseService { } async generate(variantId, regionId, quantity, config = {}) { - const variant = await this.productVariantService_.retrieve(variantId, { - relations: ["product"], - }) + return this.atomicPhase_(async manager => { + const variant = await this.productVariantService_ + .withTransaction(manager) + .retrieve(variantId, { + relations: ["product"], + }) - const region = await this.regionService_.retrieve(regionId) + const region = await this.regionService_ + .withTransaction(manager) + .retrieve(regionId) - let price - let shouldMerge = true + let price + let shouldMerge = true - if (config.unit_price && typeof config.unit_price !== `undefined`) { - // if custom unit_price, we ensure positive values - // and we choose to not merge the items - shouldMerge = false - if (config.unit_price < 0) { - price = 0 + if (config.unit_price && typeof config.unit_price !== `undefined`) { + // if custom unit_price, we ensure positive values + // and we choose to not merge the items + shouldMerge = false + if (config.unit_price < 0) { + price = 0 + } else { + price = config.unit_price + } } else { - price = config.unit_price + price = await this.productVariantService_ + .withTransaction(manager) + .getRegionPrice(variant.id, region.id) } - } else { - price = await this.productVariantService_.getRegionPrice( - variant.id, - region.id - ) - } - const toCreate = { - unit_price: price, - title: variant.product.title, - description: variant.title, - thumbnail: variant.product.thumbnail, - variant_id: variant.id, - quantity: quantity || 1, - allow_discounts: !variant.product.is_giftcard, - is_giftcard: variant.product.is_giftcard, - metadata: config?.metadata || {}, - should_merge: shouldMerge, - } + const toCreate = { + unit_price: price, + title: variant.product.title, + description: variant.title, + thumbnail: variant.product.thumbnail, + variant_id: variant.id, + quantity: quantity || 1, + allow_discounts: variant.product.discountable, + is_giftcard: variant.product.is_giftcard, + metadata: config?.metadata || {}, + should_merge: shouldMerge, + } - return toCreate + return toCreate + }) } /** From 15594f9d31f81a174021eaf5d8e524b7558fbf67 Mon Sep 17 00:00:00 2001 From: Vilfred Sikker Date: Fri, 27 Aug 2021 15:59:50 +0200 Subject: [PATCH 8/8] fix pr request --- packages/medusa-plugin-wishlist/src/api/index.js | 16 ++++++---------- .../src/api/store/customers.js | 12 +++++------- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/packages/medusa-plugin-wishlist/src/api/index.js b/packages/medusa-plugin-wishlist/src/api/index.js index 28da269189..2feb5ea241 100644 --- a/packages/medusa-plugin-wishlist/src/api/index.js +++ b/packages/medusa-plugin-wishlist/src/api/index.js @@ -28,25 +28,21 @@ export default (rootDirectory) => { decode = jwt.decode(token, JWT_SECRET) if (!decode || !decode.customer_id) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - "Invalid token", - 400 - ) + throw new MedusaError(MedusaError.Types.NOT_FOUND, "Invalid token") } } catch (err) { - res.status(400).json(err) + res.status(400).json({ message: err.message }) } try { const customer = await customerService.retrieve(decode.customer_id) - const response = { - wishlist: customer.metadata.wishlist, + const wishlist = { + items: customer.metadata.wishlist, first_name: customer.first_name, } - res.status(200).json({ response }) + res.status(200).json({ wishlist }) } catch (err) { - res.status(400).json(err) + res.status(400).json({ message: err.message }) } } ) diff --git a/packages/medusa-plugin-wishlist/src/api/store/customers.js b/packages/medusa-plugin-wishlist/src/api/store/customers.js index 2d33539ad9..f9de96f32a 100644 --- a/packages/medusa-plugin-wishlist/src/api/store/customers.js +++ b/packages/medusa-plugin-wishlist/src/api/store/customers.js @@ -83,17 +83,15 @@ export default () => { let customer = await customerService.retrieve(req.params.id) - // check customer exists else throw 404 - if (!customer?.id) { - throw new MedusaError(Medusa.Types.NOT_FOUND, "not found", 404) - } - // check customer has wishlist else throw 400 bad request if (!customer?.metadata?.wishlist) { - throw new MedusaError(Medusa.Types.INVALID_DATA, "invalid data", 400) + throw new MedusaError( + Medusa.Types.INVALID_DATA, + "Invalid data - Customer doesn't have a wishlist" + ) } - const token = await jwt.sign( + const token = jwt.sign( { customer_id: customer.id, },