forked from git.gladyson/openmonetis
- Adicionados ícones SVG para ChatGPT, Claude, Gemini e OpenRouter - Implementados ícones para modos claro e escuro do ChatGPT - Criado script de inicialização para PostgreSQL com extensão pgcrypto - Adicionado script de configuração de ambiente que faz backup do .env - Configurado tsconfig.json para TypeScript com opções de compilação
61 lines
1.3 KiB
TypeScript
61 lines
1.3 KiB
TypeScript
/**
|
|
* Pagador defaults - User seeding logic
|
|
*
|
|
* Moved from /lib/pagador-defaults.ts to /lib/pagadores/defaults.ts
|
|
*/
|
|
|
|
import { pagadores } from "@/db/schema";
|
|
import { db } from "@/lib/db";
|
|
import {
|
|
DEFAULT_PAGADOR_AVATAR,
|
|
PAGADOR_ROLE_ADMIN,
|
|
PAGADOR_STATUS_OPTIONS,
|
|
} from "./constants";
|
|
import { normalizeNameFromEmail } from "./utils";
|
|
import { eq } from "drizzle-orm";
|
|
|
|
const DEFAULT_STATUS = PAGADOR_STATUS_OPTIONS[0];
|
|
|
|
interface SeedUserLike {
|
|
id?: string;
|
|
name?: string | null;
|
|
email?: string | null;
|
|
image?: string | null;
|
|
}
|
|
|
|
export async function ensureDefaultPagadorForUser(user: SeedUserLike) {
|
|
const userId = user.id;
|
|
|
|
if (!userId) {
|
|
return;
|
|
}
|
|
|
|
const hasAnyPagador = await db.query.pagadores.findFirst({
|
|
columns: { id: true, role: true },
|
|
where: eq(pagadores.userId, userId),
|
|
});
|
|
|
|
if (hasAnyPagador) {
|
|
return;
|
|
}
|
|
|
|
const name =
|
|
(user.name && user.name.trim().length > 0
|
|
? user.name.trim()
|
|
: normalizeNameFromEmail(user.email)) || "Pagador principal";
|
|
|
|
// Usa a imagem do Google se disponível, senão usa o avatar padrão
|
|
const avatarUrl = user.image ?? DEFAULT_PAGADOR_AVATAR;
|
|
|
|
await db.insert(pagadores).values({
|
|
name,
|
|
email: user.email ?? null,
|
|
status: DEFAULT_STATUS,
|
|
role: PAGADOR_ROLE_ADMIN,
|
|
avatarUrl,
|
|
note: null,
|
|
isAutoSend: false,
|
|
userId,
|
|
});
|
|
}
|