2022-02-07 09:03:14 +00:00
|
|
|
//
|
|
|
|
// Copyright © 2020, 2021 Anticrm Platform Contributors.
|
2022-04-29 05:27:17 +00:00
|
|
|
// Copyright © 2022 Hardcore Engineering Inc.
|
2022-02-07 09:03:14 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License. You may
|
|
|
|
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
//
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
2023-03-15 14:06:03 +00:00
|
|
|
import {
|
2023-03-22 02:48:57 +00:00
|
|
|
AvatarType,
|
2023-03-15 14:06:03 +00:00
|
|
|
contactId,
|
|
|
|
formatName,
|
2023-04-05 07:15:19 +00:00
|
|
|
getFirstName,
|
|
|
|
getLastName,
|
2024-03-15 04:03:50 +00:00
|
|
|
getName,
|
2024-04-16 06:54:29 +00:00
|
|
|
type Channel,
|
|
|
|
type ChannelProvider,
|
|
|
|
type Contact,
|
|
|
|
type Employee,
|
|
|
|
type Person,
|
|
|
|
type PersonAccount
|
2023-03-15 14:06:03 +00:00
|
|
|
} from '@hcengineering/contact'
|
2024-04-23 17:36:26 +00:00
|
|
|
import core, {
|
2024-04-16 06:54:29 +00:00
|
|
|
getCurrentAccount,
|
|
|
|
toIdMap,
|
2024-05-27 07:25:57 +00:00
|
|
|
type Account,
|
2024-04-16 06:54:29 +00:00
|
|
|
type Class,
|
2023-11-20 10:01:43 +00:00
|
|
|
type Client,
|
|
|
|
type Doc,
|
|
|
|
type IdMap,
|
|
|
|
type ObjQueryType,
|
|
|
|
type Ref,
|
|
|
|
type Timestamp,
|
2024-04-23 17:36:26 +00:00
|
|
|
type TxOperations,
|
2024-05-27 07:25:57 +00:00
|
|
|
type UserStatus,
|
2024-06-25 05:03:01 +00:00
|
|
|
type WithLookup,
|
|
|
|
AggregateValue,
|
|
|
|
type Space,
|
|
|
|
type Hierarchy,
|
|
|
|
type DocumentQuery,
|
|
|
|
AggregateValueData,
|
|
|
|
matchQuery
|
2023-09-04 17:06:34 +00:00
|
|
|
} from '@hcengineering/core'
|
2024-01-11 14:46:11 +00:00
|
|
|
import notification, { type DocNotifyContext, type InboxNotification } from '@hcengineering/notification'
|
2024-03-15 04:03:50 +00:00
|
|
|
import { getEmbeddedLabel, getResource, translate } from '@hcengineering/platform'
|
2023-01-14 10:54:54 +00:00
|
|
|
import { createQuery, getClient } from '@hcengineering/presentation'
|
2023-11-20 10:01:43 +00:00
|
|
|
import { type TemplateDataProvider } from '@hcengineering/templates'
|
|
|
|
import {
|
|
|
|
getCurrentResolvedLocation,
|
2024-02-21 04:52:19 +00:00
|
|
|
getPanelURI,
|
2024-04-16 06:54:29 +00:00
|
|
|
type LabelAndProps,
|
|
|
|
type Location,
|
|
|
|
type ResolvedLocation,
|
|
|
|
type TabItem
|
2023-11-20 10:01:43 +00:00
|
|
|
} from '@hcengineering/ui'
|
2024-06-25 05:03:01 +00:00
|
|
|
import view, { type GrouppingManager, type Filter } from '@hcengineering/view'
|
2024-06-12 12:48:35 +00:00
|
|
|
import { FilterQuery, accessDeniedStore } from '@hcengineering/view-resources'
|
2023-08-04 18:06:21 +00:00
|
|
|
import { derived, get, writable } from 'svelte/store'
|
2024-02-21 04:52:19 +00:00
|
|
|
|
2023-02-21 06:40:03 +00:00
|
|
|
import contact from './plugin'
|
2024-06-25 05:03:01 +00:00
|
|
|
import { personStore } from '.'
|
2022-02-07 09:03:14 +00:00
|
|
|
|
2022-05-12 07:05:26 +00:00
|
|
|
export function formatDate (dueDateMs: Timestamp): string {
|
|
|
|
return new Date(dueDateMs).toLocaleString('default', {
|
|
|
|
month: 'short',
|
|
|
|
day: 'numeric',
|
|
|
|
hour: '2-digit',
|
|
|
|
minute: '2-digit'
|
|
|
|
})
|
|
|
|
}
|
2023-01-14 10:54:54 +00:00
|
|
|
|
2023-09-04 17:06:34 +00:00
|
|
|
export async function employeeSort (client: TxOperations, value: Array<Ref<Employee>>): Promise<Array<Ref<Employee>>> {
|
2023-10-27 05:22:43 +00:00
|
|
|
const h = client.getHierarchy()
|
2023-03-17 16:05:22 +00:00
|
|
|
return value.sort((a, b) => {
|
|
|
|
const employeeId1 = a as Ref<Employee> | null | undefined
|
|
|
|
const employeeId2 = b as Ref<Employee> | null | undefined
|
|
|
|
|
|
|
|
if (employeeId1 == null && employeeId2 != null) {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if (employeeId1 != null && employeeId2 == null) {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
if (employeeId1 != null && employeeId2 != null) {
|
|
|
|
const employee1 = get(employeeByIdStore).get(employeeId1)
|
|
|
|
const employee2 = get(employeeByIdStore).get(employeeId2)
|
2023-08-04 18:06:21 +00:00
|
|
|
const name1 = employee1 != null ? getName(h, employee1) : ''
|
|
|
|
const name2 = employee2 != null ? getName(h, employee2) : ''
|
2023-03-17 16:05:22 +00:00
|
|
|
|
|
|
|
return name1.localeCompare(name2)
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
2023-01-14 10:54:54 +00:00
|
|
|
})
|
|
|
|
}
|
2023-02-20 10:03:35 +00:00
|
|
|
|
2023-06-02 14:22:38 +00:00
|
|
|
export async function filterChannelHasMessagesResult (filter: Filter, onUpdate: () => void): Promise<ObjQueryType<any>> {
|
|
|
|
const result = await getRefs(filter, onUpdate, true)
|
|
|
|
return { $in: result }
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function filterChannelHasNewMessagesResult (
|
|
|
|
filter: Filter,
|
|
|
|
onUpdate: () => void
|
|
|
|
): Promise<ObjQueryType<any>> {
|
2024-01-11 14:46:11 +00:00
|
|
|
const inboxClient = (await getResource(notification.function.GetInboxNotificationsClient))()
|
|
|
|
const result = await getRefs(
|
|
|
|
filter,
|
|
|
|
onUpdate,
|
|
|
|
undefined,
|
2024-03-25 03:55:14 +00:00
|
|
|
get(inboxClient.contextByDoc),
|
2024-01-11 14:46:11 +00:00
|
|
|
get(inboxClient.inboxNotificationsByContext)
|
|
|
|
)
|
2023-06-02 14:22:38 +00:00
|
|
|
return { $in: result }
|
|
|
|
}
|
|
|
|
|
2023-02-20 10:03:35 +00:00
|
|
|
export async function filterChannelInResult (filter: Filter, onUpdate: () => void): Promise<ObjQueryType<any>> {
|
|
|
|
const result = await getRefs(filter, onUpdate)
|
|
|
|
return { $in: result }
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function filterChannelNinResult (filter: Filter, onUpdate: () => void): Promise<ObjQueryType<any>> {
|
|
|
|
const result = await getRefs(filter, onUpdate)
|
|
|
|
return { $nin: result }
|
|
|
|
}
|
|
|
|
|
2023-06-02 14:22:38 +00:00
|
|
|
export async function getRefs (
|
|
|
|
filter: Filter,
|
|
|
|
onUpdate: () => void,
|
|
|
|
hasMessages?: boolean,
|
2024-01-11 14:46:11 +00:00
|
|
|
docUpdates?: Map<Ref<Doc>, DocNotifyContext>,
|
|
|
|
inboxNotificationsByContext?: Map<Ref<DocNotifyContext>, InboxNotification[]>
|
2023-06-02 14:22:38 +00:00
|
|
|
): Promise<Array<Ref<Doc>>> {
|
2023-02-20 10:03:35 +00:00
|
|
|
const lq = FilterQuery.getLiveQuery(filter.index)
|
|
|
|
const client = getClient()
|
|
|
|
const mode = await client.findOne(view.class.FilterMode, { _id: filter.mode })
|
|
|
|
if (mode === undefined) return []
|
|
|
|
const promise = new Promise<Array<Ref<Doc>>>((resolve, reject) => {
|
2023-06-02 14:22:38 +00:00
|
|
|
const hasMessagesQuery = hasMessages === true ? { items: { $gt: 0 } } : {}
|
2023-02-20 10:03:35 +00:00
|
|
|
const refresh = lq.query(
|
|
|
|
contact.class.Channel,
|
|
|
|
{
|
2023-06-02 14:22:38 +00:00
|
|
|
provider: { $in: filter.value },
|
|
|
|
...hasMessagesQuery
|
2023-02-20 10:03:35 +00:00
|
|
|
},
|
|
|
|
(refs) => {
|
2023-06-02 14:22:38 +00:00
|
|
|
const filteredRefs =
|
2024-01-11 14:46:11 +00:00
|
|
|
docUpdates !== undefined && inboxNotificationsByContext !== undefined
|
2023-06-02 14:22:38 +00:00
|
|
|
? refs.filter((channel) => {
|
|
|
|
const docUpdate = docUpdates.get(channel._id)
|
2024-01-11 14:46:11 +00:00
|
|
|
return docUpdate != null
|
|
|
|
? inboxNotificationsByContext.get(docUpdate._id)?.some(({ isViewed }) => !isViewed)
|
|
|
|
: (channel.items ?? 0) > 0
|
2023-06-02 14:22:38 +00:00
|
|
|
})
|
|
|
|
: refs
|
|
|
|
const result = Array.from(new Set(filteredRefs.map((p) => p.attachedTo)))
|
2023-02-20 10:03:35 +00:00
|
|
|
FilterQuery.results.set(filter.index, result)
|
|
|
|
resolve(result)
|
|
|
|
onUpdate()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if (!refresh) {
|
|
|
|
resolve(FilterQuery.results.get(filter.index) ?? [])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return await promise
|
|
|
|
}
|
2023-02-21 06:40:03 +00:00
|
|
|
|
|
|
|
export async function getCurrentEmployeeName (): Promise<string> {
|
2023-08-04 18:06:21 +00:00
|
|
|
const me = getCurrentAccount() as PersonAccount
|
2023-08-24 15:51:30 +00:00
|
|
|
const client = getClient()
|
|
|
|
const employee = await client.findOne(contact.class.Person, { _id: me.person })
|
|
|
|
return employee !== undefined ? formatName(employee.name) : ''
|
2023-02-21 06:40:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getCurrentEmployeeEmail (): Promise<string> {
|
2023-08-04 18:06:21 +00:00
|
|
|
const me = getCurrentAccount() as PersonAccount
|
2023-02-21 06:40:03 +00:00
|
|
|
return me.email
|
|
|
|
}
|
|
|
|
|
2023-04-05 09:51:05 +00:00
|
|
|
export async function getCurrentEmployeePosition (): Promise<string | undefined> {
|
2023-08-04 18:06:21 +00:00
|
|
|
const me = getCurrentAccount() as PersonAccount
|
2023-04-05 09:51:05 +00:00
|
|
|
const client = getClient()
|
2023-08-04 18:06:21 +00:00
|
|
|
const employee = await client.findOne<Employee>(contact.mixin.Employee, { _id: me.person as Ref<Employee> })
|
2023-04-05 09:51:05 +00:00
|
|
|
if (employee !== undefined) {
|
|
|
|
return employee.position ?? ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 06:40:03 +00:00
|
|
|
export async function getContactName (provider: TemplateDataProvider): Promise<string | undefined> {
|
2023-02-28 18:36:00 +00:00
|
|
|
const value = provider.get(contact.class.Contact) as Contact
|
2023-02-21 06:40:03 +00:00
|
|
|
if (value === undefined) return
|
|
|
|
const client = getClient()
|
|
|
|
const hierarchy = client.getHierarchy()
|
|
|
|
if (hierarchy.isDerived(value._class, contact.class.Person)) {
|
2023-08-04 18:06:21 +00:00
|
|
|
return getName(client.getHierarchy(), value)
|
2023-02-21 06:40:03 +00:00
|
|
|
} else {
|
|
|
|
return value.name
|
|
|
|
}
|
|
|
|
}
|
2023-03-15 14:06:03 +00:00
|
|
|
|
2023-04-05 07:15:19 +00:00
|
|
|
export async function getContactLastName (provider: TemplateDataProvider): Promise<string | undefined> {
|
|
|
|
const value = provider.get(contact.class.Contact) as Contact
|
|
|
|
if (value === undefined) return
|
|
|
|
const client = getClient()
|
|
|
|
const hierarchy = client.getHierarchy()
|
|
|
|
if (hierarchy.isDerived(value._class, contact.class.Person)) {
|
|
|
|
return getLastName(value.name)
|
|
|
|
} else {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getContactFirstName (provider: TemplateDataProvider): Promise<string | undefined> {
|
|
|
|
const value = provider.get(contact.class.Contact) as Contact
|
|
|
|
if (value === undefined) return
|
|
|
|
const client = getClient()
|
|
|
|
const hierarchy = client.getHierarchy()
|
|
|
|
if (hierarchy.isDerived(value._class, contact.class.Person)) {
|
|
|
|
return getFirstName(value.name)
|
|
|
|
} else {
|
|
|
|
return value.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-11 10:17:47 +00:00
|
|
|
export async function getContactChannel (value: Contact, provider: Ref<ChannelProvider>): Promise<string | undefined> {
|
|
|
|
if (value === undefined) return
|
|
|
|
const client = getClient()
|
|
|
|
const res = await client.findOne(contact.class.Channel, {
|
|
|
|
attachedTo: value._id,
|
|
|
|
provider
|
|
|
|
})
|
|
|
|
return res?.value ?? ''
|
|
|
|
}
|
|
|
|
|
2023-03-20 08:45:52 +00:00
|
|
|
export async function getContactLink (doc: Doc): Promise<Location> {
|
2023-04-21 16:24:45 +00:00
|
|
|
const loc = getCurrentResolvedLocation()
|
2023-03-20 08:45:52 +00:00
|
|
|
loc.path.length = 2
|
|
|
|
loc.fragment = undefined
|
|
|
|
loc.query = undefined
|
|
|
|
loc.path[2] = contactId
|
|
|
|
loc.path[3] = doc._id
|
2023-03-16 04:47:55 +00:00
|
|
|
|
2023-03-20 08:45:52 +00:00
|
|
|
return loc
|
2023-03-15 14:06:03 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 08:45:52 +00:00
|
|
|
export async function resolveLocation (loc: Location): Promise<ResolvedLocation | undefined> {
|
|
|
|
if (loc.path[2] !== contactId) {
|
2023-03-15 14:06:03 +00:00
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2023-03-20 08:45:52 +00:00
|
|
|
const id = loc.path[3] as Ref<Contact>
|
2023-05-03 07:32:23 +00:00
|
|
|
if (id !== undefined) {
|
|
|
|
return await generateLocation(loc, id)
|
|
|
|
}
|
2023-03-15 14:06:03 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 08:45:52 +00:00
|
|
|
async function generateLocation (loc: Location, id: Ref<Contact>): Promise<ResolvedLocation | undefined> {
|
2023-03-15 14:06:03 +00:00
|
|
|
const client = getClient()
|
2023-03-20 08:45:52 +00:00
|
|
|
const doc = await client.findOne(contact.class.Contact, { _id: id })
|
2023-03-15 14:06:03 +00:00
|
|
|
if (doc === undefined) {
|
2024-06-12 12:48:35 +00:00
|
|
|
accessDeniedStore.set(true)
|
2023-03-20 08:45:52 +00:00
|
|
|
console.error(`Could not find contact ${id}.`)
|
2023-03-15 14:06:03 +00:00
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
const appComponent = loc.path[0] ?? ''
|
|
|
|
const workspace = loc.path[1] ?? ''
|
2023-06-05 16:17:16 +00:00
|
|
|
const special = client.getHierarchy().isDerived(doc._class, contact.class.Organization)
|
|
|
|
? 'companies'
|
2023-08-04 18:06:21 +00:00
|
|
|
: client.getHierarchy().isDerived(doc._class, contact.mixin.Employee)
|
2023-06-05 16:17:16 +00:00
|
|
|
? 'employees'
|
|
|
|
: 'persons'
|
2024-03-06 08:01:05 +00:00
|
|
|
|
|
|
|
const objectPanel = client.getHierarchy().classHierarchyMixin(doc._class as Ref<Class<Doc>>, view.mixin.ObjectPanel)
|
|
|
|
const component = objectPanel?.component ?? view.component.EditDoc
|
|
|
|
|
2023-03-15 14:06:03 +00:00
|
|
|
return {
|
2023-03-20 08:45:52 +00:00
|
|
|
loc: {
|
|
|
|
path: [appComponent, workspace],
|
2024-03-06 08:01:05 +00:00
|
|
|
fragment: getPanelURI(component, doc._id, doc._class, 'content')
|
2023-03-20 08:45:52 +00:00
|
|
|
},
|
|
|
|
defaultLocation: {
|
2023-06-05 16:17:16 +00:00
|
|
|
path: [appComponent, workspace, contactId, special],
|
2024-03-06 08:01:05 +00:00
|
|
|
fragment: getPanelURI(component, doc._id, doc._class, 'content')
|
2023-03-20 08:45:52 +00:00
|
|
|
}
|
2023-03-15 14:06:03 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-17 16:05:22 +00:00
|
|
|
|
2024-05-27 07:25:57 +00:00
|
|
|
export const employeeByIdStore = writable<IdMap<WithLookup<Employee>>>(new Map())
|
|
|
|
export const employeesStore = writable<Array<WithLookup<Employee>>>([])
|
2023-04-14 17:18:23 +00:00
|
|
|
|
2023-08-04 18:06:21 +00:00
|
|
|
export const personAccountByIdStore = writable<IdMap<PersonAccount>>(new Map())
|
2023-04-14 17:18:23 +00:00
|
|
|
|
2023-04-12 10:10:07 +00:00
|
|
|
export const channelProviders = writable<ChannelProvider[]>([])
|
2023-04-05 07:12:06 +00:00
|
|
|
|
2024-05-27 07:25:57 +00:00
|
|
|
export const personAccountPersonByIdStore = writable<IdMap<WithLookup<Person>>>(new Map())
|
2023-08-04 18:06:21 +00:00
|
|
|
|
2024-04-23 17:36:26 +00:00
|
|
|
export const statusByUserStore = writable<Map<Ref<Account>, UserStatus>>(new Map())
|
|
|
|
|
2023-08-04 18:06:21 +00:00
|
|
|
export const personByIdStore = derived([personAccountPersonByIdStore, employeeByIdStore], (vals) => {
|
|
|
|
const m1 = Array.from(vals[0].entries())
|
|
|
|
const m2 = Array.from(vals[1].entries())
|
|
|
|
return new Map([...m1, ...m2])
|
|
|
|
})
|
|
|
|
|
2023-04-12 10:10:07 +00:00
|
|
|
function fillStores (): void {
|
2023-04-05 07:12:06 +00:00
|
|
|
const client = getClient()
|
2023-08-04 18:06:21 +00:00
|
|
|
|
2023-04-05 07:12:06 +00:00
|
|
|
if (client !== undefined) {
|
2023-08-04 18:06:21 +00:00
|
|
|
const accountPersonQuery = createQuery(true)
|
|
|
|
|
2023-04-05 07:12:06 +00:00
|
|
|
const query = createQuery(true)
|
2024-08-19 05:41:31 +00:00
|
|
|
query.query(contact.mixin.Employee, { active: { $in: [true, false] } }, (res) => {
|
2024-08-02 05:08:24 +00:00
|
|
|
employeesStore.set(res)
|
|
|
|
employeeByIdStore.set(toIdMap(res))
|
|
|
|
})
|
2023-04-12 10:10:07 +00:00
|
|
|
|
2023-04-14 17:18:23 +00:00
|
|
|
const accountQ = createQuery(true)
|
2023-08-04 18:06:21 +00:00
|
|
|
accountQ.query(contact.class.PersonAccount, {}, (res) => {
|
|
|
|
personAccountByIdStore.set(toIdMap(res))
|
|
|
|
|
|
|
|
const persons = res.map((it) => it.person)
|
|
|
|
|
2024-08-15 10:30:25 +00:00
|
|
|
accountPersonQuery.query<Person>(contact.class.Person, { _id: { $in: persons } }, (res) => {
|
|
|
|
const personIn = toIdMap(res)
|
|
|
|
personAccountPersonByIdStore.set(personIn)
|
|
|
|
})
|
2023-04-14 17:18:23 +00:00
|
|
|
})
|
|
|
|
|
2023-04-12 10:10:07 +00:00
|
|
|
const providerQuery = createQuery(true)
|
2023-11-20 10:01:43 +00:00
|
|
|
providerQuery.query(contact.class.ChannelProvider, {}, (res) => {
|
|
|
|
channelProviders.set(res)
|
|
|
|
})
|
2023-04-05 07:12:06 +00:00
|
|
|
} else {
|
2023-11-20 10:01:43 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
fillStores()
|
|
|
|
}, 50)
|
2023-04-05 07:12:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-12 10:10:07 +00:00
|
|
|
fillStores()
|
2023-03-22 02:48:57 +00:00
|
|
|
|
2024-04-23 17:36:26 +00:00
|
|
|
const userStatusesQuery = createQuery(true)
|
|
|
|
|
|
|
|
export function loadUsersStatus (): void {
|
|
|
|
userStatusesQuery.query(core.class.UserStatus, {}, (res) => {
|
|
|
|
statusByUserStore.set(new Map(res.map((it) => [it.user, it])))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-29 14:40:00 +00:00
|
|
|
export function getAvatarTypeDropdownItems (hasGravatar: boolean, imageOnly?: boolean): TabItem[] {
|
|
|
|
if (imageOnly === true) {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
id: AvatarType.IMAGE,
|
|
|
|
labelIntl: contact.string.UseImage
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2023-03-22 02:48:57 +00:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
id: AvatarType.COLOR,
|
2023-05-26 03:02:47 +00:00
|
|
|
labelIntl: contact.string.UseColor
|
2023-03-22 02:48:57 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: AvatarType.IMAGE,
|
2023-05-26 03:02:47 +00:00
|
|
|
labelIntl: contact.string.UseImage
|
2023-03-22 02:48:57 +00:00
|
|
|
},
|
|
|
|
...(hasGravatar
|
|
|
|
? [
|
|
|
|
{
|
|
|
|
id: AvatarType.GRAVATAR,
|
2023-05-26 03:02:47 +00:00
|
|
|
labelIntl: contact.string.UseGravatar
|
2023-03-22 02:48:57 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
: [])
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2023-12-11 11:54:36 +00:00
|
|
|
export async function contactTitleProvider (client: Client, ref: Ref<Contact>, doc?: Contact): Promise<string> {
|
|
|
|
const object = doc ?? (await client.findOne(contact.class.Contact, { _id: ref }))
|
2023-04-19 16:17:41 +00:00
|
|
|
if (object === undefined) return ''
|
2023-08-04 18:06:21 +00:00
|
|
|
return getName(client.getHierarchy(), object)
|
2023-04-19 16:17:41 +00:00
|
|
|
}
|
2024-02-21 04:52:19 +00:00
|
|
|
|
|
|
|
export function getPersonTooltip (client: Client, value: Person | null | undefined): LabelAndProps | undefined {
|
|
|
|
const hierarchy = client.getHierarchy()
|
|
|
|
|
|
|
|
return value == null
|
|
|
|
? undefined
|
|
|
|
: {
|
|
|
|
label: getEmbeddedLabel(getName(hierarchy, value))
|
|
|
|
}
|
|
|
|
}
|
2024-03-15 04:03:50 +00:00
|
|
|
|
|
|
|
export async function channelIdentifierProvider (client: Client, ref: Ref<Channel>, doc?: Channel): Promise<string> {
|
|
|
|
const channel = doc ?? (await client.findOne(contact.class.Channel, { _id: ref }))
|
|
|
|
if (channel === undefined) return ''
|
|
|
|
|
|
|
|
const provider = await client.findOne(contact.class.ChannelProvider, { _id: channel.provider })
|
|
|
|
|
|
|
|
if (provider === undefined) return channel.value
|
|
|
|
|
|
|
|
return await translate(provider.label, {})
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function channelTitleProvider (client: Client, ref: Ref<Channel>, doc?: Channel): Promise<string> {
|
|
|
|
const channel = doc ?? (await client.findOne(contact.class.Channel, { _id: ref }))
|
|
|
|
|
|
|
|
if (channel === undefined) return ''
|
|
|
|
|
|
|
|
return channel.value
|
|
|
|
}
|
2024-06-25 05:03:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export const grouppingPersonManager: GrouppingManager = {
|
|
|
|
groupByCategories: groupByPersonAccountCategories,
|
|
|
|
groupValues: groupPersonAccountValues,
|
|
|
|
groupValuesWithEmpty: groupPersonAccountValuesWithEmpty,
|
|
|
|
hasValue: hasPersonAccountValue
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function groupByPersonAccountCategories (categories: any[]): AggregateValue[] {
|
|
|
|
const mgr = get(personStore)
|
|
|
|
|
|
|
|
const existingCategories: AggregateValue[] = [new AggregateValue(undefined, [])]
|
|
|
|
const personMap = new Map<string, AggregateValue>()
|
|
|
|
|
|
|
|
const usedSpaces = new Set<Ref<Space>>()
|
|
|
|
const personAccountList: Array<WithLookup<PersonAccount>> = []
|
|
|
|
for (const v of categories) {
|
|
|
|
const personAccount = mgr.getIdMap().get(v)
|
|
|
|
if (personAccount !== undefined) {
|
|
|
|
personAccountList.push(personAccount)
|
|
|
|
usedSpaces.add(personAccount.space)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const personAccount of personAccountList) {
|
|
|
|
if (personAccount !== undefined) {
|
|
|
|
let fst = personMap.get(personAccount.person)
|
|
|
|
if (fst === undefined) {
|
|
|
|
const people = mgr
|
|
|
|
.getDocs()
|
|
|
|
.filter(
|
|
|
|
(it) => it.person === personAccount.person && (categories.includes(it._id) || usedSpaces.has(it.space))
|
|
|
|
)
|
|
|
|
.sort((a, b) => a.email.localeCompare(b.email))
|
|
|
|
.map((it) => new AggregateValueData(it.person, it._id, it.space))
|
|
|
|
fst = new AggregateValue(personAccount.person, people)
|
|
|
|
personMap.set(personAccount.person, fst)
|
2024-07-16 05:07:27 +00:00
|
|
|
if (fst.name === undefined) {
|
|
|
|
existingCategories[0] = new AggregateValue(undefined, [...existingCategories[0].values, ...fst.values])
|
|
|
|
// Join with first value
|
|
|
|
} else {
|
|
|
|
existingCategories.push(fst)
|
|
|
|
}
|
2024-06-25 05:03:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return existingCategories
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function groupPersonAccountValues (val: Doc[], targets: Set<any>): Doc[] {
|
|
|
|
const values = val
|
|
|
|
const result: Doc[] = []
|
|
|
|
const unique = [...new Set(val.map((c) => (c as PersonAccount).person))]
|
|
|
|
unique.forEach((label, i) => {
|
|
|
|
let exists = false
|
|
|
|
values.forEach((c) => {
|
|
|
|
if ((c as PersonAccount).person === label) {
|
|
|
|
if (!exists) {
|
|
|
|
result[i] = c
|
|
|
|
exists = targets.has(c?._id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function hasPersonAccountValue (value: Doc | undefined | null, values: any[]): boolean {
|
|
|
|
const mgr = get(personStore)
|
|
|
|
const personSet = new Set(mgr.filter((it) => it.person === (value as PersonAccount)?.person).map((it) => it._id))
|
|
|
|
return values.some((it) => personSet.has(it))
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function groupPersonAccountValuesWithEmpty (
|
|
|
|
hierarchy: Hierarchy,
|
|
|
|
_class: Ref<Class<Doc>>,
|
|
|
|
key: string,
|
|
|
|
query: DocumentQuery<Doc> | undefined
|
|
|
|
): Array<Ref<Doc>> {
|
|
|
|
const mgr = get(personStore)
|
|
|
|
let personAccountList = mgr.getDocs()
|
|
|
|
if (query !== undefined) {
|
|
|
|
const { [key]: st, space } = query
|
|
|
|
const resQuery: DocumentQuery<Doc> = {}
|
|
|
|
if (space !== undefined) {
|
|
|
|
resQuery.space = space
|
|
|
|
}
|
|
|
|
if (st !== undefined) {
|
|
|
|
resQuery._id = st
|
|
|
|
}
|
|
|
|
personAccountList = matchQuery<Doc>(personAccountList, resQuery, _class, hierarchy) as unknown as Array<
|
|
|
|
WithLookup<PersonAccount>
|
|
|
|
>
|
|
|
|
}
|
|
|
|
return personAccountList.map((it) => it._id)
|
|
|
|
}
|