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
14 changes: 6 additions & 8 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
ADMIN_USERNAME=admin
ADMIN_PASSWORD=troque-por-uma-senha-forte
ADMIN_SESSION_SECRET=troque-este-segredo-longo-e-aleatorio
GITHUB_TOKEN=ghp_troque_por_um_token_com_repo_contents_write_e_pull_requests_write
GITHUB_REPO_OWNER=ArielAio
GITHUB_REPO_NAME=Li-Rilko
GITHUB_REPO_BRANCH=main
GITHUB_ATTENDANTS_PATH=data/attendants.json
GITHUB_ATTENDANTS_WORK_BRANCH=bot/attendants-admin
GITHUB_ATTENDANTS_AUTO_MERGE=true
GITHUB_ATTENDANTS_AUTO_MERGE_METHOD=squash
NEXT_PUBLIC_GA_MEASUREMENT_ID=
NEXT_PUBLIC_SITE_URL=localhost
NEXT_PUBLIC_SUPABASE_URL=
NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY=
SUPABASE_SERVICE_ROLE_KEY=
SUPABASE_STORAGE_BUCKET_PRODUCTS=product-images
7 changes: 7 additions & 0 deletions TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
- Unit/Integration: Vitest + Testing Library
- E2E smoke: Playwright (Chromium)
- CI pipeline: GitHub Actions (`.github/workflows/quality-gate.yml`)
- Admin persistence does not use GitHub anymore; CI only validates tests/builds.

## Commands
- `npm run test:unit`
- `npm run test:integration`
- `npm run test:e2e:smoke`
- `npm run test:known-issues`
- `npm run test:gate` (unit + integration + build + e2e smoke)
- `npm run supabase:seed` (importa catálogo inicial, atendentes e imagens no projeto Supabase configurado no ambiente)

## Supabase bootstrap
- Migration inicial: [supabase/migrations/20260319_000001_catalog_phase1.sql](/Users/arielaio/Desktop/Arquivos/Projetos/Projetos%20com%20Siconeli/Li%20Rilko/supabase/migrations/20260319_000001_catalog_phase1.sql)
- Seed local: [scripts/supabase-seed.cjs](/Users/arielaio/Desktop/Arquivos/Projetos/Projetos%20com%20Siconeli/Li%20Rilko/scripts/supabase-seed.cjs)
Comment on lines +18 to +19
- O bucket `product-images` precisa existir no projeto antes do seed.

## Gate policy
`test:gate` is the blocking suite for important deploys (push/merge to `main`).
Expand Down
132 changes: 26 additions & 106 deletions app/api/admin/attendants/route.js
Original file line number Diff line number Diff line change
@@ -1,135 +1,55 @@
import { NextResponse } from "next/server";
import { ADMIN_SESSION_COOKIE, isAdminSessionValid } from "@/lib/admin-auth";
import { adminJson, requireAdminRequest } from "@/lib/admin-route";
import {
AttendantsGitHubSyncError,
AttendantsValidationError,
readAttendantsFromRepositoryFile,
updateAttendantsInRepository,
} from "@/lib/attendants-store";
CatalogRepositoryConfigError,
CatalogRepositoryValidationError,
getAdminBootstrapSnapshot,
saveAttendantsInCatalog,
} from "@/lib/catalog-repository";

export const runtime = "nodejs";

function isAuthenticated(request) {
const sessionToken = request.cookies.get(ADMIN_SESSION_COOKIE)?.value ?? "";
return isAdminSessionValid(sessionToken);
}

function unauthorizedResponse() {
return NextResponse.json(
{ error: "Acesso negado. Faça login no painel admin." },
{
status: 401,
headers: {
"Cache-Control": "no-store",
},
},
);
}

export async function GET(request) {
if (!isAuthenticated(request)) {
return unauthorizedResponse();
const unauthorized = requireAdminRequest(request);
if (unauthorized) {
return unauthorized;
}

try {
const attendants = await readAttendantsFromRepositoryFile();

return NextResponse.json(
{ attendants },
{
status: 200,
headers: {
"Cache-Control": "no-store",
},
},
);
const catalog = await getAdminBootstrapSnapshot();
return adminJson({ attendants: catalog.attendants });
} catch {
return NextResponse.json(
{ error: "Não foi possível carregar atendentes agora." },
{
status: 500,
headers: {
"Cache-Control": "no-store",
},
},
);
return adminJson({ error: "Não foi possível carregar atendentes agora." }, 500);
}
}

export async function PUT(request) {
if (!isAuthenticated(request)) {
return unauthorizedResponse();
const unauthorized = requireAdminRequest(request);
if (unauthorized) {
return unauthorized;
}

let payload;
try {
payload = await request.json();
} catch {
return NextResponse.json(
{ error: "Payload inválido. Envie JSON no formato correto." },
{
status: 400,
headers: {
"Cache-Control": "no-store",
},
},
);
return adminJson({ error: "Payload inválido. Envie JSON no formato correto." }, 400);
}

try {
const result = await updateAttendantsInRepository(payload?.attendants);

return NextResponse.json(
{
attendants: result.attendants,
commitSha: result.commitSha,
pullRequestUrl: result.pullRequestUrl,
pullRequestNumber: result.pullRequestNumber,
workBranch: result.workBranch,
unchanged: result.unchanged,
autoMergeRequested: result.autoMergeRequested,
autoMergeStatusMessage: result.autoMergeStatusMessage,
},
{
status: 200,
headers: {
"Cache-Control": "no-store",
},
},
);
const result = await saveAttendantsInCatalog(payload?.attendants);
return adminJson({
attendants: result.catalog.attendants,
catalog: result.catalog,
});
} catch (error) {
if (error instanceof AttendantsValidationError) {
return NextResponse.json(
{ error: error.message },
{
status: 400,
headers: {
"Cache-Control": "no-store",
},
},
);
if (error instanceof CatalogRepositoryValidationError) {
return adminJson({ error: error.message }, 400);
}

if (error instanceof AttendantsGitHubSyncError) {
return NextResponse.json(
{ error: error.message },
{
status: 502,
headers: {
"Cache-Control": "no-store",
},
},
);
if (error instanceof CatalogRepositoryConfigError) {
return adminJson({ error: error.message }, 503);
}

return NextResponse.json(
{ error: "Erro interno ao salvar atendentes." },
{
status: 500,
headers: {
"Cache-Control": "no-store",
},
},
);
return adminJson({ error: "Erro interno ao salvar atendentes." }, 500);
}
}
18 changes: 18 additions & 0 deletions app/api/admin/bootstrap/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { adminJson, requireAdminRequest } from "@/lib/admin-route";
import { getAdminBootstrapSnapshot } from "@/lib/catalog-repository";

export const runtime = "nodejs";

export async function GET(request) {
const unauthorized = requireAdminRequest(request);
if (unauthorized) {
return unauthorized;
}

try {
const catalog = await getAdminBootstrapSnapshot();
return adminJson({ catalog });
} catch {
return adminJson({ error: "Não foi possível carregar os dados administrativos agora." }, 500);
}
}
37 changes: 37 additions & 0 deletions app/api/admin/categories/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { adminJson, requireAdminRequest } from "@/lib/admin-route";
import {
CatalogRepositoryConfigError,
CatalogRepositoryValidationError,
saveCategoriesInCatalog,
} from "@/lib/catalog-repository";

export const runtime = "nodejs";

export async function PUT(request) {
const unauthorized = requireAdminRequest(request);
if (unauthorized) {
return unauthorized;
}

let payload;
try {
payload = await request.json();
} catch {
return adminJson({ error: "Payload inválido. Envie JSON no formato correto." }, 400);
}

try {
const result = await saveCategoriesInCatalog(payload?.categories);
return adminJson({ catalog: result.catalog });
} catch (error) {
if (error instanceof CatalogRepositoryValidationError) {
return adminJson({ error: error.message }, 400);
}

if (error instanceof CatalogRepositoryConfigError) {
return adminJson({ error: error.message }, 503);
}

return adminJson({ error: "Erro interno ao salvar categorias." }, 500);
}
}
37 changes: 37 additions & 0 deletions app/api/admin/contact-channels/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { adminJson, requireAdminRequest } from "@/lib/admin-route";
import {
CatalogRepositoryConfigError,
CatalogRepositoryValidationError,
saveContactChannelsInCatalog,
} from "@/lib/catalog-repository";

export const runtime = "nodejs";

export async function PUT(request) {
const unauthorized = requireAdminRequest(request);
if (unauthorized) {
return unauthorized;
}

let payload;
try {
payload = await request.json();
} catch {
return adminJson({ error: "Payload inválido. Envie JSON no formato correto." }, 400);
}

try {
const result = await saveContactChannelsInCatalog(payload?.contactChannels);
return adminJson({ catalog: result.catalog });
} catch (error) {
if (error instanceof CatalogRepositoryValidationError) {
return adminJson({ error: error.message }, 400);
}

if (error instanceof CatalogRepositoryConfigError) {
return adminJson({ error: error.message }, 503);
}

return adminJson({ error: "Erro interno ao salvar canais." }, 500);
}
}
32 changes: 32 additions & 0 deletions app/api/admin/products/[id]/images/[imageId]/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { adminJson, requireAdminRequest } from "@/lib/admin-route";
import {
CatalogRepositoryConfigError,
CatalogRepositoryValidationError,
deleteProductImageAsset,
} from "@/lib/catalog-repository";

export const runtime = "nodejs";

export async function DELETE(request, context) {
const unauthorized = requireAdminRequest(request);
if (unauthorized) {
return unauthorized;
}

const { id, imageId } = await context.params;

try {
await deleteProductImageAsset(id, imageId);
return adminJson({ ok: true });
} catch (error) {
if (error instanceof CatalogRepositoryValidationError) {
return adminJson({ error: error.message }, 400);
}

if (error instanceof CatalogRepositoryConfigError) {
return adminJson({ error: error.message }, 503);
}

return adminJson({ error: "Erro interno ao excluir imagem do produto." }, 500);
}
}
41 changes: 41 additions & 0 deletions app/api/admin/products/[id]/images/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { adminJson, requireAdminRequest } from "@/lib/admin-route";
import {
CatalogRepositoryConfigError,
CatalogRepositoryValidationError,
uploadProductImageAsset,
} from "@/lib/catalog-repository";

export const runtime = "nodejs";

export async function POST(request, context) {
const unauthorized = requireAdminRequest(request);
if (unauthorized) {
return unauthorized;
}

const { id } = await context.params;
let formData;

try {
formData = await request.formData();
} catch {
return adminJson({ error: "Payload inválido. Envie multipart/form-data corretamente." }, 400);
}

const file = formData.get("file");

try {
const image = await uploadProductImageAsset(id, file);
return adminJson({ image });
} catch (error) {
if (error instanceof CatalogRepositoryValidationError) {
return adminJson({ error: error.message }, 400);
}

if (error instanceof CatalogRepositoryConfigError) {
return adminJson({ error: error.message }, 503);
}

return adminJson({ error: "Erro interno ao enviar imagem do produto." }, 500);
}
}
Loading
Loading