2022-04-01 20:04:42 +00:00
|
|
|
import { z } from "zod";
|
2022-03-27 00:19:49 +00:00
|
|
|
|
2022-03-30 12:17:55 +00:00
|
|
|
import { _UserModel as User } from "@calcom/prisma/zod";
|
2022-03-25 22:26:22 +00:00
|
|
|
|
2022-04-27 17:25:36 +00:00
|
|
|
import { timeZone } from "@lib/validations/shared/timeZone";
|
|
|
|
|
2022-05-17 17:33:18 +00:00
|
|
|
import { jsonSchema } from "./shared/jsonSchema";
|
|
|
|
|
2022-04-24 00:10:32 +00:00
|
|
|
// @note: These are the ONLY values allowed as weekStart. So user don't introduce bad data.
|
|
|
|
enum weekdays {
|
|
|
|
MONDAY = "Monday",
|
|
|
|
TUESDAY = "Tuesday",
|
|
|
|
WEDNESDAY = "Wednesday",
|
|
|
|
THURSDAY = "Thursday",
|
|
|
|
FRIDAY = "Friday",
|
|
|
|
SATURDAY = "Saturday",
|
|
|
|
SUNDAY = "Sunday",
|
|
|
|
}
|
|
|
|
|
2022-04-24 21:56:25 +00:00
|
|
|
// @note: extracted from apps/web/next-i18next.config.js, update if new locales.
|
|
|
|
enum locales {
|
|
|
|
EN = "en",
|
|
|
|
FR = "fr",
|
|
|
|
IT = "it",
|
|
|
|
RU = "ru",
|
|
|
|
ES = "es",
|
|
|
|
DE = "de",
|
|
|
|
PT = "pt",
|
|
|
|
RO = "ro",
|
|
|
|
NL = "nl",
|
|
|
|
PT_BR = "pt-BR",
|
|
|
|
ES_419 = "es-419",
|
|
|
|
KO = "ko",
|
|
|
|
JA = "ja",
|
|
|
|
PL = "pl",
|
|
|
|
AR = "ar",
|
|
|
|
IW = "iw",
|
|
|
|
ZH_CN = "zh-CN",
|
|
|
|
ZH_TW = "zh-TW",
|
|
|
|
CS = "cs",
|
|
|
|
SR = "sr",
|
|
|
|
SV = "sv",
|
|
|
|
VI = "vi",
|
|
|
|
}
|
|
|
|
enum theme {
|
|
|
|
DARK = "dark",
|
|
|
|
LIGHT = "light",
|
|
|
|
}
|
|
|
|
|
|
|
|
enum timeFormat {
|
|
|
|
TWELVE = 12,
|
|
|
|
TWENTY_FOUR = 24,
|
|
|
|
}
|
|
|
|
|
2022-04-24 00:10:32 +00:00
|
|
|
// @note: These are the values that are editable via PATCH method on the user Model
|
|
|
|
export const schemaUserBaseBodyParams = User.pick({
|
|
|
|
name: true,
|
|
|
|
bio: true,
|
|
|
|
timeZone: true,
|
|
|
|
weekStart: true,
|
|
|
|
endTime: true,
|
2022-05-17 17:33:18 +00:00
|
|
|
metadata: true,
|
2022-04-24 00:10:32 +00:00
|
|
|
bufferTime: true,
|
|
|
|
theme: true,
|
|
|
|
defaultScheduleId: true,
|
|
|
|
locale: true,
|
|
|
|
timeFormat: true,
|
|
|
|
brandColor: true,
|
|
|
|
darkBrandColor: true,
|
|
|
|
allowDynamicBooking: true,
|
|
|
|
away: true,
|
2022-04-24 21:56:25 +00:00
|
|
|
// @note: disallowing avatar changes via API for now. We can add it later if needed. User should upload image via UI.
|
|
|
|
// avatar: true,
|
2022-04-01 15:53:52 +00:00
|
|
|
}).partial();
|
2022-04-24 00:10:32 +00:00
|
|
|
// @note: partial() is used to allow for the user to edit only the fields they want to edit making all optional,
|
|
|
|
// if want to make any required do it in the schemaRequiredParams
|
2022-03-30 12:17:55 +00:00
|
|
|
|
2022-04-24 00:10:32 +00:00
|
|
|
// 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
|
2022-04-26 20:12:28 +00:00
|
|
|
const schemaUserEditParams = z.object({
|
2022-04-24 00:10:32 +00:00
|
|
|
weekStart: z.nativeEnum(weekdays).optional(),
|
2022-04-24 21:56:25 +00:00
|
|
|
brandColor: z.string().min(4).max(9).regex(/^#/).optional(),
|
2022-05-05 16:18:00 +00:00
|
|
|
darkBrandColor: z.string().min(4).max(9).regex(/^#/).optional(),
|
2022-04-27 17:25:36 +00:00
|
|
|
timeZone: timeZone.optional(),
|
2022-04-24 21:56:25 +00:00
|
|
|
bufferTime: z.number().min(0).max(86400).optional(),
|
|
|
|
startTime: z.number().min(0).max(86400).optional(),
|
|
|
|
endTime: z.number().min(0).max(86400).optional(),
|
2022-05-31 16:33:01 +00:00
|
|
|
theme: z.nativeEnum(theme).optional().nullable(),
|
2022-04-24 21:56:25 +00:00
|
|
|
timeFormat: z.nativeEnum(timeFormat).optional(),
|
|
|
|
defaultScheduleId: z
|
|
|
|
.number()
|
|
|
|
.refine((id: number) => id > 0)
|
2022-05-31 16:33:01 +00:00
|
|
|
.optional()
|
|
|
|
.nullable(),
|
|
|
|
locale: z.nativeEnum(locales).optional().nullable(),
|
2022-06-13 21:58:15 +00:00
|
|
|
metadata: jsonSchema.optional(),
|
2022-05-31 16:33:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// @note: These are the values that are editable via PATCH method on the user Model,
|
|
|
|
// merging both BaseBodyParams with RequiredParams, and omiting whatever we want at the end.
|
|
|
|
|
|
|
|
const schemaUserCreateParams = z.object({
|
|
|
|
email: z.string().email(),
|
|
|
|
weekStart: z.nativeEnum(weekdays).optional(),
|
|
|
|
brandColor: z.string().min(4).max(9).regex(/^#/).optional(),
|
|
|
|
darkBrandColor: z.string().min(4).max(9).regex(/^#/).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(),
|
|
|
|
timeFormat: z.nativeEnum(timeFormat).optional(),
|
|
|
|
defaultScheduleId: z
|
|
|
|
.number()
|
|
|
|
.refine((id: number) => id > 0)
|
|
|
|
.optional()
|
|
|
|
.nullable(),
|
2022-05-05 16:18:00 +00:00
|
|
|
locale: z.nativeEnum(locales).optional(),
|
2022-05-17 17:33:18 +00:00
|
|
|
metadata: jsonSchema,
|
2022-05-31 16:33:01 +00:00
|
|
|
createdDate: z.string().or(z.date()).optional(),
|
2022-04-01 20:04:42 +00:00
|
|
|
});
|
|
|
|
|
2022-04-24 21:56:25 +00:00
|
|
|
// @note: These are the values that are editable via PATCH method on the user Model,
|
|
|
|
// merging both BaseBodyParams with RequiredParams, and omiting whatever we want at the end.
|
2022-04-26 20:12:28 +00:00
|
|
|
export const schemaUserEditBodyParams = schemaUserBaseBodyParams.merge(schemaUserEditParams).omit({});
|
2022-05-31 16:33:01 +00:00
|
|
|
export const schemaUserCreateBodyParams = schemaUserBaseBodyParams.merge(schemaUserCreateParams).omit({});
|
2022-04-01 20:04:42 +00:00
|
|
|
|
2022-04-24 00:10:32 +00:00
|
|
|
// @note: These are the values that are always returned when reading a user
|
|
|
|
export const schemaUserReadPublic = User.pick({
|
|
|
|
id: true,
|
|
|
|
username: true,
|
|
|
|
name: true,
|
|
|
|
email: true,
|
|
|
|
emailVerified: true,
|
|
|
|
bio: true,
|
|
|
|
avatar: true,
|
2022-05-17 17:33:18 +00:00
|
|
|
metadata: true,
|
2022-04-24 00:10:32 +00:00
|
|
|
timeZone: true,
|
|
|
|
weekStart: true,
|
|
|
|
endTime: true,
|
|
|
|
bufferTime: true,
|
|
|
|
theme: true,
|
|
|
|
defaultScheduleId: true,
|
|
|
|
locale: true,
|
|
|
|
timeFormat: true,
|
|
|
|
brandColor: true,
|
|
|
|
darkBrandColor: true,
|
|
|
|
allowDynamicBooking: true,
|
|
|
|
away: true,
|
|
|
|
createdDate: true,
|
|
|
|
verified: true,
|
|
|
|
invitedTo: true,
|
2022-05-31 16:33:01 +00:00
|
|
|
}).merge(schemaUserEditBodyParams);
|