Skip to content

Commit

Permalink
fix unread counts
Browse files Browse the repository at this point in the history
  • Loading branch information
Joosakur committed Feb 12, 2025
1 parent c8609ea commit 3af6bf5
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 19 deletions.
6 changes: 1 addition & 5 deletions frontend/src/employee-frontend/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,7 @@ export default React.memo(function Header() {
const accountCounts = counts.find(
(c) => c.accountId === accountId
)
return (
sum +
(accountCounts?.unreadCount ?? 0) +
(accountCounts?.unreadCopyCount ?? 0)
)
return sum + (accountCounts?.totalUnreadCount ?? 0)
},
0
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ export default function GroupMessageAccountList({
const accountUnreadCounts = counts.find(
(a) => a.accountId === acc.account.id
)
return accountUnreadCounts
? accountUnreadCounts.unreadCount +
accountUnreadCounts.unreadCopyCount
: 0
return accountUnreadCounts ? accountUnreadCounts.totalUnreadCount : 0
})
.getOrElse(0) === 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ export default function MessageBox({
?.unreadCopyCount ?? 0
)
}
if (!isStandardView(view)) {
return (
unreadCounts.find(({ accountId }) => accountId === account.id)
?.unreadCountByFolder?.[view.id] ?? 0
)
}

return 0
})
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/lib-common/generated/api-types/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,10 @@ export interface ThreadReply {
*/
export interface UnreadCountByAccount {
accountId: MessageAccountId
totalUnreadCount: number
unreadCopyCount: number
unreadCount: number
unreadCountByFolder: Partial<Record<MessageThreadFolderId, number>>
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ data class UnreadCountByAccount(
val accountId: MessageAccountId,
val unreadCopyCount: Int,
val unreadCount: Int,
)
val unreadCountByFolder: Map<MessageThreadFolderId, Int>,
) {
val totalUnreadCount: Int
get() = unreadCount + unreadCopyCount + unreadCountByFolder.values.sum()
}

data class UnreadCountByAccountAndGroup(
val accountId: MessageAccountId,
Expand Down
41 changes: 32 additions & 9 deletions service/src/main/kotlin/fi/espoo/evaka/messaging/MessageQueries.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,48 @@ sealed class AccountAccessLimit {

fun Database.Read.getUnreadMessagesCounts(
idFilter: AccessControlFilter<MessageAccountId>
): Set<UnreadCountByAccount> =
createQuery {
sql(
"""
SELECT
acc.id as account_id,
count(*) FILTER (WHERE mtp.folder_id IS NULL AND NOT mt.is_copy) AS unread_count,
count(*) FILTER (WHERE mtp.folder_id IS NULL AND mt.is_copy) AS unread_copy_count
): Set<UnreadCountByAccount> {
data class RawData(
val accountId: MessageAccountId,
val isCopy: Boolean,
val folderId: MessageThreadFolderId?,
val count: Int,
)

val data =
createQuery {
sql(
"""
SELECT
acc.id as account_id,
coalesce(mt.is_copy, false) as is_copy,
mtp.folder_id,
count(mt.id) AS count
FROM message_account acc
LEFT JOIN message_recipients mr ON mr.recipient_id = acc.id AND mr.read_at IS NULL
LEFT JOIN message m ON mr.message_id = m.id AND m.sent_at IS NOT NULL
LEFT JOIN message_thread mt ON m.thread_id = mt.id
LEFT JOIN message_thread_participant mtp ON m.thread_id = mtp.thread_id AND mtp.participant_id = acc.id
WHERE ${predicate(idFilter.forTable("acc"))}
GROUP BY acc.id
GROUP BY acc.id, mt.is_copy, mtp.folder_id
"""
)
}
.toList<RawData>()

return data
.groupBy { it.accountId }
.map { (accountId, counts) ->
UnreadCountByAccount(
accountId = accountId,
unreadCount = counts.find { !it.isCopy && it.folderId == null }?.count ?: 0,
unreadCopyCount = counts.find { it.isCopy && it.folderId == null }?.count ?: 0,
unreadCountByFolder =
counts.filter { it.folderId != null }.associate { it.folderId!! to it.count },
)
}
.toSet()
}

fun Database.Read.getUnreadMessagesCountsByDaycare(
daycareId: DaycareId
Expand Down

0 comments on commit 3af6bf5

Please sign in to comment.