# Audit Logs

In our app we have audit log system to monitor changes that can influence user data by the time logs monitor any changes on businesses and leads (edit, create, delete, assign lead to business), also they monitor comments delete and comments update. With that system members of the team can easily monitor who performed specifyc actions. Every user can work with logs only in read only mode, there is no possibility to delete or update any of logs. Logs generating automatically when user perform actions. It is one table for all entity logs.

Now let's see what the `AuditLoggerEntity` consists of:

* **action** - Identifies which action the log recorded (e.g., business update, lead update). For each action, we use an enum `AuditLogActions` and simple integers to identify actions (for better database performance, as integers are faster than strings).\
  \&#xNAN;*Note:* Do not change these numbers if logs already exist, as this will cause incorrect display in the UI. To add a new action to monitor, add a new value at the end of the enum with the identifier number set to `max + 1`.
* **changedObjects** - A `jsonb` field that consists of three objects:
  * **previous** - The full entity object before a certain action.
  * **current** - The full entity object after the action is performed.
  * **difference** - Contains only the fields that were changed.
* **user** - A nullable relation to the `auth-users` table. On user deletion, this field is set to `null` to retain the log regardless.
* **userEmail** - A string field duplicating `user.email`, allowing tracking of who performed the action even if the user is deleted.
* **entityType** - Identifies the entity on which the action was performed (e.g., `LeadEntity` or `BusinessEntity`). This is also an integer field for performance. Other entities can be added to the `AuditLogEntityTypes` enum.
* **numberIdentifier** - Stores the ID of the record on which the action was performed.
* **isImpersonated** - A boolean field indicating if the action was performed during impersonation.
* **createdAt** - A timestamp to record when the action was performed (we don’t need an `updatedAt` field, as logs cannot be edited).
