use anyhow::Context as _;
use async_graphql::{Context, Enum, InputObject, Object, ID};
use mas_storage::{
compat::CompatSessionRepository,
job::{JobRepositoryExt, SyncDevicesJob},
RepositoryAccess,
};
use crate::graphql::{
model::{CompatSession, NodeType},
state::ContextExt,
};
#[derive(Default)]
pub struct CompatSessionMutations {
_private: (),
}
#[derive(InputObject)]
pub struct EndCompatSessionInput {
compat_session_id: ID,
}
pub enum EndCompatSessionPayload {
NotFound,
Ended(Box<mas_data_model::CompatSession>),
}
#[derive(Enum, Copy, Clone, PartialEq, Eq, Debug)]
enum EndCompatSessionStatus {
Ended,
NotFound,
}
#[Object]
impl EndCompatSessionPayload {
async fn status(&self) -> EndCompatSessionStatus {
match self {
Self::Ended(_) => EndCompatSessionStatus::Ended,
Self::NotFound => EndCompatSessionStatus::NotFound,
}
}
async fn compat_session(&self) -> Option<CompatSession> {
match self {
Self::Ended(session) => Some(CompatSession::new(*session.clone())),
Self::NotFound => None,
}
}
}
#[Object]
impl CompatSessionMutations {
async fn end_compat_session(
&self,
ctx: &Context<'_>,
input: EndCompatSessionInput,
) -> Result<EndCompatSessionPayload, async_graphql::Error> {
let state = ctx.state();
let compat_session_id = NodeType::CompatSession.extract_ulid(&input.compat_session_id)?;
let requester = ctx.requester();
let mut repo = state.repository().await?;
let clock = state.clock();
let session = repo.compat_session().lookup(compat_session_id).await?;
let Some(session) = session else {
return Ok(EndCompatSessionPayload::NotFound);
};
if !requester.is_owner_or_admin(&session) {
return Ok(EndCompatSessionPayload::NotFound);
}
let user = repo
.user()
.lookup(session.user_id)
.await?
.context("Could not load user")?;
repo.job().schedule_job(SyncDevicesJob::new(&user)).await?;
let session = repo.compat_session().finish(&clock, session).await?;
repo.save().await?;
Ok(EndCompatSessionPayload::Ended(Box::new(session)))
}
}