Merge branch 'main' into bugfix/unify-email-sending

This commit is contained in:
nicolas committed 2021-07-18 15:12:18 +02:00
commit b4ed8ae87e
51 files changed
+2713 -1423

No files matched your search

+5
View File
@@ -49,6 +49,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
},
})),
},
periodType: req.body.periodType,
periodDays: req.body.periodDays,
periodStartDate: req.body.periodStartDate,
periodEndDate: req.body.periodEndDate,
periodCountCalendarDays: req.body.periodCountCalendarDays,
};
if (req.method == "POST") {
+67 -5
View File
@@ -13,6 +13,14 @@ import logger from "../../../lib/logger";
import EventManager, { EventResult } from "@lib/events/EventManager";
import { User } from "@prisma/client";
import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone";
import dayjsBusinessDays from "dayjs-business-days";
dayjs.extend(dayjsBusinessDays);
dayjs.extend(utc);
dayjs.extend(timezone);
const translator = short();
const log = logger.getChildLogger({ prefix: ["[api] book:user"] });
@@ -50,6 +58,32 @@ function isAvailable(busyTimes, time, length) {
return t;
}
function isOutOfBounds(
time: dayjs.ConfigType,
{ periodType, periodDays, periodCountCalendarDays, periodStartDate, periodEndDate, timeZone }
): boolean {
const date = dayjs(time);
switch (periodType) {
case "rolling": {
const periodRollingEndDay = periodCountCalendarDays
? dayjs().tz(timeZone).add(periodDays, "days").endOf("day")
: dayjs().tz(timeZone).businessDaysAdd(periodDays, "days").endOf("day");
return date.endOf("day").isAfter(periodRollingEndDay);
}
case "range": {
const periodRangeStartDay = dayjs(periodStartDate).tz(timeZone).endOf("day");
const periodRangeEndDay = dayjs(periodEndDate).tz(timeZone).endOf("day");
return date.endOf("day").isBefore(periodRangeStartDay) || date.endOf("day").isAfter(periodRangeEndDay);
}
case "unlimited":
default:
return false;
}
}
interface GetLocationRequestFromIntegrationRequest {
location: string;
}
@@ -131,11 +165,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
dayjs(req.body.end).endOf("day").utc().format(),
selectedCalendars
);
const videoAvailability = await getBusyVideoTimes(
currentUser.credentials,
dayjs(req.body.start).startOf("day").utc().format(),
dayjs(req.body.end).endOf("day").utc().format()
);
const videoAvailability = await getBusyVideoTimes(currentUser.credentials);
let commonAvailability = [];
if (hasCalendarIntegrations && hasVideoIntegrations) {
@@ -175,6 +205,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
eventName: true,
title: true,
length: true,
periodType: true,
periodDays: true,
periodStartDate: true,
periodEndDate: true,
periodCountCalendarDays: true,
},
});
@@ -237,6 +272,33 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
return res.status(400).json(error);
}
let timeOutOfBounds = false;
try {
timeOutOfBounds = isOutOfBounds(req.body.start, {
periodType: selectedEventType.periodType,
periodDays: selectedEventType.periodDays,
periodEndDate: selectedEventType.periodEndDate,
periodStartDate: selectedEventType.periodStartDate,
periodCountCalendarDays: selectedEventType.periodCountCalendarDays,
timeZone: currentUser.timeZone,
});
} catch {
log.debug({
message: "Unable set timeOutOfBounds. Using false. ",
});
}
if (timeOutOfBounds) {
const error = {
errorCode: "BookingUserUnAvailable",
message: `${currentUser.name} is unavailable at this time.`,
};
log.debug(`Booking ${user} failed`, error);
return res.status(400).json(error);
}
let results: Array<EventResult> = [];
let referencesToCreate = [];
+26 -23
View File
@@ -1,27 +1,28 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { getSession } from 'next-auth/client';
import prisma from '../../../lib/prisma';
import type { NextApiRequest, NextApiResponse } from "next";
import { getSession } from "next-auth/client";
import prisma, { whereAndSelect } from "@lib/prisma";
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const session = await getSession({req: req});
const session = await getSession({ req: req });
if (!session) {
res.status(401).json({message: "Not authenticated"});
return;
res.status(401).json({ message: "Not authenticated" });
return;
}
// Get user
const user = await prisma.user.findUnique({
where: {
email: session.user.email,
const user = await whereAndSelect(
prisma.user.findUnique,
{
id: session.user.id,
},
select: {
id: true,
password: true
}
});
["id", "password"]
);
if (!user) { res.status(404).json({message: 'User not found'}); return; }
if (!user) {
res.status(404).json({ message: "User not found" });
return;
}
const username = req.body.username;
// username is changed: username is optional but it is necessary to be unique, enforce here
@@ -29,10 +30,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const userConflict = await prisma.user.findFirst({
where: {
username,
}
},
});
if (userConflict) {
return res.status(409).json({ message: 'Username already taken' });
return res.status(409).json({ message: "Username already taken" });
}
}
@@ -42,8 +43,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const timeZone = req.body.timeZone;
const weekStart = req.body.weekStart;
const hideBranding = req.body.hideBranding;
const theme = req.body.theme;
const updateUser = await prisma.user.update({
await prisma.user.update({
where: {
id: user.id,
},
@@ -52,11 +54,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
name,
avatar,
bio: description,
timeZone: timeZone,
weekStart: weekStart,
hideBranding: hideBranding,
timeZone,
weekStart,
hideBranding,
theme,
},
});
return res.status(200).json({message: 'Profile updated successfully'});
}
return res.status(200).json({ message: "Profile updated successfully" });
}