2023-11-20 10:01:43 +00:00
|
|
|
import contact, {
|
|
|
|
type Channel,
|
|
|
|
type Employee,
|
|
|
|
type Contact,
|
|
|
|
type PersonAccount,
|
|
|
|
getName as getContactName
|
|
|
|
} from '@hcengineering/contact'
|
|
|
|
import { type Doc, type IdMap, type Ref, toIdMap } from '@hcengineering/core'
|
|
|
|
import { type Message, type SharedMessage } from '@hcengineering/gmail'
|
2023-03-24 09:38:42 +00:00
|
|
|
import { getClient } from '@hcengineering/presentation'
|
2023-11-22 15:32:30 +00:00
|
|
|
import gmail from './plugin'
|
2023-03-24 09:38:42 +00:00
|
|
|
|
2021-12-30 09:13:16 +00:00
|
|
|
export function getTime (time: number): string {
|
|
|
|
let options: Intl.DateTimeFormatOptions = { hour: 'numeric', minute: 'numeric' }
|
|
|
|
if (!isCurrentYear(time)) {
|
|
|
|
options = {
|
|
|
|
year: '2-digit',
|
|
|
|
month: 'numeric',
|
|
|
|
day: 'numeric',
|
|
|
|
...options
|
|
|
|
}
|
|
|
|
} else if (!isToday(time)) {
|
|
|
|
options = {
|
|
|
|
month: 'numeric',
|
|
|
|
day: 'numeric',
|
|
|
|
...options
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Date(time).toLocaleString('default', options)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isToday (time: number): boolean {
|
|
|
|
const current = new Date()
|
|
|
|
const target = new Date(time)
|
|
|
|
return (
|
|
|
|
current.getDate() === target.getDate() &&
|
|
|
|
current.getMonth() === target.getMonth() &&
|
|
|
|
current.getFullYear() === target.getFullYear()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isCurrentYear (time: number): boolean {
|
|
|
|
const current = new Date()
|
|
|
|
const target = new Date(time)
|
|
|
|
return current.getFullYear() === target.getFullYear()
|
|
|
|
}
|
2023-03-24 09:38:42 +00:00
|
|
|
|
|
|
|
export async function checkHasEmail (doc: Doc | Doc[] | undefined): Promise<boolean> {
|
|
|
|
if (doc === undefined) return false
|
|
|
|
const client = getClient()
|
|
|
|
const arr = Array.isArray(doc) ? doc.map((p) => p._id) : [doc._id]
|
2023-04-05 07:16:34 +00:00
|
|
|
const res = await client.findAll(
|
|
|
|
contact.class.Channel,
|
|
|
|
{
|
|
|
|
provider: contact.channelProvider.Email,
|
|
|
|
attachedTo: { $in: arr }
|
|
|
|
},
|
|
|
|
{ projection: { _id: 1, attachedTo: 1 } }
|
|
|
|
)
|
|
|
|
const set = new Set(res.map((p) => p.attachedTo))
|
|
|
|
for (const val of arr) {
|
|
|
|
if (!set.has(val)) return false
|
|
|
|
}
|
|
|
|
return true
|
2023-03-24 09:38:42 +00:00
|
|
|
}
|
2023-04-07 06:09:16 +00:00
|
|
|
|
|
|
|
const EMAIL_REGEX =
|
|
|
|
/(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/
|
|
|
|
|
|
|
|
export function convertMessages (
|
|
|
|
object: Contact,
|
|
|
|
channel: Channel,
|
|
|
|
messages: Message[],
|
2023-08-04 18:06:21 +00:00
|
|
|
accounts: IdMap<PersonAccount>,
|
2023-04-07 06:09:16 +00:00
|
|
|
employees: IdMap<Employee>
|
|
|
|
): SharedMessage[] {
|
|
|
|
return messages.map((m) => {
|
|
|
|
return {
|
|
|
|
...m,
|
|
|
|
_id: m._id as string as Ref<SharedMessage>,
|
|
|
|
sender: getName(object, channel, m, accounts, employees, true),
|
|
|
|
receiver: getName(object, channel, m, accounts, employees, false)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function convertMessage (
|
|
|
|
object: Contact,
|
|
|
|
channel: Channel,
|
|
|
|
message: Message,
|
|
|
|
employees: IdMap<Employee>
|
|
|
|
): Promise<SharedMessage> {
|
|
|
|
const client = getClient()
|
2023-08-04 18:06:21 +00:00
|
|
|
const accounts = toIdMap(await client.findAll(contact.class.PersonAccount, {}))
|
2023-04-07 06:09:16 +00:00
|
|
|
return {
|
|
|
|
...message,
|
|
|
|
_id: message._id as string as Ref<SharedMessage>,
|
|
|
|
sender: getName(object, channel, message, accounts, employees, true),
|
|
|
|
receiver: getName(object, channel, message, accounts, employees, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getName (
|
|
|
|
object: Contact,
|
|
|
|
channel: Channel,
|
|
|
|
message: Message,
|
2023-08-04 18:06:21 +00:00
|
|
|
accounts: IdMap<PersonAccount>,
|
2023-04-07 06:09:16 +00:00
|
|
|
employees: IdMap<Employee>,
|
|
|
|
sender: boolean
|
|
|
|
): string {
|
2023-08-04 18:06:21 +00:00
|
|
|
const h = getClient().getHierarchy()
|
2023-11-22 15:32:30 +00:00
|
|
|
if (message._class === gmail.class.NewMessage) {
|
|
|
|
if (!sender) return `${getContactName(h, object)} (${channel.value})`
|
|
|
|
const account = accounts.get(message.modifiedBy as Ref<PersonAccount>)
|
|
|
|
const emp = account != null ? employees.get(account?.person as Ref<Employee>) : undefined
|
|
|
|
const email = account?.email
|
|
|
|
const from = accounts.get(message.from as Ref<PersonAccount>)?.email ?? message.from
|
|
|
|
return emp != null ? `${getContactName(h, emp)} (${email})` : from
|
|
|
|
}
|
2023-04-07 06:09:16 +00:00
|
|
|
if (message.incoming === sender) {
|
2023-08-04 18:06:21 +00:00
|
|
|
return `${getContactName(h, object)} (${channel.value})`
|
2023-04-07 06:09:16 +00:00
|
|
|
} else {
|
2023-08-04 18:06:21 +00:00
|
|
|
const account = accounts.get(message.modifiedBy as Ref<PersonAccount>)
|
|
|
|
const emp = account != null ? employees.get(account?.person as Ref<Employee>) : undefined
|
2023-04-07 06:09:16 +00:00
|
|
|
const value = message.incoming ? message.to : message.from
|
|
|
|
const email = value.match(EMAIL_REGEX)
|
|
|
|
const emailVal = email?.[0] ?? value
|
2023-08-04 18:06:21 +00:00
|
|
|
return emp != null ? `${getContactName(h, emp)} (${emailVal})` : emailVal
|
2023-04-07 06:09:16 +00:00
|
|
|
}
|
|
|
|
}
|