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

# ADMIN ORGANIZER LINKING

# Admin-Organizer Linking Commands

This document describes the commands and services available for linking the administrator user with an organizer account in the system.

## 🎯 Purpose

The system allows linking the existing administrator user (created by the `UsersSeed` seeder) with an organizer account so they can manage events and access organizer-specific functionality.

## 🛠️ Available Commands

### Artisan Command: `admin:link-organizer`

Main command for managing the link between admin and organizer.

#### Syntax

```bash theme={null}
php artisan admin:link-organizer {action} [options]
```

#### Available Actions

##### 1. **Link** - Link admin with organizer

```bash theme={null}
# Link with the default name
php artisan admin:link-organizer link

# Link with a custom name
php artisan admin:link-organizer link --name="Mi Organizador Personalizado"
```

**Functionality:**

* Looks up the existing admin user (`a-oceanman@mail.com`)
* Creates a new organizer or reuses an existing one
* Assigns the first available tariff in the system
* Links the admin with the organizer
* Shows detailed information about the process

##### 2. **Status** - Check current status

```bash theme={null}
php artisan admin:link-organizer status
```

**Functionality:**

* Checks whether the admin is linked to an organizer
* Shows information about the associated organizer
* Provides details of the link status

##### 3. **Unlink** - Unlink admin from the organizer

```bash theme={null}
php artisan admin:link-organizer unlink
```

**Functionality:**

* Unlinks the admin from the current organizer
* Keeps the organizer in the system
* Clears the reference on the admin user

## 🌱 Seeder: `AdminOrganizerLinkSeed`

Automatic seeder to link admin with organizer during installation.

### Usage

```bash theme={null}
# Run the specific seeder
php artisan db:seed --class=AdminOrganizerLinkSeed

# Or include it in DatabaseSeeder.php for automatic execution
```

### Functionality

* Checks the current link status
* Performs the link if not already linked
* Shows informational messages about the process
* Prevents duplicates

## 🔧 Service: `AdminOrganizerLinkService`

Internal service that handles all the linking logic.

### Main Methods

#### `linkAdminWithOrganizer($organizerName = null)`

```php theme={null}
$service = new AdminOrganizerLinkService();
$result = $service->linkAdminWithOrganizer('Mi Organizador');

if ($result['success']) {
    echo "Vinculado exitosamente: " . $result['message'];
    echo "Organizador ID: " . $result['organizer_id'];
}
```

#### `getAdminOrganizerStatus()`

```php theme={null}
$service = new AdminOrganizerLinkService();
$status = $service->getAdminOrganizerStatus();

if ($status['is_linked']) {
    echo "Admin vinculado a organizador ID: " . $status['organizer_id'];
} else {
    echo "Admin no está vinculado a ningún organizador";
}
```

#### `unlinkAdminFromOrganizer()`

```php theme={null}
$service = new AdminOrganizerLinkService();
$result = $service->unlinkAdminFromOrganizer();

if ($result['success']) {
    echo "Desvinculado exitosamente";
}
```

## 📋 Prerequisites

### 1. Existing Admin User

* The admin user must exist (created by `UsersSeed`)
* Email: `a-oceanman@mail.com`
* Password: `oceanman`

### 2. Available Tariffs

* At least one tariff must exist in the system
* Tariffs are created automatically with `TarifasSeed`

### 3. Database

* `users` table with the `organizador_id` field
* `organizadores` table with all required fields
* `tarifas` table with at least one record

## 🔍 Status Verification

### Before Linking

```bash theme={null}
php artisan admin:link-organizer status
```

### After Linking

```bash theme={null}
php artisan admin:link-organizer status
```

### Verify in the Database

```sql theme={null}
-- Verify admin user
SELECT id, name, email, organizador_id FROM users WHERE email = 'a-oceanman@mail.com';

-- Verify linked organizer
SELECT o.*, t.name as tarifa_name 
FROM organizadores o 
LEFT JOIN tarifas t ON o.tarifas_id = t.id 
WHERE o.id = (SELECT organizador_id FROM users WHERE email = 'a-oceanman@mail.com');
```

## 🚨 Troubleshooting

### Error: "Admin user not found"

* Verify that `UsersSeed` has been run
* Confirm that a user exists with the email `a-oceanman@mail.com`

### Error: "No tarifas available in the system"

* Run `TarifasSeed` to create tariffs
* Verify that active tariffs exist in the `tarifas` table

### Error: "Field 'tarifas\_id' doesn't have a default value"

* Verify that the `Organizador` model has `tarifas_id` in `$fillable`
* Confirm that the `tarifas_id` migration has been run

### Error: "Admin user is already linked"

* The admin is already linked to an organizer
* Use `status` to check the current status
* Use `unlink` if you want to change the link

## 📝 Logs and Auditing

The service logs all operations in Laravel's logs:

```php theme={null}
// Log location
storage/logs/laravel.log

// Search by:
grep "Admin user linked" storage/logs/laravel.log
grep "Admin user unlinked" storage/logs/laravel.log
```

## 🔄 Recommended Workflow

### Initial Installation

1. Run migrations: `php artisan migrate`
2. Run basic seeds: `php artisan db:seed --class=UsersSeed`
3. Run tariff seeds: `php artisan db:seed --class=TarifasSeed`
4. Link admin: `php artisan admin:link-organizer link`

### Post-Installation Verification

1. Check status: `php artisan admin:link-organizer status`
2. Test login with admin in the system
3. Confirm access to organizer functionality

### Maintenance

* Use `status` to verify the link
* Use `unlink` and `link` to change the organizer if needed
* Review logs periodically for auditing

## 📞 Support

For issues or questions about the admin-organizer link:

1. Check logs at `storage/logs/laravel.log`
2. Run `php artisan admin:link-organizer status`
3. Review this document for troubleshooting
4. Contact the development team if issues persist
