diff --git a/server/realms/routes.ts b/server/realms/routes.ts index 0925fda1..2d476f02 100644 --- a/server/realms/routes.ts +++ b/server/realms/routes.ts @@ -25,8 +25,10 @@ import { getRealmDataBySlug, getSettingsBySlug } from "./queries"; import { processBoardsNotifications, processBoardsSummary, + reduceById, } from "utils/response-utils"; +import { DbRealmBoardType } from "server/boards/sql/types"; import { RealmPermissions } from "types/permissions"; import { createInvite } from "server/realms/queries"; import debug from "debug"; @@ -243,24 +245,20 @@ router.get("/:realm_id/notifications", ensureLoggedIn, async (req, res) => { boards, }); const pinned = notifications - .filter((notification: any) => + .filter((notification) => boards.find( - (board: any) => + (board) => board.string_id == notification.id && board.pinned_order !== null ) ) - .reduce((result: any, current: any) => { - result[current.id] = { - ...current, - }; - return result; - }, {}); - const realmBoards = notifications.reduce((result: any, current: any) => { - result[current.id] = { - ...current, - }; - return result; - }, {}); + .reduce( + reduceById, + {} as Record[0]> + ); + const realmBoards = notifications.reduce( + reduceById, + {} as Record[0]> + ); const hasNotifications = notifications.some( (notification) => notification.has_updates diff --git a/utils/response-utils.ts b/utils/response-utils.ts index 766830b8..7feb036c 100644 --- a/utils/response-utils.ts +++ b/utils/response-utils.ts @@ -1,3 +1,4 @@ +import { BoardByExternalId, DbRealmBoardType } from "server/boards/sql/types"; import { BoardMetadata, Comment, @@ -17,7 +18,6 @@ import { ZodDbThreadType, } from "types/db/schemas"; -import { BoardByExternalId } from "server/boards/sql/types"; import { BoardRestrictions } from "types/permissions"; import debug from "debug"; import { getUserPermissionsForBoard } from "./permissions-utils"; @@ -384,7 +384,11 @@ export const processBoardsSummary = ({ })); }; -export const processBoardsNotifications = ({ boards }: { boards: any[] }) => { +export const processBoardsNotifications = ({ + boards, +}: { + boards: DbRealmBoardType[]; +}) => { return boards.map((board) => ({ id: board.string_id, has_updates: board.has_updates, @@ -399,3 +403,13 @@ export const processBoardsNotifications = ({ boards }: { boards: any[] }) => { last_visited_at: board.last_visit_at, })); }; + +export const reduceById = ( + result: Record, + current: T +): Record => { + result[current.id] = { + ...current, + }; + return result; +};