// @ts-check
/**
* @import {treeAction} from './../../types.js'
*/
import { query, HEX2uuid } from '@commtool/sql-query'
import { addUpdateList } from '../../server.ws.js'
import { removeFilter } from '../executeFilters/executeFilters.js'
import { listRebuildAccess } from '../rebuildList.js'
import { errorLoggerUpdate } from '../../utils/requestLogger.js'
/**
* Handles removal of dlist filter actions: include, exclude, intersect.
*
* Removes all entries produced by the filter, deletes the filter's Links
* and ObjectBase row, then flags the target list for WebSocket update.
*
* @param {treeAction} action
* @returns {Promise<void>}
*/
export const removeDlistFilterAction = async (action) => {
try {
const timestamp = action.timestamp
? (typeof action.timestamp === 'string' ? parseFloat(action.timestamp) : action.timestamp)
: null
await removeFilter(action.UIDObjectID, action.UIDoldTarget, HEX2uuid(action.UIDroot))
await query(
`DELETE FROM ObjectBase WHERE ObjectBase.UID=?`,
[action.UIDObjectID],
{ backDate: timestamp },
)
addUpdateList(action.UIDoldTarget)
} catch (e) {
errorLoggerUpdate(e || new Error('removeDlistFilterAction: Unknown error occurred'))
}
}
/**
* Handles removal of visibility filter actions: visible, changeable.
*
* Removes the filter's `list` Link, deletes the filter object itself, then
* rebuilds the access table for the affected list/dlist/email and flags it
* for WebSocket update.
*
* @param {treeAction} action
* @returns {Promise<void>}
*/
export const removeVisibilityFilterAction = async (action) => {
try {
await query(
`DELETE FROM Links WHERE UID=? AND Type='list' AND UIDTarget=?`,
[action.UIDObjectID, action.UIDoldTarget],
)
await query(
`DELETE FROM ObjectBase WHERE ObjectBase.UID=? AND ObjectBase.Type IN ('visible','changeable')`,
[action.UIDObjectID],
)
await listRebuildAccess(action.UIDoldTarget, HEX2uuid(action.UIDroot))
addUpdateList(action.UIDoldTarget)
} catch (e) {
errorLoggerUpdate(e || new Error('removeVisibilityFilterAction: Unknown error occurred'))
}
}