Files
co-lite/solvway-admin-portal/app/lib/cmsPages.ts
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

160 lines
4.2 KiB
TypeScript

import { slugify } from "./collections";
// Types and pure helpers behind the page editor (add-cms-pages).
export type BlockType = "HERO" | "RICH_TEXT" | "IMAGE_TEXT" | "FEATURED_PRODUCTS" | "COLLECTION_LIST" | "FAQ" | "FORM";
export const BLOCK_TYPES: BlockType[] = ["HERO", "RICH_TEXT", "IMAGE_TEXT", "FEATURED_PRODUCTS", "COLLECTION_LIST", "FAQ", "FORM"];
export const BLOCK_LABELS: Record<BlockType, string> = {
HERO: "CmsBlockHero",
RICH_TEXT: "CmsBlockRichText",
IMAGE_TEXT: "CmsBlockImageText",
FEATURED_PRODUCTS: "CmsBlockFeaturedProducts",
COLLECTION_LIST: "CmsBlockCollectionList",
FAQ: "CmsBlockFaq",
FORM: "CmsBlockForm",
};
export interface CmsFaqItem {
question: string;
answer: string;
}
export interface CmsBlock {
type: BlockType;
imageUrl?: string;
heading?: string;
text?: string;
buttonLabel?: string;
buttonUrl?: string;
html?: string;
imagePosition?: "LEFT" | "RIGHT";
collectionId?: number | null;
limit?: number;
collectionIds?: number[];
items?: CmsFaqItem[];
formId?: number | null;
}
export interface CmsPageDraft {
id?: number;
slug: string;
slugTouched: boolean;
title: string;
metaDescription: string;
status: "Y" | "N";
blocks: CmsBlock[];
}
export interface CmsPageListItem {
id: number;
slug: string;
title: string;
status: "Y" | "N";
updatedAt?: string | null;
}
export interface CmsPageView {
id: number;
slug: string;
title: string;
metaDescription: string | null;
status: "Y" | "N";
blocks: CmsBlock[];
updatedAt?: string | null;
}
const HELP_SLUGS = new Set(["help", "contact", "delivery-returns", "warranty"]);
export function emptyPage(): CmsPageDraft {
return {
id: undefined,
slug: "",
slugTouched: false,
title: "",
metaDescription: "",
status: "Y",
blocks: [],
};
}
export function emptyBlock(type: BlockType): CmsBlock {
switch (type) {
case "FAQ":
return { type, heading: "", items: [{ question: "", answer: "" }] };
case "IMAGE_TEXT":
return { type, imageUrl: "", heading: "", buttonLabel: "", buttonUrl: "", html: "", imagePosition: "LEFT" };
case "FEATURED_PRODUCTS":
return { type, collectionId: null, limit: 8 };
case "COLLECTION_LIST":
return { type, collectionIds: [] };
case "FORM":
return { type, heading: "", formId: null };
default:
return { type };
}
}
export function withTitle(draft: CmsPageDraft, title: string): CmsPageDraft {
return { ...draft, title, slug: draft.slugTouched ? draft.slug : slugify(title) };
}
export function moveBlock(blocks: CmsBlock[], index: number, delta: number): CmsBlock[] {
const target = index + delta;
if (target < 0 || target >= blocks.length) return blocks;
const next = [...blocks];
[next[index], next[target]] = [next[target], next[index]];
return next;
}
export function pageAddress(slug: string): string {
if (HELP_SLUGS.has(slug)) return "/" + slug;
return "/pages/" + slug;
}
function isBlank(v: unknown): boolean {
return v === "" || v === null || v === undefined;
}
export type CmsPageBody = Omit<CmsPageView, "id" | "updatedAt"> & { id?: number };
export function pageBody(draft: CmsPageDraft): CmsPageBody {
const trimmedMeta = draft.metaDescription.trim();
return {
id: draft.id,
title: draft.title.trim(),
slug: draft.slug,
metaDescription: trimmedMeta || null,
status: draft.status,
blocks: draft.blocks.map((b) => {
const obj: Record<string, unknown> = { type: b.type };
for (const key of Object.keys(b) as (keyof CmsBlock)[]) {
if (key === "type") continue;
if (key === "html") {
if (b.html != null && b.html.trim()) obj.html = b.html.trim();
continue;
}
if (key === "items") {
if (b.items) {
obj.items = b.items.map((it) => ({
question: it.question?.trim() ?? "",
answer: it.answer?.trim() ?? "",
}));
}
continue;
}
const val = b[key];
if (isBlank(val)) continue;
if (typeof val === "string" && val.trim() !== val) {
obj[key] = val.trim();
} else {
obj[key] = val;
}
}
return obj as unknown as CmsBlock;
}),
};
}