feat(product): Move mikro orm utils to the utils package (#4631)

Move utils to the utils package as much as possible

Co-authored-by: Shahed Nasser <27354907+shahednasser@users.noreply.github.com>
This commit is contained in:
Adrien de PerettiandShahed Nasser authored and GitHub committed 2023-07-31 11:30:43 +00:00
1 parent 648eb106d6
commit 4073b73130
39 files changed
+523 -375

No files matched your search

@@ -1,6 +1,6 @@
import { BaseRepository } from "../../src/repositories/base"
import { DALUtils } from "@medusajs/utils"
class CustomRepository extends BaseRepository {
class CustomRepository extends DALUtils.MikroOrmBaseRepository {
constructor({ manager }) {
// @ts-ignore
super(...arguments)
@@ -1,7 +1,6 @@
import { IProductModuleService } from "@medusajs/types"
import { IProductModuleService, ProductTypes } from "@medusajs/types"
import { Product, ProductCategory } from "@models"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { ProductTypes } from "@medusajs/types"
import { initialize } from "../../../../src"
import { DB_URL, TestDatabase } from "../../../utils"
@@ -43,15 +42,18 @@ describe("ProductModuleService product categories", () => {
status: ProductTypes.ProductStatus.PUBLISHED,
})
const productCategoriesData = [{
id: "test-1",
name: "category 1",
products: [productOne],
},{
id: "test-2",
name: "category",
products: [productTwo],
}]
const productCategoriesData = [
{
id: "test-1",
name: "category 1",
products: [productOne],
},
{
id: "test-2",
name: "category",
products: [productTwo],
},
]
productCategories = await createProductCategories(
testManager,
@@ -121,10 +123,12 @@ describe("ProductModuleService product categories", () => {
expect.objectContaining({
id: "test-1",
name: "category 1",
products: [expect.objectContaining({
id: "product-1",
title: "product 1",
})],
products: [
expect.objectContaining({
id: "product-1",
title: "product 1",
}),
],
}),
])
})
@@ -191,10 +195,12 @@ describe("ProductModuleService product categories", () => {
expect.objectContaining({
id: "test-1",
name: "category 1",
products: [expect.objectContaining({
id: "product-1",
title: "product 1",
})],
products: [
expect.objectContaining({
id: "product-1",
title: "product 1",
}),
],
}),
])
})
@@ -203,35 +209,34 @@ describe("ProductModuleService product categories", () => {
describe("retrieveCategory", () => {
it("should return the requested category", async () => {
const result = await service.retrieveCategory(productCategoryOne.id, {
select: ["id", "name"]
select: ["id", "name"],
})
expect(result).toEqual(
expect.objectContaining({
id: "test-1",
name: "category 1",
}),
})
)
})
it("should return requested attributes when requested through config", async () => {
const result = await service.retrieveCategory(
productCategoryOne.id,
{
select: ["id", "name", "products.title"],
relations: ["products"],
}
)
const result = await service.retrieveCategory(productCategoryOne.id, {
select: ["id", "name", "products.title"],
relations: ["products"],
})
expect(result).toEqual(
expect.objectContaining({
id: "test-1",
name: "category 1",
products: [expect.objectContaining({
id: "product-1",
title: "product 1",
})],
}),
products: [
expect.objectContaining({
id: "product-1",
title: "product 1",
}),
],
})
)
})
@@ -244,7 +249,9 @@ describe("ProductModuleService product categories", () => {
error = e
}
expect(error.message).toEqual("ProductCategory with id: does-not-exist was not found")
expect(error.message).toEqual(
"ProductCategory with id: does-not-exist was not found"
)
})
})
@@ -255,11 +262,14 @@ describe("ProductModuleService product categories", () => {
parent_category_id: productCategoryOne.id,
})
const [productCategory] = await service.listCategories({
name: "New Category"
}, {
select: ["name", "rank"]
})
const [productCategory] = await service.listCategories(
{
name: "New Category",
},
{
select: ["name", "rank"],
}
)
expect(productCategory).toEqual(
expect.objectContaining({
@@ -273,7 +283,7 @@ describe("ProductModuleService product categories", () => {
await service.createCategory({
name: "New Category",
parent_category_id: productCategoryOne.id,
rank: 0
rank: 0,
})
await service.createCategory({
@@ -281,11 +291,14 @@ describe("ProductModuleService product categories", () => {
parent_category_id: productCategoryOne.id,
})
const [productCategoryNew] = await service.listCategories({
name: "New Category 2"
}, {
select: ["name", "rank"]
})
const [productCategoryNew] = await service.listCategories(
{
name: "New Category 2",
},
{
select: ["name", "rank"],
}
)
expect(productCategoryNew).toEqual(
expect.objectContaining({
@@ -299,11 +312,14 @@ describe("ProductModuleService product categories", () => {
parent_category_id: productCategoryNew.id,
})
const [productCategoryWithParent] = await service.listCategories({
name: "New Category 2.1"
}, {
select: ["name", "rank", "parent_category_id"]
})
const [productCategoryWithParent] = await service.listCategories(
{
name: "New Category 2.1",
},
{
select: ["name", "rank", "parent_category_id"],
}
)
expect(productCategoryWithParent).toEqual(
expect.objectContaining({
@@ -342,12 +358,15 @@ describe("ProductModuleService product categories", () => {
it("should update the name of the category successfully", async () => {
await service.updateCategory(productCategoryZero.id, {
name: "New Category"
name: "New Category",
})
const productCategory = await service.retrieveCategory(productCategoryZero.id, {
select: ["name"]
})
const productCategory = await service.retrieveCategory(
productCategoryZero.id,
{
select: ["name"],
}
)
expect(productCategory.name).toEqual("New Category")
})
@@ -357,13 +376,15 @@ describe("ProductModuleService product categories", () => {
try {
await service.updateCategory("does-not-exist", {
name: "New Category"
name: "New Category",
})
} catch (e) {
error = e
}
expect(error.message).toEqual(`ProductCategory not found ({ id: 'does-not-exist' })`)
expect(error.message).toEqual(
`ProductCategory not found ({ id: 'does-not-exist' })`
)
})
it("should reorder rank successfully in the same parent", async () => {
@@ -371,11 +392,14 @@ describe("ProductModuleService product categories", () => {
rank: 0,
})
const productCategories = await service.listCategories({
parent_category_id: null
}, {
select: ["name", "rank"]
})
const productCategories = await service.listCategories(
{
parent_category_id: null,
},
{
select: ["name", "rank"],
}
)
expect(productCategories).toEqual(
expect.arrayContaining([
@@ -390,7 +414,7 @@ describe("ProductModuleService product categories", () => {
expect.objectContaining({
id: productCategoryOne.id,
rank: "2",
})
}),
])
)
})
@@ -398,14 +422,17 @@ describe("ProductModuleService product categories", () => {
it("should reorder rank successfully when changing parent", async () => {
await service.updateCategory(productCategoryTwo.id, {
rank: 0,
parent_category_id: productCategoryZero.id
parent_category_id: productCategoryZero.id,
})
const productCategories = await service.listCategories({
parent_category_id: productCategoryZero.id
}, {
select: ["name", "rank"]
})
const productCategories = await service.listCategories(
{
parent_category_id: productCategoryZero.id,
},
{
select: ["name", "rank"],
}
)
expect(productCategories).toEqual(
expect.arrayContaining([
@@ -424,7 +451,7 @@ describe("ProductModuleService product categories", () => {
expect.objectContaining({
id: productCategoryZeroTwo.id,
rank: "3",
})
}),
])
)
})
@@ -432,14 +459,17 @@ describe("ProductModuleService product categories", () => {
it("should reorder rank successfully when changing parent and in first position", async () => {
await service.updateCategory(productCategoryTwo.id, {
rank: 0,
parent_category_id: productCategoryZero.id
parent_category_id: productCategoryZero.id,
})
const productCategories = await service.listCategories({
parent_category_id: productCategoryZero.id
}, {
select: ["name", "rank"]
})
const productCategories = await service.listCategories(
{
parent_category_id: productCategoryZero.id,
},
{
select: ["name", "rank"],
}
)
expect(productCategories).toEqual(
expect.arrayContaining([
@@ -458,7 +488,7 @@ describe("ProductModuleService product categories", () => {
expect.objectContaining({
id: productCategoryZeroTwo.id,
rank: "3",
})
}),
])
)
})
@@ -492,7 +522,9 @@ describe("ProductModuleService product categories", () => {
error = e
}
expect(error.message).toEqual(`ProductCategory not found ({ id: 'does-not-exist' })`)
expect(error.message).toEqual(
`ProductCategory not found ({ id: 'does-not-exist' })`
)
})
it("should throw an error when it has children", async () => {
@@ -504,17 +536,22 @@ describe("ProductModuleService product categories", () => {
error = e
}
expect(error.message).toEqual(`Deleting ProductCategory (category-0-0) with category children is not allowed`)
expect(error.message).toEqual(
`Deleting ProductCategory (category-0-0) with category children is not allowed`
)
})
it("should reorder siblings rank successfully on deleting", async () => {
await service.deleteCategory(productCategoryOne.id)
const productCategories = await service.listCategories({
parent_category_id: null
}, {
select: ["id", "rank"]
})
const productCategories = await service.listCategories(
{
parent_category_id: null,
},
{
select: ["id", "rank"],
}
)
expect(productCategories).toEqual(
expect.arrayContaining([
@@ -525,10 +562,9 @@ describe("ProductModuleService product categories", () => {
expect.objectContaining({
id: productCategoryTwo.id,
rank: "1",
})
}),
])
)
})
})
})