From d2a7534615b8870b235be136c8e6425c51dee19b Mon Sep 17 00:00:00 2001 From: Kasper Fabricius Kristensen <45367945+kasperkristensen@users.noreply.github.com> Date: Fri, 4 Feb 2022 16:24:42 +0100 Subject: [PATCH] fix: admin collections default relations (#1023) * added default relations to retrieve collection endpoint * push snapshot * added created at and updated at to collection list endpoint * updated integration tests * added q param to list collections * updated snapshots --- .../admin/__snapshots__/colllections.js.snap | 64 +++++++++++++++++ .../api/__tests__/admin/colllections.js | 22 ++++++ integration-tests/api/package.json | 6 +- integration-tests/api/yarn.lock | 70 +++++++++---------- .../collections/__tests__/get-collection.js | 4 +- .../admin/collections/get-collection.ts | 7 +- .../src/api/routes/admin/collections/index.ts | 5 +- .../admin/collections/list-collections.ts | 6 +- .../medusa/src/services/product-collection.js | 31 +++++++- 9 files changed, 171 insertions(+), 44 deletions(-) diff --git a/integration-tests/api/__tests__/admin/__snapshots__/colllections.js.snap b/integration-tests/api/__tests__/admin/__snapshots__/colllections.js.snap index 2c1e5d7db0..bbd0467bf2 100644 --- a/integration-tests/api/__tests__/admin/__snapshots__/colllections.js.snap +++ b/integration-tests/api/__tests__/admin/__snapshots__/colllections.js.snap @@ -18,6 +18,7 @@ exports[`/admin/collections /admin/collections filters collections by title 1`] Object { "collections": Array [ Object { + "created_at": Any, "handle": "test-collection", "id": "test-collection", "products": Array [ @@ -77,6 +78,7 @@ Object { }, ], "title": "Test collection", + "updated_at": Any, }, ], "count": 1, @@ -89,6 +91,7 @@ exports[`/admin/collections /admin/collections lists collections 1`] = ` Object { "collections": Array [ Object { + "created_at": Any, "handle": "test-collection", "id": "test-collection", "products": Array [ @@ -148,8 +151,10 @@ Object { }, ], "title": "Test collection", + "updated_at": Any, }, Object { + "created_at": Any, "handle": "test-collection1", "id": "test-collection1", "products": Array [ @@ -209,8 +214,10 @@ Object { }, ], "title": "Test collection 1", + "updated_at": Any, }, Object { + "created_at": Any, "handle": "test-collection2", "id": "test-collection2", "products": Array [ @@ -243,6 +250,7 @@ Object { }, ], "title": "Test collection 2", + "updated_at": Any, }, ], "count": 3, @@ -259,6 +267,62 @@ Object { "handle": "test-collection", "id": "test-collection", "metadata": null, + "products": Array [ + Object { + "collection_id": "test-collection", + "created_at": Any, + "deleted_at": null, + "description": "test-product-description", + "discountable": true, + "external_id": null, + "handle": "test-product", + "height": null, + "hs_code": null, + "id": "test-product", + "is_giftcard": false, + "length": null, + "material": null, + "metadata": null, + "mid_code": null, + "origin_country": null, + "profile_id": StringMatching /\\^sp_\\*/, + "status": "draft", + "subtitle": null, + "thumbnail": null, + "title": "Test product", + "type_id": "test-type", + "updated_at": Any, + "weight": null, + "width": null, + }, + Object { + "collection_id": "test-collection", + "created_at": Any, + "deleted_at": null, + "description": "test-product-description1", + "discountable": true, + "external_id": null, + "handle": "test-product1", + "height": null, + "hs_code": null, + "id": "test-product1", + "is_giftcard": false, + "length": null, + "material": null, + "metadata": null, + "mid_code": null, + "origin_country": null, + "profile_id": StringMatching /\\^sp_\\*/, + "status": "draft", + "subtitle": null, + "thumbnail": null, + "title": "Test product1", + "type_id": "test-type", + "updated_at": Any, + "weight": null, + "width": null, + }, + ], "title": "Test collection", "updated_at": Any, }, diff --git a/integration-tests/api/__tests__/admin/colllections.js b/integration-tests/api/__tests__/admin/colllections.js index 51b0673732..38fb714a79 100644 --- a/integration-tests/api/__tests__/admin/colllections.js +++ b/integration-tests/api/__tests__/admin/colllections.js @@ -107,6 +107,20 @@ describe("/admin/collections", () => { id: "test-collection", created_at: expect.any(String), updated_at: expect.any(String), + products: [ + { + collection_id: "test-collection", + created_at: expect.any(String), + updated_at: expect.any(String), + profile_id: expect.stringMatching(/^sp_*/), + }, + { + collection_id: "test-collection", + created_at: expect.any(String), + updated_at: expect.any(String), + profile_id: expect.stringMatching(/^sp_*/), + }, + ], }, }) }) @@ -165,6 +179,8 @@ describe("/admin/collections", () => { id: "test-collection", handle: "test-collection", title: "Test collection", + created_at: expect.any(String), + updated_at: expect.any(String), products: [ { collection_id: "test-collection", @@ -184,6 +200,8 @@ describe("/admin/collections", () => { id: "test-collection1", handle: "test-collection1", title: "Test collection 1", + created_at: expect.any(String), + updated_at: expect.any(String), products: [ { collection_id: "test-collection1", @@ -203,6 +221,8 @@ describe("/admin/collections", () => { id: "test-collection2", handle: "test-collection2", title: "Test collection 2", + created_at: expect.any(String), + updated_at: expect.any(String), products: [ { collection_id: "test-collection2", @@ -232,6 +252,8 @@ describe("/admin/collections", () => { id: "test-collection", handle: "test-collection", title: "Test collection", + created_at: expect.any(String), + updated_at: expect.any(String), products: [ { collection_id: "test-collection", diff --git a/integration-tests/api/package.json b/integration-tests/api/package.json index 4cd75eb538..cd8ef7cdf0 100644 --- a/integration-tests/api/package.json +++ b/integration-tests/api/package.json @@ -8,15 +8,15 @@ "build": "babel src -d dist --extensions \".ts,.js\"" }, "dependencies": { - "@medusajs/medusa": "1.1.60-dev-1641824815181", - "medusa-interfaces": "1.1.32-dev-1641824815181", + "@medusajs/medusa": "1.1.62-dev-1643977380526", + "medusa-interfaces": "1.1.32-dev-1643977380526", "typeorm": "^0.2.31" }, "devDependencies": { "@babel/cli": "^7.12.10", "@babel/core": "^7.12.10", "@babel/node": "^7.12.10", - "babel-preset-medusa-package": "1.1.19-dev-1641824815181", + "babel-preset-medusa-package": "1.1.19-dev-1643977380526", "jest": "^26.6.3" } } diff --git a/integration-tests/api/yarn.lock b/integration-tests/api/yarn.lock index 0db9899c24..6b302d352a 100644 --- a/integration-tests/api/yarn.lock +++ b/integration-tests/api/yarn.lock @@ -1256,10 +1256,10 @@ "@types/yargs" "^15.0.0" chalk "^4.0.0" -"@medusajs/medusa-cli@1.1.25-dev-1641824815181": - version "1.1.25-dev-1641824815181" - resolved "http://localhost:4873/@medusajs%2fmedusa-cli/-/medusa-cli-1.1.25-dev-1641824815181.tgz#fc595f422bf3142a65e48df9855ea8644cedaa18" - integrity sha512-xGPb3GJJweG4O3/xESQbL4MyuBHRfTYo09Q/9zaFeD6KSw/MyBiY6odwtD7n1hbLSdUHtfEs8M7h+0WPe2o+/Q== +"@medusajs/medusa-cli@1.1.27-dev-1643977380526": + version "1.1.27-dev-1643977380526" + resolved "http://localhost:4873/@medusajs%2fmedusa-cli/-/medusa-cli-1.1.27-dev-1643977380526.tgz#a74b506cef603ac1630eedb0329cfe0243c51de2" + integrity sha512-n3clGbZYiWmr0BSyvejd0insnrjcx0MVir9LCkfXPkgnghkdC+C9lIoRAduceSnha9fxoz2XjfapV3VHeAvYoQ== dependencies: "@babel/polyfill" "^7.8.7" "@babel/runtime" "^7.9.6" @@ -1277,8 +1277,8 @@ is-valid-path "^0.1.1" joi-objectid "^3.0.1" meant "^1.0.1" - medusa-core-utils "1.1.31-dev-1641824815181" - medusa-telemetry "0.0.11-dev-1641824815181" + medusa-core-utils "1.1.31-dev-1643977380526" + medusa-telemetry "0.0.11-dev-1643977380526" netrc-parser "^3.1.6" open "^8.0.6" ora "^5.4.1" @@ -1292,13 +1292,13 @@ winston "^3.3.3" yargs "^15.3.1" -"@medusajs/medusa@1.1.60-dev-1641824815181": - version "1.1.60-dev-1641824815181" - resolved "http://localhost:4873/@medusajs%2fmedusa/-/medusa-1.1.60-dev-1641824815181.tgz#cb309b3007fff1e51181f3c65fc563f60503d9ca" - integrity sha512-uVC0L3YhM8oEmczoqrOoYevhln2e2TGcfZDDttXznl0Rjke+gmvI4Isz6KmRNL/6MBeoNMK36qpakAjqJbCPNg== +"@medusajs/medusa@1.1.62-dev-1643977380526": + version "1.1.62-dev-1643977380526" + resolved "http://localhost:4873/@medusajs%2fmedusa/-/medusa-1.1.62-dev-1643977380526.tgz#db32cba8212ba20694b81bb7a64ec40f5846abc0" + integrity sha512-Dy/PWcTlMk7sW+Y3dgt8ypQZnuOqB9mtCDz+1yDkgLkVbUWWHbe/8kGIkAmwvetKTJJt6FoaEgCHlEaD72zulQ== dependencies: "@hapi/joi" "^16.1.8" - "@medusajs/medusa-cli" "1.1.25-dev-1641824815181" + "@medusajs/medusa-cli" "1.1.27-dev-1643977380526" "@types/lodash" "^4.14.168" awilix "^4.2.3" body-parser "^1.19.0" @@ -1322,8 +1322,8 @@ joi "^17.3.0" joi-objectid "^3.0.1" jsonwebtoken "^8.5.1" - medusa-core-utils "1.1.31-dev-1641824815181" - medusa-test-utils "1.1.35-dev-1641824815181" + medusa-core-utils "1.1.31-dev-1643977380526" + medusa-test-utils "1.1.37-dev-1643977380526" morgan "^1.9.1" multer "^1.4.2" passport "^0.4.0" @@ -1947,10 +1947,10 @@ babel-preset-jest@^26.6.2: babel-plugin-jest-hoist "^26.6.2" babel-preset-current-node-syntax "^1.0.0" -babel-preset-medusa-package@1.1.19-dev-1641824815181: - version "1.1.19-dev-1641824815181" - resolved "http://localhost:4873/babel-preset-medusa-package/-/babel-preset-medusa-package-1.1.19-dev-1641824815181.tgz#7bd1f3c9098304f44ec2375b834699bc7af24281" - integrity sha512-n7IhbnTBB7ZuLalchSTTQJGrQJijwj+exgk4qff3ZPT+HYpbOJOtDjgbzCpLfvKxrkXZB2L0dU49ssvshPdMzg== +babel-preset-medusa-package@1.1.19-dev-1643977380526: + version "1.1.19-dev-1643977380526" + resolved "http://localhost:4873/babel-preset-medusa-package/-/babel-preset-medusa-package-1.1.19-dev-1643977380526.tgz#5db5e61d37a81d47e37f14cf25066fac36cc3743" + integrity sha512-WmowYmKs5I6MMqiCPWgLCzD14LFhnAbqJ+BiJnR+XgXZdIs90y/9QIrDYjvmm+hhuit0GYRfsVvqgB+swAlzyQ== dependencies: "@babel/plugin-proposal-class-properties" "^7.12.1" "@babel/plugin-proposal-decorators" "^7.12.1" @@ -5135,25 +5135,25 @@ media-typer@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.31-dev-1641824815181: - version "1.1.31-dev-1641824815181" - resolved "http://localhost:4873/medusa-core-utils/-/medusa-core-utils-1.1.31-dev-1641824815181.tgz#40510df0144690a838b683a68b8e0c07404d1ee2" - integrity sha512-SedEB/rafahrsIDzXu2/Dy8nB+3DYcqbo0hzrcirrT+UouqbuaPYkXfjB1N76YwD1rC1c3p4JmBizK7Mwj84KA== +medusa-core-utils@1.1.31-dev-1643977380526: + version "1.1.31-dev-1643977380526" + resolved "http://localhost:4873/medusa-core-utils/-/medusa-core-utils-1.1.31-dev-1643977380526.tgz#486812e5df4c5fb276591d3448a466d7df1a9c70" + integrity sha512-EilnAOhs8Z8F6BaO4GpxdlZl2UTaOCDA0AUoYHr3FwA4VIx1ZrHWFaJKcZH3ZPhtkPiAfj0hJYIrIlWZiYL2AQ== dependencies: joi "^17.3.0" joi-objectid "^3.0.1" -medusa-interfaces@1.1.32-dev-1641824815181: - version "1.1.32-dev-1641824815181" - resolved "http://localhost:4873/medusa-interfaces/-/medusa-interfaces-1.1.32-dev-1641824815181.tgz#ec5a22fb653690256371225b08e7e12de914ff27" - integrity sha512-1W5BQLOTKaIWkkOq6g5rpNcm1pAp09SpUvDtLKpy41UignIyJp8zY7FnzCqdraSe1q/CoMptNclso6I4OABQ2w== +medusa-interfaces@1.1.32-dev-1643977380526: + version "1.1.32-dev-1643977380526" + resolved "http://localhost:4873/medusa-interfaces/-/medusa-interfaces-1.1.32-dev-1643977380526.tgz#fa4e9d1fc78d49bb6acb6f00c9523ae7155018a2" + integrity sha512-Sy25gHF66/ZBzYNKo8TE7sVu5Gz9H4TKGBW2FpIlTJJ7rJKcaKutX71Qc2JT7KEgJ/Jx5Bu9OJaZlYaqbtmiFQ== dependencies: - medusa-core-utils "1.1.31-dev-1641824815181" + medusa-core-utils "1.1.31-dev-1643977380526" -medusa-telemetry@0.0.11-dev-1641824815181: - version "0.0.11-dev-1641824815181" - resolved "http://localhost:4873/medusa-telemetry/-/medusa-telemetry-0.0.11-dev-1641824815181.tgz#d49db51f90f16544379b2e6470821592f8b24ee4" - integrity sha512-xEVGR/OKw44xDnc1uHWm48edX+QPCiowmp+aLaHaWKontAT/C0jn9k2tFK85ObScqy0FshJPluQg2RedALjLOg== +medusa-telemetry@0.0.11-dev-1643977380526: + version "0.0.11-dev-1643977380526" + resolved "http://localhost:4873/medusa-telemetry/-/medusa-telemetry-0.0.11-dev-1643977380526.tgz#becabd4b43192025ae35923f82cba55b2a4ec7e6" + integrity sha512-WhM5e3VjnpKAh2xBk/L0ZzkbV7B5sJHsl8ftHfZdncz1lMnVVBpScatXVBVIVVcwZPvyMD8wj6iMLBhDIYflMg== dependencies: axios "^0.21.1" axios-retry "^3.1.9" @@ -5165,13 +5165,13 @@ medusa-telemetry@0.0.11-dev-1641824815181: remove-trailing-slash "^0.1.1" uuid "^8.3.2" -medusa-test-utils@1.1.35-dev-1641824815181: - version "1.1.35-dev-1641824815181" - resolved "http://localhost:4873/medusa-test-utils/-/medusa-test-utils-1.1.35-dev-1641824815181.tgz#5cf0e45f534a5532acf11d6094d0291850379875" - integrity sha512-1vTZ4D20qPEuh8LX4+wMdOfZd4pilRCE4wBehhJvyqLX5r7z+xmgVwz/EJC4VGCbk+keNXardVwb+leFYLywug== +medusa-test-utils@1.1.37-dev-1643977380526: + version "1.1.37-dev-1643977380526" + resolved "http://localhost:4873/medusa-test-utils/-/medusa-test-utils-1.1.37-dev-1643977380526.tgz#e211bf227b344d079595a091cd4079107b04c98b" + integrity sha512-1BM1GVFcFUVfRq6D5kwXMlpdeXaXRB9Zjl6xppCgFrgooxvoVoT4cKWPMAgyo/Q7JEvOaIaZDT1LKrtFo/QFOg== dependencies: "@babel/plugin-transform-classes" "^7.9.5" - medusa-core-utils "1.1.31-dev-1641824815181" + medusa-core-utils "1.1.31-dev-1643977380526" randomatic "^3.1.1" merge-descriptors@1.0.1: diff --git a/packages/medusa/src/api/routes/admin/collections/__tests__/get-collection.js b/packages/medusa/src/api/routes/admin/collections/__tests__/get-collection.js index 7a69a5170e..6dc23bf1b6 100644 --- a/packages/medusa/src/api/routes/admin/collections/__tests__/get-collection.js +++ b/packages/medusa/src/api/routes/admin/collections/__tests__/get-collection.js @@ -1,4 +1,5 @@ import { IdMap } from "medusa-test-utils" +import { defaultAdminCollectionsRelations } from ".." import { request } from "../../../../../helpers/test-request" import { ProductCollectionServiceMock } from "../../../../../services/__mocks__/product-collection" @@ -26,7 +27,8 @@ describe("GET /admin/categories/:id", () => { it("calls retrieve from product collection service", () => { expect(ProductCollectionServiceMock.retrieve).toHaveBeenCalledTimes(1) expect(ProductCollectionServiceMock.retrieve).toHaveBeenCalledWith( - IdMap.getId("col") + IdMap.getId("col"), + {relations: defaultAdminCollectionsRelations} ) }) diff --git a/packages/medusa/src/api/routes/admin/collections/get-collection.ts b/packages/medusa/src/api/routes/admin/collections/get-collection.ts index 27bbb45b00..cb0a449c35 100644 --- a/packages/medusa/src/api/routes/admin/collections/get-collection.ts +++ b/packages/medusa/src/api/routes/admin/collections/get-collection.ts @@ -1,3 +1,4 @@ +import { defaultAdminCollectionsRelations } from "." import ProductCollectionService from "../../../../services/product-collection" /** * @oas [get] /collections/{id} @@ -22,10 +23,14 @@ import ProductCollectionService from "../../../../services/product-collection" export default async (req, res) => { const { id } = req.params + const retrieveConfig = { + relations: defaultAdminCollectionsRelations, + } + const productCollectionService: ProductCollectionService = req.scope.resolve( "productCollectionService" ) - const collection = await productCollectionService.retrieve(id) + const collection = await productCollectionService.retrieve(id, retrieveConfig) res.status(200).json({ collection }) } diff --git a/packages/medusa/src/api/routes/admin/collections/index.ts b/packages/medusa/src/api/routes/admin/collections/index.ts index 5bef1d8aaa..ba98fa8606 100644 --- a/packages/medusa/src/api/routes/admin/collections/index.ts +++ b/packages/medusa/src/api/routes/admin/collections/index.ts @@ -1,8 +1,8 @@ import { Router } from "express" +import "reflect-metadata" import { ProductCollection } from "../../../.." import { DeleteResponse, PaginatedResponse } from "../../../../types/common" import middlewares from "../../../middlewares" -import "reflect-metadata" const route = Router() @@ -20,7 +20,7 @@ export default (app) => { return app } -export const defaultAdminCollectionsFields = ["id", "title", "handle"] +export const defaultAdminCollectionsFields = ["id", "title", "handle", "created_at", "updated_at"] export const defaultAdminCollectionsRelations = ["products"] export type AdminCollectionsListRes = PaginatedResponse & { @@ -38,3 +38,4 @@ export * from "./delete-collection" export * from "./get-collection" export * from "./list-collections" export * from "./update-collection" + diff --git a/packages/medusa/src/api/routes/admin/collections/list-collections.ts b/packages/medusa/src/api/routes/admin/collections/list-collections.ts index 05c47e1778..6ed382f6a2 100644 --- a/packages/medusa/src/api/routes/admin/collections/list-collections.ts +++ b/packages/medusa/src/api/routes/admin/collections/list-collections.ts @@ -3,7 +3,7 @@ import { IsNumber, IsOptional, IsString, ValidateNested } from "class-validator" import _, { identity } from "lodash" import { defaultAdminCollectionsFields, - defaultAdminCollectionsRelations, + defaultAdminCollectionsRelations } from "." import ProductCollectionService from "../../../../services/product-collection" import { DateComparisonOperator } from "../../../../types/common" @@ -98,4 +98,8 @@ export class AdminGetCollectionsParams extends AdminGetCollectionsPaginationPara @IsOptional() @Type(() => DateComparisonOperator) deleted_at?: DateComparisonOperator + + @IsString() + @IsOptional() + q?: string } diff --git a/packages/medusa/src/services/product-collection.js b/packages/medusa/src/services/product-collection.js index e63ec95307..35748f8c62 100644 --- a/packages/medusa/src/services/product-collection.js +++ b/packages/medusa/src/services/product-collection.js @@ -1,5 +1,6 @@ -import { BaseService } from "medusa-interfaces" import { MedusaError } from "medusa-core-utils" +import { BaseService } from "medusa-interfaces" +import { Brackets, ILike } from "typeorm" /** * Provides layer to manipulate product collections. @@ -187,7 +188,35 @@ class ProductCollectionService extends BaseService { this.productCollectionRepository_ ) + 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.title + delete where.handle + delete where.created_at + delete where.updated_at + + query.where = (qb) => { + qb.where(where) + + qb.andWhere( + new Brackets((qb) => { + qb.where({ title: ILike(`%${q}%`) }).orWhere({ + handle: ILike(`%${q}%`), + }) + }) + ) + } + } + return await productCollectionRepo.findAndCount(query) } }