# HubSpot Object Backfill Script

**Last Updated:** 2026-03-06

Script to create missing HubSpot custom objects for partners who were registered via OAuth but don't have corresponding HubSpot objects.

## Background

Due to a bug in the OAuth registration flow (fixed 2026-03-06), some partners registered via OAuth did not get HubSpot custom objects created. The `oauthCreatePartnerInHubSpot()` function silently failed without proper error logging.

**Fixed in:** `v2/pages/partner-oauth-callback.php` - now uses shared helper with proper error checking.

## Usage

### Check All Partners

```bash
# Dry run - see what would be created
php v2/scripts/affiliate/backfill-oauth-hubspot-objects.php --dry-run

# Actually create missing objects
php v2/scripts/affiliate/backfill-oauth-hubspot-objects.php
```

### Backfill Specific Partner

```bash
# Dry run for specific partner
php v2/scripts/affiliate/backfill-oauth-hubspot-objects.php --dry-run --partner-id=AP-20260306-DC0387

# Create HubSpot object for specific partner
php v2/scripts/affiliate/backfill-oauth-hubspot-objects.php --partner-id=AP-20260306-DC0387
```

## Options

- `--dry-run` - Preview what would be created without actually creating objects
- `--partner-id=AP-XXX` - Backfill specific partner ID only

## What It Does

1. Loads all partners from `affiliate_partners.json`
2. For each partner, checks if HubSpot object exists using `getAffiliatePartnerFromHubSpot()`
3. If missing, creates HubSpot object using `createAffiliatePartnerInHubSpot()`
4. Reports summary: total checked, missing, created, errors

## Output

The script outputs:
- Progress messages for each partner checked
- Summary statistics
- List of errors (if any)
- In dry-run mode: list of partners that would be created

## Example Output

```
Checking partners for missing HubSpot objects...
[DRY RUN MODE - No objects will be created]

[DRY RUN] Would create HubSpot object for:
  Partner ID: AP-20260306-DC0387
  Name: Felix Knörzer
  Email: felix@ordio.com

[DRY RUN] Would create HubSpot object for:
  Partner ID: AP-20260306-1DEE3E
  Name: Hady ElHady
  Email: hadyelhady93@gmail.com

=== Summary ===
Total checked: 12
Missing HubSpot objects: 2
Would create: 2
```

## Error Handling

- Individual partner failures don't stop the script
- Errors are logged and reported in summary
- Script exits with code 1 if any errors occurred, 0 if successful

## Related Documentation

- [OAUTH_CALLBACK_FIX.md](OAUTH_CALLBACK_FIX.md) - Details about the OAuth HubSpot creation fix
- [ADMIN_DELETE_PARTNERS.md](ADMIN_DELETE_PARTNERS.md) - Partner deletion documentation
- [HUBSPOT_INTEGRATION_SUMMARY.md](HUBSPOT_INTEGRATION_SUMMARY.md) - HubSpot integration overview
