{
  "id": "ORD-GATE-004",
  "name": "Order create: REJECT 400 when Account's only CSA Contract's date window ends before the Quote service window",
  "description": "Regression for the 2026-04-21 gate revision \u2014 the date-coverage anchor moved from Order.EffectiveDate to Quote.Service_Start_Date__c / Service_End_Date__c. Contract has the correct CSA RecordType and the Effective status, but its [StartDate, EndDate] ends before the Quote's service window, so the gate must fail closed with the new date-coverage message.",
  "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 for ORD-GATE-004 (BillingCountryCode is required by the Order.Name autonumber {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": [
        "ordgate4-acc-01",
        "ORD-GATE-004 Account",
        "{billing_country_code}",
        true,
        0,
        "{db_timestamp}"
      ]
    },
    {
      "type": "sql",
      "name": "Create CSA Contract whose date window ended before the Quote's Service_Start_Date__c (2026-05-01)",
      "sql": "INSERT INTO Contract (Id, AccountId, Status, StartDate, EndDate, ContractTerm, RecordTypeId, IsDeleted, SystemModstamp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
      "params": [
        "ordgate4-ctr-01",
        "ordgate4-acc-01",
        "Effective",
        "2020-01-01",
        "2024-01-01",
        12,
        "{csa_rt_id}",
        0,
        "{db_timestamp}"
      ]
    },
    {
      "type": "sql",
      "name": "Create Opportunity for ORD-GATE-004",
      "sql": "INSERT INTO Opportunity (Id, Name, AccountId, StageName, CloseDate, Amount, IsDeleted, SystemModstamp) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
      "params": [
        "ordgate4-opp-01",
        "ORD-GATE-004 Opp",
        "ordgate4-acc-01",
        "Prospecting",
        "2026-12-31",
        10000,
        0,
        "{db_timestamp}"
      ]
    },
    {
      "type": "sql",
      "name": "Create Accepted Quote with Service_*__c that lies AFTER the Contract's [2020,2024] window so the gate fails on date coverage",
      "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": [
        "ordgate4-qte-01",
        "ORD-GATE-004 Quote",
        "ordgate4-acc-01",
        "ordgate4-opp-01",
        "Accepted",
        "2026-05-01",
        "2026-12-31",
        0,
        "{db_timestamp}"
      ]
    }
  ],
  "payload": {
    "QuoteId": "ordgate4-qte-01",
    "AccountId": "ordgate4-acc-01",
    "EffectiveDate": "2026-04-19",
    "Status": "Draft",
    "StatusCode": "Draft",
    "OwnerId": "0055g000001i4R9AAI",
    "PLG_BD_Pool__c": false,
    "TotalAmount": 0
  },
  "timeout_seconds": 90,
  "expects": {
    "status_code": 400
  },
  "case_timeout_seconds": 240,
  "response_assertions": [
    {
      "field": "0.message",
      "operator": "contains",
      "value": "Quote's service window"
    }
  ],
  "teardown": [
    {
      "type": "sql",
      "sql": "DELETE FROM Quote WHERE Id = %s",
      "params": [
        "ordgate4-qte-01"
      ]
    },
    {
      "type": "sql",
      "sql": "DELETE FROM Opportunity WHERE Id = %s",
      "params": [
        "ordgate4-opp-01"
      ]
    },
    {
      "type": "sql",
      "sql": "DELETE FROM Contract WHERE Id = %s",
      "params": [
        "ordgate4-ctr-01"
      ]
    },
    {
      "type": "sql",
      "sql": "DELETE FROM Account WHERE Id = %s",
      "params": [
        "ordgate4-acc-01"
      ]
    }
  ],
  "priority": "P0",
  "bug_ref": "#1198",
  "scene": "order"
}
