Source: tree/treeQueue/treeAdd.listActions.js

// @ts-check
/**
 * @import {treeAction} from './../../types.js'
 */
import { query, HEX2uuid } from '@commtool/sql-query'
import { addVisibility } from '../matchObjects/matchObjects.js'
import { matchObjectsLists } from '../matchObjects/matchObjectsLists.js'
import { listRebuildAccess } from '../rebuildList.js'
import { checkPersonListMember } from '../matchObjects/checkPersonListMember.js'
import { errorLoggerUpdate } from '../../utils/requestLogger.js'

/**
 * Handles the `listMember` add action.
 *
 * Evaluates to which dlists an object should belong and which users can
 * see/modify it when the object is newly created or its list membership changes.
 * Checks all filters and updates visibility/changeability for the relevant users.
 *
 * @param {treeAction} action
 * @returns {Promise<void>}
 */
export const addListMemberAction = async (action) => {
    try {
    const timestamp = action.timestamp
        ? (typeof action.timestamp === 'string' ? parseFloat(action.timestamp) : action.timestamp)
        : null
    const asOf = timestamp ? `FOR SYSTEM_TIME AS OF FROM_UNIXTIME(${timestamp})` : ''

    const objects = await query(
        `SELECT UID FROM Objects ${asOf}
         WHERE (UIDBelongsTo=? OR UID=?) AND Type IN ('job','person','guest','extern')`,
        [action.UIDObjectID, action.UIDObjectID],
    )
    if (objects.length) {
        const objectUIDs = objects.map(o => o.UID)
        const groups = await query(
            `SELECT ObjectBase.UID FROM ObjectBase ${asOf}
             INNER JOIN Links ${asOf} ON (Links.UIDTarget=ObjectBase.UID AND Links.Type IN ('member','memberA'))
             WHERE Links.UID IN(?) AND ObjectBase.Type = 'group'
             GROUP BY ObjectBase.UID`,
            [objectUIDs],
        )
        const groupUIDs = groups.map(o => o.UID)
        await addVisibility(objectUIDs, groupUIDs, { andRemove: true })
        await checkPersonListMember(action.UIDBelongsTo, HEX2uuid(action.UIDroot))
    }
    } catch (e) {
        errorLoggerUpdate(e || new Error('treeAdd.listActions.addListMemberAction: Unknown error occurred'))
    }
}

/**
 * Handles the `listVisibility` add action.
 *
 * Fired when a visibility or changeability filter defining access for a
 * list/dlist/email/event/eventT has been modified or added.
 * Rebuilds the full access table for the affected list.
 *
 * @param {treeAction} action
 * @returns {Promise<void>}
 */
export const addListVisibilityAction = async (action) => {
    try {
        await listRebuildAccess(action.UIDnewTarget, HEX2uuid(action.UIDroot))
    } catch (e) {
        errorLoggerUpdate(e || new Error('treeAdd.listActions.addListVisibilityAction: Unknown error occurred'))
    }
}

/**
 * Handles the `list` add action.
 *
 * Fired when an entry is added to a list. Matches the entry object against
 * every filter defined on the list and adds it to the corresponding dlists.
 *
 * @param {treeAction} action
 * @returns {Promise<void>}
 */
export const addListEntryAction = async (action) => {
    try {
        matchObjectsLists(action.UIDObjectID, action.UIDnewTarget, HEX2uuid(action.UIDroot))
    } catch (e) {
        errorLoggerUpdate(e || new Error('treeAdd.listActions.addListEntryAction: Unknown error occurred'))
    }
}