refactor(register, listings, items): simplify transaction, remove unused variable, add explicit types

- Remove newUser variable and directly return created user from Prisma transaction in register route
- Add explicit casts for Prisma QueryMode to avoid TypeScript widening in listings search
- Fix items search and getItemBySource with any cast and ts-expect-error to satisfy type checks

Improves readability and type safety without changing behavior.
This commit is contained in:
Evandro Fonseca Junior committed 2025-11-05 04:38:07 -03:00
1 parent ab37b477df
commit f35fee944d
33 files changed
+811 -295

No files matched your search

+22 -6
View File
@@ -30,7 +30,7 @@ export interface TelemetryEvent {
ad_id: number | null;
user_id: number | null;
session_id: string | null;
metadata: Record<string, any>;
metadata: Prisma.JsonValue;
timestamp: Date;
}
@@ -104,15 +104,19 @@ export interface MarketplaceStats {
export async function trackEvent(
data: TrackEventData
): Promise<TelemetryEvent> {
return await prisma.telemetryEvent.create({
const event = await prisma.telemetryEvent.create({
data: {
event_type: data.event_type,
event_type: data.event_type as TelemetryEventType,
ad_id: data.ad_id || null,
user_id: data.user_id || null,
session_id: data.session_id || null,
metadata: data.metadata || {},
},
});
return {
...event,
event_type: event.event_type as TelemetryEventType,
};
}
/**
@@ -123,13 +127,13 @@ export async function trackEventsBatch(
): Promise<TelemetryEvent[]> {
if (events.length === 0) return [];
return await prisma.$transaction(
const eventsResult = await prisma.$transaction(
async (tx) => {
return await Promise.all(
events.map((event) =>
tx.telemetryEvent.create({
data: {
event_type: event.event_type,
event_type: event.event_type as TelemetryEventType,
ad_id: event.ad_id || null,
user_id: event.user_id || null,
session_id: event.session_id || null,
@@ -140,15 +144,27 @@ export async function trackEventsBatch(
);
}
);
return eventsResult.map(event => ({
...event,
event_type: event.event_type as TelemetryEventType,
}));
}
/**
* Busca analytics de um anúncio específico
*/
export async function getAdAnalytics(adId: number): Promise<AdAnalytics | null> {
return await prisma.adAnalytics.findUnique({
const analytics = await prisma.adAnalytics.findUnique({
where: { ad_id: adId },
});
if (!analytics) return null;
return {
...analytics,
ctr: Number(analytics.ctr),
avg_view_duration: Number(analytics.avg_view_duration),
bounce_rate: Number(analytics.bounce_rate),
conversion_rate: Number(analytics.conversion_rate),
};
}
/**