Skip to content

Commit

Permalink
Fix all relations + indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
olivermrbl committed Feb 23, 2024
1 parent b9dbe6f commit 550e37b
Show file tree
Hide file tree
Showing 12 changed files with 282 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ describe("Carts workflows", () => {
})

describe("deleteLineItems", () => {
it.only("should delete items in cart", async () => {
it("should delete items in cart", async () => {
const cart = await cartModuleService.create({
currency_code: "usd",
items: [
Expand Down Expand Up @@ -620,7 +620,7 @@ describe("Carts workflows", () => {
})

describe("compensation", () => {
it.only("should restore line item if delete fails", async () => {
it("should restore line item if delete fails", async () => {
const workflow = deleteLineItemsWorkflow(appContainer)

workflow.appendAction("throw", deleteLineItemsStepId, {
Expand Down
18 changes: 18 additions & 0 deletions packages/cart/src/migrations/.snapshot-medusa-cart.json
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,15 @@
"nullable": false,
"mappedType": "decimal"
},
"raw_amount": {
"name": "raw_amount",
"type": "jsonb",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "json"
},
"provider_id": {
"name": "provider_id",
"type": "text",
Expand Down Expand Up @@ -1310,6 +1319,15 @@
"nullable": false,
"mappedType": "decimal"
},
"raw_amount": {
"name": "raw_amount",
"type": "jsonb",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "json"
},
"provider_id": {
"name": "provider_id",
"type": "text",
Expand Down
1 change: 1 addition & 0 deletions packages/cart/src/migrations/Migration20240222170223.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export class Migration20240222170223 extends Migration {
"promotion_id" TEXT NULL,
"code" TEXT NULL,
"amount" NUMERIC NOT NULL,
"raw_amount" JSONB NOT NULL,
"provider_id" TEXT NULL,
"metadata" JSONB NULL,
"created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
Expand Down
8 changes: 6 additions & 2 deletions packages/cart/src/models/adjustment-line.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DAL } from "@medusajs/types"
import { BigNumber, MikroOrmBigNumberProperty } from "@medusajs/utils"
import { OptionalProps, PrimaryKey, Property } from "@mikro-orm/core"

type OptionalAdjustmentLineProps = DAL.SoftDeletableEntityDateColumns
Expand All @@ -19,8 +20,11 @@ export default abstract class AdjustmentLine {
@Property({ columnType: "text", nullable: true })
code: string | null = null

@Property({ columnType: "numeric", serializer: Number })
amount: number
@MikroOrmBigNumberProperty()
amount: BigNumber | number

@Property({ columnType: "jsonb" })
raw_amount: Record<string, unknown>

@Property({ columnType: "text", nullable: true })
provider_id: string | null = null
Expand Down
121 changes: 69 additions & 52 deletions packages/cart/src/models/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,54 @@ type OptionalCartProps =
| "billing_address"
| DAL.SoftDeletableEntityDateColumns

const RegionIdIndex = createPsqlIndexStatementHelper({
name: "IDX_cart_region_id",
tableName: "cart",
columns: "region_id",
where: "deleted_at IS NULL AND region_id IS NOT NULL",
}).MikroORMIndex

const CustomerIdIndex = createPsqlIndexStatementHelper({
name: "IDX_cart_customer_id",
tableName: "cart",
columns: "customer_id",
where: "deleted_at IS NULL AND customer_id IS NOT NULL",
}).MikroORMIndex

const SalesChannelIdIndex = createPsqlIndexStatementHelper({
name: "IDX_cart_sales_channel_id",
tableName: "cart",
columns: "sales_channel_id",
where: "deleted_at IS NULL AND sales_channel_id IS NOT NULL",
}).MikroORMIndex

const CurrencyCodeIndex = createPsqlIndexStatementHelper({
name: "IDX_cart_curency_code",
tableName: "cart",
columns: "currency_code",
where: "deleted_at IS NULL",
}).MikroORMIndex

const ShippingAddressIdIndex = createPsqlIndexStatementHelper({
name: "IDX_cart_shipping_address_id",
tableName: "cart",
columns: "shipping_address_id",
where: "deleted_at IS NULL AND shipping_address_id IS NOT NULL",
}).MikroORMIndex

const BillingAddressIdIndex = createPsqlIndexStatementHelper({
name: "IDX_cart_billing_address_id",
tableName: "cart",
columns: "billing_address_id",
where: "deleted_at IS NULL AND billing_address_id IS NOT NULL",
}).MikroORMIndex

const DeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "cart",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).MikroORMIndex

@Entity({ tableName: "cart" })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class Cart {
Expand All @@ -34,77 +82,50 @@ export default class Cart {
@PrimaryKey({ columnType: "text" })
id: string

@createPsqlIndexStatementHelper({
name: "IDX_cart_region_id",
tableName: "cart",
columns: "region_id",
where: "deleted_at IS NULL AND region_id IS NOT NULL",
}).MikroORMIndex()
@RegionIdIndex()
@Property({ columnType: "text", nullable: true })
region_id: string | null = null

@createPsqlIndexStatementHelper({
name: "IDX_cart_customer_id",
tableName: "cart",
columns: "customer_id",
where: "deleted_at IS NULL AND customer_id IS NOT NULL",
}).MikroORMIndex()
@CustomerIdIndex()
@Property({ columnType: "text", nullable: true })
customer_id: string | null = null

@createPsqlIndexStatementHelper({
name: "IDX_cart_sales_channel_id",
tableName: "cart",
columns: "sales_channel_id",
where: "deleted_at IS NULL AND sales_channel_id IS NOT NULL",
}).MikroORMIndex()
@Property({
columnType: "text",
nullable: true,
index: "IDX_cart_sales_channel_id",
})
@SalesChannelIdIndex()
@Property({ columnType: "text", nullable: true })
sales_channel_id: string | null = null

@Property({ columnType: "text", nullable: true })
email: string | null = null

@Property({ columnType: "text", index: "IDX_cart_curency_code" })
@CurrencyCodeIndex()
@Property({ columnType: "text" })
currency_code: string

@createPsqlIndexStatementHelper({
name: "IDX_cart_shipping_address_id",
tableName: "cart",
columns: "shipping_address_id",
where: "deleted_at IS NULL AND shipping_address_id IS NOT NULL",
}).MikroORMIndex()
@Property({ columnType: "text", nullable: true })
shipping_address_id?: string | null

@ShippingAddressIdIndex()
@ManyToOne({
entity: () => Address,
columnType: "text",
fieldName: "shipping_address_id",
mapToPk: true,
nullable: true,
cascade: [Cascade.PERSIST],
})
shipping_address?: Address | null

@createPsqlIndexStatementHelper({
name: "IDX_cart_billing_address_id",
tableName: "cart",
columns: "billing_address_id",
where: "deleted_at IS NULL AND billing_address_id IS NOT NULL",
}).MikroORMIndex()
@Property({ columnType: "text", nullable: true })
billing_address_id?: string | null
shipping_address_id?: string | null

@ManyToOne(() => Address, { persist: false })
shipping_address: Address | null

@BillingAddressIdIndex()
@ManyToOne({
entity: () => Address,
columnType: "text",
fieldName: "billing_address_id",
mapToPk: true,
nullable: true,
index: "IDX_cart_billing_address_id",
cascade: [Cascade.PERSIST],
})
billing_address?: Address | null
billing_address_id?: string | null

@ManyToOne(() => Address, { persist: false })
billing_address: Address | null

@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null = null
Expand Down Expand Up @@ -134,11 +155,7 @@ export default class Cart {
})
updated_at: Date

@createPsqlIndexStatementHelper({
tableName: "cart",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).MikroORMIndex()
@DeletedAtIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null

Expand Down
51 changes: 29 additions & 22 deletions packages/cart/src/models/line-item-adjustment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
} from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Check,
Entity,
Filter,
Expand All @@ -16,41 +15,49 @@ import {
import AdjustmentLine from "./adjustment-line"
import LineItem from "./line-item"

const LineItemIdIndex = createPsqlIndexStatementHelper({
name: "IDX_adjustment_item_id",
tableName: "cart_line_item_adjustment",
columns: "item_id",
where: "deleted_at IS NULL",
}).MikroORMIndex

const PromotionIdIndex = createPsqlIndexStatementHelper({
name: "IDX_line_item_adjustment_promotion_id",
tableName: "cart_line_item_adjustment",
columns: "promotion_id",
where: "deleted_at IS NULL AND promotion_id IS NOT NULL",
}).MikroORMIndex

const DeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "cart_line_item_adjustment",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).MikroORMIndex

@Entity({ tableName: "cart_line_item_adjustment" })
@Check<LineItemAdjustment>({
expression: (columns) => `${columns.amount} >= 0`,
})
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class LineItemAdjustment extends AdjustmentLine {
@ManyToOne({ entity: () => LineItem, persist: false })
item: LineItem

@LineItemIdIndex()
@ManyToOne({
entity: () => LineItem,
cascade: [Cascade.REMOVE, Cascade.PERSIST, "soft-remove"] as any,
columnType: "text",
fieldName: "item_id",
mapToPk: true,
})
item: LineItem

@createPsqlIndexStatementHelper({
name: "IDX_adjustment_item_id",
tableName: "cart_line_item_adjustment",
columns: "item_id",
where: "deleted_at IS NULL",
}).MikroORMIndex()
@Property({ columnType: "text" })
item_id: string

@createPsqlIndexStatementHelper({
name: "IDX_line_item_adjustment_promotion_id",
tableName: "cart_line_item_adjustment",
columns: "promotion_id",
where: "deleted_at IS NULL and promotion_id IS NOT NULL",
}).MikroORMIndex()
@PromotionIdIndex()
@Property({ columnType: "text", nullable: true })
promotion_id: string | null = null

@createPsqlIndexStatementHelper({
tableName: "cart_line_item_adjustment",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).MikroORMIndex()
@DeletedAtIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null

Expand Down
51 changes: 29 additions & 22 deletions packages/cart/src/models/line-item-tax-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
} from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
Entity,
Filter,
ManyToOne,
Expand All @@ -15,38 +14,46 @@ import {
import LineItem from "./line-item"
import TaxLine from "./tax-line"

const LineItemIdIndex = createPsqlIndexStatementHelper({
name: "IDX_tax_line_item_id",
tableName: "cart_line_item_tax_line",
columns: "item_id",
where: "deleted_at IS NULL",
}).MikroORMIndex

const TaxRateIdIndex = createPsqlIndexStatementHelper({
name: "IDX_line_item_tax_line_tax_rate_id",
tableName: "cart_line_item_tax_line",
columns: "tax_rate_id",
where: "deleted_at IS NULL AND tax_rate_id IS NOT NULL",
}).MikroORMIndex

const DeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "cart_line_item_tax_line",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).MikroORMIndex

@Entity({ tableName: "cart_line_item_tax_line" })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class LineItemTaxLine extends TaxLine {
@ManyToOne({ entity: () => LineItem, persist: false })
item: LineItem

@LineItemIdIndex()
@ManyToOne({
entity: () => LineItem,
cascade: [Cascade.REMOVE, Cascade.PERSIST] as any,
columnType: "text",
fieldName: "item_id",
mapToPk: true,
})
item: LineItem

@createPsqlIndexStatementHelper({
name: "IDX_tax_line_item_id",
tableName: "cart_line_item_tax_line",
columns: "item_id",
where: "deleted_at IS NULL",
}).MikroORMIndex()
@Property({ columnType: "text" })
item_id: string

@createPsqlIndexStatementHelper({
name: "IDX_line_item_tax_line_tax_rate_id",
tableName: "cart_line_item_tax_line",
columns: "tax_rate_id",
where: "deleted_at IS NULL AND tax_rate_id IS NOT NULL",
}).MikroORMIndex()
@TaxRateIdIndex()
@Property({ columnType: "text", nullable: true })
tax_rate_id: string | null = null

@createPsqlIndexStatementHelper({
tableName: "cart_line_item_tax_line",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
}).MikroORMIndex()
@DeletedAtIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null

Expand Down
Loading

0 comments on commit 550e37b

Please sign in to comment.