Skip to content

Commit 965adfe

Browse files
authored
feat(settings): view configuration enhancement (#14650)
* feat(view-configuration): Enhance view configuration to be more generic * some cleanup, integration tests and fixes * apply feature flag * cleanup * cleanup * mopre dynamic display field * semantic to category mapping and integration tests * cleanup + singularize util * middleware * middleware * cleanup * export settings util to acces registry * handle comments * entity override registry * property label and description translatable * Create gold-grapes-roll.md
1 parent 782957b commit 965adfe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+6138
-930
lines changed

.changeset/gold-grapes-roll.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@medusajs/medusa": patch
3+
"@medusajs/core-flows": patch
4+
"@medusajs/types": patch
5+
"@medusajs/utils": patch
6+
"@medusajs/settings": patch
7+
---
8+
9+
Feat(view configuration): Generic introspection and generation

integration-tests/http/__tests__/views/admin/columns.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ medusaIntegrationTestRunner({
5454
field: "display_id",
5555
data_type: "string",
5656
semantic_type: "identifier",
57-
context: "order",
57+
context: "both",
5858
})
5959

6060
// Check for missing relationships
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { SettingsTypes } from "@medusajs/framework/types"
2+
import { Modules } from "@medusajs/framework/utils"
3+
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"
4+
5+
export interface CreatePropertyLabelsStepInput {
6+
property_labels: {
7+
entity: string
8+
property: string
9+
label: string
10+
description?: string
11+
}[]
12+
}
13+
14+
export const createPropertyLabelsStepId = "create-property-labels"
15+
16+
/**
17+
* @since 2.13.2
18+
* @featureFlag view_configurations
19+
*/
20+
export const createPropertyLabelsStep = createStep(
21+
createPropertyLabelsStepId,
22+
async (
23+
data: CreatePropertyLabelsStepInput,
24+
{ container }
25+
): Promise<StepResponse<SettingsTypes.PropertyLabelDTO[], string[]>> => {
26+
if (!data.property_labels?.length) {
27+
return new StepResponse([], [])
28+
}
29+
30+
const service = container.resolve<SettingsTypes.ISettingsModuleService>(
31+
Modules.SETTINGS
32+
)
33+
34+
const created = await service.createPropertyLabels(data.property_labels)
35+
const createdArray = Array.isArray(created) ? created : [created]
36+
37+
return new StepResponse(
38+
createdArray,
39+
createdArray.map((c) => c.id)
40+
)
41+
},
42+
async (createdIds, { container }) => {
43+
if (!createdIds?.length) {
44+
return
45+
}
46+
47+
const service = container.resolve<SettingsTypes.ISettingsModuleService>(
48+
Modules.SETTINGS
49+
)
50+
await service.deletePropertyLabels(createdIds)
51+
}
52+
)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { SettingsTypes } from "@medusajs/framework/types"
2+
import { Modules } from "@medusajs/framework/utils"
3+
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"
4+
5+
export interface DeletePropertyLabelsStepInput {
6+
ids: string[]
7+
}
8+
9+
export const deletePropertyLabelsStepId = "delete-property-labels"
10+
11+
/**
12+
* @since 2.13.2
13+
* @featureFlag view_configurations
14+
*/
15+
export const deletePropertyLabelsStep = createStep(
16+
deletePropertyLabelsStepId,
17+
async (
18+
data: DeletePropertyLabelsStepInput,
19+
{ container }
20+
): Promise<StepResponse<void, SettingsTypes.PropertyLabelDTO[]>> => {
21+
if (!data.ids?.length) {
22+
return new StepResponse(void 0, [])
23+
}
24+
25+
const service = container.resolve<SettingsTypes.ISettingsModuleService>(
26+
Modules.SETTINGS
27+
)
28+
29+
const existing = await service.listPropertyLabels({
30+
id: data.ids,
31+
})
32+
33+
await service.deletePropertyLabels(data.ids)
34+
35+
return new StepResponse(void 0, existing)
36+
},
37+
async (previousLabels, { container }) => {
38+
if (!previousLabels?.length) {
39+
return
40+
}
41+
42+
const service = container.resolve<SettingsTypes.ISettingsModuleService>(
43+
Modules.SETTINGS
44+
)
45+
46+
await service.createPropertyLabels(
47+
previousLabels.map((label) => ({
48+
entity: label.entity,
49+
property: label.property,
50+
label: label.label,
51+
description: label.description ?? undefined,
52+
}))
53+
)
54+
}
55+
)
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
export * from "./create-view-configuration"
22
export * from "./update-view-configuration"
3-
export * from "./set-active-view-configuration"
3+
export * from "./set-active-view-configuration"
4+
export * from "./create-property-label"
5+
export * from "./update-property-label"
6+
export * from "./delete-property-labels"
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { SettingsTypes } from "@medusajs/framework/types"
2+
import { Modules } from "@medusajs/framework/utils"
3+
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"
4+
5+
export interface UpdatePropertyLabelsStepInput {
6+
property_labels: {
7+
id: string
8+
label?: string
9+
description?: string
10+
}[]
11+
}
12+
13+
interface UpdatePropertyLabelsCompensateData {
14+
previous: { id: string; label: string; description: string | null }[]
15+
}
16+
17+
export const updatePropertyLabelsStepId = "update-property-labels"
18+
19+
/**
20+
* @since 2.13.2
21+
* @featureFlag view_configurations
22+
*/
23+
export const updatePropertyLabelsStep = createStep(
24+
updatePropertyLabelsStepId,
25+
async (
26+
data: UpdatePropertyLabelsStepInput,
27+
{ container }
28+
): Promise<
29+
StepResponse<
30+
SettingsTypes.PropertyLabelDTO[],
31+
UpdatePropertyLabelsCompensateData
32+
>
33+
> => {
34+
if (!data.property_labels?.length) {
35+
return new StepResponse([], { previous: [] })
36+
}
37+
38+
const service = container.resolve<SettingsTypes.ISettingsModuleService>(
39+
Modules.SETTINGS
40+
)
41+
42+
const ids = data.property_labels.map((p) => p.id)
43+
const existing = await service.listPropertyLabels({ id: ids })
44+
45+
const previous = existing.map((e) => ({
46+
id: e.id,
47+
label: e.label,
48+
description: e.description,
49+
}))
50+
51+
const updated = await service.updatePropertyLabels(data.property_labels)
52+
53+
return new StepResponse(updated, { previous })
54+
},
55+
async (compensateData, { container }) => {
56+
if (!compensateData?.previous?.length) {
57+
return
58+
}
59+
60+
const service = container.resolve<SettingsTypes.ISettingsModuleService>(
61+
Modules.SETTINGS
62+
)
63+
64+
const restoreData = compensateData.previous.map((prev) => ({
65+
id: prev.id,
66+
label: prev.label,
67+
description: prev.description ?? undefined,
68+
}))
69+
70+
await service.updatePropertyLabels(restoreData)
71+
}
72+
)
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import {
2+
BatchWorkflowInput,
3+
BatchWorkflowOutput,
4+
SettingsTypes,
5+
} from "@medusajs/framework/types"
6+
import {
7+
WorkflowData,
8+
WorkflowResponse,
9+
createWorkflow,
10+
parallelize,
11+
transform,
12+
when,
13+
} from "@medusajs/framework/workflows-sdk"
14+
import { createPropertyLabelsWorkflow } from "./create-property-label"
15+
import { deletePropertyLabelsWorkflow } from "./delete-property-labels"
16+
import { updatePropertyLabelsWorkflow } from "./update-property-label"
17+
18+
/**
19+
* Input type for creating a property label in batch.
20+
*/
21+
export interface BatchPropertyLabelCreateInput {
22+
entity: string
23+
property: string
24+
label: string
25+
description?: string
26+
}
27+
28+
/**
29+
* Input type for updating a property label in batch.
30+
*/
31+
export interface BatchPropertyLabelUpdateInput {
32+
id: string
33+
label?: string
34+
description?: string
35+
}
36+
37+
/**
38+
* The property labels to manage.
39+
*/
40+
export interface BatchPropertyLabelsWorkflowInput
41+
extends BatchWorkflowInput<
42+
BatchPropertyLabelCreateInput,
43+
BatchPropertyLabelUpdateInput
44+
> {}
45+
46+
export type BatchPropertyLabelsWorkflowOutput =
47+
BatchWorkflowOutput<SettingsTypes.PropertyLabelDTO>
48+
49+
const conditionallyCreatePropertyLabels = (
50+
input: BatchPropertyLabelsWorkflowInput
51+
) =>
52+
when({ input }, ({ input }) => !!input.create?.length).then(() =>
53+
createPropertyLabelsWorkflow.runAsStep({
54+
input: { property_labels: input.create! },
55+
})
56+
)
57+
58+
const conditionallyUpdatePropertyLabels = (
59+
input: BatchPropertyLabelsWorkflowInput
60+
) =>
61+
when({ input }, ({ input }) => !!input.update?.length).then(() =>
62+
updatePropertyLabelsWorkflow.runAsStep({
63+
input: { property_labels: input.update! },
64+
})
65+
)
66+
67+
const conditionallyDeletePropertyLabels = (
68+
input: BatchPropertyLabelsWorkflowInput
69+
) =>
70+
when({ input }, ({ input }) => !!input.delete?.length).then(() =>
71+
deletePropertyLabelsWorkflow.runAsStep({
72+
input: { ids: input.delete! },
73+
})
74+
)
75+
76+
export const batchPropertyLabelsWorkflowId = "batch-property-labels"
77+
78+
/**
79+
* This workflow creates, updates, or deletes property labels in batch.
80+
*
81+
* @since 2.13.2
82+
* @featureFlag view_configurations
83+
*/
84+
export const batchPropertyLabelsWorkflow = createWorkflow(
85+
batchPropertyLabelsWorkflowId,
86+
(
87+
input: WorkflowData<BatchPropertyLabelsWorkflowInput>
88+
): WorkflowResponse<BatchPropertyLabelsWorkflowOutput> => {
89+
const res = parallelize(
90+
conditionallyCreatePropertyLabels(input),
91+
conditionallyUpdatePropertyLabels(input),
92+
conditionallyDeletePropertyLabels(input)
93+
)
94+
95+
return new WorkflowResponse(
96+
transform({ res, input }, (data) => {
97+
return {
98+
created: data.res[0] ?? [],
99+
updated: data.res[1] ?? [],
100+
deleted: data.input.delete ?? [],
101+
}
102+
})
103+
)
104+
}
105+
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { SettingsTypes } from "@medusajs/framework/types"
2+
import {
3+
WorkflowData,
4+
WorkflowResponse,
5+
createWorkflow,
6+
} from "@medusajs/framework/workflows-sdk"
7+
import { createPropertyLabelsStep } from "../steps"
8+
9+
export interface CreatePropertyLabelsWorkflowInput {
10+
property_labels: {
11+
entity: string
12+
property: string
13+
label: string
14+
description?: string
15+
}[]
16+
}
17+
18+
export const createPropertyLabelsWorkflowId = "create-property-labels"
19+
20+
/**
21+
* @since 2.13.2
22+
* @featureFlag view_configurations
23+
*/
24+
export const createPropertyLabelsWorkflow = createWorkflow(
25+
createPropertyLabelsWorkflowId,
26+
(
27+
input: WorkflowData<CreatePropertyLabelsWorkflowInput>
28+
): WorkflowResponse<SettingsTypes.PropertyLabelDTO[]> => {
29+
const propertyLabels = createPropertyLabelsStep(input)
30+
31+
return new WorkflowResponse(propertyLabels)
32+
}
33+
)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {
2+
WorkflowData,
3+
WorkflowResponse,
4+
createWorkflow,
5+
} from "@medusajs/framework/workflows-sdk"
6+
import { deletePropertyLabelsStep } from "../steps"
7+
8+
export interface DeletePropertyLabelsWorkflowInput {
9+
ids: string[]
10+
}
11+
12+
export const deletePropertyLabelsWorkflowId = "delete-property-labels"
13+
14+
/**
15+
* @since 2.13.2
16+
* @featureFlag view_configurations
17+
*/
18+
export const deletePropertyLabelsWorkflow = createWorkflow(
19+
deletePropertyLabelsWorkflowId,
20+
(
21+
input: WorkflowData<DeletePropertyLabelsWorkflowInput>
22+
): WorkflowResponse<void> => {
23+
deletePropertyLabelsStep(input)
24+
25+
return new WorkflowResponse(void 0)
26+
}
27+
)
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
export * from "./create-view-configuration"
2-
export * from "./update-view-configuration"
2+
export * from "./update-view-configuration"
3+
export * from "./create-property-label"
4+
export * from "./update-property-label"
5+
export * from "./delete-property-labels"
6+
export * from "./batch-property-labels"

0 commit comments

Comments
 (0)