- Introduced "Minhas Intenções" tab visible only to logged-in users. - Integrated MyPurchaseIntentionsPanel and useMyPurchaseIntentions hook. - Added tab badge showing count of pending intentions. - Extended session API to include user’s main character for downstream features.
82 lines
1.8 KiB
TypeScript
82 lines
1.8 KiB
TypeScript
/**
|
|
* Model Message - Representa uma mensagem em um chat
|
|
*/
|
|
|
|
import mongoose, { Schema, model, models } from "mongoose";
|
|
|
|
export interface IMessage {
|
|
chat_id: string; // Referência ao chat
|
|
sender_id: number; // ID do remetente (PostgreSQL)
|
|
content: string; // Conteúdo da mensagem (sanitizado)
|
|
created_at: Date;
|
|
read_at: Date | null; // Quando foi lida
|
|
edited_at: Date | null; // Se foi editada
|
|
deleted_at: Date | null; // Soft delete
|
|
|
|
// Metadados
|
|
message_type: "text" | "system"; // System para mensagens automáticas
|
|
metadata?: {
|
|
listing_id?: number;
|
|
action?: "listing_updated" | "listing_sold" | "price_changed";
|
|
};
|
|
}
|
|
|
|
const MessageSchema = new Schema<IMessage>(
|
|
{
|
|
chat_id: {
|
|
type: String,
|
|
required: true,
|
|
index: true,
|
|
},
|
|
sender_id: {
|
|
type: Number,
|
|
required: true,
|
|
index: true,
|
|
},
|
|
content: {
|
|
type: String,
|
|
required: true,
|
|
maxlength: 1000,
|
|
trim: true,
|
|
},
|
|
created_at: {
|
|
type: Date,
|
|
default: Date.now,
|
|
index: true,
|
|
},
|
|
read_at: {
|
|
type: Date,
|
|
default: null,
|
|
},
|
|
edited_at: {
|
|
type: Date,
|
|
default: null,
|
|
},
|
|
deleted_at: {
|
|
type: Date,
|
|
default: null,
|
|
},
|
|
message_type: {
|
|
type: String,
|
|
enum: ["text", "system"],
|
|
default: "text",
|
|
},
|
|
metadata: {
|
|
type: Schema.Types.Mixed,
|
|
default: null,
|
|
},
|
|
},
|
|
{
|
|
timestamps: false, // Usamos campos manuais
|
|
collection: "messages",
|
|
}
|
|
);
|
|
|
|
// Índices compostos para queries eficientes
|
|
MessageSchema.index({ chat_id: 1, created_at: -1 }); // Para buscar mensagens de um chat ordenadas
|
|
MessageSchema.index({ chat_id: 1, sender_id: 1, created_at: -1 });
|
|
MessageSchema.index({ sender_id: 1, created_at: -1 });
|
|
|
|
export const Message = models.Message || model<IMessage>("Message", MessageSchema);
|
|
|