import type { CollectionConfig } from "payload"; export const CrmContacts: CollectionConfig = { slug: "crm-contacts", labels: { singular: "Contact", plural: "Contacts", }, admin: { useAsTitle: "fullName", defaultColumns: ["fullName", "email", "account"], group: "CRM", description: "Contacts are the individual people linked to an Account. A person should only be created once and can be assigned to a company here.", }, access: { read: ({ req: { user } }) => Boolean(user), create: ({ req: { user } }) => Boolean(user), update: ({ req: { user } }) => Boolean(user), delete: ({ req: { user } }) => Boolean(user), }, hooks: { beforeChange: [ ({ data }) => { if (data?.firstName || data?.lastName) { data.fullName = `${data.firstName || ""} ${data.lastName || ""}`.trim(); } return data; }, ], afterRead: [ ({ doc }) => { if (!doc.fullName && (doc.firstName || doc.lastName)) { return { ...doc, fullName: `${doc.firstName || ""} ${doc.lastName || ""}`.trim(), }; } return doc; }, ], }, fields: [ { name: "fullName", type: "text", admin: { hidden: true, }, }, { type: "row", fields: [ { name: "firstName", type: "text", required: true, admin: { width: "50%", }, }, { name: "lastName", type: "text", required: true, admin: { width: "50%", }, }, ], }, { name: "email", type: "email", required: true, unique: true, admin: { description: "Primary email address for communication tracking.", }, }, { type: "row", fields: [ { name: "phone", type: "text", admin: { width: "50%", }, }, { name: "linkedIn", type: "text", admin: { width: "50%", placeholder: "https://linkedin.com/in/...", }, }, ], }, { name: "role", type: "text", label: "Job Title / Role", admin: { description: "e.g. CEO, Marketing Manager, Technical Lead", }, }, { name: "account", type: "relationship", relationTo: "crm-accounts", label: "Company / Account", admin: { description: "Link this person to an organization from the Accounts collection.", }, }, { name: "interactions", type: "join", collection: "crm-interactions", on: "contact", admin: { description: "Timeline of all communication logged directly with this person.", }, }, ], };