2023-04-07 06:09:16 +00:00
|
|
|
import contact, { Channel, Contact, Employee, EmployeeAccount, getName as getContactName } from '@hcengineering/contact'
|
|
|
|
import { Doc, IdMap, Ref, toIdMap } from '@hcengineering/core'
|
|
|
|
import { Message, SharedMessage } from '@hcengineering/gmail'
|
2023-03-24 09:38:42 +00:00
|
|
|
import { getClient } from '@hcengineering/presentation'
|
|
|
|
|
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[],
|
|
|
|
accounts: IdMap<EmployeeAccount>,
|
|
|
|
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()
|
|
|
|
const accounts = toIdMap(await client.findAll(contact.class.EmployeeAccount, {}))
|
|
|
|
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,
|
|
|
|
accounts: IdMap<EmployeeAccount>,
|
|
|
|
employees: IdMap<Employee>,
|
|
|
|
sender: boolean
|
|
|
|
): string {
|
|
|
|
if (message.incoming === sender) {
|
|
|
|
return `${getContactName(object)} (${channel.value})`
|
|
|
|
} else {
|
|
|
|
const account = accounts.get(message.modifiedBy as Ref<EmployeeAccount>)
|
|
|
|
const emp = account != null ? employees.get(account?.employee) : undefined
|
|
|
|
const value = message.incoming ? message.to : message.from
|
|
|
|
const email = value.match(EMAIL_REGEX)
|
|
|
|
const emailVal = email?.[0] ?? value
|
|
|
|
return emp != null ? `${getContactName(emp)} (${emailVal})` : emailVal
|
|
|
|
}
|
|
|
|
}
|