Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions migrations/20260608160251_add_liens/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- CreateTable
CREATE TABLE "LinkCategory" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"order" INTEGER NOT NULL DEFAULT 0,

CONSTRAINT "LinkCategory_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "LinkItem" (
"id" SERIAL NOT NULL,
"label" TEXT NOT NULL,
"url" TEXT NOT NULL,
"order" INTEGER NOT NULL DEFAULT 0,
"categoryId" INTEGER NOT NULL,

CONSTRAINT "LinkItem_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE INDEX "LinkItem_categoryId_idx" ON "LinkItem"("categoryId");

-- AddForeignKey
ALTER TABLE "LinkItem" ADD CONSTRAINT "LinkItem_categoryId_fkey" FOREIGN KEY ("categoryId") REFERENCES "LinkCategory"("id") ON DELETE CASCADE ON UPDATE CASCADE;
1 change: 1 addition & 0 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ await jiti.import("./src/env")
const nextConfig = {
output: "standalone",
transpilePackages: ["@t3-oss/env-nextjs", "@t3-oss/env-core"],
allowedDevOrigins: ["192.168.1.13"],

reactCompiler: true,
experimental: {
Expand Down
4 changes: 1 addition & 3 deletions oxlint.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ export default defineConfig({
}
},
{
// src/env.ts is the sanctioned env-access file (t3-env pattern);
// biome suppressed noProcessEnv here via biome-ignore-all.
files: ["src/env.ts"],
files: ["src/env.ts", "src/instrumentation.ts"],
rules: { "node/no-process-env": "off" }
},
{
Expand Down
26 changes: 26 additions & 0 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,32 @@ async function main() {
title: "Créer Mot de Passe",
category: "Espace Asso",
description: "Créer un mot de passe pour les représentants"
},

// Liens
{
name: "access:liens",
title: "Accès Liens",
category: "Liens",
description: "Voir la page de gestion des liens"
},
{
name: "create:lien",
title: "Créer Lien",
category: "Liens",
description: "Ajouter de nouveaux liens"
},
{
name: "edit:lien",
title: "Modifier Lien",
category: "Liens",
description: "Modifier les informations des liens"
},
{
name: "delete:lien",
title: "Supprimer Lien",
category: "Liens",
description: "Supprimer des liens"
}
]

Expand Down
18 changes: 18 additions & 0 deletions schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,24 @@ model LocationCache {
lastAccessedAt DateTime @default(now())
}

model LinkCategory {
id Int @id @default(autoincrement())
name String
order Int @default(0)
liens LinkItem[]
}

model LinkItem {
id Int @id @default(autoincrement())
label String
url String
order Int @default(0)
categoryId Int
category LinkCategory @relation(fields: [categoryId], references: [id], onDelete: Cascade)

@@index([categoryId])
}

enum Role {
MEMBER
ADMIN
Expand Down
106 changes: 106 additions & 0 deletions src/actions/links/__tests__/addLinkAction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { beforeEach, describe, expect, it, vi } from "vitest"

import { validAddLink } from "@/test/factories/links"
import { mockUser } from "@/test/factories/user"
import { itIsGatedBy } from "@/test/gates"
import { authModule, cacheModule, dbModule, sentryModule } from "@/test/mocks"

const h = vi.hoisted(() => ({
getUser: vi.fn(),
findCategory: vi.fn(),
createLink: vi.fn(),
revalidatePath: vi.fn(),
captureActionError: vi.fn()
}))

vi.mock("@/helpers/db", () =>
dbModule({
linkCategory: { findUnique: h.findCategory },
linkItem: { create: h.createLink }
})
)
vi.mock("@/helpers/supabase/auth", () => authModule(h.getUser))
vi.mock("next/cache", () => cacheModule(h.revalidatePath))
vi.mock("@/lib/sentry", () => sentryModule(h.captureActionError))

import addLinkAction from "../addLinkAction"

beforeEach(() => {
h.getUser.mockResolvedValue(mockUser(["create:lien"]))
h.findCategory.mockResolvedValue({ id: 1 })
h.createLink.mockResolvedValue({ id: 1 })
})

describe("addLinkAction", () => {
itIsGatedBy({
action: () => addLinkAction(validAddLink()),
permission: "create:lien",
getUser: h.getUser,
writes: [h.createLink]
})

it("rejects an invalid payload", async () => {
const res = await addLinkAction(validAddLink({ url: "/relatif" }))
expect(res).toEqual({
success: false,
error: "Un ou plusieurs champs sont invalides."
})
expect(h.createLink).not.toHaveBeenCalled()
})

it("returns an error when the category is not found", async () => {
h.findCategory.mockResolvedValue(null)
const res = await addLinkAction(validAddLink())
expect(res).toEqual({
success: false,
error: "Catégorie introuvable."
})
expect(h.createLink).not.toHaveBeenCalled()
})

it("captures and fails when the category lookup throws", async () => {
h.findCategory.mockRejectedValue(new Error("db down"))
const res = await addLinkAction(validAddLink())
expect(res).toEqual({
success: false,
error: "Échec de la création du lien."
})
expect(h.captureActionError).toHaveBeenCalledOnce()
expect(h.createLink).not.toHaveBeenCalled()
})

it("captures and fails when the insert throws", async () => {
h.createLink.mockRejectedValue(new Error("db down"))
const res = await addLinkAction(validAddLink())
expect(res).toEqual({
success: false,
error: "Échec de la création du lien."
})
expect(h.captureActionError).toHaveBeenCalledOnce()
expect(h.revalidatePath).not.toHaveBeenCalled()
})

it("creates the link and revalidates on the happy path", async () => {
const res = await addLinkAction(
validAddLink({
categoryId: 3,
label: "Discord",
url: "https://discord.gg/fare"
})
)
expect(res).toEqual({ success: true })
expect(h.findCategory).toHaveBeenCalledWith({
where: { id: 3 },
select: { id: true }
})
expect(h.createLink).toHaveBeenCalledWith({
data: {
label: "Discord",
url: "https://discord.gg/fare",
categoryId: 3
}
})
expect(h.revalidatePath).toHaveBeenCalledWith("/dashboard/liens")
expect(h.revalidatePath).toHaveBeenCalledWith("/liens")
})
})
70 changes: 70 additions & 0 deletions src/actions/links/__tests__/addLinkCategoryAction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { beforeEach, describe, expect, it, vi } from "vitest"

import { validAddLinkCategory } from "@/test/factories/links"
import { mockUser } from "@/test/factories/user"
import { itIsGatedBy } from "@/test/gates"
import { authModule, cacheModule, dbModule, sentryModule } from "@/test/mocks"

const h = vi.hoisted(() => ({
getUser: vi.fn(),
createCategory: vi.fn(),
revalidatePath: vi.fn(),
captureActionError: vi.fn()
}))

vi.mock("@/helpers/db", () =>
dbModule({ linkCategory: { create: h.createCategory } })
)
vi.mock("@/helpers/supabase/auth", () => authModule(h.getUser))
vi.mock("next/cache", () => cacheModule(h.revalidatePath))
vi.mock("@/lib/sentry", () => sentryModule(h.captureActionError))

import addLinkCategoryAction from "../addLinkCategoryAction"

beforeEach(() => {
h.getUser.mockResolvedValue(mockUser(["create:lien"]))
h.createCategory.mockResolvedValue({ id: 1 })
})

describe("addLinkCategoryAction", () => {
itIsGatedBy({
action: () => addLinkCategoryAction(validAddLinkCategory()),
permission: "create:lien",
getUser: h.getUser,
writes: [h.createCategory]
})

it("rejects an invalid payload", async () => {
const res = await addLinkCategoryAction(
validAddLinkCategory({ name: "" })
)
expect(res).toEqual({
success: false,
error: "Un ou plusieurs champs sont invalides."
})
expect(h.createCategory).not.toHaveBeenCalled()
})

it("captures and fails when the insert throws", async () => {
h.createCategory.mockRejectedValue(new Error("db down"))
const res = await addLinkCategoryAction(validAddLinkCategory())
expect(res).toEqual({
success: false,
error: "Échec de la création de la catégorie."
})
expect(h.captureActionError).toHaveBeenCalledOnce()
expect(h.revalidatePath).not.toHaveBeenCalled()
})

it("creates the category and revalidates on the happy path", async () => {
const res = await addLinkCategoryAction(
validAddLinkCategory({ name: "Projets" })
)
expect(res).toEqual({ success: true })
expect(h.createCategory).toHaveBeenCalledWith({
data: { name: "Projets" }
})
expect(h.revalidatePath).toHaveBeenCalledWith("/dashboard/liens")
expect(h.revalidatePath).toHaveBeenCalledWith("/liens")
})
})
52 changes: 52 additions & 0 deletions src/actions/links/__tests__/deleteLinkAction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { beforeEach, describe, expect, it, vi } from "vitest"

import { mockUser } from "@/test/factories/user"
import { itIsGatedBy } from "@/test/gates"
import { authModule, cacheModule, dbModule, sentryModule } from "@/test/mocks"

const h = vi.hoisted(() => ({
getUser: vi.fn(),
deleteLink: vi.fn(),
revalidatePath: vi.fn(),
captureActionError: vi.fn()
}))

vi.mock("@/helpers/db", () => dbModule({ linkItem: { delete: h.deleteLink } }))
vi.mock("@/helpers/supabase/auth", () => authModule(h.getUser))
vi.mock("next/cache", () => cacheModule(h.revalidatePath))
vi.mock("@/lib/sentry", () => sentryModule(h.captureActionError))

import deleteLinkAction from "../deleteLinkAction"

beforeEach(() => {
h.getUser.mockResolvedValue(mockUser(["delete:lien"]))
h.deleteLink.mockResolvedValue({ id: 1 })
})

describe("deleteLinkAction", () => {
itIsGatedBy({
action: () => deleteLinkAction(undefined, 1),
permission: "delete:lien",
getUser: h.getUser,
writes: [h.deleteLink]
})

it("captures and fails when the delete throws", async () => {
h.deleteLink.mockRejectedValue(new Error("db down"))
const res = await deleteLinkAction(undefined, 1)
expect(res).toEqual({
success: false,
error: "Echec de la suppression du lien"
})
expect(h.captureActionError).toHaveBeenCalledOnce()
expect(h.revalidatePath).not.toHaveBeenCalled()
})

it("deletes the link and revalidates on the happy path", async () => {
const res = await deleteLinkAction(undefined, 9)
expect(res).toEqual({ success: true })
expect(h.deleteLink).toHaveBeenCalledWith({ where: { id: 9 } })
expect(h.revalidatePath).toHaveBeenCalledWith("/dashboard/liens")
expect(h.revalidatePath).toHaveBeenCalledWith("/liens")
})
})
54 changes: 54 additions & 0 deletions src/actions/links/__tests__/deleteLinkCategoryAction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { beforeEach, describe, expect, it, vi } from "vitest"

import { mockUser } from "@/test/factories/user"
import { itIsGatedBy } from "@/test/gates"
import { authModule, cacheModule, dbModule, sentryModule } from "@/test/mocks"

const h = vi.hoisted(() => ({
getUser: vi.fn(),
deleteCategory: vi.fn(),
revalidatePath: vi.fn(),
captureActionError: vi.fn()
}))

vi.mock("@/helpers/db", () =>
dbModule({ linkCategory: { delete: h.deleteCategory } })
)
vi.mock("@/helpers/supabase/auth", () => authModule(h.getUser))
vi.mock("next/cache", () => cacheModule(h.revalidatePath))
vi.mock("@/lib/sentry", () => sentryModule(h.captureActionError))

import deleteLinkCategoryAction from "../deleteLinkCategoryAction"

beforeEach(() => {
h.getUser.mockResolvedValue(mockUser(["delete:lien"]))
h.deleteCategory.mockResolvedValue({ id: 1 })
})

describe("deleteLinkCategoryAction", () => {
itIsGatedBy({
action: () => deleteLinkCategoryAction(undefined, 1),
permission: "delete:lien",
getUser: h.getUser,
writes: [h.deleteCategory]
})

it("captures and fails when the delete throws", async () => {
h.deleteCategory.mockRejectedValue(new Error("db down"))
const res = await deleteLinkCategoryAction(undefined, 1)
expect(res).toEqual({
success: false,
error: "Echec de la suppression de la catégorie"
})
expect(h.captureActionError).toHaveBeenCalledOnce()
expect(h.revalidatePath).not.toHaveBeenCalled()
})

it("deletes the category and revalidates on the happy path", async () => {
const res = await deleteLinkCategoryAction(undefined, 9)
expect(res).toEqual({ success: true })
expect(h.deleteCategory).toHaveBeenCalledWith({ where: { id: 9 } })
expect(h.revalidatePath).toHaveBeenCalledWith("/dashboard/liens")
expect(h.revalidatePath).toHaveBeenCalledWith("/liens")
})
})
Loading
Loading