contract-customer-onboarding.ts 2.8 KB

1234567891011121314151617181920212223242526
  1. import type { CustomerMasterAccount, CustomerRecord } from "./customer-records";
  2. import type { OrganizationData } from "./organization-demo";
  3. import type { DemoIdentity } from "./demo-identities";
  4. import { addClaimDays, createClaimCode, customerMaterialText, fullRegionPath, nextCustomerNo, normalizedIdNo, validateCustomerDraft, type AccountIdentity, type CustomerDraft } from "./customer-onboarding.ts";
  5. export const blankInlineCustomer = (): CustomerDraft => ({ name: "", subject: "企业", idNo: "", contact: "", phone: "", address: "", regionId: "", operatorId: "", partnerId: "", teamIds: [], login: "", masterPhone: "", initialPassword: "", idNoBasis: "" });
  6. /** 只准备数据;客户、主账号和合同全部校验通过后再统一提交。密码不进入持久化数据。 */
  7. export function prepareInlineCustomer(draft: CustomerDraft, records: CustomerRecord[], data: OrganizationData, identity: DemoIdentity | null, accounts: AccountIdentity[], today: string): { ok: false; errors: Record<string, string> } | { ok: true; customer: CustomerRecord; master: CustomerMasterAccount } {
  8. const normalized = { ...draft, login: draft.login.trim(), masterPhone: draft.masterPhone.trim() };
  9. const errors = validateCustomerDraft(normalized, records, data, identity, accounts);
  10. if (Object.keys(errors).length) return { ok: false, errors };
  11. const no = nextCustomerNo(records, Number(today.slice(0, 4)));
  12. const operator = data.operators.find((item) => item.id === draft.operatorId)!;
  13. const customer: CustomerRecord = {
  14. no, name: draft.name.trim(), subject: draft.subject, idNo: normalizedIdNo(draft.idNo), contact: draft.contact.trim(), phone: draft.phone.trim(), address: draft.address.trim(),
  15. regionId: draft.regionId, regionPath: fullRegionPath(data, draft.regionId), operator: `${fullRegionPath(data, operator.region)} / ${operator.name}`,
  16. partner: data.partners.find((item) => item.id === draft.partnerId)?.name || null,
  17. teams: [...new Set(draft.teamIds)].map((id) => data.maintenanceTeams.find((item) => item.id === id)!.name),
  18. status: "未开通", quota: 0, files: [], materials: customerMaterialText(draft.subject),
  19. history: [{ at: today, by: identity?.name || "林管", field: "合同登记建档", before: "未建档", after: `同步建立主账号 ${normalized.login}(${draft.initialPassword ? "已激活,首次登录须改密" : "未激活,激活码 7 天有效"})` }],
  20. };
  21. const master: CustomerMasterAccount = { login: normalized.login, phone: normalized.masterPhone || undefined, status: "在用", claimStatus: draft.initialPassword ? "已激活" : "未激活", createdOn: today, mustChange: Boolean(draft.initialPassword),
  22. ...(!draft.initialPassword ? { claimOn: today, claimExpireOn: addClaimDays(today, 7), claimCode: createClaimCode() } : {}) };
  23. return { ok: true, customer, master };
  24. }