mirror of
https://github.com/felipegcoutinho/openmonetis.git
synced 2026-05-09 19:01:47 +00:00
feat(logo): integração Logo.dev para logos automáticos de estabelecimentos
- Nova tabela `establishment_logos` no schema (userId + nameKey → domain) - Utilitários: `buildLogoDevUrl`, `toNameKey`, `logoQueryKeys`, `LOGO_DEV_TOKEN` - `EstablishmentLogo`: exibe logo via Logo.dev com fallback para iniciais; hover mostra ícone de edição - `EstablishmentLogoPicker`: popover para buscar e fixar domínio Logo.dev por estabelecimento - API routes: `GET /api/logo/mapping` e `GET /api/logo/search` - Server actions/queries para persistência do mapeamento por usuário - CSP: libera `https://img.logo.dev` em `img-src` - `.env.example`: variáveis `NEXT_PUBLIC_LOGO_DEV_TOKEN` e `LOGO_DEV_SECRET_KEY` Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
64
src/shared/lib/logo/establishment-logo-actions.ts
Normal file
64
src/shared/lib/logo/establishment-logo-actions.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
"use server";
|
||||
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { establishmentLogos } from "@/db/schema";
|
||||
import type { ActionResult } from "@/shared/lib/actions/helpers";
|
||||
import {
|
||||
handleActionError,
|
||||
revalidateForEntity,
|
||||
} from "@/shared/lib/actions/helpers";
|
||||
import { getUserId } from "@/shared/lib/auth/server";
|
||||
import { db } from "@/shared/lib/db";
|
||||
import { toNameKey } from "@/shared/lib/logo";
|
||||
|
||||
/**
|
||||
* Salva ou atualiza o domínio Logo.dev preferido para um estabelecimento.
|
||||
*/
|
||||
export async function saveEstablishmentLogoAction(
|
||||
name: string,
|
||||
domain: string,
|
||||
): Promise<ActionResult> {
|
||||
try {
|
||||
const userId = await getUserId();
|
||||
const nameKey = toNameKey(name);
|
||||
|
||||
await db
|
||||
.insert(establishmentLogos)
|
||||
.values({ userId, nameKey, domain })
|
||||
.onConflictDoUpdate({
|
||||
target: [establishmentLogos.userId, establishmentLogos.nameKey],
|
||||
set: { domain, updatedAt: new Date() },
|
||||
});
|
||||
|
||||
revalidateForEntity("establishments", userId);
|
||||
return { success: true, message: "Logo salvo." };
|
||||
} catch (error) {
|
||||
return handleActionError(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove o mapeamento salvo, voltando ao comportamento automático do Logo.dev.
|
||||
*/
|
||||
export async function removeEstablishmentLogoAction(
|
||||
name: string,
|
||||
): Promise<ActionResult> {
|
||||
try {
|
||||
const userId = await getUserId();
|
||||
const nameKey = toNameKey(name);
|
||||
|
||||
await db
|
||||
.delete(establishmentLogos)
|
||||
.where(
|
||||
and(
|
||||
eq(establishmentLogos.userId, userId),
|
||||
eq(establishmentLogos.nameKey, nameKey),
|
||||
),
|
||||
);
|
||||
|
||||
revalidateForEntity("establishments", userId);
|
||||
return { success: true, message: "Logo restaurado." };
|
||||
} catch (error) {
|
||||
return handleActionError(error);
|
||||
}
|
||||
}
|
||||
46
src/shared/lib/logo/establishment-logo-queries.ts
Normal file
46
src/shared/lib/logo/establishment-logo-queries.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { and, eq, inArray } from "drizzle-orm";
|
||||
import { establishmentLogos } from "@/db/schema";
|
||||
import { db } from "@/shared/lib/db";
|
||||
import { toNameKey } from "@/shared/lib/logo";
|
||||
|
||||
export { toNameKey };
|
||||
|
||||
/**
|
||||
* Busca o domínio salvo para um único estabelecimento.
|
||||
*/
|
||||
export async function fetchEstablishmentLogoDomain(
|
||||
userId: string,
|
||||
name: string,
|
||||
): Promise<string | null> {
|
||||
const nameKey = toNameKey(name);
|
||||
const row = await db.query.establishmentLogos.findFirst({
|
||||
where: and(
|
||||
eq(establishmentLogos.userId, userId),
|
||||
eq(establishmentLogos.nameKey, nameKey),
|
||||
),
|
||||
columns: { domain: true },
|
||||
});
|
||||
return row?.domain ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Busca domínios salvos para múltiplos nomes de uma vez (evita N+1).
|
||||
* Retorna um Map de nameKey → domain.
|
||||
*/
|
||||
export async function fetchEstablishmentLogoMap(
|
||||
userId: string,
|
||||
names: string[],
|
||||
): Promise<Map<string, string>> {
|
||||
const nameKeys = [...new Set(names.map(toNameKey))];
|
||||
if (nameKeys.length === 0) return new Map();
|
||||
|
||||
const rows = await db.query.establishmentLogos.findMany({
|
||||
where: and(
|
||||
eq(establishmentLogos.userId, userId),
|
||||
inArray(establishmentLogos.nameKey, nameKeys),
|
||||
),
|
||||
columns: { nameKey: true, domain: true },
|
||||
});
|
||||
|
||||
return new Map(rows.map((r) => [r.nameKey, r.domain]));
|
||||
}
|
||||
@@ -39,6 +39,29 @@ export const deriveNameFromLogo = (logo?: string | null) => {
|
||||
.join(" ");
|
||||
};
|
||||
|
||||
/**
|
||||
* Normaliza o nome do estabelecimento para usar como chave de lookup no banco.
|
||||
*/
|
||||
export const toNameKey = (name: string): string => name.trim().toLowerCase();
|
||||
|
||||
// === Logo.dev ===
|
||||
|
||||
export const LOGO_DEV_TOKEN = process.env.NEXT_PUBLIC_LOGO_DEV_TOKEN;
|
||||
|
||||
export function buildLogoDevUrl(
|
||||
domain?: string | null,
|
||||
): string | null {
|
||||
if (!LOGO_DEV_TOKEN || !domain) return null;
|
||||
return `https://img.logo.dev/${domain}?token=${LOGO_DEV_TOKEN}&size=64&format=png`;
|
||||
}
|
||||
|
||||
export const logoQueryKeys = {
|
||||
mapping: (nameKey: string) => ["logo-mapping", nameKey] as const,
|
||||
search: (query: string) => ["logo-search", query] as const,
|
||||
};
|
||||
|
||||
// === Local logo resolution ===
|
||||
|
||||
const LOGO_SRC_PATTERN = /^(https?:\/\/|data:)/;
|
||||
|
||||
type ResolveLogoSrcOptions = {
|
||||
|
||||
Reference in New Issue
Block a user