Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(medusa, link-modules): sales channel <> product module link #5450

Merged
merged 67 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
f4f6bb3
feat: sales channel joiner config
fPolic Oct 19, 2023
4c6ebaf
feat: product sales channel link config, SC list method
fPolic Oct 23, 2023
268bf96
feat: migration
fPolic Oct 23, 2023
3898708
fix: refactor list SC
fPolic Oct 23, 2023
56c366c
refactor: SC repo api
fPolic Oct 23, 2023
a475a66
chore: changeset
fPolic Oct 24, 2023
a4c410f
feat: add dedicated FF
fPolic Oct 24, 2023
0a1ef33
feat: product<> sc join entity
fPolic Oct 30, 2023
130b5b6
fix: update case
fPolic Oct 30, 2023
6fa9879
fix: add FF on in the repository, fix tests
fPolic Oct 30, 2023
886d735
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Oct 30, 2023
6e821d5
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Oct 30, 2023
87c6b37
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Oct 31, 2023
67b3b3a
fix: assign id when FF is on
fPolic Oct 31, 2023
5e70379
fix: target table
fPolic Oct 31, 2023
b3f3cd1
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Oct 31, 2023
5bb1b3d
feat: product service - fetch SC with RQ
fPolic Nov 2, 2023
4c7eb86
Merge remote-tracking branch 'origin/feat/sales-channel-module--joine…
fPolic Nov 2, 2023
1e35ae3
feat: admin list products & SC with isolated product domain
fPolic Nov 2, 2023
98b3410
feat: get admin product
fPolic Nov 2, 2023
5c873e2
feat: store endpoints
fPolic Nov 2, 2023
8e3b450
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 2, 2023
1fd544c
fix: remove duplicate import
fPolic Nov 2, 2023
8fa7fb5
fix: remove "name" prop
fPolic Nov 3, 2023
c228d0f
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 3, 2023
c35b211
feat: refactor
fPolic Nov 3, 2023
87480fd
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 6, 2023
cbb2954
fix: product seeder if FF is on
fPolic Nov 6, 2023
02e81cd
fix: env
fPolic Nov 6, 2023
5e21190
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 8, 2023
4852a00
refactor: workflow product handlers to handle remote links
fPolic Nov 10, 2023
e219e10
fix: condition
fPolic Nov 10, 2023
1986dc0
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 13, 2023
62c97fc
fix: use correct method
fPolic Nov 13, 2023
364f41d
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 13, 2023
916fd00
fix: build
fPolic Nov 13, 2023
38ba66f
wip: update FF
fPolic Nov 13, 2023
6740977
fix: update FF in the handlers
fPolic Nov 13, 2023
976dbff
chore: migrate to medusav2 FF
fPolic Nov 14, 2023
5f3f23e
chore: uncomment test
fPolic Nov 14, 2023
b2a808a
fix: product factory
fPolic Nov 14, 2023
759d331
fix: unlinking SC and product
fPolic Nov 14, 2023
3634fba
fix: use module name variable
fPolic Nov 14, 2023
c151e7c
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 14, 2023
e6eb846
refactor: cleanup query definitions
fPolic Nov 14, 2023
02adccf
fix: add constraint
fPolic Nov 14, 2023
87fe8dc
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 15, 2023
9840608
chore: rename prop
fPolic Nov 15, 2023
2457d88
fix: add hook
fPolic Nov 15, 2023
b57b47f
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 15, 2023
2f2da2f
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 15, 2023
54a7367
fix: address comments
fPolic Nov 16, 2023
0a8029e
Merge branch 'develop' into feat/sales-channel-module--joiner-config
riqwan Nov 16, 2023
db4f52c
fix: temp sc filtering
fPolic Nov 16, 2023
2f225fd
Merge remote-tracking branch 'origin/feat/sales-channel-module--joine…
fPolic Nov 16, 2023
abcb96c
fix: use RQ to filter by SC
fPolic Nov 16, 2023
9b3f2e8
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 17, 2023
4b87417
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 23, 2023
4e7b6a7
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 23, 2023
6048fb2
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Nov 28, 2023
1401879
Merge remote-tracking branch 'origin/feat/sales-channel-module--joine…
fPolic Nov 28, 2023
a6a0da0
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Dec 4, 2023
7701814
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Dec 11, 2023
9203645
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Dec 12, 2023
fb8389b
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Dec 15, 2023
87fa07b
fix: add sc to filter to list
fPolic Dec 15, 2023
7a83b51
Merge branch 'develop' into feat/sales-channel-module--joiner-config
fPolic Dec 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/link-modules/src/definitions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./inventory-level-stock-location"
export * from "./product-variant-inventory-item"
export * from "./product-variant-price-set"
export * from "./product-shipping-profile"
export * from "./product-sales-channel"
62 changes: 62 additions & 0 deletions packages/link-modules/src/definitions/product-sales-channel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Modules } from "@medusajs/modules-sdk"
import { ModuleJoinerConfig } from "@medusajs/types"
import { LINKS } from "../links"

export const ProductSalesChannel: ModuleJoinerConfig = {
serviceName: LINKS.ProductSalesChannel,
isLink: true,
databaseConfig: {
tableName: "product_sales_channel",
idPrefix: "prodsc",
},
alias: [
{
name: "product_sales_channel",
},
{
name: "product_sales_channels",
},
],
primaryKeys: ["id", "product_id", "sales_channel_id"],
relationships: [
{
serviceName: Modules.PRODUCT,
primaryKey: "id",
foreignKey: "product_id",
alias: "product",
},
{
serviceName: "salesChannelService",
isInternalService: true,
primaryKey: "id",
foreignKey: "sales_channel_id",
alias: "sales_channel",
},
],
extends: [
{
serviceName: Modules.PRODUCT,
fieldAlias: {
sales_channels: "sales_channels_link.sales_channel",
},
relationship: {
serviceName: LINKS.ProductSalesChannel,
primaryKey: "product_id",
foreignKey: "id",
alias: "sales_channels_link",
isList: true,
},
},
{
serviceName: "salesChannelService",
relationship: {
serviceName: LINKS.ProductSalesChannel,
isInternalService: true,
primaryKey: "sales_channel_id",
foreignKey: "id",
alias: "products_link",
isList: true,
},
},
],
}
6 changes: 6 additions & 0 deletions packages/link-modules/src/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ export const LINKS = {
"shippingProfileService",
"profile_id"
),
ProductSalesChannel: composeLinkName(
Modules.PRODUCT,
"product_id",
"salesChannelService",
"sales_channel_id"
),
}
1 change: 1 addition & 0 deletions packages/medusa/src/joiner-configs/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * as cart from "./cart-service"
export * as customer from "./customer-service"
export * as region from "./region-service"
export * as salesChannel from "./sales-channel-service"
export * as shippingProfile from "./shipping-profile-service"
32 changes: 32 additions & 0 deletions packages/medusa/src/joiner-configs/sales-channel-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ModuleJoinerConfig } from "@medusajs/types"

export default {
serviceName: "salesChannelService",
primaryKeys: ["id"],
linkableKeys: { sales_channel_id: "SalesChannel" },
schema: `
scalar Date
scalar JSON

type SalesChannel {
id: ID!
name: String!
description: String!
is_disabled: Boolean
created_at: Date!
updated_at: Date!
deleted_at: Date
metadata: JSON
}
`,
alias: [
{
name: "sales_channel",
args: { entity: "SalesChannel" },
},
{
name: "sales_channels",
args: { entity: "SalesChannel" },
},
fPolic marked this conversation as resolved.
Show resolved Hide resolved
],
} as ModuleJoinerConfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { MigrationInterface, QueryRunner } from "typeorm"
import IsolateProductDomain from "../loaders/feature-flags/isolate-product-domain"

export const featureFlag = IsolateProductDomain.key
fPolic marked this conversation as resolved.
Show resolved Hide resolved

export class ProductSalesChannelsLink1698056997411
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE "product_sales_channel" ADD COLUMN IF NOT EXISTS "id" text;
UPDATE "product_sales_channel" SET "id" = 'prodsc_' || substr(md5(random()::text), 0, 27) WHERE id is NULL;
ALTER TABLE "product_sales_channel" ALTER COLUMN "id" SET NOT NULL;

ALTER TABLE "product_sales_channel" DROP CONSTRAINT IF EXISTS "PK_fd29b6a8bd641052628dee19583";
ALTER TABLE "product_sales_channel" ADD CONSTRAINT "product_sales_channel_pk" PRIMARY KEY (id);
ALTER TABLE "product_sales_channel" ADD CONSTRAINT "product_sales_channel_product_id_sales_channel_id_unique" UNIQUE (product_id, sales_channel_id);

ALTER TABLE "product_sales_channel" ADD COLUMN IF NOT EXISTS "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now();
ALTER TABLE "product_sales_channel" ADD COLUMN IF NOT EXISTS "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now();
ALTER TABLE "product_sales_channel" ADD COLUMN IF NOT EXISTS "deleted_at" TIMESTAMP WITH TIME ZONE;
`)
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE product_sales_channel DROP CONSTRAINT IF EXISTS "product_sales_channel_pk";
ALTER TABLE product_sales_channel DROP CONSTRAINT IF EXISTS "product_sales_channel_product_id_sales_channel_id_unique";
ALTER TABLE product_sales_channel drop column "id";
fPolic marked this conversation as resolved.
Show resolved Hide resolved

ALTER TABLE "product_sales_channel" DROP COLUMN IF EXISTS "created_at";
ALTER TABLE "product_sales_channel" DROP COLUMN IF EXISTS "updated_at";
ALTER TABLE "product_sales_channel" DROP COLUMN IF EXISTS "deleted_at";

ALTER TABLE product_sales_channel ADD CONSTRAINT "PK_product_sales_channel" PRIMARY KEY (product_id, sales_channel_id);
`)
}
}
32 changes: 28 additions & 4 deletions packages/medusa/src/repositories/sales-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ const productSalesChannelTable = "product_sales_channel"
export const SalesChannelRepository = dataSource
.getRepository(SalesChannel)
.extend({
async getFreeTextSearchResultsAndCount(
async getFreeTextSearchResults_(
q: string,
options: ExtendedFindConfig<SalesChannel> = {
options: ExtendedFindConfig<SalesChannel> & { withCount?: boolean } = {
where: {},
}
): Promise<[SalesChannel[], number]> {
): Promise<SalesChannel[] | [SalesChannel[], number]> {
const options_ = { ...options }

options_.where = options_.where as FindOptionsWhere<SalesChannel>
Expand Down Expand Up @@ -41,7 +41,31 @@ export const SalesChannelRepository = dataSource
qb = qb.withDeleted()
}

return await qb.getManyAndCount()
return await (options_.withCount ? qb.getManyAndCount() : qb.getMany())
},

async getFreeTextSearchResultsAndCount(
q: string,
options: ExtendedFindConfig<SalesChannel> = {
where: {},
}
): Promise<[SalesChannel[], number]> {
return (await this.getFreeTextSearchResults_(q, {
...options,
withCount: true,
})) as [SalesChannel[], number]
},

async getFreeTextSearchResults(
q: string,
options: ExtendedFindConfig<SalesChannel> = {
where: {},
}
): Promise<SalesChannel[]> {
return (await this.getFreeTextSearchResults_(
q,
options
)) as SalesChannel[]
},

async removeProducts(
Expand Down
6 changes: 2 additions & 4 deletions packages/medusa/src/services/__tests__/sales-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,9 @@ describe("SalesChannelService", () => {
...salesChannelData,
})

expect(
salesChannelRepositoryMock.findOne
).toHaveBeenLastCalledWith({
expect(salesChannelRepositoryMock.findOne).toHaveBeenLastCalledWith({
where: { id: IdMap.getId("sales_channel_1") },
relationLoadStrategy: "query"
relationLoadStrategy: "query",
})
})
})
Expand Down
35 changes: 34 additions & 1 deletion packages/medusa/src/services/sales-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ class SalesChannelService extends TransactionBaseService {
}

/**
* Lists sales channels based on the provided parameters and includes the count of
* Lists sales channels based on the provided parameters and include the count of
* sales channels that match the query.
*
* @return an array containing the sales channels as
* the first element and the total count of sales channels that matches the query
* as the second element.
Expand Down Expand Up @@ -158,6 +159,38 @@ class SalesChannelService extends TransactionBaseService {
return await salesChannelRepo.findAndCount(query)
}

/**
* Lists sales channels based on the provided parameters.
*
* @return an array containing the sales channels
*/
async list(
selector: QuerySelector<SalesChannel>,
config: FindConfig<SalesChannel> = {
skip: 0,
take: 20,
}
): Promise<SalesChannel[]> {
const salesChannelRepo = this.activeManager_.withRepository(
this.salesChannelRepository_
)

const selector_ = { ...selector }
let q: string | undefined
if ("q" in selector_) {
q = selector_.q
delete selector_.q
}

const query = buildQuery(selector_, config)

if (q) {
return await salesChannelRepo.getFreeTextSearchResults(q, query)
}

return await salesChannelRepo.find(query)
}

/**
* Creates a SalesChannel
*
Expand Down