{
  "id": "ORD-GATE-001",
  "name": "Order create: PASS when Quote is Accepted and Account has an Effective CSA Contract (ContractId optional)",
  "description": "Regression for PR #1198 \u2014 the order-from-quote gate was rewritten to check the Account for an Effective CSA/MA Contract instead of requiring Quote.ContractId. A Quote with ContractId=NULL must now pass when its Account has a qualifying Contract. Mirrors the user's prod quote JP-QT-26041603. This fixture assumes the PingCAP tenant overlay is applied, including the tenant Order before-save behavior that derives PLG_BD_Pool__c.",
  "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 \u2014 required by Order.Name autonumber {RecordType.RecordTypeCode}, normally seeded by cmd/setup apply)",
      "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 for ORD-GATE-001 (BillingCountryCode is required by the Order.Name autonumber format {Account.BillingCountryCode}-{RecordType.RecordTypeCode}-...; 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": [
        "ordgate1-acc-01",
        "ORD-GATE-001 Account",
        "{billing_country_code}",
        true,
        0,
        "{db_timestamp}"
      ]
    },
    {
      "type": "sql",
      "name": "Create Effective CSA Contract on that Account with a [StartDate, EndDate] window that covers the Quote service window",
      "sql": "INSERT INTO Contract (Id, AccountId, Status, StartDate, EndDate, ContractTerm, RecordTypeId, IsDeleted, SystemModstamp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
      "params": [
        "ordgate1-ctr-01",
        "ordgate1-acc-01",
        "Effective",
        "2026-01-01",
        "2027-01-01",
        12,
        "{csa_rt_id}",
        0,
        "{db_timestamp}"
      ]
    },
    {
      "type": "sql",
      "name": "Create Opportunity for ORD-GATE-001",
      "sql": "INSERT INTO Opportunity (Id, Name, AccountId, StageName, CloseDate, Amount, IsDeleted, SystemModstamp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
      "params": [
        "ordgate1-opp-01",
        "ORD-GATE-001 Opp",
        "ordgate1-acc-01",
        "Prospecting",
        "2026-12-31",
        10000,
        0,
        "{db_timestamp}"
      ]
    },
    {
      "type": "sql",
      "name": "Create Accepted Quote with NULL ContractId + Service_*__c inside Contract window on ORD-GATE-001 Account",
      "sql": "INSERT INTO Quote (Id, Name, AccountId, OpportunityId, Status, ContractId, Service_Start_Date__c, Service_End_Date__c, IsDeleted, SystemModstamp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
      "params": [
        "ordgate1-qte-01",
        "ORD-GATE-001 Quote",
        "ordgate1-acc-01",
        "ordgate1-opp-01",
        "Accepted",
        null,
        "2026-04-19",
        "2026-12-31",
        0,
        "{db_timestamp}"
      ]
    }
  ],
  "payload": {
    "QuoteId": "ordgate1-qte-01",
    "AccountId": "ordgate1-acc-01",
    "EffectiveDate": "2026-04-19",
    "Status": "Draft",
    "StatusCode": "Draft",
    "OwnerId": "0055g000001i4R9AAI",
    "PLG_BD_Pool__c": false,
    "TotalAmount": 0
  },
  "expects": {
    "status_code": 201
  },
  "case_timeout_seconds": 240,
  "extract": {
    "order_id": "id"
  },
  "query_after": {
    "object": "Order",
    "record_id": "{order_id}",
    "assertions": [
      {
        "field": "Status",
        "operator": "eq",
        "value": "Draft"
      },
      {
        "field": "AccountId",
        "operator": "eq",
        "value": "ordgate1-acc-01"
      },
      {
        "field": "QuoteId",
        "operator": "eq",
        "value": "ordgate1-qte-01"
      },
      {
        "field": "ContractId",
        "operator": "is_null",
        "value": true
      }
    ]
  },
  "teardown": [
    {
      "type": "sql",
      "sql": "DELETE FROM `Order` WHERE Id = %s",
      "params": [
        "{order_id}"
      ]
    },
    {
      "type": "sql",
      "sql": "DELETE FROM Quote WHERE Id = %s",
      "params": [
        "ordgate1-qte-01"
      ]
    },
    {
      "type": "sql",
      "sql": "DELETE FROM Opportunity WHERE Id = %s",
      "params": [
        "ordgate1-opp-01"
      ]
    },
    {
      "type": "sql",
      "sql": "DELETE FROM Contract WHERE Id = %s",
      "params": [
        "ordgate1-ctr-01"
      ]
    },
    {
      "type": "sql",
      "sql": "DELETE FROM Account WHERE Id = %s",
      "params": [
        "ordgate1-acc-01"
      ]
    }
  ],
  "priority": "P0",
  "bug_ref": "#1198",
  "scene": "order"
}
