refactor: 拆分业务、信任内容与公共工作台模块
This commit is contained in:
1 parent
357f271639
commit
466dc66907
33 files changed
+4950
-4500
No files matched your search
@@ -0,0 +1,87 @@
|
||||
import type {
|
||||
AdminTemplateContent,
|
||||
Offering,
|
||||
TemplateAdminField,
|
||||
} from "@personal-brand/api-client";
|
||||
import {
|
||||
applyPresentationValues,
|
||||
presentationFieldName,
|
||||
readPresentationValue,
|
||||
} from "../brand/presentation-bindings.js";
|
||||
|
||||
export type OfferingFormValues = Record<string, unknown>;
|
||||
|
||||
export function offeringTemplateFields(
|
||||
sections: AdminTemplateContent["admin"]["sections"],
|
||||
): TemplateAdminField[] {
|
||||
return sections
|
||||
.filter((section) => section.target === "offering")
|
||||
.flatMap((section) =>
|
||||
section.fields.filter((field) => field.binding.startsWith("offering.config.")),
|
||||
);
|
||||
}
|
||||
|
||||
export function offeringDefaultFormValues(
|
||||
templateFields: TemplateAdminField[],
|
||||
): OfferingFormValues {
|
||||
return {
|
||||
kind: "service",
|
||||
isEnabled: true,
|
||||
sortOrder: 0,
|
||||
...Object.fromEntries(
|
||||
templateFields.map((field) => [
|
||||
presentationFieldName("offering", field.key),
|
||||
field.input === "list" ? [] : undefined,
|
||||
]),
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
export function offeringEditFormValues(
|
||||
item: Offering,
|
||||
templateFields: TemplateAdminField[],
|
||||
): OfferingFormValues {
|
||||
return {
|
||||
kind: item.kind,
|
||||
name: item.name,
|
||||
summary: item.summary ?? "",
|
||||
description: item.description ?? "",
|
||||
priceText: item.priceText ?? "",
|
||||
coverUrl: item.coverUrl ?? "",
|
||||
sortOrder: item.sortOrder,
|
||||
isEnabled: item.isEnabled,
|
||||
...Object.fromEntries(
|
||||
templateFields.map((field) => {
|
||||
const value = readPresentationValue(item.config, field.binding, field);
|
||||
return [
|
||||
presentationFieldName("offering", field.key),
|
||||
value === undefined || value === null || value === ""
|
||||
? field.input === "select"
|
||||
? field.options?.[0]?.value
|
||||
: field.input === "list"
|
||||
? []
|
||||
: undefined
|
||||
: value,
|
||||
];
|
||||
}),
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
export function buildOfferingRequestBody(
|
||||
values: OfferingFormValues,
|
||||
editingOffering: Offering | null,
|
||||
templateFields: TemplateAdminField[],
|
||||
): Record<string, unknown> {
|
||||
const fields = Object.fromEntries(
|
||||
Object.entries(values).filter(([key]) => key !== "config" && !key.startsWith("offering:")),
|
||||
);
|
||||
return {
|
||||
...fields,
|
||||
config: applyPresentationValues(
|
||||
editingOffering?.config,
|
||||
[{ key: "offering", fields: templateFields }],
|
||||
values,
|
||||
),
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user