use sqlx::Postgres;
use uuid::Uuid;
use crate::error::map_sqlx_error;
use ff_core::engine_error::EngineError;
pub(crate) async fn emit<'c>(
tx: &mut sqlx::Transaction<'c, Postgres>,
partition_key: i16,
execution_uuid: Uuid,
signal_id: &str,
waitpoint_id: Option<&str>,
source_identity: Option<&str>,
delivered_at_ms: i64,
) -> Result<(), EngineError> {
sqlx::query(
"INSERT INTO ff_signal_event \
(execution_id, signal_id, waitpoint_id, source_identity, \
delivered_at_ms, partition_key, namespace, instance_tag) \
SELECT $1, $2, $3, $4, $5, $6, \
raw_fields->>'namespace', \
raw_fields->'tags'->>'cairn.instance_id' \
FROM ff_exec_core \
WHERE partition_key = $6 AND execution_id = $7::uuid \
UNION ALL \
SELECT $1, $2, $3, $4, $5, $6, NULL, NULL \
WHERE NOT EXISTS ( \
SELECT 1 FROM ff_exec_core \
WHERE partition_key = $6 AND execution_id = $7::uuid \
)",
)
.bind(execution_uuid.to_string())
.bind(signal_id)
.bind(waitpoint_id)
.bind(source_identity)
.bind(delivered_at_ms)
.bind(i32::from(partition_key))
.bind(execution_uuid)
.execute(&mut **tx)
.await
.map_err(map_sqlx_error)?;
Ok(())
}