rename updatedAt to editedAt, and set it in akka-apps instead of pg trigger

This commit is contained in:
Gustavo Trott 2024-10-02 16:22:01 -03:00
parent a6fc59ba46
commit 8dd48b5fe1
4 changed files with 19 additions and 43 deletions

View File

@ -17,7 +17,7 @@ case class ChatMessageDbModel(
senderName: String,
senderRole: Option[String],
createdAt: java.sql.Timestamp,
updatedAt: Option[java.sql.Timestamp],
editedAt: Option[java.sql.Timestamp],
deletedAt: Option[java.sql.Timestamp]
)
@ -37,13 +37,13 @@ class ChatMessageDbTableDef(tag: Tag) extends Table[ChatMessageDbModel](tag, Non
// val chat = foreignKey("chat_message_chat_fk", (chatId, meetingId), ChatTable.chats)(c => (c.chatId, c.meetingId), onDelete = ForeignKeyAction.Cascade)
// val sender = foreignKey("chat_message_sender_fk", senderId, UserTable.users)(_.userId, onDelete = ForeignKeyAction.SetNull)
val createdAt = column[java.sql.Timestamp]("createdAt")
val updatedAt = column[Option[java.sql.Timestamp]]("updatedAt")
val editedAt = column[Option[java.sql.Timestamp]]("editedAt")
val deletedAt = column[Option[java.sql.Timestamp]]("deletedAt")
override def * = (
messageId, chatId, meetingId, correlationId, chatEmphasizedText,
message, messageType, replyToMessageId, messageMetadata, senderId, senderName, senderRole,
createdAt, updatedAt, deletedAt
createdAt, editedAt, deletedAt
) <> (ChatMessageDbModel.tupled, ChatMessageDbModel.unapply)
}
@ -68,7 +68,7 @@ object ChatMessageDAO {
senderName = groupChatMessage.sender.name,
senderRole = Some(groupChatMessage.sender.role),
createdAt = new java.sql.Timestamp(System.currentTimeMillis()),
updatedAt = None,
editedAt = None,
deletedAt = None,
)
)
@ -78,26 +78,6 @@ object ChatMessageDAO {
ChatUserDAO.updateChatVisible(meetingId, chatId, visible = true)
}
// def insert(meetingId: String, chatId: String, groupChatMessage: GroupChatMessage): Unit = {
// val chatMessage = ChatMessageDbModel(
// messageId = groupChatMessage.id,
// chatId = chatId,
// meetingId = meetingId,
// correlationId = groupChatMessage.correlationId,
// createdAt = new java.sql.Timestamp(System.currentTimeMillis()),
// chatEmphasizedText = groupChatMessage.chatEmphasizedText,
// message = groupChatMessage.message,
// messageType = "default",
// messageMetadata = None,
// senderId = Some(groupChatMessage.sender.id),
// senderName = groupChatMessage.sender.name,
// senderRole = Some(groupChatMessage.sender.role),
// )
//
// val insertAction = TableQuery[ChatMessageDbTableDef].insertOrUpdate(chatMessage)
// DatabaseConnection.enqueue(insertAction)
// }
def insertSystemMsg(meetingId: String, chatId: String, message: String, messageType: String, messageMetadata: Map[String,Any], senderName: String) = {
DatabaseConnection.enqueue(
TableQuery[ChatMessageDbTableDef].forceInsert(
@ -115,7 +95,7 @@ object ChatMessageDAO {
senderName = senderName,
senderRole = None,
createdAt = new java.sql.Timestamp(System.currentTimeMillis()),
updatedAt = None,
editedAt = None,
deletedAt = None,
)
)
@ -133,8 +113,8 @@ object ChatMessageDAO {
.filter(_.chatId === chatId)
.filter(_.messageId === messageId)
.filter(_.message.nonEmpty)
.map(u => u.message)
.update(Some(message))
.map(msg => (msg.message, msg.editedAt))
.update((Some(message), Some(new java.sql.Timestamp(System.currentTimeMillis()))))
)
}
@ -146,8 +126,8 @@ object ChatMessageDAO {
.filter(_.chatId === chatId)
.filter(_.messageId === messageId)
.filter(_.message.nonEmpty)
.map(u => u.message)
.update(None)
.map(msg => (msg.message, msg.deletedAt))
.update((None, Some(new java.sql.Timestamp(System.currentTimeMillis()))))
)
}

View File

@ -998,7 +998,7 @@ CREATE TABLE "chat_message" (
"senderName" varchar(255),
"senderRole" varchar(20),
"createdAt" timestamp with time zone not null,
"updatedAt" timestamp with time zone,
"editedAt" timestamp with time zone,
"deletedAt" timestamp with time zone,
CONSTRAINT chat_fk FOREIGN KEY ("chatId", "meetingId") REFERENCES "chat"("chatId", "meetingId") ON DELETE CASCADE
);
@ -1042,7 +1042,8 @@ CREATE TABLE "chat_message_history" (
"meetingId" varchar(100),
"messageVersionSequence" integer, --populated via trigger
"message" text,
"messageCreatedAt" timestamp with time zone,
"senderId" varchar(100),
"createdAt" timestamp with time zone,
"movedToHistoryAt" timestamp with time zone default current_timestamp,
CONSTRAINT chat_message_history_pk PRIMARY KEY ("messageId", "messageVersionSequence")
);
@ -1053,20 +1054,15 @@ CREATE OR REPLACE VIEW "v_chat_message_history" AS SELECT * FROM "chat_message_h
CREATE OR REPLACE FUNCTION "update_chat_message_history_trigger_func"()
RETURNS TRIGGER AS $$
BEGIN
IF NEW."message" <> OLD."message" THEN
insert into "chat_message_history"("messageId", "meetingId", "messageVersionSequence", "message", "messageCreatedAt")
IF NEW."message" IS DISTINCT FROM OLD."message" THEN
insert into "chat_message_history"("messageId", "meetingId", "messageVersionSequence", "message", "senderId", "createdAt")
values (OLD."messageId",
OLD."meetingId",
(select count(1) from "chat_message_history" prev where prev."messageId" = OLD."messageId"),
OLD."message",
coalesce(OLD."updatedAt",OLD."createdAt")
OLD."senderId",
coalesce(OLD."editedAt",OLD."createdAt")
);
IF NEW."message" IS NULL THEN
NEW."deletedAt" = current_timestamp;
ELSE
NEW."updatedAt" = current_timestamp;
END IF;
END IF;
RETURN NEW;
@ -1127,7 +1123,7 @@ SELECT cu."meetingId",
cm."senderName",
cm."senderRole",
cm."createdAt",
cm."updatedAt",
cm."editedAt",
cm."deletedAt",
CASE WHEN chat_with."lastSeenAt" >= cm."createdAt" THEN true ELSE false end "recipientHasSeen"
FROM chat_message cm

View File

@ -44,7 +44,7 @@ select_permissions:
- senderId
- senderName
- senderRole
- updatedAt
- editedAt
filter:
_and:
- meetingId:

View File

@ -43,7 +43,7 @@ select_permissions:
- senderId
- senderName
- senderRole
- updatedAt
- editedAt
filter:
meetingId:
_eq: X-Hasura-MeetingId