- wrap root layout with QueryProvider for React Query - replace market UnderConstruction placeholder with MarketClient and real data fetching - implement search, pagination, and sort via search params using getActiveListings - add necessary client components and queries - update dependencies (e.g., prisma, pkg-types)
139 lines
2.9 KiB
TypeScript
139 lines
2.9 KiB
TypeScript
/**
|
|
* Queries de notificações usando Prisma
|
|
* Migrado de lib/db/queries/notifications.ts
|
|
*/
|
|
|
|
import { prisma } from "../app";
|
|
|
|
export type NotificationType = "bid" | "sale" | "purchase" | "escrow" | "dispute" | "system";
|
|
|
|
export interface Notification {
|
|
notification_id: number;
|
|
user_id: number;
|
|
notification_type: NotificationType;
|
|
title: string;
|
|
message: string;
|
|
related_id: number | null;
|
|
is_read: boolean;
|
|
send_whatsapp: boolean;
|
|
whatsapp_sent: boolean;
|
|
created_at: Date;
|
|
}
|
|
|
|
export interface CreateNotificationData {
|
|
user_id: number;
|
|
notification_type: NotificationType;
|
|
title: string;
|
|
message: string;
|
|
related_id?: number | null;
|
|
send_whatsapp?: boolean;
|
|
}
|
|
|
|
/**
|
|
* Cria uma nova notificação
|
|
*/
|
|
export async function createNotification(
|
|
data: CreateNotificationData
|
|
): Promise<Notification> {
|
|
return await prisma.notification.create({
|
|
data: {
|
|
user_id: data.user_id,
|
|
notification_type: data.notification_type,
|
|
title: data.title,
|
|
message: data.message,
|
|
related_id: data.related_id || null,
|
|
send_whatsapp: data.send_whatsapp || false,
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Busca notificações de um usuário
|
|
*/
|
|
export async function getUserNotifications(
|
|
userId: number,
|
|
filters: { is_read?: boolean; type?: NotificationType; limit?: number } = {}
|
|
): Promise<Notification[]> {
|
|
const { is_read, type, limit = 50 } = filters;
|
|
|
|
const where: any = { user_id: userId };
|
|
|
|
if (is_read !== undefined) {
|
|
where.is_read = is_read;
|
|
}
|
|
|
|
if (type) {
|
|
where.notification_type = type;
|
|
}
|
|
|
|
return await prisma.notification.findMany({
|
|
where,
|
|
orderBy: { created_at: "desc" },
|
|
take: limit,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Conta notificações não lidas de um usuário
|
|
*/
|
|
export async function getUnreadCount(userId: number): Promise<number> {
|
|
return await prisma.notification.count({
|
|
where: {
|
|
user_id: userId,
|
|
is_read: false,
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Marca notificação como lida
|
|
*/
|
|
export async function markAsRead(notificationId: number, userId: number): Promise<boolean> {
|
|
const result = await prisma.notification.updateMany({
|
|
where: {
|
|
notification_id: notificationId,
|
|
user_id: userId,
|
|
},
|
|
data: {
|
|
is_read: true,
|
|
},
|
|
});
|
|
|
|
return result.count > 0;
|
|
}
|
|
|
|
/**
|
|
* Marca todas as notificações de um usuário como lidas
|
|
*/
|
|
export async function markAllAsRead(userId: number): Promise<number> {
|
|
const result = await prisma.notification.updateMany({
|
|
where: {
|
|
user_id: userId,
|
|
is_read: false,
|
|
},
|
|
data: {
|
|
is_read: true,
|
|
},
|
|
});
|
|
|
|
return result.count;
|
|
}
|
|
|
|
/**
|
|
* Remove notificação
|
|
*/
|
|
export async function deleteNotification(notificationId: number, userId: number): Promise<boolean> {
|
|
try {
|
|
await prisma.notification.delete({
|
|
where: {
|
|
notification_id: notificationId,
|
|
user_id: userId,
|
|
},
|
|
});
|
|
return true;
|
|
} catch (error) {
|
|
return false;
|
|
}
|
|
}
|
|
|