feat(cart): Migration file (#6156)

* feat(cart): Migration file

* fixup migration

* Add indexes on cart table for currency, region, and sales channel

* fix indexes

* address PR comments
This commit is contained in:
Oli Juhl
2024-01-29 12:49:48 +01:00
committed by GitHub
parent 512b041929
commit c15438c744
10 changed files with 1530 additions and 261 deletions

View File

@@ -1,128 +0,0 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { AddressService } from "../../../../src/services"
import { MikroOrmWrapper } from "../../../utils"
import { createMedusaContainer } from "@medusajs/utils"
import { asValue } from "awilix"
import ContainerLoader from "../../../../src/loaders/container"
jest.setTimeout(30000)
describe("Address Service", () => {
let service: AddressService
let testManager: SqlEntityManager
let repositoryManager: SqlEntityManager
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
repositoryManager = await MikroOrmWrapper.forkManager()
testManager = await MikroOrmWrapper.forkManager()
const container = createMedusaContainer()
container.register("manager", asValue(repositoryManager))
await ContainerLoader({ container })
service = container.resolve("addressService")
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})
describe("create", () => {
it("should create an address successfully", async () => {
const [createdAddress] = await service.create([
{
first_name: "John",
last_name: "Doe",
address_1: "Test street 1",
city: "Test city",
country_code: "US",
postal_code: "12345",
},
])
const [address] = await service.list({ id: [createdAddress.id] })
expect(address).toEqual(
expect.objectContaining({
id: createdAddress.id,
first_name: "John",
last_name: "Doe",
address_1: "Test street 1",
city: "Test city",
country_code: "US",
postal_code: "12345",
})
)
})
})
describe("update", () => {
it("should throw an error if address does not exist", async () => {
const error = await service
.update([
{
id: "none-existing",
},
])
.catch((e) => e)
expect(error.message).toContain(
'Address with id "none-existing" not found'
)
})
it("should update an address successfully", async () => {
const [createdAddress] = await service.create([
{
first_name: "Jane",
last_name: "Doe",
address_1: "Test street 1",
city: "Test city",
country_code: "US",
postal_code: "12345",
},
])
await service.update([
{
id: createdAddress.id,
address_1: "Test street 2",
city: "Test city 2",
},
])
const [address] = await service.list({ id: [createdAddress.id] })
expect(address).toEqual(
expect.objectContaining({
id: createdAddress.id,
first_name: "Jane",
last_name: "Doe",
address_1: "Test street 2",
city: "Test city 2",
country_code: "US",
postal_code: "12345",
})
)
})
})
describe("delete", () => {
it("should delete a cart successfully", async () => {
const [createdAddress] = await service.create([
{
first_name: "Jane",
last_name: "Doe",
},
])
await service.delete([createdAddress.id])
const carts = await service.list({ id: [createdAddress.id] })
expect(carts.length).toEqual(0)
})
})
})

View File

@@ -1,9 +1,9 @@
import { Modules } from "@medusajs/modules-sdk"
import { ICartModuleService } from "@medusajs/types"
import { CheckConstraintViolationException } from "@mikro-orm/core"
import { MikroOrmWrapper } from "../../../utils"
import { Modules } from "@medusajs/modules-sdk"
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
import { initModules } from "medusa-test-utils"
import { MikroOrmWrapper } from "../../../utils"
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
jest.setTimeout(30000)

View File

@@ -1,122 +0,0 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { CartService } from "../../../../src/services"
import { createCarts } from "../../../__fixtures__/cart"
import { MikroOrmWrapper } from "../../../utils"
import { createMedusaContainer } from "@medusajs/utils"
import { asValue } from "awilix"
import ContainerLoader from "../../../../src/loaders/container"
jest.setTimeout(30000)
describe("Cart Service", () => {
let service: CartService
let testManager: SqlEntityManager
let repositoryManager: SqlEntityManager
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
repositoryManager = await MikroOrmWrapper.forkManager()
testManager = await MikroOrmWrapper.forkManager()
const container = createMedusaContainer()
container.register("manager", asValue(repositoryManager))
await ContainerLoader({ container })
service = container.resolve("cartService")
await createCarts(testManager)
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})
describe("create", () => {
it("should throw an error when required params are not passed", async () => {
const error = await service
.create([
{
email: "test@email.com",
} as any,
])
.catch((e) => e)
expect(error.message).toContain(
"Value for Cart.currency_code is required, 'undefined' found"
)
})
it("should create a cart successfully", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])
const [cart] = await service.list({ id: [createdCart.id] })
expect(cart).toEqual(
expect.objectContaining({
id: createdCart.id,
currency_code: "eur",
})
)
})
})
describe("update", () => {
it("should throw an error if cart does not exist", async () => {
const error = await service
.update([
{
id: "none-existing",
},
])
.catch((e) => e)
expect(error.message).toContain('Cart with id "none-existing" not found')
})
it("should update a cart successfully", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])
const [updatedCart] = await service.update([
{
id: createdCart.id,
email: "test@email.com",
},
])
const [cart] = await service.list({ id: [createdCart.id] })
expect(cart).toEqual(
expect.objectContaining({
id: createdCart.id,
currency_code: "eur",
email: updatedCart.email,
})
)
})
})
describe("delete", () => {
it("should delete a cart successfully", async () => {
const [createdCart] = await service.create([
{
currency_code: "eur",
},
])
await service.delete([createdCart.id])
const carts = await service.list({ id: [createdCart.id] })
expect(carts.length).toEqual(0)
})
})
})

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,189 @@
import { Migration } from "@mikro-orm/migrations"
export class CartModuleSetup20240122122952 extends Migration {
async up(): Promise<void> {
this.addSql(
`
CREATE TABLE IF NOT EXISTS "cart" (
"id" TEXT NOT NULL,
"region_id" TEXT NULL,
"customer_id" TEXT NULL,
"sales_channel_id" TEXT NULL,
"email" TEXT NULL,
"currency_code" TEXT NOT NULL,
"shipping_address_id" TEXT NULL,
"billing_address_id" TEXT NULL,
"metadata" JSONB NULL,
"created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
"updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
"deleted_at" TIMESTAMPTZ NULL,
CONSTRAINT "cart_pkey" PRIMARY KEY ("id")
);
ALTER TABLE "cart" ADD COLUMN IF NOT EXISTS "currency_code" TEXT NOT NULL;
ALTER TABLE "cart" DROP CONSTRAINT IF EXISTS "FK_242205c81c1152fab1b6e848470";
ALTER TABLE "cart" DROP CONSTRAINT IF EXISTS "FK_484c329f4783be4e18e5e2ff090";
ALTER TABLE "cart" DROP CONSTRAINT IF EXISTS "FK_6b9c66b5e36f7c827dfaa092f94";
ALTER TABLE "cart" DROP CONSTRAINT IF EXISTS "FK_9d1a161434c610aae7c3df2dc7e";
ALTER TABLE "cart" DROP CONSTRAINT IF EXISTS "FK_a2bd3c26f42e754b9249ba78fd6";
ALTER TABLE "cart" DROP CONSTRAINT IF EXISTS "FK_ced15a9a695d2b5db9dabce763d";
CREATE INDEX IF NOT EXISTS "IDX_cart_customer_id" ON "cart" ("customer_id");
CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_address_id" ON "cart" ("shipping_address_id");
CREATE INDEX IF NOT EXISTS "IDX_cart_billing_address_id" ON "cart" ("billing_address_id");
CREATE INDEX IF NOT EXISTS "IDX_cart_region_id" ON "cart" ("region_id");
CREATE INDEX IF NOT EXISTS "IDX_cart_sales_channel_id" ON "cart" ("sales_channel_id");
CREATE INDEX IF NOT EXISTS "IDX_cart_currency_code" ON "cart" ("currency_code");
CREATE TABLE IF NOT EXISTS "cart_address" (
"id" TEXT NOT NULL,
"customer_id" TEXT NULL,
"company" TEXT NULL,
"first_name" TEXT NULL,
"last_name" TEXT NULL,
"address_1" TEXT NULL,
"address_2" TEXT NULL,
"city" TEXT NULL,
"country_code" TEXT NULL,
"province" TEXT NULL,
"postal_code" TEXT NULL,
"phone" TEXT NULL,
"metadata" JSONB NULL,
"created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
"updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT "cart_address_pkey" PRIMARY KEY ("id")
);
CREATE TABLE IF NOT EXISTS "cart_line_item" (
"id" TEXT NOT NULL,
"cart_id" TEXT NOT NULL,
"title" TEXT NOT NULL,
"subtitle" TEXT NULL,
"thumbnail" TEXT NULL,
"quantity" INTEGER NOT NULL,
"variant_id" TEXT NULL,
"product_id" TEXT NULL,
"product_title" TEXT NULL,
"product_description" TEXT NULL,
"product_subtitle" TEXT NULL,
"product_type" TEXT NULL,
"product_collection" TEXT NULL,
"product_handle" TEXT NULL,
"variant_sku" TEXT NULL,
"variant_barcode" TEXT NULL,
"variant_title" TEXT NULL,
"variant_option_values" JSONB NULL,
"requires_shipping" BOOLEAN NOT NULL DEFAULT TRUE,
"is_discountable" BOOLEAN NOT NULL DEFAULT TRUE,
"is_tax_inclusive" BOOLEAN NOT NULL DEFAULT FALSE,
"compare_at_unit_price" NUMERIC NULL,
"unit_price" NUMERIC NOT NULL,
"created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
"updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT "cart_line_item_pkey" PRIMARY KEY ("id"),
CONSTRAINT cart_line_item_unit_price_check CHECK (unit_price >= 0)
);
ALTER TABLE "cart" ADD CONSTRAINT "cart_shipping_address_id_foreign" FOREIGN KEY ("shipping_address_id") REFERENCES "cart_address" ("id") ON UPDATE CASCADE ON DELETE SET NULL;
ALTER TABLE "cart" ADD CONSTRAINT "cart_billing_address_id_foreign" FOREIGN KEY ("billing_address_id") REFERENCES "cart_address" ("id") ON UPDATE CASCADE ON DELETE SET NULL;
CREATE INDEX IF NOT EXISTS "IDX_line_item_cart_id" ON "cart_line_item" ("cart_id");
CREATE INDEX IF NOT EXISTS "IDX_line_item_product_id" ON "cart_line_item" ("product_id");
CREATE INDEX IF NOT EXISTS "IDX_line_item_variant_id" ON "cart_line_item" ("variant_id");
CREATE TABLE IF NOT EXISTS "cart_line_item_adjustment" (
"id" TEXT NOT NULL,
"description" TEXT NULL,
"promotion_id" TEXT NULL,
"code" TEXT NULL,
"amount" NUMERIC NOT NULL,
"provider_id" TEXT NULL,
"created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
"updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
"item_id" TEXT NULL,
CONSTRAINT "cart_line_item_adjustment_pkey" PRIMARY KEY ("id"),
CONSTRAINT cart_line_item_adjustment_check CHECK (amount >= 0)
);
CREATE INDEX IF NOT EXISTS "IDX_adjustment_item_id" ON "cart_line_item_adjustment" ("item_id");
CREATE INDEX IF NOT EXISTS "IDX_line_item_adjustment_promotion_id" ON "cart_line_item_adjustment" ("promotion_id");
CREATE TABLE IF NOT EXISTS "cart_line_item_tax_line" (
"id" TEXT NOT NULL,
"description" TEXT NULL,
"tax_rate_id" TEXT NULL,
"code" TEXT NOT NULL,
"rate" NUMERIC NOT NULL,
"provider_id" TEXT NULL,
"created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
"updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
"item_id" TEXT NULL,
CONSTRAINT "cart_line_item_tax_line_pkey" PRIMARY KEY ("id")
);
CREATE INDEX IF NOT EXISTS "IDX_tax_line_item_id" ON "cart_line_item_tax_line" ("item_id");
CREATE INDEX IF NOT EXISTS "IDX_line_item_tax_line_tax_rate_id" ON "cart_line_item_tax_line" ("tax_rate_id");
CREATE TABLE IF NOT EXISTS "cart_shipping_method" (
"id" TEXT NOT NULL,
"cart_id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"description" JSONB NULL,
"amount" NUMERIC NOT NULL,
"is_tax_inclusive" BOOLEAN NOT NULL DEFAULT FALSE,
"shipping_option_id" TEXT NULL,
"data" JSONB NULL,
"metadata" JSONB NULL,
"created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
"updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT "cart_shipping_method_pkey" PRIMARY KEY ("id"),
CONSTRAINT cart_shipping_method_check CHECK (amount >= 0)
);
CREATE INDEX IF NOT EXISTS "IDX_shipping_method_cart_id" ON "cart_shipping_method" ("cart_id");
CREATE INDEX IF NOT EXISTS "IDX_shipping_method_option_id" ON "cart_shipping_method" ("shipping_option_id");
CREATE TABLE IF NOT EXISTS "cart_shipping_method_adjustment" (
"id" TEXT NOT NULL,
"description" TEXT NULL,
"promotion_id" TEXT NULL,
"code" TEXT NULL,
"amount" NUMERIC NOT NULL,
"provider_id" TEXT NULL,
"created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
"updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
"shipping_method_id" TEXT NULL,
CONSTRAINT "cart_shipping_method_adjustment_pkey" PRIMARY KEY ("id")
);
CREATE INDEX IF NOT EXISTS "IDX_adjustment_shipping_method_id" ON "cart_shipping_method_adjustment" ("shipping_method_id");
CREATE INDEX IF NOT EXISTS "IDX_shipping_method_adjustment_promotion_id" ON "cart_shipping_method_adjustment" ("promotion_id");
CREATE TABLE IF NOT EXISTS "cart_shipping_method_tax_line" (
"id" TEXT NOT NULL,
"description" TEXT NULL,
"tax_rate_id" TEXT NULL,
"code" TEXT NOT NULL,
"rate" NUMERIC NOT NULL,
"provider_id" TEXT NULL,
"created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
"updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
"shipping_method_id" TEXT NULL,
CONSTRAINT "cart_shipping_method_tax_line_pkey" PRIMARY KEY ("id")
);
CREATE INDEX IF NOT EXISTS "IDX_tax_line_shipping_method_id" ON "cart_shipping_method_tax_line" ("shipping_method_id");
CREATE INDEX IF NOT EXISTS "IDX_shipping_method_tax_line_tax_rate_id" ON "cart_shipping_method_tax_line" ("tax_rate_id");
ALTER TABLE "cart_line_item" ADD CONSTRAINT "cart_line_item_cart_id_foreign" FOREIGN KEY ("cart_id") REFERENCES "cart" ("id") ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE "cart_line_item_adjustment" ADD CONSTRAINT "cart_line_item_adjustment_item_id_foreign" FOREIGN KEY ("item_id") REFERENCES "cart_line_item" ("id") ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE "cart_line_item_tax_line" ADD CONSTRAINT "cart_line_item_tax_line_item_id_foreign" FOREIGN KEY ("item_id") REFERENCES "cart_line_item" ("id") ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE "cart_shipping_method" ADD CONSTRAINT "cart_shipping_method_cart_id_foreign" FOREIGN KEY ("cart_id") REFERENCES "cart" ("id") ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE "cart_shipping_method_adjustment" ADD CONSTRAINT "cart_shipping_method_adjustment_shipping_method_id_foreign" FOREIGN KEY ("shipping_method_id") REFERENCES "cart_shipping_method" ("id") ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE "cart_shipping_method_tax_line" ADD CONSTRAINT "cart_shipping_method_tax_line_shipping_method_id_foreign" FOREIGN KEY ("shipping_method_id") REFERENCES "cart_shipping_method" ("id") ON UPDATE CASCADE ON DELETE CASCADE;
`
)
}
}

View File

@@ -16,7 +16,10 @@ export default abstract class AdjustmentLine {
@Property({ columnType: "text", nullable: true })
description: string | null = null
@Property({ columnType: "text", nullable: true })
@Property({
columnType: "text",
nullable: true,
})
promotion_id: string | null = null
@Property({ columnType: "text", nullable: true })

View File

@@ -11,7 +11,7 @@ import {
OneToMany,
OptionalProps,
PrimaryKey,
Property
Property,
} from "@mikro-orm/core"
import Address from "./address"
import LineItem from "./line-item"
@@ -29,7 +29,11 @@ export default class Cart {
@PrimaryKey({ columnType: "text" })
id: string
@Property({ columnType: "text", nullable: true })
@Property({
columnType: "text",
nullable: true,
index: "IDX_cart_region_id",
})
region_id?: string | null
@Property({
@@ -39,13 +43,17 @@ export default class Cart {
})
customer_id?: string | null
@Property({ columnType: "text", nullable: true })
@Property({
columnType: "text",
nullable: true,
index: "IDX_cart_sales_channel_id",
})
sales_channel_id?: string | null
@Property({ columnType: "text", nullable: true })
email?: string | null
@Property({ columnType: "text" })
@Property({ columnType: "text", index: "IDX_cart_curency_code" })
currency_code: string
@Index({ name: "IDX_cart_shipping_address_id" })

View File

@@ -61,7 +61,11 @@ export default class LineItem {
})
variant_id?: string | null
@Property({ columnType: "text", nullable: true })
@Property({
columnType: "text",
nullable: true,
index: "IDX_line_item_product_id",
})
product_id?: string | null
@Property({ columnType: "text", nullable: true })

View File

@@ -43,7 +43,11 @@ export default class ShippingMethod {
@Property({ columnType: "boolean" })
is_tax_inclusive = false
@Property({ columnType: "text", nullable: true })
@Property({
columnType: "text",
nullable: true,
index: "IDX_shipping_method_option_id",
})
shipping_option_id?: string | null
@Property({ columnType: "jsonb", nullable: true })

View File

@@ -11,7 +11,10 @@ export default abstract class TaxLine {
@Property({ columnType: "text", nullable: true })
description?: string | null
@Property({ columnType: "text", nullable: true })
@Property({
columnType: "text",
nullable: true,
})
tax_rate_id?: string | null
@Property({ columnType: "text" })