1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use crate::mode::mock::ContextLock;
use tracing::info;
use vit_servicing_station_lib::db::models::funds::Fund;
use warp::{Filter, Rejection, Reply};

pub fn admin_filter(
    context: ContextLock,
) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
    let base = warp::path!("admin" / ..);

    let fund_filter = warp::path!("fund" / ..).and(fund_put_filter(context));

    base.and(fund_filter)
}

pub fn fund_put_filter(
    context: ContextLock,
) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
    let with_context = warp::any().map(move || context.clone());

    warp::path::end()
        .and(warp::put())
        .and(warp::body::json())
        .and(with_context)
        .and_then(put_fund)
}

#[tracing::instrument(skip(context), fields(fund_id = fund.id), name="mock admin command received")]
pub async fn put_fund(fund: Fund, context: ContextLock) -> Result<impl Reply, Rejection> {
    info!("put new fund");

    context
        .write()
        .unwrap()
        .state_mut()
        .vit_mut()
        .funds_mut()
        .push(fund);
    Ok(warp::reply())
}