feat(order): module foundation (#6399)
What: Initial Order module foundation. Basic models and methods to manage entites like in Cart Module
This commit is contained in:
@@ -6,6 +6,7 @@ export * as FeatureFlagUtils from "./feature-flags"
|
||||
export * as FulfillmentUtils from "./fulfillment"
|
||||
export * as ModulesSdkUtils from "./modules-sdk"
|
||||
export * as OrchestrationUtils from "./orchestration"
|
||||
export * as OrderUtils from "./order"
|
||||
export * as ProductUtils from "./product"
|
||||
export * as PromotionUtils from "./promotion"
|
||||
export * as SearchUtils from "./search"
|
||||
|
||||
@@ -9,11 +9,23 @@ describe("createPsqlIndexStatementHelper", function () {
|
||||
}
|
||||
|
||||
const indexStatement = createPsqlIndexStatementHelper(options)
|
||||
expect(indexStatement).toEqual(
|
||||
expect(indexStatement + "").toEqual(
|
||||
`CREATE INDEX IF NOT EXISTS "${options.name}" ON "${options.tableName}" (${options.columns})`
|
||||
)
|
||||
})
|
||||
|
||||
it("should generate a simple index and auto compose its name", function () {
|
||||
const options = {
|
||||
tableName: "table_name",
|
||||
columns: "column_name",
|
||||
}
|
||||
|
||||
const indexStatement = createPsqlIndexStatementHelper(options)
|
||||
expect(indexStatement + "").toEqual(
|
||||
`CREATE INDEX IF NOT EXISTS "IDX_table_name_column_name" ON "${options.tableName}" (${options.columns})`
|
||||
)
|
||||
})
|
||||
|
||||
it("should generate a composite index", function () {
|
||||
const options = {
|
||||
name: "index_name",
|
||||
@@ -22,7 +34,7 @@ describe("createPsqlIndexStatementHelper", function () {
|
||||
}
|
||||
|
||||
const indexStatement = createPsqlIndexStatementHelper(options)
|
||||
expect(indexStatement).toEqual(
|
||||
expect(indexStatement.expression).toEqual(
|
||||
`CREATE INDEX IF NOT EXISTS "${options.name}" ON "${
|
||||
options.tableName
|
||||
}" (${options.columns.join(", ")})`
|
||||
@@ -38,7 +50,7 @@ describe("createPsqlIndexStatementHelper", function () {
|
||||
}
|
||||
|
||||
const indexStatement = createPsqlIndexStatementHelper(options)
|
||||
expect(indexStatement).toEqual(
|
||||
expect(indexStatement.expression).toEqual(
|
||||
`CREATE INDEX IF NOT EXISTS "${options.name}" ON "${
|
||||
options.tableName
|
||||
}" (${options.columns.join(", ")}) WHERE ${options.where}`
|
||||
@@ -55,7 +67,7 @@ describe("createPsqlIndexStatementHelper", function () {
|
||||
}
|
||||
|
||||
const indexStatement = createPsqlIndexStatementHelper(options)
|
||||
expect(indexStatement).toEqual(
|
||||
expect(indexStatement.toString()).toEqual(
|
||||
`CREATE INDEX IF NOT EXISTS "${options.name}" ON "${
|
||||
options.tableName
|
||||
}" USING GIN (${options.columns.join(", ")}) WHERE ${options.where}`
|
||||
@@ -64,7 +76,6 @@ describe("createPsqlIndexStatementHelper", function () {
|
||||
|
||||
it("should generate unique constraint", function () {
|
||||
const options = {
|
||||
name: "index_name",
|
||||
tableName: "table_name",
|
||||
columns: ["column_name_1", "column_name_2"],
|
||||
unique: true,
|
||||
@@ -72,8 +83,8 @@ describe("createPsqlIndexStatementHelper", function () {
|
||||
}
|
||||
|
||||
const indexStatement = createPsqlIndexStatementHelper(options)
|
||||
expect(indexStatement).toEqual(
|
||||
`CREATE UNIQUE INDEX IF NOT EXISTS "${options.name}" ON "${
|
||||
expect(indexStatement.expression).toEqual(
|
||||
`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_table_name_column_name_1_column_name_2" ON "${
|
||||
options.tableName
|
||||
}" (${options.columns.join(", ")}) WHERE ${options.where}`
|
||||
)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { Index } from "@mikro-orm/core"
|
||||
|
||||
/**
|
||||
* Create a PSQL index statement
|
||||
* @param name The name of the index
|
||||
* @param name The name of the index, if not provided it will be generated in the format IDX_tableName_columnName
|
||||
* @param tableName The name of the table
|
||||
* @param columns The columns to index
|
||||
* @param type The type of index (e.g GIN, GIST, BTREE, etc)
|
||||
@@ -16,7 +18,7 @@
|
||||
* where: "email IS NOT NULL"
|
||||
* });
|
||||
*
|
||||
* // CREATE INDEX IF NOT EXISTS idx_user_email ON user USING btree (email) WHERE email IS NOT NULL;
|
||||
* // expression: CREATE INDEX IF NOT EXISTS idx_user_email ON user USING btree (email) WHERE email IS NOT NULL;
|
||||
*
|
||||
* createPsqlIndexStatementHelper({
|
||||
* name: "idx_user_email",
|
||||
@@ -24,7 +26,7 @@
|
||||
* columns: "email"
|
||||
* });
|
||||
*
|
||||
* // CREATE INDEX IF NOT EXISTS idx_user_email ON user (email);
|
||||
* // expression: CREATE INDEX IF NOT EXISTS idx_user_email ON user (email);
|
||||
*
|
||||
*/
|
||||
export function createPsqlIndexStatementHelper({
|
||||
@@ -35,17 +37,38 @@ export function createPsqlIndexStatementHelper({
|
||||
where,
|
||||
unique,
|
||||
}: {
|
||||
name: string
|
||||
name?: string
|
||||
tableName: string
|
||||
columns: string | string[]
|
||||
type?: string
|
||||
where?: string
|
||||
unique?: boolean
|
||||
}) {
|
||||
const columnsName = Array.isArray(columns) ? columns.join("_") : columns
|
||||
|
||||
columns = Array.isArray(columns) ? columns.join(", ") : columns
|
||||
name = name || `IDX_${tableName}_${columnsName}`
|
||||
|
||||
const typeStr = type ? ` USING ${type}` : ""
|
||||
const optionsStr = where ? ` WHERE ${where}` : ""
|
||||
const uniqueStr = unique ? "UNIQUE " : ""
|
||||
|
||||
return `CREATE ${uniqueStr}INDEX IF NOT EXISTS "${name}" ON "${tableName}"${typeStr} (${columns})${optionsStr}`
|
||||
const expression = `CREATE ${uniqueStr}INDEX IF NOT EXISTS "${name}" ON "${tableName}"${typeStr} (${columns})${optionsStr}`
|
||||
return {
|
||||
toString: () => {
|
||||
return expression
|
||||
},
|
||||
valueOf: () => {
|
||||
return expression
|
||||
},
|
||||
name,
|
||||
expression,
|
||||
MikroORMIndex: (options = {}) => {
|
||||
return Index({
|
||||
name,
|
||||
expression,
|
||||
...options,
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ export * from "./feature-flags"
|
||||
export * from "./fulfillment"
|
||||
export * from "./modules-sdk"
|
||||
export * from "./orchestration"
|
||||
export * from "./order"
|
||||
export * from "./payment"
|
||||
export * from "./pricing"
|
||||
export * from "./product"
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
export * from "./status"
|
||||
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* @enum
|
||||
*
|
||||
* The order's status.
|
||||
*/
|
||||
export enum OrderStatus {
|
||||
/**
|
||||
* The order is pending.
|
||||
*/
|
||||
PENDING = "pending",
|
||||
/**
|
||||
* The order is completed
|
||||
*/
|
||||
COMPLETED = "completed",
|
||||
/**
|
||||
* The order is a draft.
|
||||
*/
|
||||
DRAFT = "draft",
|
||||
/**
|
||||
* The order is archived.
|
||||
*/
|
||||
ARCHIVED = "archived",
|
||||
/**
|
||||
* The order is canceled.
|
||||
*/
|
||||
CANCELED = "canceled",
|
||||
/**
|
||||
* The order requires action.
|
||||
*/
|
||||
REQUIRES_ACTION = "requires_action",
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { BigNumberRawPriceInput, BigNumberRawValue } from "@medusajs/types"
|
||||
import { BigNumberInput, BigNumberRawValue } from "@medusajs/types"
|
||||
import { BigNumber as BigNumberJS } from "bignumber.js"
|
||||
import { isBigNumber, isString } from "../common"
|
||||
|
||||
@@ -8,11 +8,11 @@ export class BigNumber {
|
||||
private numeric_: number
|
||||
private raw_?: BigNumberRawValue
|
||||
|
||||
constructor(rawPrice: BigNumberRawPriceInput) {
|
||||
constructor(rawPrice: BigNumberInput) {
|
||||
this.setRawPriceOrThrow(rawPrice)
|
||||
}
|
||||
|
||||
setRawPriceOrThrow(rawPrice: BigNumberRawPriceInput) {
|
||||
setRawPriceOrThrow(rawPrice: BigNumberInput) {
|
||||
if (BigNumberJS.isBigNumber(rawPrice)) {
|
||||
/**
|
||||
* Example:
|
||||
@@ -67,7 +67,7 @@ export class BigNumber {
|
||||
}
|
||||
}
|
||||
|
||||
set numeric(value: BigNumberRawPriceInput) {
|
||||
set numeric(value: BigNumberInput) {
|
||||
const newValue = new BigNumber(value)
|
||||
this.numeric_ = newValue.numeric_
|
||||
this.raw_ = newValue.raw_
|
||||
@@ -77,7 +77,7 @@ export class BigNumber {
|
||||
return this.raw_
|
||||
}
|
||||
|
||||
set raw(rawValue: BigNumberRawPriceInput) {
|
||||
set raw(rawValue: BigNumberInput) {
|
||||
const newValue = new BigNumber(rawValue)
|
||||
this.numeric_ = newValue.numeric_
|
||||
this.raw_ = newValue.raw_
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { BigNumberRawPriceInput } from "@medusajs/types"
|
||||
import { BigNumberInput } from "@medusajs/types"
|
||||
import { BigNumber as BigNumberJs } from "bignumber.js"
|
||||
import { isDefined, toCamelCase } from "../common"
|
||||
import { BigNumber } from "./big-number"
|
||||
@@ -18,7 +18,7 @@ export function toBigNumberJs<T, V extends string>(
|
||||
): Output<V> {
|
||||
return fields.reduce((acc, field: string) => {
|
||||
const camelCased = toCamelCase(field)
|
||||
let val: BigNumberRawPriceInput = 0
|
||||
let val: BigNumberInput = 0
|
||||
|
||||
if (isDefined(entity[field])) {
|
||||
const entityField = entity[field]
|
||||
|
||||
Reference in New Issue
Block a user