Skip to content

Commit

Permalink
Extract prisma to it's own package (#1823)
Browse files Browse the repository at this point in the history
* Moved prisma to packages

* Add missing prisma configs

* Extracts common libs and types

* Build and pipeline fixes

* Adds missing package

* Prisma scripts cleanup

* Updates lint staged

* Type fixes

* Sort imports

* Updates yarn lock file

* Fixes for yarn dx

* Revert "Sort imports"

This reverts commit 076109decab9b9ba307fc03696c3b0da5c4896f3.

* Formatting

* Prevent double TS version

* Fix conflict

* Extracted e2e configs
  • Loading branch information
zomars authored Feb 15, 2022
1 parent fdf7516 commit fe35cf6
Show file tree
Hide file tree
Showing 128 changed files with 349 additions and 223 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# It now lives at `apps/web/.env.example`
# DATABASE_URL got moved to `packages/prisma/.env.example`
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
node_modules
apps/web/prisma/zod
packages/prisma/zod
1 change: 1 addition & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ jobs:
with:
name: videos
path: |
test-results
playwright/screenshots
playwright/videos
playwright/results
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ node_modules

# testing
coverage
/test-results/
playwright/videos
playwright/screenshots
playwright/artifacts
Expand Down
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ public
.prettierignore
.DS_Store
.eslintignore
apps/web/prisma/zod
packages/prisma/zod
2 changes: 1 addition & 1 deletion .prettierrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = {
semi: true,
printWidth: 110,
arrowParens: "always",
importOrder: ["^@ee/(.*)$", "^@lib/(.*)$", "^@components/(.*)$", "^@(server|trpc)/(.*)$", "^[./]"],
importOrder: ["^@(calcom|ee)/(.*)$", "^@lib/(.*)$", "^@components/(.*)$", "^@(server|trpc)/(.*)$", "^[./]"],
importOrderSeparation: true,
plugins: [require("./merged-prettier-plugin")],
};
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ Here is what you need to be able to run Cal.

```sh
cp apps/web/.env.example apps/web/.env
cp packages/prisma/.env.example packages/prisma/.env
```

1. Install packages with yarn
Expand Down
6 changes: 6 additions & 0 deletions apps/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,27 @@
The official product, support and developer documentation, containing information and guides about using the product as well as support for self-hosted installations. This documentation site runs on [Nextra](https://nextra.vercel.app), so you may refer to their documentation should you need information on anything that isn't covered here.

## Prerequisites

- Git
- Node.js & npm
- Yarn

## Installation

Firstly, clone the repository using Git:

```console
git clone https://github.com/calcom/docs.git
```

Now, you can install the dependencies with yarn:

```console
yarn install
```

## Editing

To create, edit and delete documentation pages, you can simply create markdown (.mdx) files in the `pages/` folder. You can edit Markdown with any text editor, but VS Code and WebStorm have side-by-side previews so you can see your formatted content whilst writing markdown.

You will also need to add it as an entry to the `meta.json` file found in whichever directory that the .mdx file is in.
Expand All @@ -52,6 +57,7 @@ This command generates static content into the `build` directory and can be serv
## How to easily contribute

## Existing Page

1. From the documentation's GitHub repository, head to the folder called 'pages' and open it.
2. From here you can view all current pages on the documentation site. Select the page you would like to contribute to.
3. You should now be able to view the page you have selected. Located at the top right of the page will be a pencil icon. Pressing this will bring you up an editor to edit and make changes. You can add formatting using the buttons at the top, which will automatically insert the relevant markdown content needed to style the text.
Expand Down
82 changes: 49 additions & 33 deletions apps/docs/pages/self-hosting/install.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ Here is what you need to be able to run Cal.
1. Go to the project folder

```sh
cd calendso
cd cal.com
```

1. Copy `.env.example` to `.env`

```sh
cp .env.example .env
cp apps/web/.env.example apps/web/.env
cp packages/prisma/.env.example packages/prisma/.env
```

1. Install packages with yarn
Expand All @@ -42,57 +43,72 @@ Here is what you need to be able to run Cal.

> - **Requires Docker and Docker Compose to be installed**
> - Will start a local Postgres instance with a few test users - the credentials will be logged in the console
```sh
yarn dx
```

### Manual setup

1. Configure environment variables in the .env file. Replace `<user>`, `<pass>`, `<db-host>`, `<db-port>` with their applicable values
1. Configure database in the `packages/prisma/.env` file. Replace `<user>`, `<pass>`, `<db-host>`, `<db-port>` with their applicable values

```text
DATABASE_URL='postgresql://<user>:<pass>@<db-host>:<db-port>'
```
```text
DATABASE_URL='postgresql://<user>:<pass>@<db-host>:<db-port>'
```

<details>
<summary>If you don't know how to configure the DATABASE_URL, then follow the steps here to create a quick DB using Heroku</summary>
<details>

1. Create a free account with [Heroku](https://www.heroku.com/).
<summary>
If you don't know how to configure the DATABASE_URL, then follow the steps here to create a quick DB
using Heroku
</summary>

2. Create a new app.
<img width="306" alt="Create an App" src="https://user-images.githubusercontent.com/16905768/115322780-b3d58c00-a17e-11eb-8a52-b758fb0ea942.png" />
1. Create a free account with [Heroku](https://www.heroku.com/).

3. In your new app, go to `Overview` and next to `Installed add-ons`, click `Configure Add-ons`. We need this to set up our database.
![image](https://user-images.githubusercontent.com/16905768/115323232-a53ba480-a17f-11eb-98db-58e2f8c52426.png)
2. Create a new app.

4. Once you clicked on `Configure Add-ons`, click on `Find more add-ons` and search for `postgres`. One of the options will be `Heroku Postgres` - click on that option.
![image](https://user-images.githubusercontent.com/16905768/115323126-5beb5500-a17f-11eb-8030-7380310807a9.png)
<img
width="306"
alt="Create an App"
src="https://user-images.githubusercontent.com/16905768/115322780-b3d58c00-a17e-11eb-8a52-b758fb0ea942.png"
/>

5. Once the pop-up appears, click `Submit Order Form` - plan name should be `Hobby Dev - Free`.
<img width="512" alt="Submit Order Form" src="https://user-images.githubusercontent.com/16905768/115323265-b4baed80-a17f-11eb-99f0-d67f019aa6df.png" />
3. In your new app, go to `Overview` and next to `Installed add-ons`, click `Configure Add-ons`. We need this to set up our database.
![image](https://user-images.githubusercontent.com/16905768/115323232-a53ba480-a17f-11eb-98db-58e2f8c52426.png)

6. Once you completed the above steps, click on your newly created `Heroku Postgres` and go to its `Settings`.
![image](https://user-images.githubusercontent.com/16905768/115323367-e92ea980-a17f-11eb-9ff4-dec95f2ec349.png)
4. Once you clicked on `Configure Add-ons`, click on `Find more add-ons` and search for `postgres`. One of the options will be `Heroku Postgres` - click on that option.
![image](https://user-images.githubusercontent.com/16905768/115323126-5beb5500-a17f-11eb-8030-7380310807a9.png)

7. In `Settings`, copy your URI to your Cal.com .env file and replace the `postgresql://<user>:<pass>@<db-host>:<db-port>` with it.
![image](https://user-images.githubusercontent.com/16905768/115323556-4591c900-a180-11eb-9808-2f55d2aa3995.png)
![image](https://user-images.githubusercontent.com/16905768/115323697-7a9e1b80-a180-11eb-9f08-a742b1037f90.png)
5. Once the pop-up appears, click `Submit Order Form` - plan name should be `Hobby Dev - Free`.

8. To view your DB, once you add new data in Prisma, you can use [Heroku Data Explorer](https://heroku-data-explorer.herokuapp.com/).
</details>
<img
width="512"
alt="Submit Order Form"
src="https://user-images.githubusercontent.com/16905768/115323265-b4baed80-a17f-11eb-99f0-d67f019aa6df.png"
/>

1. Set a 32 character random string in your .env file for the `CALENDSO_ENCRYPTION_KEY` (You can use a command like `openssl rand -base64 24` to generate one).
1. Set up the database using the Prisma schema (found in `prisma/schema.prisma`)
6. Once you completed the above steps, click on your newly created `Heroku Postgres` and go to its `Settings`.
![image](https://user-images.githubusercontent.com/16905768/115323367-e92ea980-a17f-11eb-9ff4-dec95f2ec349.png)

```sh
npx prisma migrate deploy
```
7. In `Settings`, copy your URI to your Cal.com .env file and replace the `postgresql://<user>:<pass>@<db-host>:<db-port>` with it.
![image](https://user-images.githubusercontent.com/16905768/115323556-4591c900-a180-11eb-9808-2f55d2aa3995.png)
![image](https://user-images.githubusercontent.com/16905768/115323697-7a9e1b80-a180-11eb-9f08-a742b1037f90.png)

1. Run (in development mode)
8. To view your DB, once you add new data in Prisma, you can use [Heroku Data Explorer](https://heroku-data-explorer.herokuapp.com/).
</details>

```sh
yarn dev --scope=@calcom/web
```
1. Set a 32 character random string in your `apps/web/.env` file for the `CALENDSO_ENCRYPTION_KEY` (You can use a command like `openssl rand -base64 24` to generate one).
1. Set up the database using the Prisma schema (found in `packages/prisma/schema.prisma`)

```sh
npx prisma migrate deploy
```

1. Run (in development mode)

```sh
yarn dev --scope=@calcom/web
```

### Setting up your first user

Expand Down
3 changes: 1 addition & 2 deletions apps/web/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
# - Acquire a commercial license to remove these terms by visiting: cal.com/sales
NEXT_PUBLIC_LICENSE_CONSENT=''

# DATABASE_URL='postgresql://<user>:<pass>@<db-host>:<db-port>/<db-name>'
DATABASE_URL="postgresql://postgres:@localhost:5450/calendso"
# ⚠️ ⚠️ ⚠️ DATABASE_URL got moved to `packages/prisma/.env.example` ⚠️ ⚠️ ⚠️

# Needed to enable Google Calendar integration and Login with Google
# @see https://github.com/calendso/calendso#obtaining-the-google-api-credentials
Expand Down
1 change: 1 addition & 0 deletions apps/web/@prisma/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "@calcom/prisma/client";
3 changes: 2 additions & 1 deletion apps/web/components/eventtype/CreateEventType.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { ChevronDownIcon, PlusIcon } from "@heroicons/react/solid";
import { zodResolver } from "@hookform/resolvers/zod/dist/zod";
import { SchedulingType } from "@prisma/client";
import { useRouter } from "next/router";
import { createEventTypeInput } from "prisma/zod/eventtypeCustom";
import React, { useEffect } from "react";
import { useForm } from "react-hook-form";
import type { z } from "zod";

import { createEventTypeInput } from "@calcom/prisma/zod/eventtypeCustom";

import { HttpError } from "@lib/core/http/error";
import { useLocale } from "@lib/hooks/useLocale";
import { useToggleQuery } from "@lib/hooks/useToggleQuery";
Expand Down
127 changes: 1 addition & 126 deletions apps/web/lib/availability.ts
Original file line number Diff line number Diff line change
@@ -1,126 +1 @@
import { Availability } from "@prisma/client";
import dayjs, { ConfigType } from "dayjs";
import customParseFormat from "dayjs/plugin/customParseFormat";
import timezone from "dayjs/plugin/timezone";
import utc from "dayjs/plugin/utc";

import { Schedule, TimeRange, WorkingHours } from "./types/schedule";

dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.extend(customParseFormat);
// sets the desired time in current date, needs to be current date for proper DST translation
export const defaultDayRange: TimeRange = {
start: new Date(new Date().setUTCHours(9, 0, 0, 0)),
end: new Date(new Date().setUTCHours(17, 0, 0, 0)),
};

export const DEFAULT_SCHEDULE: Schedule = [
[],
[defaultDayRange],
[defaultDayRange],
[defaultDayRange],
[defaultDayRange],
[defaultDayRange],
[],
];

export function getAvailabilityFromSchedule(schedule: Schedule): Availability[] {
return schedule.reduce((availability: Availability[], times: TimeRange[], day: number) => {
const addNewTime = (time: TimeRange) =>
({
days: [day],
startTime: time.start,
endTime: time.end,
} as Availability);

const filteredTimes = times.filter((time) => {
let idx;
if (
(idx = availability.findIndex(
(schedule) => schedule.startTime === time.start && schedule.endTime === time.end
)) !== -1
) {
availability[idx].days.push(day);
return false;
}
return true;
});
filteredTimes.forEach((time) => {
availability.push(addNewTime(time));
});
return availability;
}, [] as Availability[]);
}

export const MINUTES_IN_DAY = 60 * 24;
export const MINUTES_DAY_END = MINUTES_IN_DAY - 1;
export const MINUTES_DAY_START = 0;

/**
* Allows "casting" availability (days, startTime, endTime) given in UTC to a timeZone or utcOffset
*/
export function getWorkingHours(
relativeTimeUnit: {
timeZone?: string;
utcOffset?: number;
},
availability: { days: number[]; startTime: ConfigType; endTime: ConfigType }[]
) {
// clearly bail when availability is not set, set everything available.
if (!availability.length) {
return [
{
days: [0, 1, 2, 3, 4, 5, 6],
// shorthand for: dayjs().startOf("day").tz(timeZone).diff(dayjs.utc().startOf("day"), "minutes")
startTime: MINUTES_DAY_START,
endTime: MINUTES_DAY_END,
},
];
}

const utcOffset = relativeTimeUnit.utcOffset ?? dayjs().tz(relativeTimeUnit.timeZone).utcOffset();

const workingHours = availability.reduce((workingHours: WorkingHours[], schedule) => {
// Get times localised to the given utcOffset/timeZone
const startTime =
dayjs.utc(schedule.startTime).get("hour") * 60 +
dayjs.utc(schedule.startTime).get("minute") -
utcOffset;
const endTime =
dayjs.utc(schedule.endTime).get("hour") * 60 + dayjs.utc(schedule.endTime).get("minute") - utcOffset;

// add to working hours, keeping startTime and endTimes between bounds (0-1439)
const sameDayStartTime = Math.max(MINUTES_DAY_START, Math.min(MINUTES_DAY_END, startTime));
const sameDayEndTime = Math.max(MINUTES_DAY_START, Math.min(MINUTES_DAY_END, endTime));
if (sameDayStartTime !== sameDayEndTime) {
workingHours.push({
days: schedule.days,
startTime: sameDayStartTime,
endTime: sameDayEndTime,
});
}
// check for overflow to the previous day
if (startTime < MINUTES_DAY_START || endTime < MINUTES_DAY_START) {
workingHours.push({
days: schedule.days.map((day) => day - 1),
startTime: startTime + MINUTES_IN_DAY,
endTime: Math.min(endTime + MINUTES_IN_DAY, MINUTES_DAY_END),
});
}
// else, check for overflow in the next day
else if (startTime > MINUTES_DAY_END || endTime > MINUTES_DAY_END) {
workingHours.push({
days: schedule.days.map((day) => day + 1),
startTime: Math.max(startTime - MINUTES_IN_DAY, MINUTES_DAY_START),
endTime: endTime - MINUTES_IN_DAY,
});
}

return workingHours;
}, []);

workingHours.sort((a, b) => a.startTime - b.startTime);

return workingHours;
}
export * from "@calcom/lib/availability";
11 changes: 1 addition & 10 deletions apps/web/lib/location.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1 @@
export enum LocationType {
InPerson = "inPerson",
Phone = "phone",
GoogleMeet = "integrations:google:meet",
Zoom = "integrations:zoom",
Daily = "integrations:daily",
Jitsi = "integrations:jitsi",
Huddle01 = "integrations:huddle01",
Tandem = "integrations:tandem",
}
export * from "@calcom/lib/location";
2 changes: 1 addition & 1 deletion apps/web/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const withTM = require("@vercel/edge-functions-ui/transpile")([]);
const withTM = require("@vercel/edge-functions-ui/transpile")(["@calcom/lib", "@calcom/prisma"]);
const { i18n } = require("./next-i18next.config");

// So we can test deploy previews preview
Expand Down
Loading

0 comments on commit fe35cf6

Please sign in to comment.