mirror of
https://github.com/felipegcoutinho/openmonetis.git
synced 2026-05-09 19:01:47 +00:00
refactor: traduz contratos compartilhados do schema
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { and, eq, isNull } from "drizzle-orm";
|
||||
import { NextResponse } from "next/server";
|
||||
import { tokensApi } from "@/db/schema";
|
||||
import { apiTokens } from "@/db/schema";
|
||||
import {
|
||||
extractBearerToken,
|
||||
hashToken,
|
||||
@@ -33,11 +33,11 @@ export async function POST(request: Request) {
|
||||
}
|
||||
|
||||
// Verificar se token não foi revogado
|
||||
const tokenRecord = await db.query.tokensApi.findFirst({
|
||||
const tokenRecord = await db.query.apiTokens.findFirst({
|
||||
where: and(
|
||||
eq(tokensApi.id, payload.tokenId),
|
||||
eq(tokensApi.userId, payload.sub),
|
||||
isNull(tokensApi.revokedAt),
|
||||
eq(apiTokens.id, payload.tokenId),
|
||||
eq(apiTokens.userId, payload.sub),
|
||||
isNull(apiTokens.revokedAt),
|
||||
),
|
||||
});
|
||||
|
||||
@@ -60,7 +60,7 @@ export async function POST(request: Request) {
|
||||
|
||||
// Atualizar hash do token e último uso
|
||||
await db
|
||||
.update(tokensApi)
|
||||
.update(apiTokens)
|
||||
.set({
|
||||
tokenHash: hashToken(result.accessToken),
|
||||
lastUsedAt: new Date(),
|
||||
@@ -69,7 +69,7 @@ export async function POST(request: Request) {
|
||||
request.headers.get("x-real-ip"),
|
||||
expiresAt: result.expiresAt,
|
||||
})
|
||||
.where(eq(tokensApi.id, payload.tokenId));
|
||||
.where(eq(apiTokens.id, payload.tokenId));
|
||||
|
||||
return NextResponse.json({
|
||||
accessToken: result.accessToken,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { headers } from "next/headers";
|
||||
import { NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { tokensApi } from "@/db/schema";
|
||||
import { apiTokens } from "@/db/schema";
|
||||
import {
|
||||
generateTokenPair,
|
||||
getTokenPrefix,
|
||||
@@ -35,7 +35,7 @@ export async function POST(request: Request) {
|
||||
);
|
||||
|
||||
// Salvar hash do token no banco
|
||||
await db.insert(tokensApi).values({
|
||||
await db.insert(apiTokens).values({
|
||||
id: tokenId,
|
||||
userId: session.user.id,
|
||||
name,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { headers } from "next/headers";
|
||||
import { NextResponse } from "next/server";
|
||||
import { tokensApi } from "@/db/schema";
|
||||
import { apiTokens } from "@/db/schema";
|
||||
import { auth } from "@/shared/lib/auth/config";
|
||||
import { db } from "@/shared/lib/db";
|
||||
|
||||
@@ -21,10 +21,10 @@ export async function DELETE(_request: Request, { params }: RouteParams) {
|
||||
}
|
||||
|
||||
// Verificar se token pertence ao usuário
|
||||
const token = await db.query.tokensApi.findFirst({
|
||||
const token = await db.query.apiTokens.findFirst({
|
||||
where: and(
|
||||
eq(tokensApi.id, tokenId),
|
||||
eq(tokensApi.userId, session.user.id),
|
||||
eq(apiTokens.id, tokenId),
|
||||
eq(apiTokens.userId, session.user.id),
|
||||
),
|
||||
});
|
||||
|
||||
@@ -37,9 +37,9 @@ export async function DELETE(_request: Request, { params }: RouteParams) {
|
||||
|
||||
// Revogar token (soft delete)
|
||||
await db
|
||||
.update(tokensApi)
|
||||
.update(apiTokens)
|
||||
.set({ revokedAt: new Date() })
|
||||
.where(eq(tokensApi.id, tokenId));
|
||||
.where(eq(apiTokens.id, tokenId));
|
||||
|
||||
return NextResponse.json({
|
||||
message: "Token revogado com sucesso",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { and, desc, eq, isNull } from "drizzle-orm";
|
||||
import { headers } from "next/headers";
|
||||
import { NextResponse } from "next/server";
|
||||
import { tokensApi } from "@/db/schema";
|
||||
import { apiTokens } from "@/db/schema";
|
||||
import { auth } from "@/shared/lib/auth/config";
|
||||
import { db } from "@/shared/lib/db";
|
||||
|
||||
@@ -17,19 +17,19 @@ export async function GET() {
|
||||
// Buscar tokens ativos do usuário
|
||||
const activeTokens = await db
|
||||
.select({
|
||||
id: tokensApi.id,
|
||||
name: tokensApi.name,
|
||||
tokenPrefix: tokensApi.tokenPrefix,
|
||||
lastUsedAt: tokensApi.lastUsedAt,
|
||||
lastUsedIp: tokensApi.lastUsedIp,
|
||||
expiresAt: tokensApi.expiresAt,
|
||||
createdAt: tokensApi.createdAt,
|
||||
id: apiTokens.id,
|
||||
name: apiTokens.name,
|
||||
tokenPrefix: apiTokens.tokenPrefix,
|
||||
lastUsedAt: apiTokens.lastUsedAt,
|
||||
lastUsedIp: apiTokens.lastUsedIp,
|
||||
expiresAt: apiTokens.expiresAt,
|
||||
createdAt: apiTokens.createdAt,
|
||||
})
|
||||
.from(tokensApi)
|
||||
.from(apiTokens)
|
||||
.where(
|
||||
and(eq(tokensApi.userId, session.user.id), isNull(tokensApi.revokedAt)),
|
||||
and(eq(apiTokens.userId, session.user.id), isNull(apiTokens.revokedAt)),
|
||||
)
|
||||
.orderBy(desc(tokensApi.createdAt));
|
||||
.orderBy(desc(apiTokens.createdAt));
|
||||
|
||||
return NextResponse.json({ tokens: activeTokens });
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { and, eq, isNull } from "drizzle-orm";
|
||||
import { NextResponse } from "next/server";
|
||||
import { tokensApi } from "@/db/schema";
|
||||
import { apiTokens } from "@/db/schema";
|
||||
import { extractBearerToken, hashToken } from "@/shared/lib/auth/api-token";
|
||||
import { db } from "@/shared/lib/db";
|
||||
|
||||
@@ -29,10 +29,10 @@ export async function POST(request: Request) {
|
||||
const tokenHash = hashToken(token);
|
||||
|
||||
// Buscar token no banco
|
||||
const tokenRecord = await db.query.tokensApi.findFirst({
|
||||
const tokenRecord = await db.query.apiTokens.findFirst({
|
||||
where: and(
|
||||
eq(tokensApi.tokenHash, tokenHash),
|
||||
isNull(tokensApi.revokedAt),
|
||||
eq(apiTokens.tokenHash, tokenHash),
|
||||
isNull(apiTokens.revokedAt),
|
||||
),
|
||||
});
|
||||
|
||||
@@ -50,12 +50,12 @@ export async function POST(request: Request) {
|
||||
null;
|
||||
|
||||
await db
|
||||
.update(tokensApi)
|
||||
.update(apiTokens)
|
||||
.set({
|
||||
lastUsedAt: new Date(),
|
||||
lastUsedIp: clientIp,
|
||||
})
|
||||
.where(eq(tokensApi.id, tokenRecord.id));
|
||||
.where(eq(apiTokens.id, tokenRecord.id));
|
||||
|
||||
return NextResponse.json({
|
||||
valid: true,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { and, eq, isNull } from "drizzle-orm";
|
||||
import { NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { preLancamentos, tokensApi } from "@/db/schema";
|
||||
import { apiTokens, inboxItems } from "@/db/schema";
|
||||
import { extractBearerToken, hashToken } from "@/shared/lib/auth/api-token";
|
||||
import { db } from "@/shared/lib/db";
|
||||
import { inboxBatchSchema } from "@/shared/lib/schemas/inbox";
|
||||
@@ -59,10 +59,10 @@ export async function POST(request: Request) {
|
||||
const tokenHash = hashToken(token);
|
||||
|
||||
// Buscar token no banco
|
||||
const tokenRecord = await db.query.tokensApi.findFirst({
|
||||
const tokenRecord = await db.query.apiTokens.findFirst({
|
||||
where: and(
|
||||
eq(tokensApi.tokenHash, tokenHash),
|
||||
isNull(tokensApi.revokedAt),
|
||||
eq(apiTokens.tokenHash, tokenHash),
|
||||
isNull(apiTokens.revokedAt),
|
||||
),
|
||||
});
|
||||
|
||||
@@ -91,7 +91,7 @@ export async function POST(request: Request) {
|
||||
for (const item of items) {
|
||||
try {
|
||||
const [inserted] = await db
|
||||
.insert(preLancamentos)
|
||||
.insert(inboxItems)
|
||||
.values({
|
||||
userId: tokenRecord.userId,
|
||||
sourceApp: item.sourceApp,
|
||||
@@ -103,7 +103,7 @@ export async function POST(request: Request) {
|
||||
parsedAmount: item.parsedAmount?.toString(),
|
||||
status: "pending",
|
||||
})
|
||||
.returning({ id: preLancamentos.id });
|
||||
.returning({ id: inboxItems.id });
|
||||
|
||||
results.push({
|
||||
clientId: item.clientId,
|
||||
@@ -126,12 +126,12 @@ export async function POST(request: Request) {
|
||||
null;
|
||||
|
||||
await db
|
||||
.update(tokensApi)
|
||||
.update(apiTokens)
|
||||
.set({
|
||||
lastUsedAt: new Date(),
|
||||
lastUsedIp: clientIp,
|
||||
})
|
||||
.where(eq(tokensApi.id, tokenRecord.id));
|
||||
.where(eq(apiTokens.id, tokenRecord.id));
|
||||
|
||||
const successCount = results.filter((r) => r.success).length;
|
||||
const failCount = results.filter((r) => !r.success).length;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { and, eq, isNull } from "drizzle-orm";
|
||||
import { NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { preLancamentos, tokensApi } from "@/db/schema";
|
||||
import { apiTokens, inboxItems } from "@/db/schema";
|
||||
import { extractBearerToken, hashToken } from "@/shared/lib/auth/api-token";
|
||||
import { db } from "@/shared/lib/db";
|
||||
import { inboxItemSchema } from "@/shared/lib/schemas/inbox";
|
||||
@@ -52,10 +52,10 @@ export async function POST(request: Request) {
|
||||
const tokenHash = hashToken(token);
|
||||
|
||||
// Buscar token no banco
|
||||
const tokenRecord = await db.query.tokensApi.findFirst({
|
||||
const tokenRecord = await db.query.apiTokens.findFirst({
|
||||
where: and(
|
||||
eq(tokensApi.tokenHash, tokenHash),
|
||||
isNull(tokensApi.revokedAt),
|
||||
eq(apiTokens.tokenHash, tokenHash),
|
||||
isNull(apiTokens.revokedAt),
|
||||
),
|
||||
});
|
||||
|
||||
@@ -80,7 +80,7 @@ export async function POST(request: Request) {
|
||||
|
||||
// Inserir item na inbox
|
||||
const [inserted] = await db
|
||||
.insert(preLancamentos)
|
||||
.insert(inboxItems)
|
||||
.values({
|
||||
userId: tokenRecord.userId,
|
||||
sourceApp: data.sourceApp,
|
||||
@@ -92,7 +92,7 @@ export async function POST(request: Request) {
|
||||
parsedAmount: data.parsedAmount?.toString(),
|
||||
status: "pending",
|
||||
})
|
||||
.returning({ id: preLancamentos.id });
|
||||
.returning({ id: inboxItems.id });
|
||||
|
||||
// Atualizar último uso do token
|
||||
const clientIp =
|
||||
@@ -101,12 +101,12 @@ export async function POST(request: Request) {
|
||||
null;
|
||||
|
||||
await db
|
||||
.update(tokensApi)
|
||||
.update(apiTokens)
|
||||
.set({
|
||||
lastUsedAt: new Date(),
|
||||
lastUsedIp: clientIp,
|
||||
})
|
||||
.where(eq(tokensApi.id, tokenRecord.id));
|
||||
.where(eq(apiTokens.id, tokenRecord.id));
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user