import { Injectable } from '@nestjs/common';
import { DatabaseService } from '../../../infrastructure/database/database.service';
@Injectable()
export class ExternalIntegrationReadModelService {
constructor(private readonly database:DatabaseService) {}
async overview(input:{organizationId:string;locationId:string}) {
const connections=await this.database.query<any>(
`select id,category,provider,display_name,status,sync_direction,system_role,
last_health_check_at,last_health_ok,last_successful_sync_at,last_error_code
from integration_connections
where organization_id=$1
and category in ('pms','external_pos','supplier')
and (location_id is null or location_id=$2)
order by category,display_name`,
[input.organizationId,input.locationId],
);
const conflicts=await this.database.query<any>(
`select id,integration_connection_id,conflict_type,severity,summary,created_at
from external_integration_conflicts
where organization_id=$1 and ($2::uuid is null or location_id=$2)
and status='open'
order by case severity when 'critical' then 0 when 'high' then 1 else 2 end,created_at`,
[input.organizationId,input.locationId],
);
return {connections,conflicts};
}
}
