import { Injectable } from '@nestjs/common';
import { DatabaseService } from '../../../infrastructure/database/database.service';
@Injectable()
export class FiscalIntegrationReadModelService {
constructor(private readonly database:DatabaseService) {}
async overview(input:{organizationId:string;locationId:string}) {
const connections=await this.database.query<any>(
`select id,provider,display_name,status,environment,fiscal_outage_mode,
last_health_check_at,last_health_ok,last_successful_fiscal_at,
fiscal_production_enabled_at,last_error_code
from integration_connections
where organization_id=$1 and category='fiscal'
and (location_id is null or location_id=$2)
order by created_at`,
[input.organizationId,input.locationId],
);
const mappings=await this.database.query<any>(
`select id,integration_connection_id,display_name,status,provider_device_serial,
provider_client_serial,last_seen_at,cash_register_id
from fiscal_device_mappings
where organization_id=$1 and location_id=$2 and effective_until is null
order by display_name`,
[input.organizationId,input.locationId],
);
const recoveries=await this.database.query<any>(
`select id,fiscal_transaction_id,issue_type,severity,status,summary,attempt_count,created_at
from fiscal_recovery_cases
where organization_id=$1 and location_id=$2
and status in ('open','retrying','provider_support')
order by case severity when 'critical' then 0 else 1 end,created_at`,
[input.organizationId,input.locationId],
);
return {connections,mappings,recoveries};
}
}
