use crate::{AppState, utils::Bidder};
use axum::{
Json,
extract::{Path, Query, State},
http::StatusCode,
};
use fts_core::{
models::{AuctionOutcome, AuthId, DateTimeRangeQuery, DateTimeRangeResponse},
ports::{AuthFailure, AuthRepository, MarketRepository},
};
use tracing::{Level, event};
type GetOutcomesBody = DateTimeRangeResponse<AuctionOutcome>;
#[utoipa::path(
get,
path = "/v0/auths/{auth_id}/outcomes",
responses(
(status = OK, body = GetOutcomesBody),
(status = UNAUTHORIZED), // no jwt token, handled by extractor
(status = FORBIDDEN), // not allowed to see auth
(status = NOT_FOUND), // no auth by that id
(status = INTERNAL_SERVER_ERROR)
),
params(
("auth_id" = AuthId, description = "Unique identifier of the authorization"),
DateTimeRangeQuery
),
tags = ["auths", "outcomes"]
)]
pub async fn get_outcomes<T: MarketRepository>(
State(state): State<AppState<T>>,
Bidder(bidder_id): Bidder,
Path(auth_id): Path<AuthId>,
Query(query): Query<DateTimeRangeQuery>,
) -> Result<Json<DateTimeRangeResponse<AuctionOutcome>>, StatusCode> {
let result = AuthRepository::get_outcomes(&state.market, bidder_id, auth_id, query, 100)
.await
.map_err(|err| {
event!(Level::ERROR, error = ?err);
StatusCode::INTERNAL_SERVER_ERROR
})?
.map_err(|err| match err {
AuthFailure::AccessDenied => StatusCode::FORBIDDEN,
AuthFailure::DoesNotExist => StatusCode::NOT_FOUND,
error => {
event!(Level::ERROR, ?error, "unexpected failure");
StatusCode::INTERNAL_SERVER_ERROR
}
})?;
Ok(Json(result))
}