# Users

Comprehensive user data model that consolidates user information with role-based activity flags.
Combines base user profile data with aggregated role indicators from application users, team owners, 
and team members to provide a complete view of user engagement across different partner functions.
Includes activity status tracking and deletion timestamps for each role type.

A user is included in this table if either of the following conditions are met:

* They are a member of one of your teams.
* They are an owner of one of your teams.
* They have used a white-label application linked to you.

**SQL:** `users`

## Schema

| Column Name                 | Data Type     | Description                                                                                              |
|:----------------------------|:--------------|:---------------------------------------------------------------------------------------------------------|
| **Primary ID**              |               |                                                                                                          |
| id                          | INTEGER       | Primary key                                                                                              |
| **Other IDs**               |               |                                                                                                          |
| stripe_customer_id          | VARCHAR       | External identifier linking the user to their Stripe customer record for payment processing              |
| **Timestamps**              |               |                                                                                                          |
| created_at                  | TIMESTAMP_NTZ | Timestamp when the user record was originally created                                                    |
| updated_at                  | TIMESTAMP_NTZ | Timestamp when the user record was last modified                                                         |
| deleted_at                  | TIMESTAMP_NTZ | Timestamp when the user was deleted, null for active users                                               |
| blocked_at                  | TIMESTAMP_NTZ | Timestamp when the user was blocked from accessing the platform                                          |
| application_user_deleted_at | TIMESTAMP_NTZ | Latest deletion timestamp across all application user records for this user                              |
| team_owner_team_deleted_at  | TIMESTAMP_NTZ | Latest team deletion timestamp where this user was a team owner                                          |
| team_member_deleted_at      | TIMESTAMP_NTZ | Latest deletion timestamp across all team member records for this user                                   |
| team_member_team_deleted_at | TIMESTAMP_NTZ | Latest team deletion timestamp where this user was a team member                                         |
| **Filters**                 |               |                                                                                                          |
| is_active_application_user  | BOOLEAN       | Indicates whether the user has at least one active application user record                               |
| is_active_team_owner        | BOOLEAN       | Indicates whether the user is currently an active owner of at least one team                             |
| is_active_team_member       | BOOLEAN       | Indicates whether the user is currently an active member of at least one team                            |
| is_application_user         | INTEGER       | Count of application user roles associated with this user, 0 if none                                     |
| is_team_owner               | INTEGER       | Count of team owner roles associated with this user, 0 if none                                           |
| is_team_member              | INTEGER       | Count of team member roles associated with this user, 0 if none                                          |
| **Attributes**              |               |                                                                                                          |
| first_name                  | VARCHAR       | User's first name as provided during registration                                                        |
| last_name                   | VARCHAR       | User's last name as provided during registration                                                         |
| email                       | VARCHAR       | User's primary email address used for account access and communications                                  |
| phone                       | VARCHAR       | User's phone number for contact and verification purposes                                                |
| country_name                | VARCHAR       | Full name of the country where the user is located                                                       |
| country_currency_code       | VARCHAR       | ISO currency code for the user's country used for billing and payments                                   |
| address1                    | VARCHAR       | Primary street address line for the user's location                                                      |
| address2                    | VARCHAR       | Secondary address line for apartment, suite, or unit information                                         |
| address3                    | VARCHAR       | Additional address line for complex addressing requirements                                              |
| city                        | VARCHAR       | City or municipality where the user is located                                                           |
| zip                         | VARCHAR       | Postal or ZIP code for the user's address                                                                |
| timezone                    | VARCHAR       | User's timezone setting for proper time display and scheduling                                           |
| national_id                 | VARCHAR       | Optional national identity number required by some countries/operators to verify a user's identity (e.g. "kennitala" in Iceland) |

## Changes from last version
### Summary

  - Filter Changes: Removed deleted_at is null filter; now exposes deleted users with deletion tracking columns
  - Deletion Tracking: Adds 4 deletion tracking columns from different user role relationships (application_user, team_owner, team_member)
  - Active Flags: Adds 3 boolean flags tracking if user has active (non-deleted) relationships in each role

### Added columns (9)
- application_user_deleted_at
- team_owner_team_deleted_at
- team_member_deleted_at
- team_member_team_deleted_at
- is_active_application_user
- is_active_team_owner
- is_active_team_member
- is_deleted_pii
- national_id

### Removed columns (0)

### Renamed columns (1)
- user_id -> id

## Inclusion Criteria
* the `from_wallet_owner_type` or `to_wallet_owner_type` equals 'TEAM' and the respective wallet_owner_id is among the operator's non-null `teams.id` values.
* or the `from_wallet_owner_type` or `to_wallet_owner_type` equals 'OPERATOR' and the respective wallet_owner_id is among the operator's non-null `operators.id` values.

## Data flow
| Upstream tables     | Downstream tables |
|:--------------------|:------------------|
| `teams`     | ---               |
| `operators` | ---               |

## Alignment
### Monta API
- Aligns with Monta API [get/wallet-transactions](https://developer.monta.com/api/v1/wallet-transactions) endpoint 
- The endpoint only includes non-deleted operator transactions where the operator is not deleted 
```sql
where deleted_at is null
and is_operator_transaction = true
and (
    (is_from_operator_wallet_match = true and from_wallet_operator_deleted_at is null)
    or (is_to_operator_wallet_match = true and to_wallet_operator_deleted_at is null)
)
```
