> ## 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.

# Report Templates

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

## Introduction to the Report\_Templates Table

The report\_templates table is designed to store and manage various types of report templates within the Serial application. This table plays a crucial role in organizing, versioning, and controlling access to different reporting configurations across the system.

## Table Structure

The report\_templates table is structured to accommodate diverse report types while maintaining version control and access management. Here's a detailed breakdown of its columns:

| Column Name          | Data Type                | Constraints                            | Description                                            |
| -------------------- | ------------------------ | -------------------------------------- | ------------------------------------------------------ |
| id                   | uuid                     | not null, default uuid\_generate\_v4() | Unique identifier for each template                    |
| revision             | bigint                   | not null                               | Version number of the template                         |
| company\_id          | uuid                     | not null                               | Associated company identifier                          |
| type                 | text                     | not null                               | Type of report template                                |
| name                 | text                     | not null                               | Name of the template                                   |
| description          | text                     |                                        | Optional description of the template                   |
| created\_at          | timestamp with time zone | not null, default now()                | Timestamp of template creation                         |
| created\_by          | uuid                     | not null                               | User identifier of the creator                         |
| last\_edited\_at     | timestamp with time zone | default now()                          | Timestamp of last edit                                 |
| last\_edited\_by     | uuid                     |                                        | User identifier of the last editor                     |
| is\_public           | boolean                  | not null, default false                | Flag indicating if the template is publicly accessible |
| shared\_with         | jsonb                    | not null, default '{}'::jsonb          | JSON object storing sharing permissions                |
| config               | jsonb                    | not null, default '{}'::jsonb          | JSON object storing template configuration             |
| is\_latest\_revision | boolean                  | not null, default false                | Flag indicating if this is the latest revision         |

## Usage and Functionality

The report\_templates table is designed to be flexible and secure, accommodating various reporting needs within the Serial application. Here are some key points about its usage:

1. **Version Control**: The table implements a revision system, allowing multiple versions of a template to be stored. The `revision` column, along with the `is_latest_revision` flag, helps manage different iterations of a template.

2. **Access Control**: Through the `is_public` flag and the `shared_with` JSON column, the table provides granular control over who can access and use each template. This is further reinforced by row-level security policies.

3. **Template Diversity**: The `type` column allows for different kinds of report templates, such as GridBuilderView, GraphBuilderView, SnLookupPdfTemplate, and Dashboard. The `config` JSON column stores the specific configuration for each template type.

## Notes

* The table uses triggers to automatically increment the revision number and update the `last_edited_at` timestamp when changes are made.

* Row-level security is implemented with policies that restrict access based on user roles and company associations. For example:

  ```sql
  create policy "Insert - Data - SameCompany"
  on "public"."report_templates"
  as permissive
  for insert
  to authenticated
  with check ((company_id IN ( SELECT privileges.company_id
     FROM privileges
    WHERE (privileges.supabase_uid = auth.uid()))));
  ```

* The table has foreign key constraints linking to the `companies` and `users` tables, ensuring data integrity across the database.

By leveraging the report\_templates table, the Serial application provides a robust and flexible system for creating, managing, and sharing various types of report templates across different companies and users, while maintaining version control and enforcing proper access restrictions.
