Files
pagodera/lib/prisma/queries/notifications.ts
T
Evandro Fonseca Junior f35fee944d 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.
2025-11-05 04:38:07 -03:00

147 lines
3.2 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> {
const notification = await prisma.notification.create({
data: {
user_id: data.user_id,
notification_type: data.notification_type as NotificationType,
title: data.title,
message: data.message,
related_id: data.related_id || null,
send_whatsapp: data.send_whatsapp || false,
},
});
return {
...notification,
notification_type: notification.notification_type as NotificationType,
};
}
/**
* 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;
}
const notifications = await prisma.notification.findMany({
where,
orderBy: { created_at: "desc" },
take: limit,
});
return notifications.map(notification => ({
...notification,
notification_type: notification.notification_type as NotificationType,
}));
}
/**
* 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;
}
}