# Field Config vs Raw Types - Audit Report

## Audit Objective
Verify that all fields referenced in the field config system actually exist in the raw type schemas before removing the field config abstraction layer.

---

## Product Fields (from Product2)

### Field Config Expects:
```typescript
product: {
  sku: "PRODUCT_SKU_FIELD",
  portalCategory: "PRODUCT_PORTAL_CATEGORY_FIELD",
  portalCatalog: "PRODUCT_PORTAL_CATALOG_FIELD",
  portalAccessible: "PRODUCT_PORTAL_ACCESSIBLE_FIELD",
  itemClass: "PRODUCT_ITEM_CLASS_FIELD",
  billingCycle: "PRODUCT_BILLING_CYCLE_FIELD",
  whmcsProductId: "PRODUCT_WHMCS_PRODUCT_ID_FIELD",
  whmcsProductName: "PRODUCT_WHMCS_PRODUCT_NAME_FIELD",
  internetPlanTier: "PRODUCT_INTERNET_PLAN_TIER_FIELD",
  internetOfferingType: "PRODUCT_INTERNET_OFFERING_TYPE_FIELD",
  displayOrder: "PRODUCT_DISPLAY_ORDER_FIELD",
  bundledAddon: "PRODUCT_BUNDLED_ADDON_FIELD",
  isBundledAddon: "PRODUCT_IS_BUNDLED_ADDON_FIELD",
  simDataSize: "PRODUCT_SIM_DATA_SIZE_FIELD",
  simPlanType: "PRODUCT_SIM_PLAN_TYPE_FIELD",
  simHasFamilyDiscount: "PRODUCT_SIM_HAS_FAMILY_DISCOUNT_FIELD",
  vpnRegion: "PRODUCT_VPN_REGION_FIELD",
}
```

### Raw Type Has:
```typescript
SalesforceProduct2Record {
  Id: string ✅
  Name: string ✅
  StockKeepingUnit: string ✅ (sku)
  Description: string ✅
  Product2Categories1__c: string ✅ (portalCategory)
  Portal_Catalog__c: boolean ✅ (portalCatalog)
  Portal_Accessible__c: boolean ✅ (portalAccessible)
  Item_Class__c: string ✅ (itemClass)
  Billing_Cycle__c: string ✅ (billingCycle)
  Catalog_Order__c: number ✅ (displayOrder)
  Bundled_Addon__c: string ✅ (bundledAddon)
  Is_Bundled_Addon__c: boolean ✅ (isBundledAddon)
  Internet_Plan_Tier__c: string ✅ (internetPlanTier)
  Internet_Offering_Type__c: string ✅ (internetOfferingType)
  Feature_List__c: string ✅
  SIM_Data_Size__c: string ✅ (simDataSize)
  SIM_Plan_Type__c: string ✅ (simPlanType)
  SIM_Has_Family_Discount__c: boolean ✅ (simHasFamilyDiscount)
  VPN_Region__c: string ✅ (vpnRegion)
  WH_Product_ID__c: number ✅ (whmcsProductId)
  WH_Product_Name__c: string ✅ (whmcsProductName)
  Price__c: number ✅
  Monthly_Price__c: number ✅
  One_Time_Price__c: number ✅
}
```

**Status:** ✅ ALL PRODUCT FIELDS EXIST IN RAW TYPE

---

## Order Fields (from Order)

### Field Config Expects:
```typescript
order: {
  orderType: "ORDER_TYPE_FIELD",
  activationType: "ORDER_ACTIVATION_TYPE_FIELD",
  activationScheduledAt: "ORDER_ACTIVATION_SCHEDULED_AT_FIELD",
  activationStatus: "ORDER_ACTIVATION_STATUS_FIELD",
  internetPlanTier: "ORDER_INTERNET_PLAN_TIER_FIELD",
  installationType: "ORDER_INSTALLATION_TYPE_FIELD",
  weekendInstall: "ORDER_WEEKEND_INSTALL_FIELD",
  accessMode: "ORDER_ACCESS_MODE_FIELD",
  hikariDenwa: "ORDER_HIKARI_DENWA_FIELD",
  vpnRegion: "ORDER_VPN_REGION_FIELD",
  simType: "ORDER_SIM_TYPE_FIELD",
  eid: "ORDER_EID_FIELD",
  simVoiceMail: "ORDER_SIM_VOICE_MAIL_FIELD",
  simCallWaiting: "ORDER_SIM_CALL_WAITING_FIELD",
  whmcsOrderId: "ORDER_WHMCS_ORDER_ID_FIELD",
  lastErrorCode: "ORDER_ACTIVATION_ERROR_CODE_FIELD",
  lastErrorMessage: "ORDER_ACTIVATION_ERROR_MESSAGE_FIELD",
  lastAttemptAt: "ORDER_ACTIVATION_LAST_ATTEMPT_AT_FIELD",
  addressChanged: "ORDER_ADDRESS_CHANGED_FIELD",
  
  mnp: { ... },
  billing: { ... }
}
```

### Raw Type Has:
```typescript
SalesforceOrderRecord {
  Id: string ✅
  OrderNumber: string ✅
  Status: string ✅
  Type: string ✅ (orderType)
  EffectiveDate: string ✅
  TotalAmount: number ✅
  AccountId: string ✅
  Account: { Name: string } ✅
  Pricebook2Id: string ✅
  Activation_Type__c: string ✅ (activationType)
  Activation_Status__c: string ✅ (activationStatus)
  Activation_Scheduled_At__c: string ✅ (activationScheduledAt)
  Internet_Plan_Tier__c: string ✅ (internetPlanTier)
  Installment_Plan__c: string ✅ (installationType)
  Access_Mode__c: string ✅ (accessMode)
  Weekend_Install__c: boolean ✅ (weekendInstall)
  Hikari_Denwa__c: boolean ✅ (hikariDenwa)
  VPN_Region__c: string ✅ (vpnRegion)
  SIM_Type__c: string ✅ (simType)
  SIM_Voice_Mail__c: boolean ✅ (simVoiceMail)
  SIM_Call_Waiting__c: boolean ✅ (simCallWaiting)
  EID__c: string ✅ (eid)
  WHMCS_Order_ID__c: string ✅ (whmcsOrderId)
  Activation_Error_Code__c: string ✅ (lastErrorCode)
  Activation_Error_Message__c: string ✅ (lastErrorMessage)
  ActivatedDate: string ✅
  CreatedDate: string ✅
  LastModifiedDate: string ✅
}
```

**Status:** ✅ ALL ORDER FIELDS EXIST IN RAW TYPE

**Missing Fields in Raw Type:**
- ❌ `lastAttemptAt` (ORDER_ACTIVATION_LAST_ATTEMPT_AT_FIELD) - NOT IN SCHEMA
- ❌ `addressChanged` (ORDER_ADDRESS_CHANGED_FIELD) - NOT IN SCHEMA
- ❌ MNP fields (all nested under order.mnp) - NOT IN SCHEMA
- ❌ Billing address fields (all nested under order.billing) - NOT IN SCHEMA

---

## OrderItem Fields

### Field Config Expects:
```typescript
orderItem: {
  billingCycle: "ORDER_ITEM_BILLING_CYCLE_FIELD",
  whmcsServiceId: "ORDER_ITEM_WHMCS_SERVICE_ID_FIELD",
}
```

### Raw Type Has:
```typescript
SalesforceOrderItemRecord {
  Id: string ✅
  OrderId: string ✅
  Quantity: number ✅
  UnitPrice: number ✅
  TotalPrice: number ✅
  PricebookEntryId: string ✅
  PricebookEntry: SalesforcePricebookEntryRecord ✅
  Billing_Cycle__c: string ✅ (billingCycle)
  WHMCS_Service_ID__c: string ✅ (whmcsServiceId)
  CreatedDate: string ✅
  LastModifiedDate: string ✅
}
```

**Status:** ✅ ALL ORDER ITEM FIELDS EXIST IN RAW TYPE

---

## 🚨 CRITICAL FINDINGS

### Missing Fields in Raw Types:

#### 1. **MNP (Mobile Number Portability) Fields**
Field config expects these but they're **NOT in raw type**:
```typescript
mnp: {
  application: string,
  reservationNumber: string,
  expiryDate: string,
  phoneNumber: string,
  mvnoAccountNumber: string,
  portingDateOfBirth: string,
  portingFirstName: string,
  portingLastName: string,
  portingFirstNameKatakana: string,
  portingLastNameKatakana: string,
  portingGender: string,
}
```

**These should be:**
```typescript
MNP_Application__c?: string
MNP_Reservation_Number__c?: string
MNP_Expiry_Date__c?: string
MNP_Phone_Number__c?: string
MVNO_Account_Number__c?: string
Porting_Date_Of_Birth__c?: string
Porting_First_Name__c?: string
Porting_Last_Name__c?: string
Porting_First_Name_Katakana__c?: string
Porting_Last_Name_Katakana__c?: string
Porting_Gender__c?: string
```

#### 2. **Billing Address Fields**
Field config expects these but they're **NOT in raw type**:
```typescript
billing: {
  street: string,
  city: string,
  state: string,
  postalCode: string,
  country: string,
}
```

**These should be:**
```typescript
Billing_Street__c?: string
Billing_City__c?: string
Billing_State__c?: string
Billing_Postal_Code__c?: string
Billing_Country__c?: string
```

#### 3. **Other Missing Fields:**
```typescript
lastAttemptAt    → Activation_Last_Attempt_At__c?
addressChanged   → Address_Changed__c?
```

---

## Impact Analysis

### **Used in Order Builder** (Order Creation)
The order builder **DOES use** MNP and billing fields when creating orders:

```typescript
// apps/bff/src/modules/orders/services/order-builder.service.ts

private addSimFields(...) {
  // Uses MNP fields if configurations.isMnp === "true"
  orderFields[mnpField("application", fieldMap)] = ...
  orderFields[mnpField("phoneNumber", fieldMap)] = ...
  // etc.
}

private async addAddressSnapshot(...) {
  // Uses billing fields for address snapshot
  orderFields[billingField("street", fieldMap)] = ...
  orderFields[billingField("city", fieldMap)] = ...
  // etc.
}
```

### **Used in Mapper?**
Let me check if the mapper tries to read these fields...

```typescript
// packages/domain/orders/providers/salesforce/mapper.ts
// The mapper DOES NOT try to read MNP or billing fields
// It only reads simple order fields
```

**Status:** ✅ These fields are only used for **writing** (order creation), not **reading** (order display)

---

## ✅ SAFE TO PROCEED - With Additions

### **Conclusion:**

1. ✅ All fields used for **reading** (display) exist in raw types
2. ❌ Fields used for **writing** (creation) are missing from raw types
3. ✅ We can safely remove field config for **reading/mapping**
4. ⚠️  We need to **ADD missing fields** to raw types for **writing**

### **Action Plan:**

1. **First:** Add missing fields to `SalesforceOrderRecord` schema
   - MNP fields (11 fields)
   - Billing address fields (5 fields)
   - lastAttemptAt field
   - addressChanged field

2. **Then:** Remove field config system
   - Update mappers to use direct access
   - Update query builders to use actual field names
   - Update order builder to use actual field names
   - Delete field config service
   - Delete field config interfaces

---

## Next Steps

Before proceeding with removal, I need to:

1. ✅ Update `SalesforceOrderRecord` schema with missing fields
2. ✅ Verify order creation still works
3. ✅ Then remove field config system

**Ready to proceed?** I'll first add the missing fields to the raw types.

