user: Add email and username, remove bufferTime,startTime,endTime (#202)

Co-authored-by: zomars <zomars@me.com>
pull/9078/head
Alex van Andel 2022-11-17 18:35:06 +00:00 committed by GitHub
parent 0f5017010f
commit 51bc3d93c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 11 deletions

View File

@ -1,6 +1,8 @@
import { z } from "zod"; import { z } from "zod";
import { checkUsername } from "@calcom/lib/server/checkUsername";
import { _UserModel as User } from "@calcom/prisma/zod"; import { _UserModel as User } from "@calcom/prisma/zod";
import { iso8601 } from "@calcom/prisma/zod-utils";
import { timeZone } from "@lib/validations/shared/timeZone"; import { timeZone } from "@lib/validations/shared/timeZone";
@ -50,14 +52,26 @@ enum timeFormat {
TWENTY_FOUR = 24, TWENTY_FOUR = 24,
} }
const usernameSchema = z
.string()
.transform((v) => v.toLowerCase())
// .refine(() => {})
.superRefine(async (val, ctx) => {
if (val) {
const result = await checkUsername(val);
if (!result.available) ctx.addIssue({ code: z.ZodIssueCode.custom, message: "already_in_use_error" });
if (result.premium) ctx.addIssue({ code: z.ZodIssueCode.custom, message: "premium_username" });
}
});
// @note: These are the values that are editable via PATCH method on the user Model // @note: These are the values that are editable via PATCH method on the user Model
export const schemaUserBaseBodyParams = User.pick({ export const schemaUserBaseBodyParams = User.pick({
name: true, name: true,
email: true,
username: true,
bio: true, bio: true,
timeZone: true, timeZone: true,
weekStart: true, weekStart: true,
endTime: true,
bufferTime: true,
theme: true, theme: true,
defaultScheduleId: true, defaultScheduleId: true,
locale: true, locale: true,
@ -75,13 +89,12 @@ export const schemaUserBaseBodyParams = User.pick({
// Here we can both require or not (adding optional or nullish) and also rewrite validations for any value // Here we can both require or not (adding optional or nullish) and also rewrite validations for any value
// for example making weekStart only accept weekdays as input // for example making weekStart only accept weekdays as input
const schemaUserEditParams = z.object({ const schemaUserEditParams = z.object({
email: z.string().email(),
username: usernameSchema,
weekStart: z.nativeEnum(weekdays).optional(), weekStart: z.nativeEnum(weekdays).optional(),
brandColor: z.string().min(4).max(9).regex(/^#/).optional(), brandColor: z.string().min(4).max(9).regex(/^#/).optional(),
darkBrandColor: z.string().min(4).max(9).regex(/^#/).optional(), darkBrandColor: z.string().min(4).max(9).regex(/^#/).optional(),
timeZone: timeZone.optional(), timeZone: timeZone.optional(),
bufferTime: z.number().min(0).max(86400).optional(),
startTime: z.number().min(0).max(86400).optional(),
endTime: z.number().min(0).max(86400).optional(),
theme: z.nativeEnum(theme).optional().nullable(), theme: z.nativeEnum(theme).optional().nullable(),
timeFormat: z.nativeEnum(timeFormat).optional(), timeFormat: z.nativeEnum(timeFormat).optional(),
defaultScheduleId: z defaultScheduleId: z
@ -97,13 +110,11 @@ const schemaUserEditParams = z.object({
const schemaUserCreateParams = z.object({ const schemaUserCreateParams = z.object({
email: z.string().email(), email: z.string().email(),
username: usernameSchema,
weekStart: z.nativeEnum(weekdays).optional(), weekStart: z.nativeEnum(weekdays).optional(),
brandColor: z.string().min(4).max(9).regex(/^#/).optional(), brandColor: z.string().min(4).max(9).regex(/^#/).optional(),
darkBrandColor: z.string().min(4).max(9).regex(/^#/).optional(), darkBrandColor: z.string().min(4).max(9).regex(/^#/).optional(),
timeZone: timeZone.optional(), timeZone: timeZone.optional(),
bufferTime: z.number().min(0).max(86400).optional(),
startTime: z.number().min(0).max(86400).optional(),
endTime: z.number().min(0).max(86400).optional(),
theme: z.nativeEnum(theme).optional().nullable(), theme: z.nativeEnum(theme).optional().nullable(),
timeFormat: z.nativeEnum(timeFormat).optional(), timeFormat: z.nativeEnum(timeFormat).optional(),
defaultScheduleId: z defaultScheduleId: z
@ -112,7 +123,7 @@ const schemaUserCreateParams = z.object({
.optional() .optional()
.nullable(), .nullable(),
locale: z.nativeEnum(locales).optional(), locale: z.nativeEnum(locales).optional(),
createdDate: z.string().or(z.date()).optional(), createdDate: iso8601.optional(),
}); });
// @note: These are the values that are editable via PATCH method on the user Model, // @note: These are the values that are editable via PATCH method on the user Model,
@ -120,6 +131,7 @@ const schemaUserCreateParams = z.object({
export const schemaUserEditBodyParams = schemaUserBaseBodyParams export const schemaUserEditBodyParams = schemaUserBaseBodyParams
.merge(schemaUserEditParams) .merge(schemaUserEditParams)
.omit({}) .omit({})
.partial()
.strict(); .strict();
export const schemaUserCreateBodyParams = schemaUserBaseBodyParams export const schemaUserCreateBodyParams = schemaUserBaseBodyParams

View File

@ -59,7 +59,7 @@ export async function patchHandler(req: NextApiRequest) {
// Here we only check for ownership of the user if the user is not admin, otherwise we let ADMIN's edit any user // Here we only check for ownership of the user if the user is not admin, otherwise we let ADMIN's edit any user
if (!isAdmin && query.userId !== req.userId) throw new HttpError({ statusCode: 403, message: "Forbidden" }); if (!isAdmin && query.userId !== req.userId) throw new HttpError({ statusCode: 403, message: "Forbidden" });
const body = schemaUserEditBodyParams.parse(req.body); const body = await schemaUserEditBodyParams.parseAsync(req.body);
const userSchedules = await prisma.schedule.findMany({ const userSchedules = await prisma.schedule.findMany({
where: { userId: query.userId }, where: { userId: query.userId },
}); });

View File

@ -25,7 +25,7 @@ async function postHandler(req: NextApiRequest) {
const { prisma, isAdmin } = req; const { prisma, isAdmin } = req;
// If user is not ADMIN, return unauthorized. // If user is not ADMIN, return unauthorized.
if (!isAdmin) throw new HttpError({ statusCode: 401, message: "You are not authorized" }); if (!isAdmin) throw new HttpError({ statusCode: 401, message: "You are not authorized" });
const data = schemaUserCreateBodyParams.parse(req.body); const data = await schemaUserCreateBodyParams.parseAsync(req.body);
const user = await prisma.user.create({ data }); const user = await prisma.user.create({ data });
req.statusCode = 201; req.statusCode = 201;
return { user }; return { user };