Skip to content

Commit

Permalink
fix: 既読状態の更新に失敗する不具合を修正
Browse files Browse the repository at this point in the history
  • Loading branch information
rito528 committed Nov 21, 2024
1 parent b05fa26 commit 1bda156
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
39 changes: 27 additions & 12 deletions server/infra/resource/src/database/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ use domain::{
user::models::Role,
};
use errors::infra::InfraError;
use sea_orm::Value;
use types::Id;
use uuid::Uuid;

use crate::{
database::{
components::NotificationDatabase,
connection::{batch_insert, execute_and_values, query_all_and_values, ConnectionPool},
connection::{execute_and_values, query_all_and_values, ConnectionPool},
},
dto::{NotificationDto, NotificationSourceInformationDto, NotificationSourceTypeDto, UserDto},
};
Expand Down Expand Up @@ -55,7 +56,7 @@ impl NotificationDatabase for ConnectionPool {
FROM notifications
INNER JOIN users ON notifications.recipient_id = users.id
WHERE recipient_id = ?",
[recipient_id.into()],
[recipient_id.to_string().into()],
txn,
).await?;

Expand Down Expand Up @@ -93,14 +94,14 @@ impl NotificationDatabase for ConnectionPool {
WHERE notifications.id IN (?{})",
", ?".repeat(notification_ids.len() - 1)
)
.as_str(),
.as_str(),
notification_ids
.into_iter()
.map(Id::into_inner)
.map(Into::into),
txn,
)
.await?;
.await?;

rs.into_iter()
.map(|rs| {
Expand Down Expand Up @@ -129,8 +130,8 @@ impl NotificationDatabase for ConnectionPool {
.collect::<Result<Vec<_>, _>>()
})
})
.await
.map_err(Into::into)
.await
.map_err(Into::into)
}

async fn update_read_status(
Expand All @@ -139,14 +140,28 @@ impl NotificationDatabase for ConnectionPool {
) -> Result<(), InfraError> {
self.read_write_transaction(|txn| {
Box::pin(async move {
batch_insert(
r"INSERT INTO notifications (id, is_read) VALUES (?, ?) ON DUPLICATE KEY UPDATE is_read = VALUES(is_read)",
notification_id_with_is_read.into_iter().flat_map(|(id, is_read)| [id.to_string().into(), is_read.into()]),
txn,
).await?;
let placeholders = vec!["?"; notification_id_with_is_read.len()].join(", ");

let query = format!(
r"UPDATE notifications
SET is_read = ELT(FIELD(id, {placeholders}), {placeholders})
WHERE id IN ({placeholders})"
);

let (notification_ids, is_reads): (Vec<Value>, Vec<Value>) =
notification_id_with_is_read
.into_iter()
.map(|(id, is_read)| (id.into_inner().to_string().into(), is_read.into()))
.unzip();

let params = [notification_ids.to_owned(), is_reads, notification_ids].concat();

execute_and_values(&query, params, txn).await?;

Ok::<_, InfraError>(())
})
}).await.map_err(Into::into)
})
.await
.map_err(Into::into)
}
}
2 changes: 1 addition & 1 deletion server/migration/src/m20220101_000001_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ impl MigrationTrait for Migration {
r"CREATE TABLE IF NOT EXISTS notifications(
id UUID NOT NULL PRIMARY KEY,
source_type ENUM('MESSAGE') NOT NULL,
source_id UUID NOT NULL,
recipient_id CHAR(36) NOT NULL,
related_id UUID NOT NULL,
is_read BOOL DEFAULT FALSE NOT NULL,
FOREIGN KEY fk_notification_recipient_id(recipient_id) REFERENCES users(id)
)",
Expand Down

0 comments on commit 1bda156

Please sign in to comment.