Add FirstMatchValue and Filter functions (#10042)

Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
Denis Bykhov
2025-10-07 23:17:04 +07:00
committed by GitHub
parent 0a682d03e3
commit f8205e57a1
29 changed files with 423 additions and 68 deletions
@@ -14,20 +14,21 @@
//
import contact, { Employee, Person } from '@hcengineering/contact'
import { Doc, Ref, Timestamp } from '@hcengineering/core'
import core, { Doc, matchQuery, Ref, Timestamp } from '@hcengineering/core'
import { Execution, parseContext } from '@hcengineering/process'
import { ProcessControl } from '@hcengineering/server-process'
import { getContextValue } from './utils'
// #region ArrayReduce
export function FirstValue (value: Doc[]): Doc {
export function FirstValue (value: Doc[]): Doc | undefined {
if (!Array.isArray(value)) return value
return value[0]
}
export function LastValue (value: Doc[]): Doc {
export function LastValue (value: Doc[]): Doc | undefined {
if (!Array.isArray(value)) return value
if (value.length === 0) return
return value[value.length - 1]
}
@@ -40,16 +41,36 @@ export function All (value: Doc[]): Doc[] {
return value
}
export async function FirstMatchValue (
value: any[],
props: Record<string, any>,
control: ProcessControl
): Promise<any | undefined> {
if (value == null) {
return
}
if (!Array.isArray(value)) return value
const { _class, ...otherProps } = props
if (_class == null) return
if (value.length === 0) return
if (typeof value[0] === 'string') {
const docs = await control.client.findAll(_class, { _id: { $in: value } })
return matchQuery(docs, otherProps, core.class.Doc, control.client.getHierarchy(), true)[0]?._id
} else if (typeof value[0] === 'object') {
return matchQuery(value, otherProps, core.class.Doc, control.client.getHierarchy(), true)[0]
}
}
// #endregion
// #region Array
export async function Insert (
value: Doc[],
value: any[],
props: Record<string, any>,
control: ProcessControl,
execution: Execution
): Promise<Doc[]> {
): Promise<any[]> {
if (value == null) {
value = []
}
@@ -66,11 +87,11 @@ export async function Insert (
}
export async function Remove (
value: Doc[],
value: any[],
props: Record<string, any>,
control: ProcessControl,
execution: Execution
): Promise<Doc[]> {
): Promise<any[]> {
if (value == null) {
value = []
}
@@ -85,7 +106,7 @@ export async function Remove (
}
}
export function RemoveFirst (value: Doc[], props: Record<string, any>): Doc[] {
export function RemoveFirst (value: any[]): any[] {
if (value == null) {
value = []
}
@@ -93,7 +114,7 @@ export function RemoveFirst (value: Doc[], props: Record<string, any>): Doc[] {
return value.slice(1)
}
export function RemoveLast (value: Doc[], props: Record<string, any>): Doc[] {
export function RemoveLast (value: any[]): any[] {
if (value == null) {
value = []
}
@@ -101,6 +122,23 @@ export function RemoveLast (value: Doc[], props: Record<string, any>): Doc[] {
return value.slice(0, -1)
}
export async function Filter (value: any[], props: Record<string, any>, control: ProcessControl): Promise<any[]> {
if (value == null) {
return []
}
if (!Array.isArray(value)) return value
const { _class, ...otherProps } = props
if (_class == null) return value
if (value.length === 0) return value
if (typeof value[0] === 'string') {
const docs = await control.client.findAll(_class, { _id: { $in: value } })
return matchQuery(docs, otherProps, core.class.Doc, control.client.getHierarchy(), true).map((p) => p._id)
} else if (typeof value[0] === 'object') {
return matchQuery(value, otherProps, core.class.Doc, control.client.getHierarchy(), true)
}
return value
}
// #endregion
// #region String