> ## Documentation Index
> Fetch the complete documentation index at: https://docs.serial.okos.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Filter Conditions

> An in-depth look at the structure and purpose of the `filter_conditions` table in the Serial database

## Introduction to the Filter\_Conditions Table

The filter\_conditions table is designed to store and manage filter conditions for process steps in the Serial application. This table plays a crucial role in enabling dynamic filtering of data and controlling the visibility of process steps based on specific criteria.

## Table Structure

The filter\_conditions table is structured to capture various aspects of a filter condition, including its associated company, process step, type, and operator. Here's a detailed breakdown of its columns:

| Column Name       | Data Type | Constraints             | Description                                         |
| ----------------- | --------- | ----------------------- | --------------------------------------------------- |
| id                | uuid      | primary key             | Unique identifier for the filter condition          |
| company\_id       | uuid      | not null, foreign key   | Reference to the associated company                 |
| process\_step\_id | uuid      |                         | Reference to the associated process step            |
| name              | text      | not null                | Name of the filter condition                        |
| type              | text      | not null                | Type of the filter condition                        |
| dataset\_id       | uuid      |                         | Reference to the associated dataset (if applicable) |
| operator          | text      | not null                | Operator used for the filter condition              |
| value             | text      | not null                | Value to compare against in the filter condition    |
| label             | text      |                         | Optional label for the filter condition             |
| created\_at       | timestamp | not null, default now() | Timestamp of when the filter condition was created  |

## Usage and Functionality

The filter\_conditions table is designed to be flexible and support various types of filtering operations. Here are some key points about its usage:

1. **Process Step Association**: Each filter condition is typically associated with a specific process step through the `process_step_id` column. This allows for step-specific filtering within a process.

2. **Company-Specific Filtering**: The `company_id` column ensures that filter conditions are segregated by company, allowing for company-specific configurations.

3. **Flexible Condition Types**: The `type` column allows for different types of filter conditions, which can be used to filter various data types such as numeric values, dates, or categorical data.

4. **Customizable Operators**: The `operator` column stores the comparison operator (e.g., equals, not equals, greater than) used in the filter condition, providing flexibility in how conditions are evaluated.

5. **Security and Access Control**: Row-level security policies ensure that only users with the 'ADMIN' role from the same company can modify (insert, update, delete) filter conditions, while all users from the same company can view them.

## Notes

* The table includes indexes on `company_id` and `process_step_id` columns to optimize query performance when filtering by these attributes.

* The `dataset_id` column allows for linking filter conditions to specific datasets, which can be useful for more complex filtering scenarios.

* Example usage in TypeScript:

  ```typescript
  const allowedOperators = {
    [CollectionType.ParametricQuantitative]: [
      FilterConditionOperator.Equals,
      FilterConditionOperator.NotEquals,
      FilterConditionOperator.GreaterThan,
      FilterConditionOperator.LessThan,
      FilterConditionOperator.GreaterThanOrEqualTo,
      FilterConditionOperator.LessThanOrEqualTo,
    ],
    // ... other collection types
  };
  ```

* The `testFilterCondition` and `testFilter` functions in the codebase are used to evaluate filter conditions against actual data, determining whether a specific data point meets the criteria defined in the filter\_conditions table.

By leveraging the filter\_conditions table, the Serial application can implement dynamic and flexible filtering mechanisms, allowing users to customize their data views and control the visibility of process steps based on specific criteria. This enhances the application's ability to handle complex workflows and data management scenarios across different companies and processes.
