From 229207c8f823776c76b7c6debb03f655cc590b4e Mon Sep 17 00:00:00 2001 From: kaf-lamed-beyt Date: Fri, 14 Jul 2023 16:40:06 +0100 Subject: [PATCH 1/7] chore: create and consume JobPresenter class --- .../app/presenters/JobPresenter.server.ts | 22 ++++++++++ .../route.tsx | 40 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 apps/webapp/app/presenters/JobPresenter.server.ts diff --git a/apps/webapp/app/presenters/JobPresenter.server.ts b/apps/webapp/app/presenters/JobPresenter.server.ts new file mode 100644 index 000000000..1f40882f8 --- /dev/null +++ b/apps/webapp/app/presenters/JobPresenter.server.ts @@ -0,0 +1,22 @@ +import { PrismaClient, prisma } from "~/db.server"; +import { Job } from "~/models/job.server"; + +export class JobsListPresenter { + #prismaClient: PrismaClient; + + constructor(prismaClient: PrismaClient = prisma) { + this.#prismaClient = prismaClient; + } + + public async getProjectJob( + projectSlug: string, + jobId: string + ): Promise { + return this.#prismaClient.job.findFirst({ + where: { + projectId: projectSlug, + id: jobId, + }, + }); + } +} diff --git a/apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam._index/route.tsx b/apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam._index/route.tsx index 29b4f69df..c5e489a43 100644 --- a/apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam._index/route.tsx +++ b/apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam._index/route.tsx @@ -27,6 +27,46 @@ import { trimTrailingSlash, } from "~/utils/pathBuilder"; import { BreadcrumbLink } from "~/components/navigation/NavBar"; +import { requireUserId } from "~/services/session.server"; +import { LoaderArgs } from "@remix-run/server-runtime"; +import invariant from "tiny-invariant"; +import { typedjson } from "remix-typedjson"; +import { analytics } from "~/services/analytics.server"; +import { JobsListPresenter } from "~/presenters/JobPresenter.server"; + +export const loader = async ({ request, params }: LoaderArgs) => { + const { projectParam, jobId } = params; + const userId = await requireUserId(request); + invariant(projectParam, "projectParam not found"); + invariant(jobId, "jobId not found"); + + try { + const jobsListPresenter = new JobsListPresenter(); + const jobs = await jobsListPresenter.getProjectJob(projectParam, jobId); + + if (!jobs) { + throw new Response("Not found", { + status: 404, + statusText: `Project with this ${projectParam} was not found in your Organization`, + }); + } + + return typedjson({ + jobs, + }); + } catch (error) { + if (error instanceof Response) { + throw error; + } + + console.error(error); + throw new Response(undefined, { + status: 400, + statusText: + "Something went wrong. If the problem persists contact support", + }); + } +}; export const handle: Handle = { breadcrumb: (match) => ( From 2611b2c1714490cb7a9044c7876c04bb84ce3cde Mon Sep 17 00:00:00 2001 From: kaf-lamed-beyt Date: Fri, 14 Jul 2023 16:49:46 +0100 Subject: [PATCH 2/7] chore(docs): add docker setup guideline --- docker-setup.md | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 docker-setup.md diff --git a/docker-setup.md b/docker-setup.md new file mode 100644 index 000000000..8d09928ab --- /dev/null +++ b/docker-setup.md @@ -0,0 +1,63 @@ +## Setting up Docker for the first time. + +In thc contributing guide of Trigger.dev, there's a section that requires you to start Docker. + +If you don't have Docker installed on yiur machine, you'll run into some complications (errors). + +Below are the steps on how you can avoid that. + +First you need to setup docker-compose as it is an underlying tool that this command: `pnpm run docker` fires behind the scene. + +To install Docker Compose on Linux Ubuntu via the terminal, you can follow these steps: + +1. Update the package index on your system by running the following command: + + ```shell + sudo apt update + ``` + +2. Install the required dependencies by running the following command: + + ```shell + sudo apt install curl + ``` + +3. Download the Docker Compose binary into the `/usr/local/bin` directory using the `curl` command: + + ```shell + sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose + ``` + +4. Set the appropriate permissions to make the `docker-compose` binary executable: + + ```shell + sudo chmod +x /usr/local/bin/docker-compose + ``` + +5. Verify that Docker Compose has been successfully installed by running the following command: + + ```shell + docker-compose --version + ``` + + This command should display the version information of Docker Compose without any errors. + +After following these steps, you should have Docker Compose installed on your Ubuntu system, and you can use it by running `docker-compose` commands in the terminal. + +## Windows + +1. Download the Docker Desktop installer from the Docker website: [Docker Desktop for Windows](https://www.docker.com/products/docker-desktop) + +2. Run the installer and follow the instructions to install Docker Desktop. + +3. After installation, Docker Desktop should be running automatically. + +## macOS + +1. Download the Docker Desktop installer from the Docker website: [Docker Desktop for Mac](https://www.docker.com/products/docker-desktop) + +2. Run the installer and follow the instructions to install Docker Desktop. + +3. After installation, Docker Desktop should be running automatically. + +Please note that the instructions provided above are for the most common scenarios. For specific versions or different distributions, it's always a good idea to consult the official Docker documentation for the respective operating systems. From 46383e57d20f9b324e97dbd192e1c9508cf8633a Mon Sep 17 00:00:00 2001 From: kaf-lamed-beyt Date: Fri, 14 Jul 2023 16:55:00 +0100 Subject: [PATCH 3/7] chore: update guide --- docker-setup.md | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/docker-setup.md b/docker-setup.md index 8d09928ab..978ff633b 100644 --- a/docker-setup.md +++ b/docker-setup.md @@ -44,6 +44,65 @@ To install Docker Compose on Linux Ubuntu via the terminal, you can follow these After following these steps, you should have Docker Compose installed on your Ubuntu system, and you can use it by running `docker-compose` commands in the terminal. +When you've verified that the `docker-compose` package installed and you proceed to start Docker with `pnpm run docker`. + +You'll probably get an error similar to the one below: + +```shell +Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? + ELIFECYCLE  Command failed with exit code 1. +``` + +The error message suggests that the Docker daemon is not running on your system. The Docker daemon is responsible for managing and running Docker containers. + +To resolve this issue, you may need to install Docker properly on your Ubuntu system. Here are the steps to install Docker on Ubuntu: + +1. Update the package index on your system by running the following command: + + ```shell + sudo apt update + ``` + +2. Install the necessary packages to allow apt to use repositories over HTTPS: + + ```shell + sudo apt install apt-transport-https ca-certificates curl software-properties-common + ``` + +3. Add the official Docker GPG key to your system by running the following command: + + ```shell + curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg + ``` + +4. Add the Docker repository to the APT sources list: + + ```shell + echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null + ``` + +5. Update the package index again: + + ```shell + sudo apt update + ``` + +6. Install Docker by running the following command: + + ```shell + sudo apt install docker-ce docker-ce-cli containerd.io + ``` + +7. After the installation is complete, verify that Docker is installed correctly by running the following command: + + ```shell + docker --version + ``` + + This command should display the version information of Docker without any errors. + +Once Docker is installed and verified, you should be able to start the Docker daemon and run the `pnpm run docker` command without encountering any issues. + ## Windows 1. Download the Docker Desktop installer from the Docker website: [Docker Desktop for Windows](https://www.docker.com/products/docker-desktop) From 9861cb9aa727e91228a25708617fbd554af02326 Mon Sep 17 00:00:00 2001 From: kaf-lamed-beyt Date: Fri, 14 Jul 2023 19:23:51 +0100 Subject: [PATCH 4/7] refactor: rewrite JobsPresenter to return a list of Jobs for the project and direct new contributors to the docker setup guide --- CONTRIBUTING.md | 92 ++++++++++--------- .../app/presenters/JobPresenter.server.ts | 18 ++-- .../route.tsx | 7 +- docker-setup.md | 4 +- 4 files changed, 66 insertions(+), 55 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6c1d1275a..2eeadcb49 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -13,6 +13,7 @@ requests should be made against. The changes on the `main` branch are tagged into a release monthly. ### Prerequisites + - [Node.js](https://nodejs.org/en) version >=16.x - [pnpm package manager](https://pnpm.io/installation) version 7 - [Docker](https://www.docker.com/get-started/) @@ -21,60 +22,63 @@ branch are tagged into a release monthly. 1. Clone the repo into a public GitHub repository or [fork the repo](https://github.com/triggerdotdev/trigger.dev/fork). If you plan to distribute the code, keep the source code public to comply with the [Apache Licence 2.0](https://github.com/triggerdotdev/trigger.dev/blob/main/LICENSE). - ``` - git clone https://github.com/triggerdotdev/trigger.dev.git - ``` - > If you are on windows, run the following command on gitbash with admin privileges: - > `git clone -c core.symlinks=true https://triggerdotdev/trigger.dev.git` + ``` + git clone https://github.com/triggerdotdev/trigger.dev.git + ``` + + > If you are on windows, run the following command on gitbash with admin privileges: + > `git clone -c core.symlinks=true https://triggerdotdev/trigger.dev.git` + 2. Navigate to the project folder - ``` - cd trigger.dev - ``` + ``` + cd trigger.dev + ``` 3. Install the required packages using pnpm. - ``` - pnpm i - ``` + ``` + pnpm i + ``` 4. Create your `.env` files - ``` - cp .env.example .env && cp packages/database/.env.example packages/database/.env - ``` + ``` + cp .env.example .env && cp packages/database/.env.example packages/database/.env + ``` 5. Open the root `.env` file and fill in the required values Magic Link: - - Both of these secrets should be random strings, which you can easily generate (and copy into your pasteboard) with the following command: - - ```sh - openssl rand -hex 16 | pbcopy - ``` -

Then set them here:

+ Both of these secrets should be random strings, which you can easily generate (and copy into your pasteboard) with the following command: - ``` - SESSION_SECRET= - MAGIC_LINK_SECRET= - ``` - -7. Start Docker. This starts the required services like Postgres. - ``` - pnpm run docker - ``` -8. Migrate the database - ``` - pnpm run db:migrate - ``` -9. Run the seed script - ``` - pnpm run db:seed - ``` -5. Run the app. See the section below. + ```sh + openssl rand -hex 16 | pbcopy + ``` + +

Then set them here:

+ + ``` + SESSION_SECRET= + MAGIC_LINK_SECRET= + ``` + +6. Start Docker. This starts the required services like Postgres. If this is your first time using Docker, consider going through this [guide](docker-setup.md) + ``` + pnpm run docker + ``` +7. Migrate the database + ``` + pnpm run db:migrate + ``` +8. Run the seed script + ``` + pnpm run db:seed + ``` +9. Run the app. See the section below. ## Running 1. You can run the app with: - ``` - pnpm run dev --filter webapp - ``` - - It should run on port `3030`: [http://localhost:3030](http://localhost:3030/) + + ``` + pnpm run dev --filter webapp + ``` + + It should run on port `3030`: [http://localhost:3030](http://localhost:3030/) 2. Once the app is running click the magic link button and enter your email. 3. Check your terminal, the magic link email should have printed out. diff --git a/apps/webapp/app/presenters/JobPresenter.server.ts b/apps/webapp/app/presenters/JobPresenter.server.ts index 1f40882f8..16560045c 100644 --- a/apps/webapp/app/presenters/JobPresenter.server.ts +++ b/apps/webapp/app/presenters/JobPresenter.server.ts @@ -1,3 +1,4 @@ +import { User } from "~/models/user.server"; import { PrismaClient, prisma } from "~/db.server"; import { Job } from "~/models/job.server"; @@ -8,14 +9,17 @@ export class JobsListPresenter { this.#prismaClient = prismaClient; } - public async getProjectJob( - projectSlug: string, - jobId: string - ): Promise { - return this.#prismaClient.job.findFirst({ + public async call({ + slug, + userId, + }: { + slug: string; + userId: string; + }): Promise { + return this.#prismaClient.job.findMany({ where: { - projectId: projectSlug, - id: jobId, + projectId: slug, + id: userId, }, }); } diff --git a/apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam._index/route.tsx b/apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam._index/route.tsx index c5e489a43..dc3a41efe 100644 --- a/apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam._index/route.tsx +++ b/apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam._index/route.tsx @@ -41,8 +41,11 @@ export const loader = async ({ request, params }: LoaderArgs) => { invariant(jobId, "jobId not found"); try { - const jobsListPresenter = new JobsListPresenter(); - const jobs = await jobsListPresenter.getProjectJob(projectParam, jobId); + const jobsPresenter = new JobsListPresenter(); + const jobs = await jobsPresenter.call({ + userId, + slug: projectParam, + }); if (!jobs) { throw new Response("Not found", { diff --git a/docker-setup.md b/docker-setup.md index 978ff633b..375426946 100644 --- a/docker-setup.md +++ b/docker-setup.md @@ -2,7 +2,7 @@ In thc contributing guide of Trigger.dev, there's a section that requires you to start Docker. -If you don't have Docker installed on yiur machine, you'll run into some complications (errors). +If you don't have Docker installed on your machine, you'll run into some complications (errors). Below are the steps on how you can avoid that. @@ -44,7 +44,7 @@ To install Docker Compose on Linux Ubuntu via the terminal, you can follow these After following these steps, you should have Docker Compose installed on your Ubuntu system, and you can use it by running `docker-compose` commands in the terminal. -When you've verified that the `docker-compose` package installed and you proceed to start Docker with `pnpm run docker`. +When you've verified that the `docker-compose` package is installed and you proceed to start Docker with `pnpm run docker`. You'll probably get an error similar to the one below: From 184c24cace5ecba535583998c4756df7841864fa Mon Sep 17 00:00:00 2001 From: kaf-lamed-beyt Date: Sat, 15 Jul 2023 13:41:44 +0100 Subject: [PATCH 5/7] refactor: use userId instaed of jobId in JobsPresenter --- .../route.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam._index/route.tsx b/apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam._index/route.tsx index dc3a41efe..dde5fbd9d 100644 --- a/apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam._index/route.tsx +++ b/apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam._index/route.tsx @@ -35,10 +35,10 @@ import { analytics } from "~/services/analytics.server"; import { JobsListPresenter } from "~/presenters/JobPresenter.server"; export const loader = async ({ request, params }: LoaderArgs) => { - const { projectParam, jobId } = params; + const { projectParam } = params; const userId = await requireUserId(request); invariant(projectParam, "projectParam not found"); - invariant(jobId, "jobId not found"); + invariant(userId, "jobId not found"); try { const jobsPresenter = new JobsListPresenter(); From 6099e06a71aa9376195ea88fc00113b7a4407e57 Mon Sep 17 00:00:00 2001 From: kaf-lamed-beyt Date: Sun, 16 Jul 2023 15:21:19 +0100 Subject: [PATCH 6/7] update JobPresenter query to use findFirst. --- .../app/presenters/JobPresenter.server.ts | 30 ++++++++++++++----- .../route.tsx | 1 - 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/apps/webapp/app/presenters/JobPresenter.server.ts b/apps/webapp/app/presenters/JobPresenter.server.ts index 16560045c..ae1ee1b87 100644 --- a/apps/webapp/app/presenters/JobPresenter.server.ts +++ b/apps/webapp/app/presenters/JobPresenter.server.ts @@ -12,15 +12,31 @@ export class JobsListPresenter { public async call({ slug, userId, - }: { - slug: string; - userId: string; - }): Promise { - return this.#prismaClient.job.findMany({ + }: Pick & { userId: User["id"] }) { + const project = await this.#prismaClient.project.findFirst({ where: { - projectId: slug, - id: userId, + slug, + organization: { + members: { + some: { + userId, + }, + }, + }, + }, + select: { + jobs: { + where: { + internal: false, + }, + }, }, }); + + if (!project) { + return undefined; + } + + return project.jobs.filter((job) => !job.internal); } } diff --git a/apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam._index/route.tsx b/apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam._index/route.tsx index dde5fbd9d..ec914e993 100644 --- a/apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam._index/route.tsx +++ b/apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam._index/route.tsx @@ -38,7 +38,6 @@ export const loader = async ({ request, params }: LoaderArgs) => { const { projectParam } = params; const userId = await requireUserId(request); invariant(projectParam, "projectParam not found"); - invariant(userId, "jobId not found"); try { const jobsPresenter = new JobsListPresenter(); From eafe6a0d7bbd4c5e17f1bf86e911509cd2f1b4ad Mon Sep 17 00:00:00 2001 From: kaf-lamed-beyt Date: Mon, 17 Jul 2023 12:17:09 +0100 Subject: [PATCH 7/7] chore(docs): add docker setup guide. remove prev code implementation --- .../app/presenters/JobPresenter.server.ts | 42 ------------------- .../route.tsx | 42 ------------------- 2 files changed, 84 deletions(-) delete mode 100644 apps/webapp/app/presenters/JobPresenter.server.ts diff --git a/apps/webapp/app/presenters/JobPresenter.server.ts b/apps/webapp/app/presenters/JobPresenter.server.ts deleted file mode 100644 index ae1ee1b87..000000000 --- a/apps/webapp/app/presenters/JobPresenter.server.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { User } from "~/models/user.server"; -import { PrismaClient, prisma } from "~/db.server"; -import { Job } from "~/models/job.server"; - -export class JobsListPresenter { - #prismaClient: PrismaClient; - - constructor(prismaClient: PrismaClient = prisma) { - this.#prismaClient = prismaClient; - } - - public async call({ - slug, - userId, - }: Pick & { userId: User["id"] }) { - const project = await this.#prismaClient.project.findFirst({ - where: { - slug, - organization: { - members: { - some: { - userId, - }, - }, - }, - }, - select: { - jobs: { - where: { - internal: false, - }, - }, - }, - }); - - if (!project) { - return undefined; - } - - return project.jobs.filter((job) => !job.internal); - } -} diff --git a/apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam._index/route.tsx b/apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam._index/route.tsx index ec914e993..29b4f69df 100644 --- a/apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam._index/route.tsx +++ b/apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam._index/route.tsx @@ -27,48 +27,6 @@ import { trimTrailingSlash, } from "~/utils/pathBuilder"; import { BreadcrumbLink } from "~/components/navigation/NavBar"; -import { requireUserId } from "~/services/session.server"; -import { LoaderArgs } from "@remix-run/server-runtime"; -import invariant from "tiny-invariant"; -import { typedjson } from "remix-typedjson"; -import { analytics } from "~/services/analytics.server"; -import { JobsListPresenter } from "~/presenters/JobPresenter.server"; - -export const loader = async ({ request, params }: LoaderArgs) => { - const { projectParam } = params; - const userId = await requireUserId(request); - invariant(projectParam, "projectParam not found"); - - try { - const jobsPresenter = new JobsListPresenter(); - const jobs = await jobsPresenter.call({ - userId, - slug: projectParam, - }); - - if (!jobs) { - throw new Response("Not found", { - status: 404, - statusText: `Project with this ${projectParam} was not found in your Organization`, - }); - } - - return typedjson({ - jobs, - }); - } catch (error) { - if (error instanceof Response) { - throw error; - } - - console.error(error); - throw new Response(undefined, { - status: 400, - statusText: - "Something went wrong. If the problem persists contact support", - }); - } -}; export const handle: Handle = { breadcrumb: (match) => (