cal.pub0.org/packages/prisma/zod-utils.ts

284 lines
8.1 KiB
TypeScript
Raw Normal View History

import z, { ZodNullable, ZodObject, ZodOptional } from "zod";
/* eslint-disable no-underscore-dangle */
import type {
objectInputType,
objectOutputType,
ZodNullableDef,
ZodOptionalDef,
ZodRawShape,
ZodTypeAny,
} from "zod";
import { appDataSchemas } from "@calcom/app-store/apps.schemas.generated";
2022-06-28 20:40:58 +00:00
import dayjs from "@calcom/dayjs";
import { slugify } from "@calcom/lib/slugify";
// Let's not import 118kb just to get an enum
export enum Frequency {
YEARLY = 0,
MONTHLY = 1,
WEEKLY = 2,
DAILY = 3,
HOURLY = 4,
MINUTELY = 5,
SECONDLY = 6,
}
Introduce EventTypeAppCard in app-store and make it super easy to add it through CLI - Also adds Fathom app (#4727) * Add OmniInstall button * Make AppCards configurable by the app itself * Make OmniInstallAppButton not redirect * Fixes * Add extendsFeature support to CLI * Move to automatic file generation approach as dynamic import checking doesnt work correctly * Use zod everywhere consistenly for metadata and fix all TS issues * Fix viewer.eventTypes endpoint. Make prisma base select and _ prefixed models consistent in expecting scalars only * Remove unnecessary zod parsing of event-types as it is making the scope of the PR huge * Fix UI TS errors * wip * Add zod types support in EventTypeAppCard.tsx * Fixes during PR review and other failing tests * Remove unused app * Fix stripe installation flow * More fixes * Fix apps and active apps count * self review * Add loading attribute to OmniInsall button * Handle empty state * Improve types * Fix stripe app installation bug * added fathom app (#4804) * added fathom app wrapper, needs script injection to public booking page * new logo * Add Fathom script support on booking pages and add it as an eventTypeapp * Add automation and analytics apps * Add missing pieces for analytics category * Rename BookingPageScripts to BookingPageTagManager Co-authored-by: Hariom Balhara <hariombalhara@gmail.com> * Fix lint error * Fix runtime error with legayAppData being undefined * Remove duplicate automation key Co-authored-by: Peer Richelsen <peeroke@gmail.com>
2022-10-14 16:24:43 +00:00
export const EventTypeMetaDataSchema = z
.object({
smartContractAddress: z.string().optional(),
blockchainId: z.number().optional(),
giphyThankYouPage: z.string().optional(),
apps: z.object(appDataSchemas).partial().optional(),
additionalNotesRequired: z.boolean().optional(),
Introduce EventTypeAppCard in app-store and make it super easy to add it through CLI - Also adds Fathom app (#4727) * Add OmniInstall button * Make AppCards configurable by the app itself * Make OmniInstallAppButton not redirect * Fixes * Add extendsFeature support to CLI * Move to automatic file generation approach as dynamic import checking doesnt work correctly * Use zod everywhere consistenly for metadata and fix all TS issues * Fix viewer.eventTypes endpoint. Make prisma base select and _ prefixed models consistent in expecting scalars only * Remove unnecessary zod parsing of event-types as it is making the scope of the PR huge * Fix UI TS errors * wip * Add zod types support in EventTypeAppCard.tsx * Fixes during PR review and other failing tests * Remove unused app * Fix stripe installation flow * More fixes * Fix apps and active apps count * self review * Add loading attribute to OmniInsall button * Handle empty state * Improve types * Fix stripe app installation bug * added fathom app (#4804) * added fathom app wrapper, needs script injection to public booking page * new logo * Add Fathom script support on booking pages and add it as an eventTypeapp * Add automation and analytics apps * Add missing pieces for analytics category * Rename BookingPageScripts to BookingPageTagManager Co-authored-by: Hariom Balhara <hariombalhara@gmail.com> * Fix lint error * Fix runtime error with legayAppData being undefined * Remove duplicate automation key Co-authored-by: Peer Richelsen <peeroke@gmail.com>
2022-10-14 16:24:43 +00:00
})
.nullable();
export const eventTypeLocations = z.array(
2022-03-13 15:56:56 +00:00
z.object({
AppStore CLI: Making video app creation a breeze with major cleanup of locations code throughout (#3825) * Fix breadcrumb colors * HorizontalTabs * Team List Item WIP * Horizontal Tabs * Cards * Remove team list item WIP * Login Page * Add welcome back i118n * EventType page work * Update EventType Icons * WIP Availability * Horizontal Tab Work * Add build command for in root * Update build DIr/command * Add Edit Button + change buttons to v2 * Availablitiy page * Fix IPAD * Make mobile look a little nicer * WIP bookingshell * Remove list items from breaking build * Add Embed ModalBox for routing forms * Mian bulk of Booking Page. * Few updates to components * Fix chormatic feedback * Add duplicate form support * Fix duplication logic * Change to feathericons everywhere and other fixes * Dont allow routes for fallback route * Fix banner * Fix Empty Screen * Text area + embded window fixes * Semi fix avatar * Fix all TS issues * Fix tests * Troubleshoot container + Active on count * Support routing using query params * Improve mobile * NITS * Fix padding on input * Support multiselect in router endpoint * Fix the issue where app goes in embed mode after viewing embed once * Fix icons * Add router url tests * Add Responses download and form toggling tests * Add required validation test * Change Icons everywhere * App typeform app * Improvements in cli * Starting to move event types settings to tabs * Begin migration to single page form * Single page tabs * Limits Page * Advanced tab * Add RHF to dependancies * Add typeform how-to-use page * Add typeform how-to-use page and screenshots * Most of advanced tab * Solved RHF mismtach * Build fixes * RHF conditionals fixes * Improved legibility * Fix TS error * Add missing image * Update CliApp.tsx * Major refactor/organisation into optional V2 UI * Portal EditLocationModal * Fix dialoug form * Update imports * Auto Animate + custom inputs WIP * Custom Inputs * WIP Apps * Fixing stories imports * Stripe app * Remove duplicate dialog * Remove duplicate dialog * Major locations cleanup, 10s of bug fixes and app-store improvements * Fix missing pieces * More fixes * Fix embed URL * Fix app toggles + number of active apps * Fix container padding on disabledBorder prop * Removes strict * more fixes * EventType Team page WIP * Fix embed * NIT * Add Darkmode gray color * V2 Shell WIP * Fix headings on shell V2 * Fix mobile layout with V2 shell * V2 create event type button * Checked Team Select * Hidden to happen on save - not on toggle * Team Attendee Select animation * Fix scheduling type and remove multi select label * Fix overflow on teams url * Revert console * Revert api * Fix Embed TS errors * Fix TS errors * Fix Eslint errors * Fix TS errors for UI * Fix ESLINT error * Fix TS errors * Add missing import * Fix CLI * Add a default placeholder * Remove hardcoded daily:integrations * Fix message for payment page * Revert api and console to main * Update README * Fix TS errors * Fix Lint warnings * Fix Tests * Fix conflict issues * Fix conflict issues Co-authored-by: sean-brydon <55134778+sean-brydon@users.noreply.github.com> Co-authored-by: Peer Richelsen <peeroke@gmail.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> Co-authored-by: zomars <zomars@me.com>
2022-08-26 00:48:50 +00:00
// TODO: Couldn't find a way to make it a union of types from App Store locations
// Creating a dynamic union by iterating over the object doesn't seem to make TS happy
type: z.string(),
2022-03-13 15:56:56 +00:00
address: z.string().optional(),
link: z.string().url().optional(),
displayLocationPublicly: z.boolean().optional(),
hostPhoneNumber: z.string().optional(),
2022-03-13 15:56:56 +00:00
})
);
// Matching RRule.Options: rrule/dist/esm/src/types.d.ts
export const recurringEventType = z
.object({
dtstart: z.date().optional(),
interval: z.number(),
count: z.number(),
freq: z.nativeEnum(Frequency),
until: z.date().optional(),
tzid: z.string().optional(),
})
.nullable();
// dayjs iso parsing is very buggy - cant use :( - turns ISO string into Date object
export const iso8601 = z.string().transform((val, ctx) => {
const time = Date.parse(val);
if (!time) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Invalid ISO Date",
});
}
const d = new Date();
d.setTime(time);
return d;
});
Feat Booking Limits (#4759) * Add db relevant stuff * Basic UI there still buggy * This UI is hard - some progress * Fix awful state mangament * Fix re-ordering * Working UI logic! * Partical working minMax function * Fix min max * bookingLImits api + tests * Moved checkBookingLimits to backend only code * Fix httperror import * Return busy times * Remove avaliablity calc * Working for everything but year * Remove redundant + fix async forloop * Add compatible type * Future proof with evenTypeId filter * Fix commonjson * Sorting + validation + tests + passing * Add empty test * Move validation check to backend * Add bookinglimits in trpc * Add test for undefined * Apply suggestions from code review Co-authored-by: Jeroen Reumkens <hello@jeroenreumkens.nl> * Update apps/web/components/v2/eventtype/EventLimitsTab.tsx Co-authored-by: Jeroen Reumkens <hello@jeroenreumkens.nl> * Rename value for eligiability * Rename keyof type * status code * Fix toggle not toggling off * Update apps/web/pages/v2/event-types/[type]/index.tsx Co-authored-by: Omar López <zomars@me.com> * Update apps/web/pages/v2/event-types/[type]/index.tsx Co-authored-by: Omar López <zomars@me.com> * Change back to undefined as it is working for sean. See if it fails on testapp * Fixing test builder Co-authored-by: Peer Richelsen <peeroke@gmail.com> Co-authored-by: Alex van Andel <me@alexvanandel.com> Co-authored-by: Jeroen Reumkens <hello@jeroenreumkens.nl> Co-authored-by: Hariom Balhara <hariombalhara@gmail.com> Co-authored-by: Omar López <zomars@me.com> Co-authored-by: Leo Giovanetti <hello@leog.me>
2022-10-12 05:29:04 +00:00
export const bookingLimitsType = z
.object({
PER_DAY: z.number().optional(),
PER_WEEK: z.number().optional(),
PER_MONTH: z.number().optional(),
PER_YEAR: z.number().optional(),
})
.nullable();
export const eventTypeSlug = z.string().transform((val) => slugify(val.trim()));
export const stringToDate = z.string().transform((a) => new Date(a));
export const stringOrNumber = z.union([
z.string().transform((v, ctx) => {
const parsed = parseInt(v);
if (isNaN(parsed)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Not a number",
});
}
return parsed;
}),
z.number().int(),
]);
export const stringToDayjs = z.string().transform((val) => dayjs(val));
2022-06-10 18:38:46 +00:00
export const bookingCreateBodySchema = z.object({
email: z.string(),
end: z.string(),
eventTypeId: z.number(),
eventTypeSlug: z.string().optional(),
2022-06-10 18:38:46 +00:00
guests: z.array(z.string()).optional(),
location: z.string(),
name: z.string(),
notes: z.string().optional(),
rescheduleUid: z.string().optional(),
recurringEventId: z.string().optional(),
start: z.string(),
timeZone: z.string(),
user: z.union([z.string(), z.array(z.string())]).optional(),
language: z.string(),
bookingUid: z.string().optional(),
customInputs: z.array(z.object({ label: z.string(), value: z.union([z.string(), z.boolean()]) })),
metadata: z.record(z.string()),
hasHashedBookingLink: z.boolean().optional(),
2022-06-10 18:38:46 +00:00
hashedLink: z.string().nullish(),
ethSignature: z.string().optional(),
2022-06-10 18:38:46 +00:00
});
export const requiredCustomInputSchema = z.union([
// string must be given & nonempty
z.string().trim().min(1),
// boolean must be true if set.
z.boolean().refine((v) => v === true),
]);
export type BookingCreateBody = z.input<typeof bookingCreateBodySchema>;
export const bookingConfirmPatchBodySchema = z.object({
bookingId: z.number(),
confirmed: z.boolean(),
recurringEventId: z.string().optional(),
reason: z.string().optional(),
});
2022-06-10 18:38:46 +00:00
export const extendedBookingCreateBody = bookingCreateBodySchema.merge(
z.object({
noEmail: z.boolean().optional(),
recurringCount: z.number().optional(),
rescheduleReason: z.string().optional(),
Workflows (#3236) * build basic database structure and basic design * create simple workflow list * add editing dots to list * add mutation to create workflows * add createMutation on submit + redirect to editing page * redirect to edit page when clicking on row * add functionality to delete workflow * add timeUnit + input validation * add empty screen view * add time before it triggers to description * add multi select with checkboxes * remove getServerSideProps * set default time period to 24 * fetch eventypes and display in dropdown * add functionality to update workflows + many-to-many relationship * fix all checked event types * add SMS reminders * fix bug with trigger + relocate sms template * clean code * add model for unscheduled reminders * fix selected eventTypes * fixing value to show how many event types selected * fix plural of event types in select * add onDelete cascade for all relations * fix errors * add functionality to send SMS to specific number * fix type error for timeUnit * set default value for time unit + fix type issues * remove console.logs * fix error in checking if scheduled date is more than 1h in advance * fix build errors * add migration for workflows * add basic UI for editing workflow steps * add formSchema * improve functionality to update a step * remove console logs * fix issue with active event types * allow null value for time and timeUnit * sort steps asc step number * add action to workflow (frontend) * add phone number input for SMS to specific number * use PhoneInput for number input + input validation * improve invalid input for phone number * improve UI of phoneInput * Improve design and validation * fix undefined error * set default action when adding action * include all team event types * fix phone number input for editing steps * fix update muation to add steps * remove console logs * fix order of steps * functionality to delete steps * add trigger when event is cancelled * add custom email body * sms and email reminder updates * add custom emails * add custom email subject * send reminder email to all attendees * update migration * fix default value for time and timeUnit * save email reminders to database * clean code * add custom template to SMS actions * schedule emails with sendgrid * clean code * add workflow templates * keep custom template saved when changing templates * create reminder template for email * add dot at the end of sentace for email template * fix merge error * fix issue that template was not saved * include sending emails for when event is cancelled * fix bug that email was always sent * add templates to sms reminders * add info that sending sms to attendees won't trigger for already exisitng bookings * only schedule sms for attendees when smsReminderNumber exists * only schedule sms for attendees when smsReminderNumber exists * set scheduled of workflow reminder to false when longer than 72 hours * add cron for email scheduling + fixes for for sms an email scheduling * adjust step number when deleting a step * cast to boolean with !! * update cron job for email reminders * update sms template * send reminder email not to guests * remove sendTo from workflow reminder * fixes sending sms without name + removing sendTo everywhere * fix undefined name in sms template * set user name to undefined for sending sms to a specific number * fix singular and plural for time unit * set to edit mode when changing action and custom template is selected * delete reminders when booking cancelled or not active anymore * fix type errors * fix error that deleted reminders twice * create booking reminders for existing bookings when eventType is set active * improve email and sms templates * use BookingInfo type instead of calendarEvent for reminder emails * schedule emails for already existing bookings * add and remove reminders for new active event types and cancelled events * connect add action button with last step * fix step container width for mobile view * helper functions that return options for select * fix typo and remove comment * clean code * add/improve error messages for forms * fix typo * clean code * improve email template * clean code * fix missing prop * save reference id when scheduling reminder * fix step not added because of changed id for new steps * small fixes + code cleanup * code cleanup * show error message when number is invalid * fix typo * fix phone number input when location is already phone * set multi select checkbox to read only * change email scheduling in cron job from 7 days to 72 hours * show active event types in workflow list * fix trigger information for workflow list * improve layout for small screens in workflow list * remove optional from zod type for workflow name * order workflows by id * use link icon to show active event types * fix plural and add translation for showing nr of active eventtypes * fix text for sms reminder template * add reminders for added steps * remove optional for activeOn * improve reminder templates * improve design of custom input fields * set edit mode to false when phone number isn't needed anymore * set sendTo in workflow step only for SMS_NUMBER action * set email body and subject only when custom template * only delete reminders that belong to workflow steps * improve text for new event book trigger * move reminders folder to workflows * fix issue that save button was sometimes enabled in edit mode * fix form issues for send to * delete all scheduled reminders when workflow is deleted * use enum for method * fix imports for workflow methods * add missing import * fix edit mode * create reminders when event is confirmed * add reminderScheduler to reduce duplicate code * make workflow enterprise and pro only feature * move all files to /ee/ folder * move package.json change to /ee/ folder * add pro badge to shell * set to edit mode to true if email subject is missing when action changes * fix loading bug * add migration * fix old imports * don't schedule reminders for opt-ins * fix style of email body * code clean up * Update yarn.lock * fix isLoading for active on dropdown * update import for prisma Co-authored-by: Omar López <zomars@me.com> * update imports * remove console * use session to check if user has valid license * use defaultHandler * clean up code * Create db-staging-snapshot.yml * move LisenceRequired inside shell * update import for FormValues * fix phone input design * fix disabled save button for edit mode * squah all migration into a single one * use isAfter and isBefore instead of isBetween * import dayjs from @calcom * validate phone number for sms reminders when booking event * Allows auto approvals for crowdin Co-authored-by: CarinaWolli <wollencarina@gmail.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> Co-authored-by: zomars <zomars@me.com>
2022-07-14 00:10:45 +00:00
smsReminderNumber: z.string().optional(),
appsStatus: z
.array(
z.object({
appName: z.string(),
success: z.number(),
failures: z.number(),
type: z.string(),
})
)
.optional(),
2022-06-10 18:38:46 +00:00
})
);
2022-06-14 20:07:54 +00:00
export const vitalSettingsUpdateSchema = z.object({
connected: z.boolean().optional(),
selectedParam: z.string().optional(),
sleepValue: z.number().optional(),
});
export const createdEventSchema = z
.object({
id: z.string(),
password: z.union([z.string(), z.undefined()]),
onlineMeetingUrl: z.string().nullable(),
})
.passthrough();
2022-06-14 20:07:54 +00:00
export const userMetadata = z
.object({
2022-06-16 19:33:23 +00:00
proPaidForByTeamId: z.number().optional(),
2022-06-14 20:07:54 +00:00
stripeCustomerId: z.string().optional(),
vitalSettings: vitalSettingsUpdateSchema.optional(),
feature/settings-username-update (#2306) * WIP feature/settings-username-update * WIP username change * WIP downgrade stripe * stripe downgrade and prorate preview * new UI for username premium component * Fix server side props * Remove migration, changed field to metadata user * WIP for update subscriptions * WIP intent username table * WIP saving and updating username via hooks * WIP saving working username sub update * WIP, update html to work with tests * Added stripe test for username update go to stripe * WIP username change test * Working test for username change * Fix timeout for flaky test * Review changes, remove logs * Move input username as a self contained component * Self review changes * Removing unnecesary arrow function * Removed intentUsername table and now using user metadata * Update website * Update turbo.json * Update e2e.yml * Update yarn.lock * Fixes for self host username update * Revert yarn lock from main branch * E2E fixes * Centralizes username check * Improvements * WIP separate logic between premium and save username button * WIP refactor username premium update * Saving WIP * WIP redo of username check * WIP obtain action normal, update or downgrade * Update username change components * Fix test for change-username self host or cal server * Fix user type for premiumTextfield * Using now a global unique const to know if is selfhosted, css fixes * Remove unused import * Using dynamic import for username textfield, prevent submit on enter Co-authored-by: Peer Richelsen <peeroke@gmail.com> Co-authored-by: zomars <zomars@me.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2022-07-06 19:31:07 +00:00
isPremium: z.boolean().optional(),
intentUsername: z.string().optional(),
checkoutSessionId: z.string().nullable().optional(),
2022-06-14 20:07:54 +00:00
})
.nullable();
/**
* Ensures that it is a valid HTTP URL
* It automatically avoids
* - XSS attempts through javascript:alert('hi')
* - mailto: links
*/
export const successRedirectUrl = z
.union([
z.literal(""),
z
.string()
.url()
.regex(/^http(s)?:\/\/.*/),
])
.optional();
export type ZodDenullish<T extends ZodTypeAny> = T extends ZodNullable<infer U> | ZodOptional<infer U>
? ZodDenullish<U>
: T;
export type ZodDenullishShape<T extends ZodRawShape> = {
[k in keyof T]: ZodDenullish<T[k]>;
};
export const denullish = <T extends ZodTypeAny>(schema: T): ZodDenullish<T> =>
(schema instanceof ZodNullable || schema instanceof ZodOptional
? denullish((schema._def as ZodNullableDef | ZodOptionalDef).innerType)
: schema) as ZodDenullish<T>;
type UnknownKeysParam = "passthrough" | "strict" | "strip";
/**
* @see https://github.com/3x071c/lsg-remix/blob/e2a9592ba3ec5103556f2cf307c32f08aeaee32d/app/lib/util/zod.ts
*/
export function denullishShape<
T extends ZodRawShape,
UnknownKeys extends UnknownKeysParam = "strip",
Catchall extends ZodTypeAny = ZodTypeAny,
Output = objectOutputType<T, Catchall>,
Input = objectInputType<T, Catchall>
>(
obj: ZodObject<T, UnknownKeys, Catchall, Output, Input>
): ZodObject<ZodDenullishShape<T>, UnknownKeys, Catchall> {
const a = entries(obj.shape).map(([field, schema]) => [field, denullish(schema)] as const) as {
[K in keyof T]: [K, ZodDenullish<T[K]>];
}[keyof T][];
return new ZodObject({
...obj._def,
shape: () => fromEntries(a) as unknown as ZodDenullishShape<T>, // TODO: Safely assert type
});
}
/**
* Like Object.entries, but with actually useful typings
* @param obj The object to turn into a tuple array (`[key, value][]`)
* @returns The constructed tuple array from the given object
* @see https://github.com/3x071c/lsg-remix/blob/e2a9592ba3ec5103556f2cf307c32f08aeaee32d/app/lib/util/entries.ts
*/
export const entries = <O>(
obj: O
): {
readonly [K in keyof O]: [K, O[K]];
}[keyof O][] => {
return Object.entries(obj) as {
[K in keyof O]: [K, O[K]];
}[keyof O][];
};
/**
* Returns a type with all readonly notations removed (traverses recursively on an object)
*/
type DeepWriteable<T> = T extends Readonly<{
-readonly [K in keyof T]: T[K];
}>
? {
-readonly [K in keyof T]: DeepWriteable<T[K]>;
}
: T; /* Make it work with readonly types (this is not strictly necessary) */
type FromEntries<T> = T extends [infer Keys, unknown][]
? { [K in Keys & PropertyKey]: Extract<T[number], [K, unknown]>[1] }
: never;
/**
* Like Object.fromEntries, but with actually useful typings
* @param arr The tuple array (`[key, value][]`) to turn into an object
* @returns Object constructed from the given entries
* @see https://github.com/3x071c/lsg-remix/blob/e2a9592ba3ec5103556f2cf307c32f08aeaee32d/app/lib/util/fromEntries.ts
*/
export const fromEntries = <
E extends [PropertyKey, unknown][] | ReadonlyArray<readonly [PropertyKey, unknown]>
>(
entries: E
): FromEntries<DeepWriteable<E>> => {
return Object.fromEntries(entries) as FromEntries<DeepWriteable<E>>;
};