feat: Cart SalesChannel link + clean-up (#6418)
This commit is contained in:
@@ -2,18 +2,18 @@ import { updateCartsWorkflow } from "@medusajs/core-flows"
|
||||
import { UpdateCartDataDTO } from "@medusajs/types"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
||||
|
||||
import { defaultStoreCartRemoteQueryObject } from "../query-config"
|
||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
||||
import { defaultStoreCartFields } from "../query-config"
|
||||
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const remoteQuery = req.scope.resolve("remoteQuery")
|
||||
|
||||
const variables = { id: req.params.id }
|
||||
|
||||
const query = {
|
||||
cart: {
|
||||
...defaultStoreCartRemoteQueryObject,
|
||||
},
|
||||
}
|
||||
const query = remoteQueryObjectFromString({
|
||||
entryPoint: "cart",
|
||||
fields: defaultStoreCartFields,
|
||||
})
|
||||
|
||||
const [cart] = await remoteQuery(query, { cart: variables })
|
||||
|
||||
|
||||
@@ -4,7 +4,36 @@ export const defaultStoreCartFields = [
|
||||
"email",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"deleted_at",
|
||||
"items.id",
|
||||
"items.created_at",
|
||||
"items.updated_at",
|
||||
"items.title",
|
||||
"items.quantity",
|
||||
"items.unit_price",
|
||||
"shipping_address.id",
|
||||
"shipping_address.first_name",
|
||||
"shipping_address.last_name",
|
||||
"shipping_address.address_1",
|
||||
"shipping_address.address_2",
|
||||
"shipping_address.city",
|
||||
"shipping_address.postal_code",
|
||||
"shipping_address.country_code",
|
||||
"shipping_address.region_code",
|
||||
"shipping_address.phone",
|
||||
"billing_address.id",
|
||||
"billing_address.first_name",
|
||||
"billing_address.last_name",
|
||||
"billing_address.address_1",
|
||||
"billing_address.address_2",
|
||||
"billing_address.city",
|
||||
"billing_address.postal_code",
|
||||
"billing_address.country_code",
|
||||
"billing_address.region_code",
|
||||
"billing_address.phone",
|
||||
"region.id",
|
||||
"region.name",
|
||||
"region.currency_code",
|
||||
"sales_channel_id",
|
||||
]
|
||||
|
||||
export const defaultStoreCartRelations = [
|
||||
@@ -15,54 +44,18 @@ export const defaultStoreCartRelations = [
|
||||
"shipping_methods",
|
||||
]
|
||||
|
||||
export const allowedRelations = [
|
||||
"items",
|
||||
"region",
|
||||
"shipping_address",
|
||||
"billing_address",
|
||||
"shipping_methods",
|
||||
"sales_channel",
|
||||
]
|
||||
|
||||
export const retrieveTransformQueryConfig = {
|
||||
defaultFields: defaultStoreCartFields,
|
||||
defaultRelations: defaultStoreCartRelations,
|
||||
allowedRelations: defaultStoreCartRelations,
|
||||
isList: false,
|
||||
}
|
||||
|
||||
export const defaultStoreCartRemoteQueryObject = {
|
||||
fields: defaultStoreCartFields,
|
||||
items: {
|
||||
fields: [
|
||||
"id",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"deleted_at",
|
||||
"title",
|
||||
"quantity",
|
||||
"unit_price",
|
||||
],
|
||||
},
|
||||
shipping_address: {
|
||||
fields: [
|
||||
"id",
|
||||
"first_name",
|
||||
"last_name",
|
||||
"address_1",
|
||||
"address_2",
|
||||
"city",
|
||||
"postal_code",
|
||||
"country_code",
|
||||
"region_code",
|
||||
"phone",
|
||||
],
|
||||
},
|
||||
billing_address: {
|
||||
fields: [
|
||||
"id",
|
||||
"first_name",
|
||||
"last_name",
|
||||
"address_1",
|
||||
"address_2",
|
||||
"city",
|
||||
"postal_code",
|
||||
"country_code",
|
||||
"region_code",
|
||||
"phone",
|
||||
],
|
||||
},
|
||||
region: {
|
||||
fields: ["id", "name", "currency_code"],
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { createCartWorkflow } from "@medusajs/core-flows"
|
||||
import { CreateCartDTO } from "@medusajs/types"
|
||||
import { remoteQueryObjectFromString } from "@medusajs/utils"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
|
||||
import { defaultStoreCartRemoteQueryObject } from "../carts/query-config"
|
||||
import { defaultStoreCartFields } from "../carts/query-config"
|
||||
|
||||
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const workflow = createCartWorkflow(req.scope)
|
||||
@@ -19,11 +20,10 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
|
||||
const variables = { id: result[0].id }
|
||||
|
||||
const query = {
|
||||
cart: {
|
||||
...defaultStoreCartRemoteQueryObject,
|
||||
},
|
||||
}
|
||||
const query = remoteQueryObjectFromString({
|
||||
entryPoint: "cart",
|
||||
fields: defaultStoreCartFields,
|
||||
})
|
||||
|
||||
const [cart] = await remoteQuery(query, { cart: variables })
|
||||
|
||||
|
||||
@@ -205,9 +205,8 @@ export default async (req, res) => {
|
||||
|
||||
return createdCart
|
||||
})
|
||||
// }
|
||||
|
||||
cart = await cartService.retrieveWithTotals(cart!.id, {
|
||||
cart = await cartService.retrieveWithTotals(cart.id, {
|
||||
select: defaultStoreCartFields,
|
||||
relations: defaultStoreCartRelations,
|
||||
})
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
import { MigrationInterface, QueryRunner } from "typeorm"
|
||||
import { MedusaV2Flag } from "@medusajs/utils"
|
||||
|
||||
import SalesChannelFeatureFlag from "../loaders/feature-flags/sales-channels"
|
||||
|
||||
export const featureFlag = [SalesChannelFeatureFlag.key, MedusaV2Flag.key]
|
||||
|
||||
export class CartSalesChannelsLink1698160215000 implements MigrationInterface {
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
CREATE TABLE IF NOT EXISTS "cart_sales_channel"
|
||||
(
|
||||
"id" character varying NOT NULL,
|
||||
"cart_id" character varying NOT NULL,
|
||||
"sales_channel_id" character varying NOT NULL,
|
||||
"created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
|
||||
"updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
|
||||
"deleted_at" TIMESTAMP WITH TIME ZONE,
|
||||
CONSTRAINT "cart_sales_channel_pk" PRIMARY KEY ("cart_id", "sales_channel_id"),
|
||||
CONSTRAINT "cart_sales_channel_cart_id_unique" UNIQUE ("cart_id")
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS "IDX_id_cart_sales_channel" ON "cart_sales_channel" ("id");
|
||||
|
||||
insert into "cart_sales_channel" (id, cart_id, sales_channel_id)
|
||||
(select 'cartsc_' || substr(md5(random()::text), 0, 27), id, sales_channel_id from "cart" WHERE sales_channel_id IS NOT NULL);
|
||||
|
||||
ALTER TABLE IF EXISTS "cart" DROP CONSTRAINT IF EXISTS "FK_a2bd3c26f42e754b9249ba78fd6";
|
||||
|
||||
ALTER TABLE IF EXISTS "store" DROP CONSTRAINT IF EXISTS "FK_61b0f48cccbb5f41c750bac7286";
|
||||
`)
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
UPDATE "cart" SET "sales_channel_id" = "cart_sales_channel"."sales_channel_id"
|
||||
FROM "cart_sales_channel"
|
||||
WHERE "cart"."id" = "cart_sales_channel"."cart_id";
|
||||
|
||||
DROP TABLE IF EXISTS "cart_sales_channel";
|
||||
|
||||
ALTER TABLE IF EXISTS "cart" ADD CONSTRAINT "FK_a2bd3c26f42e754b9249ba78fd6" FOREIGN KEY ("sales_channel_id") REFERENCES "sales_channel"("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
|
||||
|
||||
ALTER TABLE IF EXISTS "store" ADD CONSTRAINT "FK_61b0f48cccbb5f41c750bac7286" FOREIGN KEY ("default_sales_channel_id") REFERENCES "sales_channel"("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
|
||||
`)
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
import { BeforeInsert, Column, Index, PrimaryColumn } from "typeorm"
|
||||
import { MedusaV2Flag, SalesChannelFeatureFlag } from "@medusajs/utils"
|
||||
|
||||
import { generateEntityId } from "../utils"
|
||||
import { SoftDeletableEntity } from "../interfaces"
|
||||
import { FeatureFlagEntity } from "../utils/feature-flag-decorators"
|
||||
|
||||
@FeatureFlagEntity([MedusaV2Flag.key, SalesChannelFeatureFlag.key])
|
||||
export class CartSalesChannel extends SoftDeletableEntity {
|
||||
@Column()
|
||||
id: string
|
||||
|
||||
@Index("cart_sales_channel_cart_id_unique", {
|
||||
unique: true,
|
||||
})
|
||||
@PrimaryColumn()
|
||||
cart_id: string
|
||||
|
||||
@PrimaryColumn()
|
||||
sales_channel_id: string
|
||||
|
||||
/**
|
||||
* @apiIgnore
|
||||
*/
|
||||
@BeforeInsert()
|
||||
private beforeInsert(): void {
|
||||
this.id = generateEntityId(this.id, "cartsc")
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import { RemoteQueryFunction } from "@medusajs/types"
|
||||
import {
|
||||
FlagRouter,
|
||||
isDefined,
|
||||
@@ -45,6 +46,11 @@ import {
|
||||
SalesChannel,
|
||||
ShippingMethod,
|
||||
} from "../models"
|
||||
import { AddressRepository } from "../repositories/address"
|
||||
import { CartRepository } from "../repositories/cart"
|
||||
import { LineItemRepository } from "../repositories/line-item"
|
||||
import { PaymentSessionRepository } from "../repositories/payment-session"
|
||||
import { ShippingMethodRepository } from "../repositories/shipping-method"
|
||||
import {
|
||||
CartCreateProps,
|
||||
CartUpdateProps,
|
||||
@@ -59,16 +65,8 @@ import {
|
||||
TotalField,
|
||||
WithRequiredProperty,
|
||||
} from "../types/common"
|
||||
import { buildQuery, isString, setMetadata } from "../utils"
|
||||
|
||||
import { Modules, RemoteLink } from "@medusajs/modules-sdk"
|
||||
import { RemoteQueryFunction } from "@medusajs/types"
|
||||
import { AddressRepository } from "../repositories/address"
|
||||
import { CartRepository } from "../repositories/cart"
|
||||
import { LineItemRepository } from "../repositories/line-item"
|
||||
import { PaymentSessionRepository } from "../repositories/payment-session"
|
||||
import { ShippingMethodRepository } from "../repositories/shipping-method"
|
||||
import { PaymentSessionInput } from "../types/payment"
|
||||
import { buildQuery, isString, setMetadata } from "../utils"
|
||||
import { validateEmail } from "../utils/is-email"
|
||||
|
||||
type InjectedDependencies = {
|
||||
@@ -101,7 +99,6 @@ type InjectedDependencies = {
|
||||
productVariantInventoryService: ProductVariantInventoryService
|
||||
pricingService: PricingService
|
||||
remoteQuery: RemoteQueryFunction
|
||||
remoteLink: RemoteLink
|
||||
}
|
||||
|
||||
type TotalsConfig = {
|
||||
@@ -144,7 +141,6 @@ class CartService extends TransactionBaseService {
|
||||
protected readonly lineItemAdjustmentService_: LineItemAdjustmentService
|
||||
protected readonly featureFlagRouter_: FlagRouter
|
||||
protected remoteQuery_: RemoteQueryFunction
|
||||
protected remoteLink_: RemoteLink
|
||||
// eslint-disable-next-line max-len
|
||||
protected readonly productVariantInventoryService_: ProductVariantInventoryService
|
||||
protected readonly pricingService_: PricingService
|
||||
@@ -176,7 +172,6 @@ class CartService extends TransactionBaseService {
|
||||
featureFlagRouter,
|
||||
storeService,
|
||||
remoteQuery,
|
||||
remoteLink,
|
||||
productVariantInventoryService,
|
||||
pricingService,
|
||||
}: InjectedDependencies) {
|
||||
@@ -211,7 +206,6 @@ class CartService extends TransactionBaseService {
|
||||
this.productVariantInventoryService_ = productVariantInventoryService
|
||||
this.pricingService_ = pricingService
|
||||
this.remoteQuery_ = remoteQuery
|
||||
this.remoteLink_ = remoteLink
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -500,14 +494,7 @@ class CartService extends TransactionBaseService {
|
||||
data.sales_channel_id
|
||||
)
|
||||
|
||||
await this.remoteLink_.create({
|
||||
[Modules.CART]: {
|
||||
cart_id: cart.id,
|
||||
},
|
||||
[Modules.SALES_CHANNEL]: {
|
||||
sales_channel_id: salesChannel.id,
|
||||
},
|
||||
})
|
||||
cart.sales_channel_id = salesChannel.id
|
||||
}
|
||||
|
||||
await this.eventBus_
|
||||
@@ -1287,33 +1274,7 @@ class CartService extends TransactionBaseService {
|
||||
|
||||
await this.onSalesChannelChange(cart, data.sales_channel_id)
|
||||
|
||||
/**
|
||||
* TODO: remove this once update cart workflow is build
|
||||
* since this will be handled in a handler by the workflow
|
||||
*/
|
||||
if (this.featureFlagRouter_.isFeatureEnabled(MedusaV2Flag.key)) {
|
||||
if (cart.sales_channel_id) {
|
||||
await this.remoteLink_.dismiss({
|
||||
[Modules.CART]: {
|
||||
cart_id: cart.id,
|
||||
},
|
||||
[Modules.SALES_CHANNEL]: {
|
||||
sales_channel_id: cart.sales_channel_id,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
await this.remoteLink_.create({
|
||||
[Modules.CART]: {
|
||||
cart_id: cart.id,
|
||||
},
|
||||
[Modules.SALES_CHANNEL]: {
|
||||
sales_channel_id: salesChannel.id,
|
||||
},
|
||||
})
|
||||
} else {
|
||||
cart.sales_channel_id = salesChannel.id
|
||||
}
|
||||
cart.sales_channel_id = salesChannel.id
|
||||
}
|
||||
|
||||
if (isDefined(data.discounts) && data.discounts.length) {
|
||||
|
||||
Reference in New Issue
Block a user