- Parte 3 : Viaggi

- Chat
This commit is contained in:
Surya Paolo
2025-12-24 00:26:38 +01:00
parent b78e3ce544
commit cb965eaa27
20 changed files with 1752 additions and 793 deletions

View File

@@ -79,6 +79,16 @@ const ChatSchema = new Schema(
ref: 'User',
},
],
deletedBy: [
{
type: Schema.Types.ObjectId,
ref: 'User',
},
],
clearedBefore: {
type: Map,
of: Date,
},
metadata: {
type: Schema.Types.Mixed,
},
@@ -114,19 +124,17 @@ ChatSchema.methods.getUnreadForUser = function (userId) {
// ✅ FIX: incrementUnread (assicura conversione corretta)
ChatSchema.methods.incrementUnread = function (excludeUserId) {
const excludeIdStr = excludeUserId.toString();
this.participants.forEach((participantId) => {
// Gestisci sia ObjectId che oggetti popolati
const id = participantId._id
? participantId._id.toString()
: participantId.toString();
const id = participantId._id ? participantId._id.toString() : participantId.toString();
if (id !== excludeIdStr) {
const current = this.unreadCount.get(id) || 0;
this.unreadCount.set(id, current + 1);
}
});
return this.save();
};
@@ -151,7 +159,7 @@ ChatSchema.methods.updateLastMessage = function (message) {
// ✅ FIX: Gestisce sia ObjectId che oggetti User popolati
ChatSchema.methods.hasParticipant = function (userId) {
const userIdStr = userId.toString();
return this.participants.some((p) => {
// Se p è un oggetto popolato (ha _id), usa p._id
// Altrimenti p è già un ObjectId
@@ -164,14 +172,13 @@ ChatSchema.methods.hasParticipant = function (userId) {
// ✅ FIX: Metodo isBlockedFor (stesso problema)
ChatSchema.methods.isBlockedFor = function (userId) {
const userIdStr = userId.toString();
return this.blockedBy.some((id) => {
const blockedId = id._id ? id._id.toString() : id.toString();
return blockedId === userIdStr;
});
};
// Metodo statico per trovare o creare una chat diretta
ChatSchema.statics.findOrCreateDirect = async function (idapp, userId1, userId2, rideId = null) {
// Cerca chat esistente tra i due utenti