From b85a43a6c5fe8e0eb5bd99c8c64962c736f83ccc Mon Sep 17 00:00:00 2001 From: Mariano Fuentes Date: Wed, 12 Feb 2025 10:45:54 -0800 Subject: [PATCH 1/4] allow publish --- .../policies/[id]/Actions/publish-policy.ts | 59 +++++++++++++++++++ .../[id]/Components/PolicyOverview.tsx | 29 +++++++++ .../(dashboard)/policies/[id]/layout.tsx | 2 +- .../(app)/(dashboard)/policies/[id]/page.tsx | 2 - .../components/policies/policy-overview.tsx | 25 +++++++- 5 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 apps/app/src/app/[locale]/(app)/(dashboard)/policies/[id]/Actions/publish-policy.ts create mode 100644 apps/app/src/app/[locale]/(app)/(dashboard)/policies/[id]/Components/PolicyOverview.tsx diff --git a/apps/app/src/app/[locale]/(app)/(dashboard)/policies/[id]/Actions/publish-policy.ts b/apps/app/src/app/[locale]/(app)/(dashboard)/policies/[id]/Actions/publish-policy.ts new file mode 100644 index 0000000000..246cd11317 --- /dev/null +++ b/apps/app/src/app/[locale]/(app)/(dashboard)/policies/[id]/Actions/publish-policy.ts @@ -0,0 +1,59 @@ +"use server"; + +import { authActionClient } from "@/actions/safe-action"; +import { db, type OrganizationPolicy } from "@bubba/db"; +import { z } from "zod"; + +const schema = z.object({ + id: z.string(), +}); + +export type PublishPolicyResponse = { + success: boolean; + data?: OrganizationPolicy; + error?: string; +}; + +export const publishPolicy = authActionClient + .schema(schema) + .metadata({ + name: "publish-policy", + track: { + event: "publish-policy", + channel: "server", + }, + }) + .action(async ({ ctx, parsedInput }) => { + const { user } = ctx; + const { id } = parsedInput; + + if (!user.organizationId) { + return { + success: false, + error: "Not authorized - no organization found", + }; + } + + try { + const policy = await db.organizationPolicy.update({ + where: { + id, + organizationId: user.organizationId!, + }, + data: { + status: "published", + updatedAt: new Date(), + }, + }); + + return { + success: true, + data: policy, + }; + } catch (error) { + return { + success: false, + error: "Failed to publish policy", + }; + } + }); diff --git a/apps/app/src/app/[locale]/(app)/(dashboard)/policies/[id]/Components/PolicyOverview.tsx b/apps/app/src/app/[locale]/(app)/(dashboard)/policies/[id]/Components/PolicyOverview.tsx new file mode 100644 index 0000000000..2d15cd0097 --- /dev/null +++ b/apps/app/src/app/[locale]/(app)/(dashboard)/policies/[id]/Components/PolicyOverview.tsx @@ -0,0 +1,29 @@ +"use client"; + +import type { JSONContent } from "@tiptap/react"; +import PolicyEditor from "@/components/editor/advanced-editor"; +import { usePolicy } from "@/app/[locale]/(app)/(dashboard)/policies/hooks/usePolicy"; +import { Button } from "@bubba/ui/button"; +import { Separator } from "@bubba/ui/separator"; + +export function PolicyOverview({ policyId }: { policyId: string }) { + const { data: policy } = usePolicy({ policyId }); + + if (!policy) return null; + + const content = policy.content as JSONContent; + + if (!content) return null; + + return ( +
+
+ +
+ + +
+ ); +} diff --git a/apps/app/src/app/[locale]/(app)/(dashboard)/policies/[id]/layout.tsx b/apps/app/src/app/[locale]/(app)/(dashboard)/policies/[id]/layout.tsx index 13e511a661..a64030c109 100644 --- a/apps/app/src/app/[locale]/(app)/(dashboard)/policies/[id]/layout.tsx +++ b/apps/app/src/app/[locale]/(app)/(dashboard)/policies/[id]/layout.tsx @@ -20,7 +20,7 @@ export default async function Layout({ } return ( -
+
{children}
); diff --git a/apps/app/src/app/[locale]/(app)/(dashboard)/policies/[id]/page.tsx b/apps/app/src/app/[locale]/(app)/(dashboard)/policies/[id]/page.tsx index da98f65d53..b257dd6e28 100644 --- a/apps/app/src/app/[locale]/(app)/(dashboard)/policies/[id]/page.tsx +++ b/apps/app/src/app/[locale]/(app)/(dashboard)/policies/[id]/page.tsx @@ -1,6 +1,4 @@ -import { auth } from "@/auth"; import { PolicyOverview } from "@/components/policies/policy-overview"; -import { redirect } from "next/navigation"; interface PageProps { params: Promise<{ id: string }>; diff --git a/apps/app/src/components/policies/policy-overview.tsx b/apps/app/src/components/policies/policy-overview.tsx index 286b9b5293..7d98cdcd80 100644 --- a/apps/app/src/components/policies/policy-overview.tsx +++ b/apps/app/src/components/policies/policy-overview.tsx @@ -3,9 +3,22 @@ import type { JSONContent } from "@tiptap/react"; import PolicyEditor from "../editor/advanced-editor"; import { usePolicy } from "@/app/[locale]/(app)/(dashboard)/policies/hooks/usePolicy"; +import { Button } from "@bubba/ui/button"; +import { Separator } from "@bubba/ui/separator"; +import { useAction } from "next-safe-action/hooks"; +import { publishPolicy } from "@/app/[locale]/(app)/(dashboard)/policies/[id]/Actions/publish-policy"; +import { toast } from "sonner"; export function PolicyOverview({ policyId }: { policyId: string }) { const { data: policy } = usePolicy({ policyId }); + const { execute, isExecuting } = useAction( + () => publishPolicy({ id: policyId }), + { + onSuccess: () => { + toast.success("Policy published successfully"); + }, + } + ); if (!policy) return null; @@ -14,7 +27,17 @@ export function PolicyOverview({ policyId }: { policyId: string }) { if (!content) return null; return ( -
+
+
+ +
+
); From 50087ede7cf2954552babccc147bc2a64c7904c6 Mon Sep 17 00:00:00 2001 From: Mariano Fuentes Date: Wed, 12 Feb 2025 10:57:26 -0800 Subject: [PATCH 2/4] add db migrations to prod merge --- .github/workflows/database-migrations.yml | 43 +++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 .github/workflows/database-migrations.yml diff --git a/.github/workflows/database-migrations.yml b/.github/workflows/database-migrations.yml new file mode 100644 index 0000000000..e43b13ab58 --- /dev/null +++ b/.github/workflows/database-migrations.yml @@ -0,0 +1,43 @@ +name: Database Migrations + +on: + push: + branches: + - main + paths: + - "packages/db/prisma/schema.prisma" + - "packages/db/prisma/migrations/**" + workflow_dispatch: # Allows manual triggering + +env: + NODE_VERSION: "20.x" + +jobs: + migrate: + name: Run Database Migrations + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: ${{ env.NODE_VERSION }} + cache: "npm" + + - name: Install dependencies + run: npm ci + + - name: Apply database migrations + env: + DATABASE_URL: ${{ secrets.DATABASE_URL_PROD }} + run: | + cd packages/db + npx prisma migrate deploy + + - name: Generate Prisma Client + run: | + cd packages/db + npx prisma generate From 251f130f9b1e95b593320bf11f79ed1778c1f741 Mon Sep 17 00:00:00 2001 From: Mariano Fuentes Date: Wed, 12 Feb 2025 11:05:10 -0800 Subject: [PATCH 3/4] add migration check on PR --- .github/workflows/database-migrations.yml | 34 +++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/.github/workflows/database-migrations.yml b/.github/workflows/database-migrations.yml index e43b13ab58..9d30e1b891 100644 --- a/.github/workflows/database-migrations.yml +++ b/.github/workflows/database-migrations.yml @@ -7,14 +7,48 @@ on: paths: - "packages/db/prisma/schema.prisma" - "packages/db/prisma/migrations/**" + pull_request: + paths: + - "packages/db/prisma/schema.prisma" + - "packages/db/prisma/migrations/**" workflow_dispatch: # Allows manual triggering env: NODE_VERSION: "20.x" jobs: + check-migrations: + name: Check Migrations + if: github.event_name == 'pull_request' + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: ${{ env.NODE_VERSION }} + cache: "npm" + + - name: Install dependencies + run: npm ci + + - name: Check migration diff + env: + DATABASE_URL: ${{ secrets.DATABASE_URL_PROD }} + run: | + cd packages/db + # Show what changes would be applied + npx prisma migrate diff \ + --from-url "$DATABASE_URL" \ + --to-schema-datamodel ./prisma/schema.prisma \ + --exit-code + migrate: name: Run Database Migrations + if: github.event_name == 'push' || github.event_name == 'workflow_dispatch' runs-on: ubuntu-latest steps: From 4a6de50ad7f601940cc700a2950bdb919b0e250c Mon Sep 17 00:00:00 2001 From: Mariano Fuentes Date: Wed, 12 Feb 2025 11:08:38 -0800 Subject: [PATCH 4/4] remove migrations check since it's overkill --- .github/workflows/database-migrations.yml | 34 ----------------------- 1 file changed, 34 deletions(-) diff --git a/.github/workflows/database-migrations.yml b/.github/workflows/database-migrations.yml index 9d30e1b891..e43b13ab58 100644 --- a/.github/workflows/database-migrations.yml +++ b/.github/workflows/database-migrations.yml @@ -7,48 +7,14 @@ on: paths: - "packages/db/prisma/schema.prisma" - "packages/db/prisma/migrations/**" - pull_request: - paths: - - "packages/db/prisma/schema.prisma" - - "packages/db/prisma/migrations/**" workflow_dispatch: # Allows manual triggering env: NODE_VERSION: "20.x" jobs: - check-migrations: - name: Check Migrations - if: github.event_name == 'pull_request' - runs-on: ubuntu-latest - - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - - name: Setup Node.js - uses: actions/setup-node@v4 - with: - node-version: ${{ env.NODE_VERSION }} - cache: "npm" - - - name: Install dependencies - run: npm ci - - - name: Check migration diff - env: - DATABASE_URL: ${{ secrets.DATABASE_URL_PROD }} - run: | - cd packages/db - # Show what changes would be applied - npx prisma migrate diff \ - --from-url "$DATABASE_URL" \ - --to-schema-datamodel ./prisma/schema.prisma \ - --exit-code - migrate: name: Run Database Migrations - if: github.event_name == 'push' || github.event_name == 'workflow_dispatch' runs-on: ubuntu-latest steps: