import { createElement, lazy } from 'react' import dayjs from 'dayjs' import InfoCircleIcon from '../../components/Icons/InfoCircleIcon' import PlusIcon from '../../components/Icons/PlusIcon' import ListIcon from '../../components/Icons/ListIcon' import EmailMessageIcon from '../../components/Icons/EmailMessageIcon' import DuplicateIcon from '../../components/Icons/DuplicateIcon' const EmailMessageInfo = lazy( () => import('../../components/Dashboard/Management/EmailMessages/EmailMessageInfo') ) const NewEmailMessage = lazy( () => import('../../components/Dashboard/Management/EmailMessages/NewEmailMessage') ) const readOnlyAfterCreate = (data) => data?._id != null export const EmailMessage = { name: 'emailMessage', label: 'Email Message', labelPlural: 'Email Messages', endpoint: 'emailmessages', url: '/dashboard/management/emailmessages', prefix: 'EMS', icon: EmailMessageIcon, readOnly: true, actions: [ { name: 'new', type: 'modal', pageName: 'list', modalWidth: 1100, label: 'New Email Message', icon: PlusIcon, content: (objectData, { onOk } = {}) => createElement(NewEmailMessage, { defaultValues: objectData, onOk }) }, { name: 'duplicate', type: 'alias', objectType: 'emailMessage', aliasAction: 'new', pageName: 'info', label: 'Duplicate', icon: DuplicateIcon, objectData: (objectData) => objectData }, { name: 'list', type: 'page', pageName: 'list', label: 'List', icon: ListIcon }, { name: 'info', type: 'page', pageName: 'info', label: 'Info', default: true, row: true, icon: InfoCircleIcon } ], pages: [ { name: 'info', content: () => createElement(EmailMessageInfo) } ], columns: [ '_reference', 'name', 'state', 'recipientEmail', 'fromEmail', 'messageId', 'emailTemplate', 'emailAccount', 'objectType', 'sentAt', 'readAt', 'createdAt', 'updatedAt' ], filters: [ 'name', 'state', 'recipientEmail', 'fromEmail', 'messageId', 'emailTemplate', 'emailAccount', 'objectType', 'recipientType', 'read', 'sentAt', 'readAt', 'createdAt', 'updatedAt', '_reference' ], group: [ 'state', 'emailTemplate', 'emailAccount', 'objectType', 'recipientType' ], sorters: [ 'name', 'state', 'recipientEmail', 'fromEmail', 'messageId', 'sentAt', 'readAt', 'createdAt', 'updatedAt' ], properties: [ { name: '_id', label: 'ID', type: 'id', objectType: 'emailMessage', showCopy: true, columnWidth: 140 }, { name: 'createdAt', label: 'Created At', type: 'dateTime', readOnly: true, columnWidth: 200 }, { name: '_reference', label: 'Reference', type: 'reference', objectType: 'emailMessage', showCopy: true, readOnly: true, columnFixed: 'left', columnWidth: 180 }, { name: 'updatedAt', label: 'Updated At', type: 'dateTime', readOnly: true, columnWidth: 200 }, { name: 'name', label: 'Name', type: 'text', required: true, readOnly: readOnlyAfterCreate, value: (data) => data?.createdAt == null ? `${data?.emailTemplate?.name || 'Email'} ${dayjs().format('YYYY-MM-DD HH:mm:ss')}` : undefined, columnFixed: 'left', columnWidth: 240 }, { name: 'sentAt', label: 'Sent At', type: 'dateTime', readOnly: true, columnWidth: 200 }, { name: 'state', label: 'Status', type: 'state', showName: false, readOnly: true, columnWidth: 240 }, { name: 'readAt', label: 'Read At', type: 'dateTime', readOnly: true, columnWidth: 200 }, { name: 'read', label: 'Read', type: 'bool', readOnly: true, columnWidth: 100 }, { name: 'emailTemplate', label: 'Template', type: 'object', objectType: 'emailTemplate', required: true, readOnly: readOnlyAfterCreate, showHyperlink: true, masterFilter: (data) => ({ active: true, global: false, objectType: data?.objectType }), columnWidth: 200 }, { name: 'objectType', label: 'Object Type', type: 'objectType', required: true, readOnly: readOnlyAfterCreate, columnWidth: 180 }, { name: 'object', label: 'Object', type: 'object', objectType: (data) => data?.objectType, required: true, readOnly: readOnlyAfterCreate, showHyperlink: true, columnWidth: 200 }, { name: 'emailAccount', label: 'Email Account', type: 'object', objectType: 'emailAccount', required: true, readOnly: readOnlyAfterCreate, showHyperlink: true, masterFilter: { active: true }, columnWidth: 200 }, { name: 'recipientEmail', label: 'Recipient Email', type: 'email', required: true, readOnly: readOnlyAfterCreate, options: (data) => Array.isArray(data?._recipientCandidates) && data._recipientCandidates.length ? data._recipientCandidates.map((candidate) => ({ value: candidate.email, label: candidate.label })) : undefined, columnWidth: 260 }, { name: 'recipientType', label: 'Recipient Type', type: 'objectType', readOnly: readOnlyAfterCreate, value: (data) => { if ( !Array.isArray(data?._recipientCandidates) || !data._recipientCandidates.length ) { return undefined } const match = data._recipientCandidates.find( (candidate) => candidate.email === data?.recipientEmail ) if (!match) return undefined return match.recipientType ?? null }, columnWidth: 160 }, { name: 'recipient', label: 'Recipient', type: 'object', objectType: (data) => data?.recipientType, readOnly: readOnlyAfterCreate, showHyperlink: true, value: (data) => { if ( !Array.isArray(data?._recipientCandidates) || !data._recipientCandidates.length ) { return undefined } const match = data._recipientCandidates.find( (candidate) => candidate.email === data?.recipientEmail ) if (!match) return undefined return match.recipient ?? null }, columnWidth: 200 }, { name: 'fromEmail', label: 'From Email', type: 'email', required: true, readOnly: true, value: (data) => data?.emailAccount?.fromEmail, columnWidth: 260 }, { name: 'messageId', label: 'Message ID', type: 'text', readOnly: true, showCopy: true, columnWidth: 280 }, { name: 'subject', label: 'Rendered Subject', type: 'text', readOnly: true, columnWidth: 300 }, { name: 'content', label: 'Rendered Body', type: 'codeBlock', language: 'html', readOnly: true }, { name: 'attachments', label: 'Attachments', type: 'objectList', objectType: 'file', readOnly: true, showHyperlink: true, columnWidth: 230 } ] }