-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2957: feat(sdf, dal): Add ability to start a ChangeSet Approval Flow via WSEvents r=stack72 a=stack72 Co-authored-by: stack72 <[email protected]>
- Loading branch information
Showing
6 changed files
with
181 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
UPDATE change_sets | ||
SET status = $2, updated_at = now() | ||
WHERE pk = $1 | ||
RETURNING updated_at |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
lib/sdf-server/src/server/service/change_set/begin_approval_process.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use crate::server::extract::{AccessBuilder, HandlerContext, PosthogClient}; | ||
use crate::server::tracking::track; | ||
use crate::service::change_set::{ChangeSetError, ChangeSetResult}; | ||
use axum::extract::OriginalUri; | ||
use axum::Json; | ||
use dal::{ChangeSet, Visibility, WsEvent}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Deserialize, Serialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct BeginMergeFlow { | ||
#[serde(flatten)] | ||
pub visibility: Visibility, | ||
} | ||
|
||
pub async fn begin_approval_process( | ||
OriginalUri(original_uri): OriginalUri, | ||
PosthogClient(posthog_client): PosthogClient, | ||
HandlerContext(builder): HandlerContext, | ||
AccessBuilder(request_ctx): AccessBuilder, | ||
Json(request): Json<BeginMergeFlow>, | ||
) -> ChangeSetResult<Json<()>> { | ||
let mut ctx = builder.build(request_ctx.build(request.visibility)).await?; | ||
|
||
let mut change_set = ChangeSet::get_by_pk(&ctx, &ctx.visibility().change_set_pk) | ||
.await? | ||
.ok_or(ChangeSetError::ChangeSetNotFound)?; | ||
change_set.begin_approval_flow(&mut ctx).await?; | ||
|
||
track( | ||
&posthog_client, | ||
&ctx, | ||
&original_uri, | ||
"begin_approval_process", | ||
serde_json::json!({ | ||
"how": "/change_set/begin_approval_process", | ||
"change_set_pk": ctx.visibility().change_set_pk, | ||
}), | ||
); | ||
|
||
WsEvent::change_set_begin_approval_process(&ctx, ctx.visibility().change_set_pk) | ||
.await? | ||
.publish_on_commit(&ctx) | ||
.await?; | ||
|
||
ctx.commit().await?; | ||
|
||
Ok(Json(())) | ||
} |
60 changes: 60 additions & 0 deletions
60
lib/sdf-server/src/server/service/change_set/merge_vote.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use crate::server::extract::{AccessBuilder, HandlerContext, PosthogClient}; | ||
use crate::server::tracking::track; | ||
use crate::service::change_set::{ChangeSetError, ChangeSetResult}; | ||
use axum::extract::OriginalUri; | ||
use axum::Json; | ||
use dal::{HistoryActor, User, Visibility, WsEvent}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Deserialize, Serialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct MergeVoteRequest { | ||
pub approve: bool, | ||
#[serde(flatten)] | ||
pub visibility: Visibility, | ||
} | ||
|
||
pub async fn merge_vote( | ||
OriginalUri(original_uri): OriginalUri, | ||
PosthogClient(posthog_client): PosthogClient, | ||
HandlerContext(builder): HandlerContext, | ||
AccessBuilder(request_ctx): AccessBuilder, | ||
Json(request): Json<MergeVoteRequest>, | ||
) -> ChangeSetResult<Json<()>> { | ||
let ctx = builder.build(request_ctx.build(request.visibility)).await?; | ||
|
||
let user = match ctx.history_actor() { | ||
HistoryActor::User(user_pk) => User::get_by_pk(&ctx, *user_pk) | ||
.await? | ||
.ok_or(ChangeSetError::InvalidUser(*user_pk))?, | ||
|
||
HistoryActor::SystemInit => return Err(ChangeSetError::InvalidUserSystemInit), | ||
}; | ||
|
||
track( | ||
&posthog_client, | ||
&ctx, | ||
&original_uri, | ||
"merge_vote", | ||
serde_json::json!({ | ||
"how": "/change_set/merge_vote", | ||
"change_set_pk": ctx.visibility().change_set_pk, | ||
"user_pk": user.pk(), | ||
"vote": request.approve, | ||
}), | ||
); | ||
|
||
WsEvent::change_set_merge_vote( | ||
&ctx, | ||
ctx.visibility().change_set_pk, | ||
user.pk(), | ||
request.approve, | ||
) | ||
.await? | ||
.publish_on_commit(&ctx) | ||
.await?; | ||
|
||
ctx.commit().await?; | ||
|
||
Ok(Json(())) | ||
} |