-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #892 from Shelf-nu/booking-add-to-calendar
testing feature that allows users to add a specific booking to their …
- Loading branch information
Showing
5 changed files
with
174 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { OrganizationRoles } from "@prisma/client"; | ||
import { json, type LoaderFunctionArgs } from "@remix-run/node"; | ||
import { z } from "zod"; | ||
import { getBooking } from "~/modules/booking/service.server"; | ||
import { getClientHint } from "~/utils/client-hints"; | ||
import { formatDatesForICal } from "~/utils/date-fns"; | ||
import { SERVER_URL } from "~/utils/env"; | ||
import { makeShelfError, ShelfError } from "~/utils/error"; | ||
import { error, getParams } from "~/utils/http.server"; | ||
import { PermissionAction, PermissionEntity } from "~/utils/permissions/types"; | ||
import { requirePermission } from "~/utils/roles.server"; | ||
|
||
export async function loader({ request, context, params }: LoaderFunctionArgs) { | ||
const authSession = context.getSession(); | ||
const { userId } = authSession; | ||
const { bookingId } = getParams(params, z.object({ bookingId: z.string() }), { | ||
additionalData: { userId }, | ||
}); | ||
|
||
try { | ||
/** Check if the current user is allowed to read booking */ | ||
const { organizationId, role } = await requirePermission({ | ||
userId: authSession.userId, | ||
request, | ||
entity: PermissionEntity.booking, | ||
action: PermissionAction.read, | ||
}); | ||
const booking = await getBooking({ | ||
id: bookingId, | ||
organizationId: organizationId, | ||
}); | ||
|
||
/** Check if the user is self service */ | ||
const isSelfService = role === OrganizationRoles.SELF_SERVICE; | ||
|
||
/** For self service users, we only allow them to read their own bookings */ | ||
if (isSelfService && booking.custodianUserId !== authSession.userId) { | ||
throw new ShelfError({ | ||
cause: null, | ||
message: | ||
"You are not authorized to download the calendar for this booking", | ||
status: 403, | ||
label: "Booking", | ||
shouldBeCaptured: false, | ||
}); | ||
} | ||
const hints = getClientHint(request); | ||
|
||
const formattedFromDate = formatDatesForICal(booking.from as Date, hints); | ||
const formattedToDate = formatDatesForICal(booking.to as Date, hints); | ||
const formattedDTSTAMP = formatDatesForICal(new Date(Date.now()), hints); | ||
|
||
const ics = ` | ||
BEGIN:VCALENDAR | ||
VERSION:2.0 | ||
PRODID:-//ZContent.net//Zap Calendar 1.0//EN | ||
CALSCALE:GREGORIAN | ||
METHOD:PUBLISH | ||
BEGIN:VEVENT | ||
SUMMARY:${booking.name} | ||
UID:${booking.id} | ||
SEQUENCE:${Date.now()} | ||
STATUS:CONFIRMED | ||
TRANSP:TRANSPARENT | ||
DTSTART:${formattedFromDate} | ||
DTEND:${formattedToDate} | ||
DTSTAMP:${formattedDTSTAMP} | ||
CATEGORIES:Shelf.nu booking | ||
LOCATION:shelf.nu | ||
DESCRIPTION:Shelf.nu booking (Asset / Equipment checkout) | ||
URL:${SERVER_URL}/bookings/${bookingId} | ||
END:VEVENT | ||
END:VCALENDAR`.trim(); | ||
|
||
return new Response(ics, { | ||
headers: { | ||
"Content-Type": "text/calendar", | ||
"Content-Disposition": `attachment; filename="${booking.name} - shelf.nu.ics"`, | ||
}, | ||
}); | ||
} catch (cause) { | ||
const reason = makeShelfError(cause, { userId }); | ||
return json(error(reason), { status: reason.status }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters