fix: delete reaction from comment SQL query should be based on comment id, not view id

This commit is contained in:
khorshuheng 2024-07-29 14:47:33 +08:00
parent b108325c7c
commit b6c9f541e2
4 changed files with 14 additions and 14 deletions

View File

@ -1,6 +1,6 @@
{ {
"db_name": "PostgreSQL", "db_name": "PostgreSQL",
"query": "\n DELETE FROM af_published_view_reaction\n WHERE view_id = $1 AND created_by = (SELECT uid FROM af_user WHERE uuid = $2) AND reaction_type = $3\n ", "query": "\n DELETE FROM af_published_view_reaction\n WHERE comment_id = $1 AND created_by = (SELECT uid FROM af_user WHERE uuid = $2) AND reaction_type = $3\n ",
"describe": { "describe": {
"columns": [], "columns": [],
"parameters": { "parameters": {
@ -12,5 +12,5 @@
}, },
"nullable": [] "nullable": []
}, },
"hash": "3bf9811b3cfc16b677c76acee21342b892cf815954e4516589493aae01555dc0" "hash": "816a026ca4c25329b2fb24d59efde9ab71798ff8b31ce7320e02344d4e8b3e42"
} }

View File

@ -111,8 +111,8 @@ impl Client {
pub async fn create_reaction_on_comment( pub async fn create_reaction_on_comment(
&self, &self,
reaction_type: &str, reaction_type: &str,
comment_id: &uuid::Uuid,
view_id: &uuid::Uuid, view_id: &uuid::Uuid,
comment_id: &uuid::Uuid,
) -> Result<(), AppResponseError> { ) -> Result<(), AppResponseError> {
let url = format!( let url = format!(
"{}/api/workspace/published-info/{}/reaction", "{}/api/workspace/published-info/{}/reaction",

View File

@ -1365,24 +1365,24 @@ pub async fn insert_reaction_on_comment<'a, E: Executor<'a, Database = Postgres>
pub async fn delete_reaction_from_comment<'a, E: Executor<'a, Database = Postgres>>( pub async fn delete_reaction_from_comment<'a, E: Executor<'a, Database = Postgres>>(
executor: E, executor: E,
view_id: &Uuid, comment_id: &Uuid,
user_uuid: &Uuid, user_uuid: &Uuid,
reaction_type: &str, reaction_type: &str,
) -> Result<(), AppError> { ) -> Result<(), AppError> {
let res = sqlx::query!( let res = sqlx::query!(
r#" r#"
DELETE FROM af_published_view_reaction DELETE FROM af_published_view_reaction
WHERE view_id = $1 AND created_by = (SELECT uid FROM af_user WHERE uuid = $2) AND reaction_type = $3 WHERE comment_id = $1 AND created_by = (SELECT uid FROM af_user WHERE uuid = $2) AND reaction_type = $3
"#, "#,
view_id, comment_id,
user_uuid, user_uuid,
reaction_type, reaction_type,
).execute(executor).await?; ).execute(executor).await?;
if res.rows_affected() != 1 { if res.rows_affected() != 1 {
tracing::error!( tracing::error!(
"Failed to delete reaction from published view, view_id: {}, user_id: {}, reaction_type: {}, rows_affected: {}", "Failed to delete reaction from published comment, comment_id: {}, user_id: {}, reaction_type: {}, rows_affected: {}",
view_id, user_uuid, reaction_type, res.rows_affected() comment_id, user_uuid, reaction_type, res.rows_affected()
); );
}; };

View File

@ -454,23 +454,23 @@ async fn test_publish_reactions() {
let like_emoji = "👍"; let like_emoji = "👍";
let party_emoji = "🎉"; let party_emoji = "🎉";
page_owner_client page_owner_client
.create_reaction_on_comment(like_emoji, &likable_comment_id, &view_id) .create_reaction_on_comment(like_emoji, &view_id, &likable_comment_id)
.await .await
.unwrap(); .unwrap();
let guest_client = localhost_client(); let guest_client = localhost_client();
let result = guest_client let result = guest_client
.create_reaction_on_comment(like_emoji, &likable_comment_id, &view_id) .create_reaction_on_comment(like_emoji, &view_id, &likable_comment_id)
.await; .await;
assert!(result.is_err()); assert!(result.is_err());
assert_eq!(result.unwrap_err().code, ErrorCode::NotLoggedIn); assert_eq!(result.unwrap_err().code, ErrorCode::NotLoggedIn);
let (user_client, _) = generate_unique_registered_user_client().await; let (user_client, _) = generate_unique_registered_user_client().await;
user_client user_client
.create_reaction_on_comment(like_emoji, &likable_comment_id, &view_id) .create_reaction_on_comment(like_emoji, &view_id, &likable_comment_id)
.await .await
.unwrap(); .unwrap();
user_client user_client
.create_reaction_on_comment(party_emoji, &party_comment_id, &view_id) .create_reaction_on_comment(party_emoji, &view_id, &party_comment_id)
.await .await
.unwrap(); .unwrap();
@ -489,12 +489,12 @@ async fn test_publish_reactions() {
// Test if the reactions are deleted correctly based on view and comment id // Test if the reactions are deleted correctly based on view and comment id
let result = guest_client let result = guest_client
.delete_reaction_on_comment(like_emoji, &likable_comment_id, &view_id) .delete_reaction_on_comment(like_emoji, &view_id, &likable_comment_id)
.await; .await;
assert!(result.is_err()); assert!(result.is_err());
assert_eq!(result.unwrap_err().code, ErrorCode::NotLoggedIn); assert_eq!(result.unwrap_err().code, ErrorCode::NotLoggedIn);
user_client user_client
.delete_reaction_on_comment(like_emoji, &likable_comment_id, &view_id) .delete_reaction_on_comment(like_emoji, &view_id, &likable_comment_id)
.await .await
.unwrap(); .unwrap();