{
  "id": "ORD-INSERT-INHERITANCE-DIRECT-POST",
  "name": "Direct POST /sobjects/Order clones quote_product__c rows into order_product__c via tenant ChildCloneRules (issue #1847 cutover, Order-half)",
  "description": "Round-4 reviewer requirement: the Order half of the child-clone cutover was only covered by parity unit tests; no runtime oracle. This case closes the gap. After apply-tenant seeds _System_Object.ChildCloneRules for Order, POSTing an Order with a QuoteId referencing an Accepted Quote (with Effective CSA/MA Contract coverage so the tenant-private Quote->Order gate passes) must produce an order_product__c row for each quote_product__c source row, with Quote_Product__c stamped as back-reference and Opportunity_Product__c filled from the source's OpportunityProduct__c per fieldMappings. If the ChildCloneRules JSON, the applyChildCloneRules hook, the BuildChildCloneTarget core, or the SourceIdField stamping ever regresses, this case fails.",
  "object": "Order",
  "method": "POST",
  "endpoint": "/services/data/v63.0/sobjects/Order",
  "setup": [
    {
      "type": "sql",
      "name": "Look up Contract RecordType Id for 'TiDB Cloud Services Agreement'",
      "sql": "SELECT rt.Id AS csa_rt_id FROM _System_RecordType rt JOIN _System_Object so ON so.Id = rt.ObjectId WHERE so.ApiName IN ('Contract','contract') AND rt.Name = 'TiDB Cloud Services Agreement' AND rt.IsActive = 1 AND (rt.IsDeleted = 0 OR rt.IsDeleted IS NULL) LIMIT 1",
      "fetch": "one",
      "extract": {
        "csa_rt_id": "csa_rt_id"
      }
    },
    {
      "type": "sql",
      "name": "Ensure CSA RecordType has a record_type_code (idempotent — required by Order.Name autonumber {RecordType.RecordTypeCode})",
      "sql": "UPDATE _System_RecordType SET record_type_code = 'CSA' WHERE Id = %s AND (record_type_code IS NULL OR record_type_code = '')",
      "params": [
        "{csa_rt_id}"
      ]
    },
    {
      "type": "sql",
      "name": "Create Account (need BillingCountryCode so Order.Name autonumber resolves; PLG_BD_Pool__c is required by tenant Order before-save behavior)",
      "sql": "INSERT INTO Account (Id, Name, BillingCountryCode, PLG_BD_Pool__c, IsDeleted, SystemModstamp) VALUES (%s, %s, %s, %s, %s, %s)",
      "params": [
        "ord-inh-account-01",
        "ORD-INH Account",
        "{billing_country_code}",
        true,
        0,
        "{db_timestamp}"
      ]
    },
    {
      "type": "sql",
      "name": "Create Contract (Effective CSA RecordType, window covers Quote service window so Order gate passes)",
      "sql": "INSERT INTO Contract (Id, AccountId, Status, StartDate, EndDate, ContractTerm, RecordTypeId, IsDeleted, SystemModstamp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
      "params": [
        "ord-inh-contract-01",
        "ord-inh-account-01",
        "Effective",
        "2026-01-01",
        "2027-12-31",
        24,
        "{csa_rt_id}",
        0,
        "{db_timestamp}"
      ]
    },
    {
      "type": "sql",
      "name": "Create Opportunity (parent for Quote)",
      "sql": "INSERT INTO Opportunity (Id, Name, AccountId, StageName, CloseDate, Amount, IsDeleted, SystemModstamp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
      "params": [
        "ord-inh-opp-01",
        "ORD-INH Opp",
        "ord-inh-account-01",
        "Prospecting",
        "2026-12-31",
        2000,
        0,
        "{db_timestamp}"
      ]
    },
    {
      "type": "sql",
      "name": "Create Quote (Accepted, with service window Contract covers)",
      "sql": "INSERT INTO Quote (Id, Name, AccountId, OpportunityId, Status, Service_Start_Date__c, Service_End_Date__c, IsDeleted, SystemModstamp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
      "params": [
        "ord-inh-quote-01",
        "US-QT-260501-01",
        "ord-inh-account-01",
        "ord-inh-opp-01",
        "Accepted",
        "2026-05-01",
        "2026-12-31",
        0,
        "{db_timestamp}"
      ]
    },
    {
      "type": "sql",
      "name": "Create referenced Opportunity_Product__c row for cross-object lookup copy",
      "sql": "INSERT INTO Opportunity_Product__c (Id, Name, Opportunity__c, Product__c, IsDeleted, SystemModstamp) VALUES (%s, %s, %s, %s, %s, %s)",
      "params": [
        "ord-inh-oppprod-01",
        "ORD-INH Opportunity Product",
        "ord-inh-opp-01",
        "01t5g000000liLsAAI",
        0,
        "{db_timestamp}"
      ]
    },
    {
      "type": "sql",
      "name": "Create first quote_product__c (source for clone — Product + cross-named Opportunity_Product_c chain)",
      "sql": "INSERT INTO quote_product__c (Id, Name, Quote__c, Product__c, OpportunityProduct__c, Quantity__c, Unit_Net_Price__c, IsDeleted, CreatedDate, LastModifiedDate, SystemModstamp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
      "params": [
        "ord-inh-qp-01",
        "QuotePro-00000001",
        "ord-inh-quote-01",
        "01t5g000000liLsAAI",
        "ord-inh-oppprod-01",
        3,
        99.99,
        0,
        "{db_timestamp}",
        "{db_timestamp}",
        "{db_timestamp}"
      ]
    }
  ],
  "payload": {
    "AccountId": "ord-inh-account-01",
    "QuoteId": "ord-inh-quote-01",
    "EffectiveDate": "2026-05-01",
    "EndDate": "2026-12-31",
    "Name": "ORD-INSERT-INHERITANCE-DIRECT-POST",
    "Status": "Draft",
    "StatusCode": "Draft",
    "PLG_BD_Pool__c": false
  },
  "expects": {
    "status_code": 201
  },
  "timeout_seconds": 120,
  "case_timeout_seconds": 240,
  "extract": {
    "order_id": "id"
  },
  "query_after": {
    "object": "order_product__c",
    "id_field": "Order__c",
    "record_id": "{order_id}",
    "select_fields": ["Id", "Product__c", "Quote_Product__c", "Opportunity_Product__c", "Quantity__c", "Order__c"],
    "response_assertions": [
      {
        "field": "totalSize",
        "operator": "equals",
        "value": 1,
        "comment": "ChildCloneRules for Order produced exactly one order_product__c row from the one quote_product__c source"
      }
    ],
    "assertions": [
      {
        "field": "Quote_Product__c",
        "operator": "equals",
        "value": "ord-inh-qp-01",
        "comment": "sourceIdField back-reference stamped: Quote_Product__c = <source quote_product__c.Id>. Proves BuildChildCloneTarget's back-ref path is wired."
      },
      {
        "field": "Opportunity_Product__c",
        "operator": "equals",
        "value": "ord-inh-oppprod-01",
        "comment": "Cross-named fieldMappings applied: quote_product__c.OpportunityProduct__c → order_product__c.Opportunity_Product__c"
      },
      {
        "field": "Product__c",
        "operator": "equals",
        "value": "01t5g000000liLsAAI"
      },
      {
        "field": "Quantity__c",
        "operator": "equals",
        "value": 3
      },
      {
        "field": "Order__c",
        "operator": "equals",
        "value": "{order_id}"
      }
    ]
  },
  "teardown": [
    {
      "type": "sql",
      "sql": "DELETE FROM order_product__c WHERE Order__c = %s",
      "params": ["{order_id}"]
    },
    {
      "type": "sql",
      "sql": "DELETE FROM `Order` WHERE Id = %s",
      "params": ["{order_id}"]
    },
    {
      "type": "sql",
      "sql": "DELETE FROM quote_product__c WHERE Id = %s",
      "params": ["ord-inh-qp-01"]
    },
    {
      "type": "sql",
      "sql": "DELETE FROM Opportunity_Product__c WHERE Id = %s",
      "params": ["ord-inh-oppprod-01"]
    },
    {
      "type": "sql",
      "sql": "DELETE FROM Quote WHERE Id = %s",
      "params": ["ord-inh-quote-01"]
    },
    {
      "type": "sql",
      "sql": "DELETE FROM Opportunity WHERE Id = %s",
      "params": ["ord-inh-opp-01"]
    },
    {
      "type": "sql",
      "sql": "DELETE FROM Contract WHERE Id = %s",
      "params": ["ord-inh-contract-01"]
    },
    {
      "type": "sql",
      "sql": "DELETE FROM Account WHERE Id = %s",
      "params": ["ord-inh-account-01"]
    }
  ],
  "priority": "P0",
  "bug_ref": "#1847",
  "scene": "order"
}
