Files
co-lite/solvway-admin-portal/app/lib/cmsForms.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

159 lines
3.9 KiB
TypeScript

// Types and pure helpers behind the form editor (add-cms-forms).
export type FieldType = "TEXT" | "TEXTAREA" | "EMAIL" | "PHONE" | "SELECT" | "CHECKBOX";
export const FIELD_TYPES: FieldType[] = ["TEXT", "TEXTAREA", "EMAIL", "PHONE", "SELECT", "CHECKBOX"];
export const FIELD_LABELS: Record<FieldType, string> = {
TEXT: "FrmFieldTypeText",
TEXTAREA: "FrmFieldTypeTextarea",
EMAIL: "Email",
PHONE: "Phone",
SELECT: "FrmFieldTypeSelect",
CHECKBOX: "FrmFieldTypeCheckbox",
};
export interface CmsFormField {
key: string;
label: string;
type: FieldType;
required: boolean;
options?: string[];
}
/** A field as the editor holds it: options as one-per-line text. */
export interface CmsFormFieldDraft {
key: string;
label: string;
type: FieldType;
required: boolean;
optionsText: string;
}
export interface CmsFormDraft {
id?: number;
name: string;
submitLabel: string;
successMessage: string;
notifyEmailsText: string;
status: "Y" | "N";
fields: CmsFormFieldDraft[];
}
export interface CmsFormView {
id: number;
name: string;
submitLabel: string;
successMessage: string;
notifyEmails: string[];
status: "Y" | "N";
fields: CmsFormField[];
updatedAt?: string | null;
}
export interface CmsFormListItem {
id: number;
name: string;
status: "Y" | "N";
fieldCount: number;
submissionCount: number;
updatedAt?: string | null;
}
export interface CmsFormAnswer {
key: string;
label: string;
type: FieldType;
value: string;
}
export interface CmsFormSubmission {
id: number;
formId: number;
pageSlug: string | null;
answers: CmsFormAnswer[];
createdAt: string;
}
export interface SubmissionsPage {
content: CmsFormSubmission[];
totalElements: number;
}
export interface CmsFormBody {
id?: number;
name: string;
submitLabel: string;
successMessage: string;
notifyEmails: string[];
status: "Y" | "N";
fields: CmsFormField[];
}
/** A stable field key the server accepts (^[a-z0-9]{1,16}$); never changes once made. */
function newKey(): string {
return crypto.randomUUID().replace(/-/g, "").slice(0, 8);
}
export function newField(type: FieldType = "TEXT", label = ""): CmsFormFieldDraft {
return { key: newKey(), label, type, required: false, optionsText: "" };
}
export function emptyForm(): CmsFormDraft {
return {
id: undefined,
name: "",
submitLabel: "Send",
successMessage: "Thanks! We'll be in touch soon.",
notifyEmailsText: "",
status: "Y",
fields: [{ ...newField("TEXT", "Name"), required: true }],
};
}
export function draftFromView(view: CmsFormView): CmsFormDraft {
return {
id: view.id,
name: view.name,
submitLabel: view.submitLabel,
successMessage: view.successMessage,
notifyEmailsText: (view.notifyEmails ?? []).join(", "),
status: view.status,
fields: view.fields.map((f) => ({
key: f.key,
label: f.label,
type: f.type,
required: f.required,
optionsText: (f.options ?? []).join("\n"),
})),
};
}
export function moveField<T>(fields: T[], index: number, delta: number): T[] {
const target = index + delta;
if (target < 0 || target >= fields.length) return fields;
const next = [...fields];
[next[index], next[target]] = [next[target], next[index]];
return next;
}
function lines(text: string, separator: RegExp): string[] {
return text.split(separator).map((s) => s.trim()).filter(Boolean);
}
export function formBody(draft: CmsFormDraft): CmsFormBody {
return {
id: draft.id,
name: draft.name.trim(),
submitLabel: draft.submitLabel.trim(),
successMessage: draft.successMessage.trim(),
notifyEmails: lines(draft.notifyEmailsText, /[,\n]/),
status: draft.status,
fields: draft.fields.map((f) => {
const field: CmsFormField = { key: f.key, label: f.label.trim(), type: f.type, required: f.required };
if (f.type === "SELECT") field.options = lines(f.optionsText, /\n/);
return field;
}),
};
}