From 6f0e172f22041d6db8056c7e3b442c91220360af Mon Sep 17 00:00:00 2001 From: Pjaijai Date: Sat, 30 Mar 2024 14:09:27 -0400 Subject: [PATCH] feat: update clean storage api logic --- .../chat/components/cards/message/message.tsx | 10 ++-------- .../lists/conversation/conversation.tsx | 1 + .../chat/components/lists/message/message.tsx | 3 +++ .../chat/components/sections/right/right.tsx | 1 + client/modules/chat/state/conversations.tsx | 1 + client/types/api/response/chat/message-list.ts | 1 + client/types/api/response/conversation-list.ts | 1 + client/types/supabase.ts | 3 +++ client/utils/common/api/index.tsx | 3 ++- .../functions/clean-conversation-storage/index.ts | 15 ++++++++++++++- ..._add_is_document_expired_for_message_table.sql | 3 +++ 11 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 supabase/migrations/20240330174617_add_is_document_expired_for_message_table.sql diff --git a/client/modules/chat/components/cards/message/message.tsx b/client/modules/chat/components/cards/message/message.tsx index 3f84f429..5f596601 100644 --- a/client/modules/chat/components/cards/message/message.tsx +++ b/client/modules/chat/components/cards/message/message.tsx @@ -10,23 +10,17 @@ interface IMessageCardProps { sentByUser: boolean createdAt: string // datetime string document?: any + isDocumentExpired: boolean } const MessageCard: React.FunctionComponent = ({ text, sentByUser, createdAt, document, + isDocumentExpired, }) => { const { data: formattedDate } = useCreatedAt({ createdAt }) - const dayDiff = compareDateDifferenceHelper({ - oldDate: createdAt, - newDate: new Date().toISOString(), - unit: "day", - }) - - const isDocumentExpired = dayDiff > 7 - const linkify = (text: string | null) => { if (!text) return "" var urlRegex = diff --git a/client/modules/chat/components/lists/conversation/conversation.tsx b/client/modules/chat/components/lists/conversation/conversation.tsx index 6b107b1d..37a9a062 100644 --- a/client/modules/chat/components/lists/conversation/conversation.tsx +++ b/client/modules/chat/components/lists/conversation/conversation.tsx @@ -37,6 +37,7 @@ const ConversationList = () => { body: last_message_uuid.body, createdByUuid: last_message_uuid.sender_uuid, createdAt: last_message_uuid.created_at, + isDocumentExpired: last_message_uuid.is_document_expired, } : null diff --git a/client/modules/chat/components/lists/message/message.tsx b/client/modules/chat/components/lists/message/message.tsx index a8750b6d..546329e0 100644 --- a/client/modules/chat/components/lists/message/message.tsx +++ b/client/modules/chat/components/lists/message/message.tsx @@ -16,6 +16,7 @@ interface IMessageListProps { body?: string createdAt?: string document?: IMediaResponse + isDocumentExpired?: boolean } } const MessageList: React.FunctionComponent = ({ @@ -91,6 +92,7 @@ const MessageList: React.FunctionComponent = ({ key={data.uuid} createdAt={data.created_at} document={data.document} + isDocumentExpired={data.is_document_expired} /> ) })} @@ -103,6 +105,7 @@ const MessageList: React.FunctionComponent = ({ sentByUser={lastMessage.sentByUser} createdAt={lastMessage.createdAt || ""} document={lastMessage.document} + isDocumentExpired={lastMessage.isDocumentExpired ?? false} /> )} diff --git a/client/modules/chat/components/sections/right/right.tsx b/client/modules/chat/components/sections/right/right.tsx index 080fa854..1b58293b 100644 --- a/client/modules/chat/components/sections/right/right.tsx +++ b/client/modules/chat/components/sections/right/right.tsx @@ -92,6 +92,7 @@ const ChatRightSection = () => { sentByUser: currentConversation.lastMessage?.createdByUuid === userUuid, createdAt: currentConversation.lastMessage?.createdAt, + isDocumentExpired: currentConversation.lastMessage?.isDocumentExpired, }} /> { .select("*") .not("document", "is", null) .lte("created_at", date.toISOString()) + .eq("is_document_expired", false) + const uuids = data.map((d) => d.uuid) const paths = data.map((d) => d.document.internalPath) const { data: removeRes } = await server.storage .from("conversation_documents") .remove(paths) - return new Response(JSON.stringify(removeRes), { + const updateRes = await Promise.all( + uuids.map(async (uuid) => { + const { data, error } = await server + .from("message") + .update({ is_document_expired: true }) + .eq("uuid", uuid) + .select("uuid") + return data + }), + ) + + return new Response(JSON.stringify(updateRes), { headers: { ...corsHeaders, "Content-Type": "application/json" }, // Be sure to add CORS headers here too status: 200, }) diff --git a/supabase/migrations/20240330174617_add_is_document_expired_for_message_table.sql b/supabase/migrations/20240330174617_add_is_document_expired_for_message_table.sql new file mode 100644 index 00000000..c0e6f7be --- /dev/null +++ b/supabase/migrations/20240330174617_add_is_document_expired_for_message_table.sql @@ -0,0 +1,3 @@ +alter table "public"."message" add column "is_document_expired" boolean not null default false; + +