group BlockingStateSqlDao;

CHECK_TENANT() ::= "tenant_record_id = :tenantRecordId"
AND_CHECK_TENANT() ::= "AND <CHECK_TENANT()>"

getBlockingStateFor() ::= <<
    select
        id
      , state
      , type
      , service
      , block_change
      , block_entitlement
      , block_billing
      , created_date
      , tenant_record_id
    from blocking_states
    where id = :id
    <AND_CHECK_TENANT()>
    -- We want the current state, hence the order desc and limit 1
    order by created_date desc, record_id desc
    limit 1
    ;
>>

getBlockingHistoryFor() ::= <<
    select
       id
      , state
      , type
      , service
      , block_change
      , block_entitlement
      , block_billing
      , created_date
      , tenant_record_id
    from blocking_states
    where id = :id
    <AND_CHECK_TENANT()>
    -- We want the history in order
    order by created_date asc, record_id asc
    ;
>>

setBlockingState() ::= <<
    insert into blocking_states (
       id
      , state
      , type
      , service
      , block_change
      , block_entitlement
      , block_billing
      , created_date
      , account_record_id
      , tenant_record_id
    ) values (
        :id
      , :state
      , :type
      , :service
      , :block_change
      , :block_entitlement
      , :block_billing
      , :created_date
      , :accountRecordId
      , :tenantRecordId
    );
>>
