@aglyn/plugins-crm 1.4.0 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +14 -14
- package/src/lib/components/add-to-list-dialog.js +7 -3
- package/src/lib/components/add-to-list-dialog.js.map +1 -1
- package/src/lib/components/company-contact-roles-card.d.ts +3 -0
- package/src/lib/components/company-contact-roles-card.js +4 -2
- package/src/lib/components/company-contact-roles-card.js.map +1 -1
- package/src/lib/components/company-detail-page.js +5 -3
- package/src/lib/components/company-detail-page.js.map +1 -1
- package/src/lib/components/company-edit-drawer.d.ts +3 -0
- package/src/lib/components/company-edit-drawer.js +2 -1
- package/src/lib/components/company-edit-drawer.js.map +1 -1
- package/src/lib/components/company-properties-card.d.ts +3 -0
- package/src/lib/components/company-properties-card.js.map +1 -1
- package/src/lib/components/contact-associations-card.d.ts +3 -0
- package/src/lib/components/contact-associations-card.js +3 -1
- package/src/lib/components/contact-associations-card.js.map +1 -1
- package/src/lib/components/contact-detail-page.js +5 -3
- package/src/lib/components/contact-detail-page.js.map +1 -1
- package/src/lib/components/contact-properties-card.d.ts +3 -0
- package/src/lib/components/contact-properties-card.js +3 -1
- package/src/lib/components/contact-properties-card.js.map +1 -1
- package/src/lib/components/deal-contact-roles-card.d.ts +3 -0
- package/src/lib/components/deal-contact-roles-card.js +4 -2
- package/src/lib/components/deal-contact-roles-card.js.map +1 -1
- package/src/lib/components/deal-detail-page.js +7 -4
- package/src/lib/components/deal-detail-page.js.map +1 -1
- package/src/lib/components/deal-edit-drawer.d.ts +12 -1
- package/src/lib/components/deal-edit-drawer.js +150 -53
- package/src/lib/components/deal-edit-drawer.js.map +1 -1
- package/src/lib/components/deal-products-card.d.ts +3 -0
- package/src/lib/components/deal-products-card.js +4 -2
- package/src/lib/components/deal-products-card.js.map +1 -1
- package/src/lib/components/deals-section.js +1 -0
- package/src/lib/components/deals-section.js.map +1 -1
- package/src/lib/components/fields-section.js +7 -3
- package/src/lib/components/fields-section.js.map +1 -1
- package/src/lib/components/lead-campaigns-card.d.ts +3 -1
- package/src/lib/components/lead-campaigns-card.js +3 -2
- package/src/lib/components/lead-campaigns-card.js.map +1 -1
- package/src/lib/components/lead-detail-page.js +3 -1
- package/src/lib/components/lead-detail-page.js.map +1 -1
- package/src/lib/components/lead-properties-card.d.ts +3 -1
- package/src/lib/components/lead-properties-card.js +3 -2
- package/src/lib/components/lead-properties-card.js.map +1 -1
- package/src/lib/components/picklist-values-card.js +4 -2
- package/src/lib/components/picklist-values-card.js.map +1 -1
- package/src/lib/components/pipeline-stages-dialog.d.ts +3 -0
- package/src/lib/components/pipeline-stages-dialog.js +43 -14
- package/src/lib/components/pipeline-stages-dialog.js.map +1 -1
- package/src/lib/components/pipelines-dialog.d.ts +3 -0
- package/src/lib/components/pipelines-dialog.js +5 -2
- package/src/lib/components/pipelines-dialog.js.map +1 -1
- package/src/lib/components/record-tasks-card.js +3 -2
- package/src/lib/components/record-tasks-card.js.map +1 -1
- package/src/lib/components/task-edit-drawer.d.ts +2 -0
- package/src/lib/components/task-edit-drawer.js +2 -1
- package/src/lib/components/task-edit-drawer.js.map +1 -1
- package/src/lib/components/tasks-section.js +3 -2
- package/src/lib/components/tasks-section.js.map +1 -1
- package/src/lib/hooks/use-contact-field-definitions.d.ts +3 -0
- package/src/lib/hooks/use-contact-field-definitions.js +3 -2
- package/src/lib/hooks/use-contact-field-definitions.js.map +1 -1
- package/src/lib/hooks/use-crm-picklist.d.ts +3 -0
- package/src/lib/hooks/use-crm-picklist.js +5 -3
- package/src/lib/hooks/use-crm-picklist.js.map +1 -1
- package/src/lib/hooks/use-crm-tasks.d.ts +3 -0
- package/src/lib/hooks/use-crm-tasks.js +3 -1
- package/src/lib/hooks/use-crm-tasks.js.map +1 -1
- package/src/lib/hooks/use-lead-source-picklist.js +5 -3
- package/src/lib/hooks/use-lead-source-picklist.js.map +1 -1
- package/src/lib/hooks/use-lead-status-picklist.js +5 -3
- package/src/lib/hooks/use-lead-status-picklist.js.map +1 -1
- package/src/lib/hooks/use-pipeline.d.ts +3 -0
- package/src/lib/hooks/use-pipeline.js +3 -1
- package/src/lib/hooks/use-pipeline.js.map +1 -1
- package/src/lib/model/crm-list-query.d.ts +7 -0
- package/src/lib/model/crm-list-query.js +10 -0
- package/src/lib/model/crm-list-query.js.map +1 -1
- package/src/lib/model/deal-form-model.d.ts +35 -0
- package/src/lib/model/deal-form-model.js +103 -0
- package/src/lib/model/deal-form-model.js.map +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../../libs/plugins/crm/src/lib/model/deal-form-model.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * The deal drawer's values and the documents they become (AGL-2598), as pure\n * functions — the drawer holds strings the way inputs do, and this module is\n * the one place those strings turn into the typed fields a `CrmDeal` stores.\n * Kept out of the component so the translation can be tested without a\n * Firestore or a DOM, and so the contact card and the company card, which\n * open the same drawer with a link preselected, build a deal the same way.\n */\n\nimport {\n contactDisplayName,\n type CrmDeal,\n type CrmDealStage,\n type CrmForecastCategory,\n type CrmPicklist,\n crmPicklistDefaultLabel,\n DEAL_NEXT_STEP_MAX,\n dealContactRolesOf,\n dealContactRolesWithPrimary,\n dealStageForecastCategory,\n isCrmForecastCategory,\n judgeCrmPicklistValue,\n nameSearchKey,\n nameSearchToken,\n readDealProbability,\n} from '@aglyn/aglyn'\nimport {\n amountInputValue,\n dateInputMs,\n dateInputValue,\n DEFAULT_DEAL_CURRENCY,\n type DealDoc,\n openStages,\n parseAmountInput,\n type PipelineDoc,\n} from './deal-board-model'\n\n/** What the drawer edits — strings as the inputs hold them. */\nexport interface DealFormValues {\n title: string\n pipelineId: string\n stageId: string\n /** As typed: `1,250.00`; parsed at save. */\n amount: string\n currency: string\n /** `YYYY-MM-DD` from a date input, or empty. */\n expectedClose: string\n ownerUid: string\n contactId: string\n contactName: string\n companyId: string\n companyName: string\n notes: string\n /*\n * Salesforce's Opportunity fields (AGL-3516).\n */\n /** The `opportunityType` label, or empty. */\n type: string\n /** The `leadSource` label, or empty. */\n leadSource: string\n nextStep: string\n /** The override as typed, `35`; empty means the stage's. */\n probability: string\n /** Empty means the stage's — what a save stamps when nobody picked one. */\n forecastCategory: '' | CrmForecastCategory\n /** One of the org's campaigns, or empty. */\n campaignId: string\n}\n\nexport const DEAL_TITLE_MAX = 120\nexport const DEAL_NOTES_MAX = 4000\n\n/**\n * A blank form aimed at a pipeline's first open stage.\n *\n * `defaults` is how a contact's page or a company's page starts a deal\n * already linked to itself: the caller passes the ids and names it knows and\n * the drawer opens with those fields filled. Anything it does not pass stays\n * blank rather than being guessed.\n */\nexport function emptyDealForm(\n pipeline: Pick<PipelineDoc, '$id' | 'stages'> | null | undefined,\n defaults: Partial<DealFormValues> = {},\n): DealFormValues {\n const firstOpen = openStages(pipeline)[0]\n return {\n title: '',\n pipelineId: pipeline?.$id ?? '',\n stageId: firstOpen?.id ?? '',\n amount: '',\n currency: DEFAULT_DEAL_CURRENCY,\n expectedClose: '',\n ownerUid: '',\n contactId: '',\n contactName: '',\n companyId: '',\n companyName: '',\n notes: '',\n type: '',\n leadSource: '',\n nextStep: '',\n probability: '',\n forecastCategory: '',\n campaignId: '',\n ...defaults,\n }\n}\n\n/** The form an existing deal opens as. */\nexport function dealFormFromDoc(deal: DealDoc): DealFormValues {\n return {\n title: String(deal.title ?? ''),\n pipelineId: String(deal.pipelineId ?? ''),\n stageId: String(deal.stageId ?? ''),\n amount: amountInputValue(deal.amountCents),\n currency: String(deal.currency || DEFAULT_DEAL_CURRENCY).toLowerCase(),\n expectedClose: dateInputValue(deal.expectedCloseAtMs),\n ownerUid: String(deal.ownerUid ?? ''),\n contactId: String(deal.contactId ?? ''),\n contactName: String(deal.contactName ?? ''),\n companyId: String(deal.companyId ?? ''),\n companyName: String(deal.companyName ?? ''),\n notes: String(deal.notes ?? ''),\n type: String(deal.type ?? ''),\n leadSource: String(deal.leadSource ?? ''),\n nextStep: String(deal.nextStep ?? ''),\n probability: typeof deal.probability === 'number' ? String(deal.probability) : '',\n forecastCategory: isCrmForecastCategory(deal.forecastCategory) ? deal.forecastCategory : '',\n campaignId: String(deal.campaignId ?? ''),\n }\n}\n\n/**\n * Why the form cannot be saved, or `null` when it can.\n *\n * A title is the one thing a deal needs — a card with no title is a card\n * nobody can find on the board. The amount is optional but, if typed, has to\n * parse: a deal saved with an amount the merchant typed and the model dropped\n * would forecast at zero without saying so. The stage is only checked on a\n * new deal, because an existing deal's stage is moved by the stage route and\n * is not this form's to write.\n */\nexport function dealFormProblem(\n values: DealFormValues,\n mode: 'create' | 'edit',\n): string | null {\n if (!values.title.trim()) return 'A deal needs a title.'\n if (values.amount.trim() && parseAmountInput(values.amount) === null) {\n return 'The amount has to be a number of zero or more.'\n }\n if (values.expectedClose.trim() && dateInputMs(values.expectedClose) === null) {\n return 'The expected close has to be a date.'\n }\n if (readDealProbability(values.probability) === undefined) {\n return 'The probability has to be a whole number from 0 to 100.'\n }\n if (mode === 'create' && (!values.pipelineId || !values.stageId)) {\n return 'Pick the stage the deal starts in.'\n }\n return null\n}\n\n/** What every deal write stamps besides the fields the form edits. */\nexport interface DealWriteContext {\n visibleTo: string[]\n hostId: string\n uid: string\n nowMs: number\n /**\n * The stage the deal is in, whose forecast category a form left on\n * \"from the stage\" stamps (AGL-3516). Absent, the field is left unset and\n * every reader derives it from the stage.\n */\n stage?: Pick<CrmDealStage, 'kind' | 'forecastCategory'> | null\n}\n\n/**\n * The form's two picklist fields judged against the org's lists (AGL-3516),\n * the way every server door judges them: an active value is stored as the\n * list spells it, a blank clears, the deal's own current value is kept, and\n * anything else is refused naming what the list allows. A NEW deal with no\n * Type takes the list's default, as Salesforce's does; the lead source is\n * never defaulted — it records where a deal came from.\n */\nexport function judgeDealFormPicklists(\n values: DealFormValues,\n lists: { type: CrmPicklist; leadSource: CrmPicklist },\n options: { current?: Partial<Pick<CrmDeal, 'type' | 'leadSource'>> | null; created: boolean },\n): { ok: true; values: DealFormValues } | { ok: false; error: string } {\n const typed = values.type.trim() || (options.created ? (crmPicklistDefaultLabel(lists.type) ?? '') : '')\n const type = judgeCrmPicklistValue('opportunityType', lists.type, typed, options.current?.type)\n if (type.ok === false) return type\n const leadSource = judgeCrmPicklistValue(\n 'leadSource',\n lists.leadSource,\n values.leadSource,\n options.current?.leadSource,\n )\n if (leadSource.ok === false) return leadSource\n return {\n ok: true,\n values: { ...values, type: type.value ?? '', leadSource: leadSource.value ?? '' },\n }\n}\n\n/** The forecast category a save stamps: the form's, else the stage's, else none. */\nfunction forecastCategoryFor(\n values: Pick<DealFormValues, 'forecastCategory'>,\n stage: DealWriteContext['stage'],\n): CrmForecastCategory | null {\n if (isCrmForecastCategory(values.forecastCategory)) return values.forecastCategory\n return stage ? dealStageForecastCategory(stage) : null\n}\n\n/**\n * The optional fields, as the values to SET and the keys to CLEAR.\n *\n * Split rather than written as `null` because a deal's optional ids are\n * typed as absent-or-string, and the indexes on `contactId` and `companyId`\n * are what the contact and company pages query — a `null` there would be a\n * row those pages cannot see and a filter on \"no contact\" could not find\n * either. On a create the cleared keys are simply not written; on an edit\n * the component turns them into field deletes.\n */\nfunction optionalFields(values: DealFormValues): {\n set: Record<string, string | number>\n clear: string[]\n} {\n const set: Record<string, string | number> = {}\n const clear: string[] = []\n const put = (key: string, value: string | number | null) => {\n if (value === null || value === '') clear.push(key)\n else set[key] = value\n }\n put('amountCents', parseAmountInput(values.amount))\n put('expectedCloseAtMs', dateInputMs(values.expectedClose))\n put('ownerUid', values.ownerUid.trim())\n put('contactId', values.contactId.trim())\n put('contactName', values.contactId.trim() ? values.contactName.trim() : '')\n put('companyId', values.companyId.trim())\n put('companyName', values.companyId.trim() ? values.companyName.trim() : '')\n put('notes', values.notes.trim().slice(0, DEAL_NOTES_MAX))\n put('type', values.type.trim())\n put('leadSource', values.leadSource.trim())\n put('nextStep', values.nextStep.trim().replace(/\\s+/g, ' ').slice(0, DEAL_NEXT_STEP_MAX))\n put('probability', readDealProbability(values.probability) ?? null)\n put('campaignId', values.campaignId.trim())\n return { set, clear }\n}\n\n/**\n * The document a new deal is created as. Open, in the chosen stage, with\n * the stage clock started now so \"days in stage\" counts from creation.\n */\nexport function dealDocumentFromForm(\n values: DealFormValues,\n context: DealWriteContext,\n): Record<string, unknown> {\n const title = values.title.trim().slice(0, DEAL_TITLE_MAX)\n const { set } = optionalFields(values)\n const forecastCategory = forecastCategoryFor(values, context.stage)\n const contactId = values.contactId.trim()\n return {\n title,\n titleLower: nameSearchKey(title),\n pipelineId: values.pipelineId,\n stageId: values.stageId,\n status: 'open' satisfies CrmDeal['status'],\n // The contact picked is the deal's Primary contact role (AGL-3521).\n ...(contactId ? { contactRoles: [{ contactId, primary: true }] } : {}),\n currency: String(values.currency || DEFAULT_DEAL_CURRENCY).toLowerCase(),\n stageChangedAtMs: context.nowMs,\n ...(forecastCategory ? { forecastCategory } : {}),\n ...set,\n visibleTo: context.visibleTo,\n hostId: context.hostId,\n createdByUid: context.uid,\n createdAt: new Date(context.nowMs),\n updatedAt: new Date(context.nowMs),\n }\n}\n\n/**\n * The patch an edit writes. Never the stage, the status or the scope: the\n * stage goes through the server route so the automations hear it, and the\n * scope is changed only by an org-wide member on purpose.\n *\n * Nor the amount or the currency of a deal whose amount is DERIVED\n * (`amountDerived`, AGL-2620): with line items on the deal the amount is\n * their sum and the currency is theirs, both owned by the products card. The\n * form shows them read-only, and a patch that re-wrote the number it was\n * seeded with would overwrite a line added from another tab meanwhile.\n */\nexport function dealPatchFromForm(\n values: DealFormValues,\n nowMs: number,\n options: {\n amountDerived?: boolean\n stage?: DealWriteContext['stage']\n /**\n * The deal as stored, whose contact roles a changed contact moves\n * (AGL-3521): the contact picked becomes the Primary — added with no\n * role when the deal did not name them — and a cleared contact leaves\n * the deal with no Primary. An unchanged contact writes no roles, so a\n * role edited on the deal's page meanwhile is not overwritten.\n */\n current?: Pick<CrmDeal, 'contactId' | 'contactRoles'> | null\n } = {},\n): { set: Record<string, unknown>; clear: string[] } {\n const title = values.title.trim().slice(0, DEAL_TITLE_MAX)\n const optional = optionalFields(values)\n const set: Record<string, unknown> = optional.set\n const { clear } = optional\n const contactId = values.contactId.trim()\n if (options.current && contactId !== (options.current.contactId ?? '')) {\n set['contactRoles'] = dealContactRolesWithPrimary(dealContactRolesOf(options.current), contactId || null)\n }\n // The category picked, else the stage's again; with neither, the field\n // goes and every reader derives it from the stage.\n const forecastCategory = forecastCategoryFor(values, options.stage)\n if (forecastCategory) set['forecastCategory'] = forecastCategory\n else clear.push('forecastCategory')\n if (options.amountDerived) {\n delete set['amountCents']\n return {\n set: { title, titleLower: nameSearchKey(title), ...set, updatedAt: new Date(nowMs) },\n clear: clear.filter((key) => key !== 'amountCents'),\n }\n }\n return {\n set: {\n title,\n titleLower: nameSearchKey(title),\n currency: String(values.currency || DEFAULT_DEAL_CURRENCY).toLowerCase(),\n ...set,\n updatedAt: new Date(nowMs),\n },\n clear,\n }\n}\n\n/** A contact as the picker offers it. */\nexport interface ContactChoice {\n id: string\n name: string\n email: string\n}\n\n/**\n * The contacts a typed query matches, from a window of recent contact rows,\n * in the list's own grammar.\n *\n * In memory rather than by query because the contacts collection has no\n * composite index over `visibleTo` with `nameLower` or `email`, and a\n * scoped list can carry only one array clause — so the picker reads the\n * newest rows the viewer may see (bounded, ordered) and matches here. The\n * match is what the list's search does: the normalized query as a prefix of\n * the email, or as a prefix of any word of the name (`nameTokens` holds\n * every word prefix a write produced; `nameLower` covers rows written before\n * the tokens existed). The holder's own name for the person wins over the\n * canonical one, as everywhere else.\n */\nexport function contactChoicesFor(\n queryText: string,\n rows: readonly Record<string, unknown>[],\n /**\n * The holder each row is named through: one group for every row under a\n * site, or a resolver per row at the organization level (AGL-2630), where\n * each contact reads through its own primary holder.\n */\n groupId: string | ((row: Record<string, unknown>) => string),\n max = 8,\n): ContactChoice[] {\n const key = nameSearchKey(queryText)\n const token = nameSearchToken(queryText)\n const choices: ContactChoice[] = []\n for (const row of rows) {\n const email = String(row['email'] ?? '').toLowerCase()\n const name = contactDisplayName(\n row,\n typeof groupId === 'function' ? groupId(row) : groupId,\n )\n const nameLower = nameSearchKey(name)\n const tokens = Array.isArray(row['nameTokens'])\n ? (row['nameTokens'] as unknown[]).map(String)\n : []\n const matches =\n !key ||\n email.startsWith(key) ||\n nameLower.startsWith(key) ||\n nameLower.includes(` ${key}`) ||\n (token ? tokens.includes(token) : false)\n if (!matches) continue\n choices.push({ id: String(row['$id'] ?? ''), name, email })\n if (choices.length >= max) break\n }\n return choices\n}\n"],"names":["contactDisplayName","crmPicklistDefaultLabel","DEAL_NEXT_STEP_MAX","dealContactRolesOf","dealContactRolesWithPrimary","dealStageForecastCategory","isCrmForecastCategory","judgeCrmPicklistValue","nameSearchKey","nameSearchToken","readDealProbability","amountInputValue","dateInputMs","dateInputValue","DEFAULT_DEAL_CURRENCY","openStages","parseAmountInput","DEAL_TITLE_MAX","DEAL_NOTES_MAX","emptyDealForm","pipeline","defaults","firstOpen","title","pipelineId","$id","stageId","id","amount","currency","expectedClose","ownerUid","contactId","contactName","companyId","companyName","notes","type","leadSource","nextStep","probability","forecastCategory","campaignId","dealFormFromDoc","deal","String","amountCents","toLowerCase","expectedCloseAtMs","dealFormProblem","values","mode","trim","undefined","judgeDealFormPicklists","lists","options","typed","created","current","ok","value","forecastCategoryFor","stage","optionalFields","set","clear","put","key","push","slice","replace","dealDocumentFromForm","context","titleLower","status","contactRoles","primary","stageChangedAtMs","nowMs","visibleTo","hostId","createdByUid","uid","createdAt","Date","updatedAt","dealPatchFromForm","optional","amountDerived","filter","contactChoicesFor","queryText","rows","groupId","max","token","choices","row","email","name","nameLower","tokens","Array","isArray","map","matches","startsWith","includes","length"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED;;;;;;;CAOC,GAED,SACEA,kBAAkB,EAKlBC,uBAAuB,EACvBC,kBAAkB,EAClBC,kBAAkB,EAClBC,2BAA2B,EAC3BC,yBAAyB,EACzBC,qBAAqB,EACrBC,qBAAqB,EACrBC,aAAa,EACbC,eAAe,EACfC,mBAAmB,QACd,eAAc;AACrB,SACEC,gBAAgB,EAChBC,WAAW,EACXC,cAAc,EACdC,qBAAqB,EAErBC,UAAU,EACVC,gBAAgB,QAEX,wBAAoB;AAkC3B,OAAO,MAAMC,iBAAiB,IAAG;AACjC,OAAO,MAAMC,iBAAiB,KAAI;AAElC;;;;;;;CAOC,GACD,OAAO,SAASC,cACdC,QAAgE,EAChEC,WAAoC,CAAC,CAAC;;IAEtC,MAAMC,YAAYP,WAAWK,SAAS,CAAC,EAAE;IACzC,OAAO;QACLG,OAAO;QACPC,UAAU,UAAEJ,4BAAAA,SAAUK,GAAG,mBAAI;QAC7BC,OAAO,WAAEJ,6BAAAA,UAAWK,EAAE,oBAAI;QAC1BC,QAAQ;QACRC,UAAUf;QACVgB,eAAe;QACfC,UAAU;QACVC,WAAW;QACXC,aAAa;QACbC,WAAW;QACXC,aAAa;QACbC,OAAO;QACPC,MAAM;QACNC,YAAY;QACZC,UAAU;QACVC,aAAa;QACbC,kBAAkB;QAClBC,YAAY;OACTrB;AAEP;AAEA,wCAAwC,GACxC,OAAO,SAASsB,gBAAgBC,IAAa;QAE3BA,aACKA,kBACHA,eAICA,gBACCA,iBACEA,mBACFA,iBACEA,mBACNA,aACDA,YACMA,kBACFA,gBAGEA;IAlBrB,OAAO;QACLrB,OAAOsB,QAAOD,cAAAA,KAAKrB,KAAK,YAAVqB,cAAc;QAC5BpB,YAAYqB,QAAOD,mBAAAA,KAAKpB,UAAU,YAAfoB,mBAAmB;QACtClB,SAASmB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C,CAAC,CAAC,EAAExC,KAAK,KAC3B4B,CAAAA,QAAQM,OAAOM,QAAQ,CAACZ,SAAS,KAAI;QACxC,IAAI,CAACU,SAAS;QACdT,QAAQ5B,IAAI,CAAC;YAAE1C,IAAIkB,QAAOqD,WAAAA,GAAG,CAAC,MAAM,YAAVA,WAAc;YAAKE;YAAMD;QAAM;QACzD,IAAIF,QAAQY,MAAM,IAAId,KAAK;IAC7B;IACA,OAAOE;AACT"}
|
|
1
|
+
{"version":3,"sources":["../../../../../../../libs/plugins/crm/src/lib/model/deal-form-model.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * The deal drawer's values and the documents they become (AGL-2598), as pure\n * functions — the drawer holds strings the way inputs do, and this module is\n * the one place those strings turn into the typed fields a `CrmDeal` stores.\n * Kept out of the component so the translation can be tested without a\n * Firestore or a DOM, and so the contact card and the company card, which\n * open the same drawer with a link preselected, build a deal the same way.\n */\n\nimport {\n contactDisplayName,\n type CrmDeal,\n type CrmDealStage,\n type CrmForecastCategory,\n type CrmPicklist,\n crmPicklistDefaultLabel,\n DEAL_NEXT_STEP_MAX,\n dealContactRolesOf,\n dealContactRolesWithPrimary,\n dealStageForecastCategory,\n isCrmForecastCategory,\n judgeCrmPicklistValue,\n nameSearchKey,\n nameSearchToken,\n readDealProbability,\n} from '@aglyn/aglyn'\nimport {\n amountInputValue,\n dateInputMs,\n dateInputValue,\n DEFAULT_DEAL_CURRENCY,\n type DealDoc,\n openStages,\n parseAmountInput,\n type PipelineDoc,\n} from './deal-board-model'\n\n/** What the drawer edits — strings as the inputs hold them. */\nexport interface DealFormValues {\n title: string\n pipelineId: string\n stageId: string\n /** As typed: `1,250.00`; parsed at save. */\n amount: string\n currency: string\n /** `YYYY-MM-DD` from a date input, or empty. */\n expectedClose: string\n ownerUid: string\n contactId: string\n contactName: string\n companyId: string\n companyName: string\n notes: string\n /*\n * Salesforce's Opportunity fields (AGL-3516).\n */\n /** The `opportunityType` label, or empty. */\n type: string\n /** The `leadSource` label, or empty. */\n leadSource: string\n nextStep: string\n /** The override as typed, `35`; empty means the stage's. */\n probability: string\n /** Empty means the stage's — what a save stamps when nobody picked one. */\n forecastCategory: '' | CrmForecastCategory\n /** One of the org's campaigns, or empty. */\n campaignId: string\n}\n\nexport const DEAL_TITLE_MAX = 120\nexport const DEAL_NOTES_MAX = 4000\n\n/**\n * A blank form aimed at a pipeline's first open stage.\n *\n * `defaults` is how a contact's page or a company's page starts a deal\n * already linked to itself: the caller passes the ids and names it knows and\n * the drawer opens with those fields filled. Anything it does not pass stays\n * blank rather than being guessed.\n */\nexport function emptyDealForm(\n pipeline: Pick<PipelineDoc, '$id' | 'stages'> | null | undefined,\n defaults: Partial<DealFormValues> = {},\n): DealFormValues {\n const firstOpen = openStages(pipeline)[0]\n return {\n title: '',\n pipelineId: pipeline?.$id ?? '',\n stageId: firstOpen?.id ?? '',\n amount: '',\n currency: DEFAULT_DEAL_CURRENCY,\n expectedClose: '',\n ownerUid: '',\n contactId: '',\n contactName: '',\n companyId: '',\n companyName: '',\n notes: '',\n type: '',\n leadSource: '',\n nextStep: '',\n probability: '',\n forecastCategory: '',\n campaignId: '',\n ...defaults,\n }\n}\n\n/** The form an existing deal opens as. */\nexport function dealFormFromDoc(deal: DealDoc): DealFormValues {\n return {\n title: String(deal.title ?? ''),\n pipelineId: String(deal.pipelineId ?? ''),\n stageId: String(deal.stageId ?? ''),\n amount: amountInputValue(deal.amountCents),\n currency: String(deal.currency || DEFAULT_DEAL_CURRENCY).toLowerCase(),\n expectedClose: dateInputValue(deal.expectedCloseAtMs),\n ownerUid: String(deal.ownerUid ?? ''),\n contactId: String(deal.contactId ?? ''),\n contactName: String(deal.contactName ?? ''),\n companyId: String(deal.companyId ?? ''),\n companyName: String(deal.companyName ?? ''),\n notes: String(deal.notes ?? ''),\n type: String(deal.type ?? ''),\n leadSource: String(deal.leadSource ?? ''),\n nextStep: String(deal.nextStep ?? ''),\n probability: typeof deal.probability === 'number' ? String(deal.probability) : '',\n forecastCategory: isCrmForecastCategory(deal.forecastCategory) ? deal.forecastCategory : '',\n campaignId: String(deal.campaignId ?? ''),\n }\n}\n\n/**\n * Why the form cannot be saved, or `null` when it can.\n *\n * A title is the one thing a deal needs — a card with no title is a card\n * nobody can find on the board. The amount is optional but, if typed, has to\n * parse: a deal saved with an amount the merchant typed and the model dropped\n * would forecast at zero without saying so. The stage is only checked on a\n * new deal, because an existing deal's stage is moved by the stage route and\n * is not this form's to write.\n */\nexport function dealFormProblem(\n values: DealFormValues,\n mode: 'create' | 'edit',\n): string | null {\n if (!values.title.trim()) return 'A deal needs a title.'\n if (values.amount.trim() && parseAmountInput(values.amount) === null) {\n return 'The amount has to be a number of zero or more.'\n }\n if (values.expectedClose.trim() && dateInputMs(values.expectedClose) === null) {\n return 'The expected close has to be a date.'\n }\n if (readDealProbability(values.probability) === undefined) {\n return 'The probability has to be a whole number from 0 to 100.'\n }\n if (mode === 'create' && (!values.pipelineId || !values.stageId)) {\n return 'Pick the stage the deal starts in.'\n }\n return null\n}\n\n/** What every deal write stamps besides the fields the form edits. */\nexport interface DealWriteContext {\n visibleTo: string[]\n hostId: string\n uid: string\n nowMs: number\n /**\n * The stage the deal is in, whose forecast category a form left on\n * \"from the stage\" stamps (AGL-3516). Absent, the field is left unset and\n * every reader derives it from the stage.\n */\n stage?: Pick<CrmDealStage, 'kind' | 'forecastCategory'> | null\n}\n\n/**\n * The form's two picklist fields judged against the org's lists (AGL-3516),\n * the way every server door judges them: an active value is stored as the\n * list spells it, a blank clears, the deal's own current value is kept, and\n * anything else is refused naming what the list allows. A NEW deal with no\n * Type takes the list's default, as Salesforce's does; the lead source is\n * never defaulted — it records where a deal came from.\n */\nexport function judgeDealFormPicklists(\n values: DealFormValues,\n lists: { type: CrmPicklist; leadSource: CrmPicklist },\n options: { current?: Partial<Pick<CrmDeal, 'type' | 'leadSource'>> | null; created: boolean },\n): { ok: true; values: DealFormValues } | { ok: false; error: string } {\n const typed = values.type.trim() || (options.created ? (crmPicklistDefaultLabel(lists.type) ?? '') : '')\n const type = judgeCrmPicklistValue('opportunityType', lists.type, typed, options.current?.type)\n if (type.ok === false) return type\n const leadSource = judgeCrmPicklistValue(\n 'leadSource',\n lists.leadSource,\n values.leadSource,\n options.current?.leadSource,\n )\n if (leadSource.ok === false) return leadSource\n return {\n ok: true,\n values: { ...values, type: type.value ?? '', leadSource: leadSource.value ?? '' },\n }\n}\n\n/** The forecast category a save stamps: the form's, else the stage's, else none. */\nfunction forecastCategoryFor(\n values: Pick<DealFormValues, 'forecastCategory'>,\n stage: DealWriteContext['stage'],\n): CrmForecastCategory | null {\n if (isCrmForecastCategory(values.forecastCategory)) return values.forecastCategory\n return stage ? dealStageForecastCategory(stage) : null\n}\n\n/**\n * The optional fields, as the values to SET and the keys to CLEAR.\n *\n * Split rather than written as `null` because a deal's optional ids are\n * typed as absent-or-string, and the indexes on `contactId` and `companyId`\n * are what the contact and company pages query — a `null` there would be a\n * row those pages cannot see and a filter on \"no contact\" could not find\n * either. On a create the cleared keys are simply not written; on an edit\n * the component turns them into field deletes.\n */\nfunction optionalFields(values: DealFormValues): {\n set: Record<string, string | number>\n clear: string[]\n} {\n const set: Record<string, string | number> = {}\n const clear: string[] = []\n const put = (key: string, value: string | number | null) => {\n if (value === null || value === '') clear.push(key)\n else set[key] = value\n }\n put('amountCents', parseAmountInput(values.amount))\n put('expectedCloseAtMs', dateInputMs(values.expectedClose))\n put('ownerUid', values.ownerUid.trim())\n put('contactId', values.contactId.trim())\n put('contactName', values.contactId.trim() ? values.contactName.trim() : '')\n put('companyId', values.companyId.trim())\n put('companyName', values.companyId.trim() ? values.companyName.trim() : '')\n put('notes', values.notes.trim().slice(0, DEAL_NOTES_MAX))\n put('type', values.type.trim())\n put('leadSource', values.leadSource.trim())\n put('nextStep', values.nextStep.trim().replace(/\\s+/g, ' ').slice(0, DEAL_NEXT_STEP_MAX))\n put('probability', readDealProbability(values.probability) ?? null)\n put('campaignId', values.campaignId.trim())\n return { set, clear }\n}\n\n/**\n * The document a new deal is created as. Open, in the chosen stage, with\n * the stage clock started now so \"days in stage\" counts from creation.\n */\nexport function dealDocumentFromForm(\n values: DealFormValues,\n context: DealWriteContext,\n): Record<string, unknown> {\n const title = values.title.trim().slice(0, DEAL_TITLE_MAX)\n const { set } = optionalFields(values)\n const forecastCategory = forecastCategoryFor(values, context.stage)\n const contactId = values.contactId.trim()\n return {\n title,\n titleLower: nameSearchKey(title),\n pipelineId: values.pipelineId,\n stageId: values.stageId,\n status: 'open' satisfies CrmDeal['status'],\n // The contact picked is the deal's Primary contact role (AGL-3521).\n ...(contactId ? { contactRoles: [{ contactId, primary: true }] } : {}),\n currency: String(values.currency || DEFAULT_DEAL_CURRENCY).toLowerCase(),\n stageChangedAtMs: context.nowMs,\n ...(forecastCategory ? { forecastCategory } : {}),\n ...set,\n visibleTo: context.visibleTo,\n hostId: context.hostId,\n createdByUid: context.uid,\n createdAt: new Date(context.nowMs),\n updatedAt: new Date(context.nowMs),\n }\n}\n\n/**\n * The patch an edit writes. Never the stage, the status or the scope: the\n * stage goes through the server route so the automations hear it, and the\n * scope is changed only by an org-wide member on purpose.\n *\n * Nor the amount or the currency of a deal whose amount is DERIVED\n * (`amountDerived`, AGL-2620): with line items on the deal the amount is\n * their sum and the currency is theirs, both owned by the products card. The\n * form shows them read-only, and a patch that re-wrote the number it was\n * seeded with would overwrite a line added from another tab meanwhile.\n */\nexport function dealPatchFromForm(\n values: DealFormValues,\n nowMs: number,\n options: {\n amountDerived?: boolean\n stage?: DealWriteContext['stage']\n /**\n * The deal as stored, whose contact roles a changed contact moves\n * (AGL-3521): the contact picked becomes the Primary — added with no\n * role when the deal did not name them — and a cleared contact leaves\n * the deal with no Primary. An unchanged contact writes no roles, so a\n * role edited on the deal's page meanwhile is not overwritten.\n */\n current?: Pick<CrmDeal, 'contactId' | 'contactRoles'> | null\n } = {},\n): { set: Record<string, unknown>; clear: string[] } {\n const title = values.title.trim().slice(0, DEAL_TITLE_MAX)\n const optional = optionalFields(values)\n const set: Record<string, unknown> = optional.set\n const { clear } = optional\n const contactId = values.contactId.trim()\n if (options.current && contactId !== (options.current.contactId ?? '')) {\n set['contactRoles'] = dealContactRolesWithPrimary(dealContactRolesOf(options.current), contactId || null)\n }\n // The category picked, else the stage's again; with neither, the field\n // goes and every reader derives it from the stage.\n const forecastCategory = forecastCategoryFor(values, options.stage)\n if (forecastCategory) set['forecastCategory'] = forecastCategory\n else clear.push('forecastCategory')\n if (options.amountDerived) {\n delete set['amountCents']\n return {\n set: { title, titleLower: nameSearchKey(title), ...set, updatedAt: new Date(nowMs) },\n clear: clear.filter((key) => key !== 'amountCents'),\n }\n }\n return {\n set: {\n title,\n titleLower: nameSearchKey(title),\n currency: String(values.currency || DEFAULT_DEAL_CURRENCY).toLowerCase(),\n ...set,\n updatedAt: new Date(nowMs),\n },\n clear,\n }\n}\n\n/** The document fields each form field writes. A blank list writes nothing on an edit. */\nconst DEAL_FORM_FIELD_WRITES: Readonly<Record<keyof DealFormValues, readonly string[]>> = {\n title: ['title', 'titleLower'],\n pipelineId: [],\n stageId: [],\n amount: ['amountCents'],\n currency: ['currency'],\n expectedClose: ['expectedCloseAtMs'],\n ownerUid: ['ownerUid'],\n contactId: ['contactId', 'contactName', 'contactRoles'],\n contactName: ['contactName'],\n companyId: ['companyId', 'companyName'],\n companyName: ['companyName'],\n notes: ['notes'],\n type: ['type'],\n leadSource: ['leadSource'],\n nextStep: ['nextStep'],\n probability: ['probability'],\n forecastCategory: ['forecastCategory'],\n campaignId: ['campaignId'],\n}\n\nconst DEAL_FORM_FIELDS = Object.keys(DEAL_FORM_FIELD_WRITES) as (keyof DealFormValues)[]\n\n/** What {@link dealEditFromForm} found the reader changed. */\nexport interface DealEdit {\n set: Record<string, unknown>\n clear: string[]\n}\n\n/**\n * The stored deal moved while the form was open (AGL-4132): the fields the\n * reader has not touched follow it, and the ones they have stay as typed.\n *\n * A listener hands over a new object for every snapshot, a confirmation of\n * unchanged data included, so a form rebuilt from each one loses whatever was\n * typed the moment the server answers.\n */\nexport function rebaseDealForm(\n current: DealFormValues,\n seed: DealFormValues,\n stored: DealFormValues,\n): DealFormValues {\n return Object.fromEntries(\n DEAL_FORM_FIELDS.map((field) => [\n field,\n current[field] === seed[field] ? stored[field] : current[field],\n ]),\n ) as unknown as DealFormValues\n}\n\n/**\n * The write a save makes on an existing deal: {@link dealPatchFromForm},\n * narrowed to the fields the reader changed from `seed`, with no `updatedAt`\n * so a caller can tell an edit from nothing at all (AGL-4132).\n *\n * The narrowing is what protects the deal. A patch of every field sent from a\n * read the server has not confirmed writes that read back over the document,\n * and one that held no `contactId` deletes the link. This one cannot touch a\n * field the reader left alone, however old the read behind the form.\n */\nexport function dealEditFromForm(\n values: DealFormValues,\n seed: DealFormValues,\n nowMs: number,\n options: Parameters<typeof dealPatchFromForm>[2] = {},\n): DealEdit {\n const { set, clear } = dealPatchFromForm(values, nowMs, options)\n const changed = new Set(\n DEAL_FORM_FIELDS\n .filter((field) => values[field] !== seed[field])\n .flatMap((field) => DEAL_FORM_FIELD_WRITES[field]),\n )\n return {\n set: Object.fromEntries(Object.entries(set).filter(([key]) => changed.has(key))),\n clear: clear.filter((key) => changed.has(key)),\n }\n}\n\n/** The fields an edit works out from the stored deal rather than takes as typed. */\nconst DEAL_EDIT_FIELDS_FROM_THE_READ = ['contactRoles', 'amountCents', 'currency'] as const\n\n/**\n * Whether an edit can only be trusted on a read the server has confirmed\n * (AGL-4132).\n *\n * The contact roles are the stored list with the Primary moved, so writing\n * them from an old read takes out a role added since. The amount and the\n * currency are the form's only while the deal has no line items, which the\n * read is what says. Everything else in an edit is a value the reader typed.\n */\nexport function dealEditNeedsConfirmedRead(edit: DealEdit): boolean {\n return DEAL_EDIT_FIELDS_FROM_THE_READ.some(\n (field) => field in edit.set || edit.clear.includes(field),\n )\n}\n\n/** A contact as the picker offers it. */\nexport interface ContactChoice {\n id: string\n name: string\n email: string\n}\n\n/**\n * The contacts a typed query matches, from a window of recent contact rows,\n * in the list's own grammar.\n *\n * In memory rather than by query because the contacts collection has no\n * composite index over `visibleTo` with `nameLower` or `email`, and a\n * scoped list can carry only one array clause — so the picker reads the\n * newest rows the viewer may see (bounded, ordered) and matches here. The\n * match is what the list's search does: the normalized query as a prefix of\n * the email, or as a prefix of any word of the name (`nameTokens` holds\n * every word prefix a write produced; `nameLower` covers rows written before\n * the tokens existed). The holder's own name for the person wins over the\n * canonical one, as everywhere else.\n */\nexport function contactChoicesFor(\n queryText: string,\n rows: readonly Record<string, unknown>[],\n /**\n * The holder each row is named through: one group for every row under a\n * site, or a resolver per row at the organization level (AGL-2630), where\n * each contact reads through its own primary holder.\n */\n groupId: string | ((row: Record<string, unknown>) => string),\n max = 8,\n): ContactChoice[] {\n const key = nameSearchKey(queryText)\n const token = nameSearchToken(queryText)\n const choices: ContactChoice[] = []\n for (const row of rows) {\n const email = String(row['email'] ?? '').toLowerCase()\n const name = contactDisplayName(\n row,\n typeof groupId === 'function' ? groupId(row) : groupId,\n )\n const nameLower = nameSearchKey(name)\n const tokens = Array.isArray(row['nameTokens'])\n ? (row['nameTokens'] as unknown[]).map(String)\n : []\n const matches =\n !key ||\n email.startsWith(key) ||\n nameLower.startsWith(key) ||\n nameLower.includes(` ${key}`) ||\n (token ? tokens.includes(token) : false)\n if (!matches) continue\n choices.push({ id: String(row['$id'] ?? ''), name, email })\n if (choices.length >= max) break\n }\n return choices\n}\n"],"names":["contactDisplayName","crmPicklistDefaultLabel","DEAL_NEXT_STEP_MAX","dealContactRolesOf","dealContactRolesWithPrimary","dealStageForecastCategory","isCrmForecastCategory","judgeCrmPicklistValue","nameSearchKey","nameSearchToken","readDealProbability","amountInputValue","dateInputMs","dateInputValue","DEFAULT_DEAL_CURRENCY","openStages","parseAmountInput","DEAL_TITLE_MAX","DEAL_NOTES_MAX","emptyDealForm","pipeline","defaults","firstOpen","title","pipelineId","$id","stageId","id","amount","currency","expectedClose","ownerUid","contactId","contactName","companyId","companyName","notes","type","leadSource","nextStep","probability","forecastCategory","campaignId","dealFormFromDoc","deal","String","amountCents","toLowerCase","expectedCloseAtMs","dealFormProblem","values","mode","trim","undefined","judgeDealFormPicklists","lists","options","typed","created","current","ok","value","forecastCategoryFor","stage","optionalFields","set","clear","put","key","push","slice","replace","dealDocumentFromForm","context","titleLower","status","contactRoles","primary","stageChangedAtMs","nowMs","visibleTo","hostId","createdByUid","uid","createdAt","Date","updatedAt","dealPatchFromForm","optional","amountDerived","filter","DEAL_FORM_FIELD_WRITES","DEAL_FORM_FIELDS","Object","keys","rebaseDealForm","seed","stored","fromEntries","map","field","dealEditFromForm","changed","Set","flatMap","entries","has","DEAL_EDIT_FIELDS_FROM_THE_READ","dealEditNeedsConfirmedRead","edit","some","includes","contactChoicesFor","queryText","rows","groupId","max","token","choices","row","email","name","nameLower","tokens","Array","isArray","matches","startsWith","length"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED;;;;;;;CAOC,GAED,SACEA,kBAAkB,EAKlBC,uBAAuB,EACvBC,kBAAkB,EAClBC,kBAAkB,EAClBC,2BAA2B,EAC3BC,yBAAyB,EACzBC,qBAAqB,EACrBC,qBAAqB,EACrBC,aAAa,EACbC,eAAe,EACfC,mBAAmB,QACd,eAAc;AACrB,SACEC,gBAAgB,EAChBC,WAAW,EACXC,cAAc,EACdC,qBAAqB,EAErBC,UAAU,EACVC,gBAAgB,QAEX,wBAAoB;AAkC3B,OAAO,MAAMC,iBAAiB,IAAG;AACjC,OAAO,MAAMC,iBAAiB,KAAI;AAElC;;;;;;;CAOC,GACD,OAAO,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