feat: Typescript for API layer (#817)
Co-authored-by: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com> Co-authored-by: Zakaria El Asri <33696020+zakariaelas@users.noreply.github.com> Co-authored-by: Kasper Fabricius Kristensen <45367945+kasperkristensen@users.noreply.github.com> Co-authored-by: Philip Korsholm <philip.korsholm@hotmail.com> Co-authored-by: Sebastian Rindom <seb@medusa-commerce.com>
This commit is contained in:
co-authored by
Philip Korsholm
Zakaria El Asri
Kasper Fabricius Kristensen
Philip Korsholm
Sebastian Rindom
parent
55e200bf68
commit
373532ecbc
@@ -83,37 +83,33 @@
|
||||
*/
|
||||
|
||||
import {
|
||||
Entity,
|
||||
AfterLoad,
|
||||
BeforeInsert,
|
||||
Index,
|
||||
Column,
|
||||
DeleteDateColumn,
|
||||
CreateDateColumn,
|
||||
UpdateDateColumn,
|
||||
PrimaryColumn,
|
||||
OneToOne,
|
||||
OneToMany,
|
||||
ManyToOne,
|
||||
ManyToMany,
|
||||
DeleteDateColumn,
|
||||
Entity,
|
||||
Index,
|
||||
JoinColumn,
|
||||
JoinTable,
|
||||
AfterLoad,
|
||||
Timestamp,
|
||||
BeforeUpdate,
|
||||
ManyToMany,
|
||||
ManyToOne,
|
||||
OneToMany,
|
||||
OneToOne,
|
||||
PrimaryColumn,
|
||||
UpdateDateColumn,
|
||||
} from "typeorm"
|
||||
import { ulid } from "ulid"
|
||||
import { resolveDbType, DbAwareColumn } from "../utils/db-aware-column"
|
||||
|
||||
import { Region } from "./region"
|
||||
import { DbAwareColumn, resolveDbType } from "../utils/db-aware-column"
|
||||
import { Address } from "./address"
|
||||
import { LineItem } from "./line-item"
|
||||
import { Discount } from "./discount"
|
||||
import { Customer } from "./customer"
|
||||
import { PaymentSession } from "./payment-session"
|
||||
import { Payment } from "./payment"
|
||||
import { Discount } from "./discount"
|
||||
import { GiftCard } from "./gift-card"
|
||||
import { LineItem } from "./line-item"
|
||||
import { Payment } from "./payment"
|
||||
import { PaymentSession } from "./payment-session"
|
||||
import { Region } from "./region"
|
||||
import { ShippingMethod } from "./shipping-method"
|
||||
import { CustomShippingOption } from "./custom-shipping-option"
|
||||
|
||||
export enum CartType {
|
||||
DEFAULT = "default",
|
||||
@@ -151,11 +147,9 @@ export class Cart {
|
||||
@JoinColumn({ name: "shipping_address_id" })
|
||||
shipping_address: Address
|
||||
|
||||
@OneToMany(
|
||||
() => LineItem,
|
||||
lineItem => lineItem.cart,
|
||||
{ cascade: ["insert", "remove"] }
|
||||
)
|
||||
@OneToMany(() => LineItem, (lineItem) => lineItem.cart, {
|
||||
cascade: ["insert", "remove"],
|
||||
})
|
||||
items: LineItem[]
|
||||
|
||||
@Index()
|
||||
@@ -204,11 +198,9 @@ export class Cart {
|
||||
|
||||
payment_session: PaymentSession
|
||||
|
||||
@OneToMany(
|
||||
() => PaymentSession,
|
||||
paymentSession => paymentSession.cart,
|
||||
{ cascade: true }
|
||||
)
|
||||
@OneToMany(() => PaymentSession, (paymentSession) => paymentSession.cart, {
|
||||
cascade: true,
|
||||
})
|
||||
payment_sessions: PaymentSession[]
|
||||
|
||||
@Index()
|
||||
@@ -219,11 +211,9 @@ export class Cart {
|
||||
@JoinColumn({ name: "payment_id" })
|
||||
payment: Payment
|
||||
|
||||
@OneToMany(
|
||||
() => ShippingMethod,
|
||||
method => method.cart,
|
||||
{ cascade: ["soft-remove", "remove"] }
|
||||
)
|
||||
@OneToMany(() => ShippingMethod, (method) => method.cart, {
|
||||
cascade: ["soft-remove", "remove"],
|
||||
})
|
||||
shipping_methods: ShippingMethod[]
|
||||
|
||||
@DbAwareColumn({ type: "enum", enum: CartType, default: "default" })
|
||||
@@ -264,16 +254,18 @@ export class Cart {
|
||||
gift_card_total: number
|
||||
|
||||
@BeforeInsert()
|
||||
private beforeInsert() {
|
||||
if (this.id) return
|
||||
private beforeInsert(): undefined | void {
|
||||
if (this.id) {
|
||||
return
|
||||
}
|
||||
const id = ulid()
|
||||
this.id = `cart_${id}`
|
||||
}
|
||||
|
||||
@AfterLoad()
|
||||
private afterLoad() {
|
||||
private afterLoad(): void {
|
||||
if (this.payment_sessions) {
|
||||
this.payment_session = this.payment_sessions.find(p => p.is_selected)
|
||||
this.payment_session = this.payment_sessions.find((p) => p.is_selected)!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,22 @@
|
||||
import {
|
||||
Entity,
|
||||
Generated,
|
||||
BeforeInsert,
|
||||
Index,
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
UpdateDateColumn,
|
||||
PrimaryColumn,
|
||||
OneToOne,
|
||||
Entity,
|
||||
Generated,
|
||||
Index,
|
||||
JoinColumn,
|
||||
OneToOne,
|
||||
PrimaryColumn,
|
||||
UpdateDateColumn,
|
||||
} from "typeorm"
|
||||
import { ulid } from "ulid"
|
||||
import {
|
||||
resolveDbType,
|
||||
resolveDbGenerationStrategy,
|
||||
DbAwareColumn,
|
||||
resolveDbGenerationStrategy,
|
||||
resolveDbType,
|
||||
} from "../utils/db-aware-column"
|
||||
import { manualAutoIncrement } from "../utils/manual-auto-increment"
|
||||
|
||||
import { Cart } from "./cart"
|
||||
import { Order } from "./order"
|
||||
|
||||
@@ -77,14 +76,18 @@ export class DraftOrder {
|
||||
idempotency_key: string
|
||||
|
||||
@BeforeInsert()
|
||||
private async beforeInsert() {
|
||||
private async beforeInsert(): Promise<void> {
|
||||
if (!this.id) {
|
||||
const id = ulid()
|
||||
this.id = `dorder_${id}`
|
||||
}
|
||||
|
||||
if (process.env.NODE_ENV === "development" && !this.display_id) {
|
||||
this.display_id = await manualAutoIncrement("draft_order")
|
||||
const disId = await manualAutoIncrement("draft_order")
|
||||
|
||||
if (disId) {
|
||||
this.display_id = disId
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,17 @@
|
||||
import {
|
||||
Entity,
|
||||
BeforeInsert,
|
||||
Column,
|
||||
DeleteDateColumn,
|
||||
CreateDateColumn,
|
||||
UpdateDateColumn,
|
||||
DeleteDateColumn,
|
||||
Entity,
|
||||
Index,
|
||||
RelationId,
|
||||
PrimaryColumn,
|
||||
OneToOne,
|
||||
OneToMany,
|
||||
ManyToOne,
|
||||
ManyToMany,
|
||||
JoinColumn,
|
||||
JoinTable,
|
||||
ManyToOne,
|
||||
PrimaryColumn,
|
||||
UpdateDateColumn,
|
||||
} from "typeorm"
|
||||
import { ulid } from "ulid"
|
||||
import { resolveDbType, DbAwareColumn } from "../utils/db-aware-column"
|
||||
|
||||
import { resolveDbType } from "../utils/db-aware-column"
|
||||
import { Currency } from "./currency"
|
||||
import { ProductVariant } from "./product-variant"
|
||||
import { Region } from "./region"
|
||||
@@ -38,17 +32,15 @@ export class MoneyAmount {
|
||||
amount: number
|
||||
|
||||
@Column({ type: "int", nullable: true, default: null })
|
||||
sale_amount: number
|
||||
sale_amount?: number
|
||||
|
||||
@Index()
|
||||
@Column({ nullable: true })
|
||||
variant_id: string
|
||||
|
||||
@ManyToOne(
|
||||
() => ProductVariant,
|
||||
variant => variant.prices,
|
||||
{ onDelete: "cascade" }
|
||||
)
|
||||
@ManyToOne(() => ProductVariant, (variant) => variant.prices, {
|
||||
onDelete: "CASCADE",
|
||||
})
|
||||
@JoinColumn({ name: "variant_id" })
|
||||
variant: ProductVariant
|
||||
|
||||
@@ -70,8 +62,10 @@ export class MoneyAmount {
|
||||
deleted_at: Date
|
||||
|
||||
@BeforeInsert()
|
||||
private beforeInsert() {
|
||||
if (this.id) return
|
||||
private beforeInsert(): undefined | void {
|
||||
if (this.id) {
|
||||
return
|
||||
}
|
||||
const id = ulid()
|
||||
this.id = `ma_${id}`
|
||||
}
|
||||
|
||||
@@ -1,44 +1,43 @@
|
||||
import {
|
||||
Entity,
|
||||
Generated,
|
||||
BeforeInsert,
|
||||
Index,
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
UpdateDateColumn,
|
||||
PrimaryColumn,
|
||||
OneToOne,
|
||||
OneToMany,
|
||||
ManyToOne,
|
||||
ManyToMany,
|
||||
Entity,
|
||||
Generated,
|
||||
Index,
|
||||
JoinColumn,
|
||||
JoinTable,
|
||||
ManyToMany,
|
||||
ManyToOne,
|
||||
OneToMany,
|
||||
OneToOne,
|
||||
PrimaryColumn,
|
||||
UpdateDateColumn,
|
||||
} from "typeorm"
|
||||
import { ulid } from "ulid"
|
||||
import {
|
||||
resolveDbType,
|
||||
resolveDbGenerationStrategy,
|
||||
DbAwareColumn,
|
||||
resolveDbGenerationStrategy,
|
||||
resolveDbType,
|
||||
} from "../utils/db-aware-column"
|
||||
import { manualAutoIncrement } from "../utils/manual-auto-increment"
|
||||
|
||||
import { Address } from "./address"
|
||||
import { LineItem } from "./line-item"
|
||||
import { Cart } from "./cart"
|
||||
import { ClaimOrder } from "./claim-order"
|
||||
import { Currency } from "./currency"
|
||||
import { Customer } from "./customer"
|
||||
import { Region } from "./region"
|
||||
import { Discount } from "./discount"
|
||||
import { DraftOrder } from "./draft-order"
|
||||
import { Fulfillment } from "./fulfillment"
|
||||
import { GiftCard } from "./gift-card"
|
||||
import { GiftCardTransaction } from "./gift-card-transaction"
|
||||
import { LineItem } from "./line-item"
|
||||
import { Payment } from "./payment"
|
||||
import { Cart } from "./cart"
|
||||
import { Fulfillment } from "./fulfillment"
|
||||
import { Return } from "./return"
|
||||
import { Refund } from "./refund"
|
||||
import { Swap } from "./swap"
|
||||
import { ClaimOrder } from "./claim-order"
|
||||
import { Region } from "./region"
|
||||
import { Return } from "./return"
|
||||
import { ShippingMethod } from "./shipping-method"
|
||||
import { DraftOrder } from "./draft-order"
|
||||
import { Swap } from "./swap"
|
||||
|
||||
export enum OrderStatus {
|
||||
PENDING = "pending",
|
||||
@@ -174,53 +173,29 @@ export class Order {
|
||||
})
|
||||
gift_cards: GiftCard[]
|
||||
|
||||
@OneToMany(
|
||||
() => ShippingMethod,
|
||||
method => method.order,
|
||||
{ cascade: ["insert"] }
|
||||
)
|
||||
@OneToMany(() => ShippingMethod, (method) => method.order, {
|
||||
cascade: ["insert"],
|
||||
})
|
||||
shipping_methods: ShippingMethod[]
|
||||
|
||||
@OneToMany(
|
||||
() => Payment,
|
||||
payment => payment.order,
|
||||
{ cascade: ["insert"] }
|
||||
)
|
||||
@OneToMany(() => Payment, (payment) => payment.order, { cascade: ["insert"] })
|
||||
payments: Payment[]
|
||||
|
||||
@OneToMany(
|
||||
() => Fulfillment,
|
||||
fulfillment => fulfillment.order,
|
||||
{ cascade: ["insert"] }
|
||||
)
|
||||
@OneToMany(() => Fulfillment, (fulfillment) => fulfillment.order, {
|
||||
cascade: ["insert"],
|
||||
})
|
||||
fulfillments: Fulfillment[]
|
||||
|
||||
@OneToMany(
|
||||
() => Return,
|
||||
ret => ret.order,
|
||||
{ cascade: ["insert"] }
|
||||
)
|
||||
@OneToMany(() => Return, (ret) => ret.order, { cascade: ["insert"] })
|
||||
returns: Return[]
|
||||
|
||||
@OneToMany(
|
||||
() => ClaimOrder,
|
||||
co => co.order,
|
||||
{ cascade: ["insert"] }
|
||||
)
|
||||
@OneToMany(() => ClaimOrder, (co) => co.order, { cascade: ["insert"] })
|
||||
claims: ClaimOrder[]
|
||||
|
||||
@OneToMany(
|
||||
() => Refund,
|
||||
ref => ref.order,
|
||||
{ cascade: ["insert"] }
|
||||
)
|
||||
@OneToMany(() => Refund, (ref) => ref.order, { cascade: ["insert"] })
|
||||
refunds: Refund[]
|
||||
|
||||
@OneToMany(
|
||||
() => Swap,
|
||||
swap => swap.order,
|
||||
{ cascade: ["insert"] }
|
||||
)
|
||||
@OneToMany(() => Swap, (swap) => swap.order, { cascade: ["insert"] })
|
||||
swaps: Swap[]
|
||||
|
||||
@Column({ nullable: true })
|
||||
@@ -230,17 +205,12 @@ export class Order {
|
||||
@JoinColumn({ name: "draft_order_id" })
|
||||
draft_order: DraftOrder
|
||||
|
||||
@OneToMany(
|
||||
() => LineItem,
|
||||
lineItem => lineItem.order,
|
||||
{ cascade: ["insert"] }
|
||||
)
|
||||
@OneToMany(() => LineItem, (lineItem) => lineItem.order, {
|
||||
cascade: ["insert"],
|
||||
})
|
||||
items: LineItem[]
|
||||
|
||||
@OneToMany(
|
||||
() => GiftCardTransaction,
|
||||
gc => gc.order
|
||||
)
|
||||
@OneToMany(() => GiftCardTransaction, (gc) => gc.order)
|
||||
gift_card_transactions: GiftCardTransaction[]
|
||||
|
||||
@Column({ nullable: true, type: resolveDbType("timestamptz") })
|
||||
@@ -256,7 +226,7 @@ export class Order {
|
||||
metadata: any
|
||||
|
||||
@Column({ type: "boolean", nullable: true })
|
||||
no_notification: Boolean
|
||||
no_notification: boolean
|
||||
|
||||
@Column({ nullable: true })
|
||||
idempotency_key: string
|
||||
@@ -273,14 +243,18 @@ export class Order {
|
||||
gift_card_total: number
|
||||
|
||||
@BeforeInsert()
|
||||
private async beforeInsert() {
|
||||
private async beforeInsert(): Promise<void> {
|
||||
if (!this.id) {
|
||||
const id = ulid()
|
||||
this.id = `order_${id}`
|
||||
}
|
||||
|
||||
if (process.env.NODE_ENV === "development" && !this.display_id) {
|
||||
this.display_id = await manualAutoIncrement("order")
|
||||
const disId = await manualAutoIncrement("order")
|
||||
|
||||
if (disId) {
|
||||
this.display_id = disId
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
import {
|
||||
BeforeInsert,
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
DeleteDateColumn,
|
||||
Entity,
|
||||
Index,
|
||||
JoinColumn,
|
||||
BeforeInsert,
|
||||
DeleteDateColumn,
|
||||
CreateDateColumn,
|
||||
UpdateDateColumn,
|
||||
ManyToOne,
|
||||
Column,
|
||||
PrimaryColumn,
|
||||
UpdateDateColumn,
|
||||
} from "typeorm"
|
||||
import { ulid } from "ulid"
|
||||
import { resolveDbType, DbAwareColumn } from "../utils/db-aware-column"
|
||||
|
||||
import { DbAwareColumn, resolveDbType } from "../utils/db-aware-column"
|
||||
import { ProductOption } from "./product-option"
|
||||
import { ProductVariant } from "./product-variant"
|
||||
|
||||
@@ -28,10 +27,7 @@ export class ProductOptionValue {
|
||||
@Column()
|
||||
option_id: string
|
||||
|
||||
@ManyToOne(
|
||||
() => ProductOption,
|
||||
option => option.values
|
||||
)
|
||||
@ManyToOne(() => ProductOption, (option) => option.values)
|
||||
@JoinColumn({ name: "option_id" })
|
||||
option: ProductOption
|
||||
|
||||
@@ -39,11 +35,9 @@ export class ProductOptionValue {
|
||||
@Column()
|
||||
variant_id: string
|
||||
|
||||
@ManyToOne(
|
||||
() => ProductVariant,
|
||||
variant => variant.options,
|
||||
{ onDelete: "cascade" }
|
||||
)
|
||||
@ManyToOne(() => ProductVariant, (variant) => variant.options, {
|
||||
onDelete: "CASCADE",
|
||||
})
|
||||
@JoinColumn({ name: "variant_id" })
|
||||
variant: ProductVariant
|
||||
|
||||
@@ -60,8 +54,10 @@ export class ProductOptionValue {
|
||||
metadata: any
|
||||
|
||||
@BeforeInsert()
|
||||
private beforeInsert() {
|
||||
if (this.id) return
|
||||
private beforeInsert(): void | undefined {
|
||||
if (this.id) {
|
||||
return
|
||||
}
|
||||
const id = ulid()
|
||||
this.id = `optval_${id}`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user