Skip to content

Commit

Permalink
Merge pull request #1566 from Shelf-nu/feat-bookings-filters
Browse files Browse the repository at this point in the history
fix: not handling properly other bookings routes caused a crash
  • Loading branch information
DonKoko authored Jan 9, 2025
2 parents df39bda + 4a6ba57 commit c83000e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
43 changes: 30 additions & 13 deletions app/routes/_layout+/assets.$assetId.bookings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { BookingStatus } from "@prisma/client";
import { json, type LoaderFunctionArgs } from "@remix-run/node";
import { z } from "zod";
import type { HeaderData } from "~/components/layout/header/types";
import { hasGetAllValue } from "~/hooks/use-model-filters";
import { getBookings } from "~/modules/booking/service.server";
import { setSelectedOrganizationIdCookie } from "~/modules/organization/context.server";
import { getTeamMemberForCustodianFilter } from "~/modules/team-member/service.server";
import { getDateTimeFormat } from "~/utils/client-hints";
import {
setCookie,
Expand Down Expand Up @@ -50,25 +52,39 @@ export async function loader({ context, request, params }: LoaderFunctionArgs) {
});

const searchParams = getCurrentSearchParams(request);
const { page, perPageParam, search, status } =
const { page, perPageParam, search, status, teamMemberIds } =
getParamsValues(searchParams);

const cookie = await updateCookieWithPerPage(request, perPageParam);
const { perPage } = cookie;

const { bookings, bookingCount } = await getBookings({
organizationId,
page,
perPage,
search,
userId: authSession?.userId,
assetIds: [assetId],
statuses: status ? [status] : BOOKING_STATUS_TO_SHOW,
...(isSelfServiceOrBase && {
// If the user is self service, we only show bookings that belong to that user)
custodianUserId: authSession?.userId,
const [{ bookings, bookingCount }, teamMembersData] = await Promise.all([
getBookings({
organizationId,
page,
perPage,
search,
userId: authSession?.userId,
assetIds: [assetId],
statuses: status ? [status] : BOOKING_STATUS_TO_SHOW,
...(isSelfServiceOrBase && {
// If the user is self service, we only show bookings that belong to that user)
custodianUserId: authSession?.userId,
}),
custodianTeamMemberIds: teamMemberIds,
}),
});

// team members/custodian
getTeamMemberForCustodianFilter({
organizationId,
selectedTeamMembers: teamMemberIds,
getAll:
searchParams.has("getAll") &&
hasGetAllValue(searchParams, "teamMember"),
isSelfService: isSelfServiceOrBase, // we can assume this is false because this view is not allowed for
userId,
}),
]);

const totalPages = Math.ceil(bookingCount / perPage);

Expand Down Expand Up @@ -114,6 +130,7 @@ export async function loader({ context, request, params }: LoaderFunctionArgs) {
totalPages,
perPage,
modelName,
...teamMembersData,
}),
{
headers: [
Expand Down
7 changes: 6 additions & 1 deletion app/routes/_layout+/bookings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,12 @@ export default function BookingsIndexPage({
roles,
entity: PermissionEntity.custody,
action: PermissionAction.read,
}) && !isBaseOrSelfService
}) &&
!isBaseOrSelfService &&
!["$userId.bookings", "me.bookings"].includes(
// on the user bookings page we dont want to show the custodian filter becuase they are alreayd filtered for that user
currentRoute?.handle?.name
)
}
>
<DynamicDropdown
Expand Down

0 comments on commit c83000e

Please sign in to comment.