feat: past and overlapping ooo (#17373)

* feat: past and overlapping ooo

* use existing props

* added e2e test

* disallow duplicates added tests

---------

Co-authored-by: Peer Richelsen <[email protected]>
This commit is contained in:
Amit SharmaandPeer Richelsen authored and GitHub committed 2024-11-24 18:58:22 +00:00
1 parent 3936a0ae94
commit 5af10a2f61
5 files changed
+131 -54

No files matched your search

@@ -162,6 +162,7 @@ export const CreateOrEditOutOfOfficeEntryModal = ({
control={control}
render={({ field: { onChange, value } }) => (
<DateRangePicker
minDate={null}
dates={{ startDate: value.startDate, endDate: value.endDate }}
onDatesChange={(values) => {
onChange(values);
@@ -38,11 +38,6 @@ export const outOfOfficeCreateOrUpdate = async ({ ctx, input }: TBookingRedirect
throw new TRPCError({ code: "BAD_REQUEST", message: "start_date_must_be_before_end_date" });
}
// If start date is before to today throw error
if (inputStartTime.isBefore(dayjs().startOf("day").subtract(1, "day"))) {
throw new TRPCError({ code: "BAD_REQUEST", message: "start_date_must_be_in_the_future" });
}
let toUserId: number | null = null;
if (input.toTeamUserId) {
@@ -73,51 +68,6 @@ export const outOfOfficeCreateOrUpdate = async ({ ctx, input }: TBookingRedirect
toUserId = user?.id;
}
// Validate if OOO entry for these dates already exists
const outOfOfficeEntry = await prisma.outOfOfficeEntry.findFirst({
where: {
AND: [
{ userId: ctx.user.id },
{
uuid: {
not: input.uuid ?? "",
},
},
{
OR: [
{
start: {
lt: inputEndTime.toISOString(), //existing start is less than or equal to input end time
},
end: {
gt: inputStartTime.toISOString(), //existing end is greater than or equal to input start time
},
},
{
//existing start is within the new input range
start: {
gt: inputStartTime.toISOString(),
lt: inputEndTime.toISOString(),
},
},
{
//existing end is within the new input range
end: {
gt: inputStartTime.toISOString(),
lt: inputEndTime.toISOString(),
},
},
],
},
],
},
});
// don't allow overlapping entries
if (outOfOfficeEntry) {
throw new TRPCError({ code: "CONFLICT", message: "out_of_office_entry_already_exists" });
}
if (!input.reasonId) {
throw new TRPCError({ code: "BAD_REQUEST", message: "reason_id_required" });
}
@@ -155,9 +105,22 @@ export const outOfOfficeCreateOrUpdate = async ({ ctx, input }: TBookingRedirect
if (existingOutOfOfficeEntry) {
throw new TRPCError({ code: "BAD_REQUEST", message: "booking_redirect_infinite_not_allowed" });
}
const startTimeUtc = dayjs.utc(startDate).add(input.offset, "minute").startOf("day").toISOString();
const endTimeUtc = dayjs.utc(endDate).add(input.offset, "minute").endOf("day").toISOString();
const isDuplicateOutOfOfficeEntry = await prisma.outOfOfficeEntry.findFirst({
where: {
userId: ctx.user.id,
start: startTimeUtc,
end: endTimeUtc,
},
});
if (isDuplicateOutOfOfficeEntry) {
throw new TRPCError({ code: "CONFLICT", message: "out_of_office_entry_already_exists" });
}
// Get the existing redirected user from existing out of office entry to send that user appropriate email.
const previousOutOfOfficeEntry = await prisma.outOfOfficeEntry.findUnique({
where: {
@@ -21,7 +21,7 @@ function Calendar({
}: CalendarProps) {
return (
<DayPicker
fromDate={fromDate || new Date()}
fromDate={fromDate}
toDate={toDate || undefined}
showOutsideDays={showOutsideDays}
className={cn("p-3", className)}
@@ -14,7 +14,7 @@ type DatePickerWithRangeProps = {
dates: { startDate: Date; endDate?: Date };
onDatesChange: ({ startDate, endDate }: { startDate?: Date; endDate?: Date }) => void;
disabled?: boolean;
minDate?: Date;
minDate?: Date | null;
maxDate?: Date;
};
@@ -34,6 +34,7 @@ export function DatePickerWithRange({
onDatesChange({ startDate: onChangeValues?.from, endDate: onChangeValues?.to });
}
}
const fromDate = minDate ?? new Date();
return (
<div className={cn("grid gap-2", className)}>
@@ -63,7 +64,8 @@ export function DatePickerWithRange({
sideOffset={4}>
<Calendar
initialFocus
fromDate={minDate}
//When explicitly null, we want past dates to be shown as well, otherwise show only dates passed or from current date
fromDate={minDate === null ? undefined : fromDate}
toDate={maxDate}
mode="range"
defaultMonth={dates?.startDate}