Skip to content

Commit

Permalink
feat: update clean storage api logic
Browse files Browse the repository at this point in the history
  • Loading branch information
pauljaijai committed Mar 30, 2024
1 parent ccb01da commit 6f0e172
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 10 deletions.
10 changes: 2 additions & 8 deletions client/modules/chat/components/cards/message/message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,17 @@ interface IMessageCardProps {
sentByUser: boolean
createdAt: string // datetime string
document?: any
isDocumentExpired: boolean
}
const MessageCard: React.FunctionComponent<IMessageCardProps> = ({
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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions client/modules/chat/components/lists/message/message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface IMessageListProps {
body?: string
createdAt?: string
document?: IMediaResponse
isDocumentExpired?: boolean
}
}
const MessageList: React.FunctionComponent<IMessageListProps> = ({
Expand Down Expand Up @@ -91,6 +92,7 @@ const MessageList: React.FunctionComponent<IMessageListProps> = ({
key={data.uuid}
createdAt={data.created_at}
document={data.document}
isDocumentExpired={data.is_document_expired}
/>
)
})}
Expand All @@ -103,6 +105,7 @@ const MessageList: React.FunctionComponent<IMessageListProps> = ({
sentByUser={lastMessage.sentByUser}
createdAt={lastMessage.createdAt || ""}
document={lastMessage.document}
isDocumentExpired={lastMessage.isDocumentExpired ?? false}
/>
)}
</div>
Expand Down
1 change: 1 addition & 0 deletions client/modules/chat/components/sections/right/right.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const ChatRightSection = () => {
sentByUser:
currentConversation.lastMessage?.createdByUuid === userUuid,
createdAt: currentConversation.lastMessage?.createdAt,
isDocumentExpired: currentConversation.lastMessage?.isDocumentExpired,
}}
/>
<SendMessageForm
Expand Down
1 change: 1 addition & 0 deletions client/modules/chat/state/conversations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface IConversation {
createdByUuid: string
body: string
createdAt: string
isDocumentExpired: boolean
} | null
}

Expand Down
1 change: 1 addition & 0 deletions client/types/api/response/chat/message-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export interface IMessageListResponse {
status: "active" | "inactive"
uuid: string
document: any //IMediaResponse
is_document_expired: boolean
}
1 change: 1 addition & 0 deletions client/types/api/response/conversation-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface IGetConversationListByUserUuidResponse {
body: string
created_at: string
document: IMediaResponse | null
is_document_expired: boolean
} | null
last_updated_at: string //datetime string withe time zone
}
3 changes: 3 additions & 0 deletions client/types/supabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ export interface Database {
created_at: string
document: Json | null
id: number
is_document_expired: boolean
sender_uuid: string
status: Database["public"]["Enums"]["message_status"]
uuid: string
Expand All @@ -208,6 +209,7 @@ export interface Database {
created_at?: string
document?: Json | null
id?: number
is_document_expired?: boolean
sender_uuid?: string
status?: Database["public"]["Enums"]["message_status"]
uuid?: string
Expand All @@ -218,6 +220,7 @@ export interface Database {
created_at?: string
document?: Json | null
id?: number
is_document_expired?: boolean
sender_uuid?: string
status?: Database["public"]["Enums"]["message_status"]
uuid?: string
Expand Down
3 changes: 2 additions & 1 deletion client/utils/common/api/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,8 @@ export const getConversationListByUserUuid = async ({
uuid,
sender_uuid,
body,
document
document,
is_document_expired
)
`
)
Expand Down
15 changes: 14 additions & 1 deletion supabase/functions/clean-conversation-storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,27 @@ serve(async (req: any) => {
.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,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
alter table "public"."message" add column "is_document_expired" boolean not null default false;


0 comments on commit 6f0e172

Please sign in to comment.