This commit is contained in:
payacom
2026-08-31 20:45:41 +03:30
parent fa9f23c61d
commit 3f416a7a60
367 changed files with 621895 additions and 6443 deletions

View File

@@ -1,57 +1,81 @@
const NotificationModel = require('../models/NotificationModel')
const { resolveCourseOwnerId } = require('./likeNotification')
const { notifyUser } = require("./notifyUser");
const COMMENT_TITLE = 'یک کامنت برای شما ثبت شد'
const COMMENT_DESCRIPTION = 'یک کامنت برای شما ثبت شد'
const RATING_TITLE = 'یک امتیاز برای شما ثبت شد'
const RATING_DESCRIPTION = 'یک امتیاز برای شما ثبت شد'
const COMMENT_TITLE = "یک کامنت برای شما ثبت شد";
const COMMENT_DESCRIPTION = "یک کامنت برای شما ثبت شد";
const RATING_TITLE = "یک امتیاز برای شما ثبت شد";
const RATING_DESCRIPTION = "یک امتیاز برای شما ثبت شد";
function normalizeId(value) {
if (value == null) return null;
if (typeof value === "object" && value._id) return String(value._id);
return String(value);
}
/**
* @param {object} params
* @param {import('socket.io').Server} [params.io] بدون این، فقط توی دیتابیس ثبت می‌شه، نه سوکت/پوش زنده
* @param {'post_comment'|'profile_comment'|'academy_comment'|'billboard_comment'|'shop_product_comment'} [params.type]
*/
const createCommentNotification = async ({
io,
ownerId,
commenterId,
entityId,
type = 'post_comment'
type = "post_comment",
}) => {
try {
if (!ownerId || !commenterId || !entityId) return
if (String(ownerId) === String(commenterId)) return
const owner = normalizeId(ownerId);
const commenter = normalizeId(commenterId);
const entity = normalizeId(entityId);
await NotificationModel.create({
user_id: ownerId,
project_post_id: entityId,
if (!owner || !commenter || !entity) return;
if (owner === commenter) return;
await notifyUser(io, owner, {
type,
title: COMMENT_TITLE,
description: COMMENT_DESCRIPTION
})
description: COMMENT_DESCRIPTION,
project_post_id: entity,
actor_id: commenter,
});
} catch (error) {
console.error('Error creating comment notification:', error)
console.error("Error creating comment notification:", error);
}
}
};
/**
* @param {object} params
* @param {import('socket.io').Server} [params.io] بدون این، فقط توی دیتابیس ثبت می‌شه، نه سوکت/پوش زنده
* @param {'post_rating'|'profile_rating'|'academy_rating'|'billboard_rating'} [params.type]
*/
const createRatingNotification = async ({
io,
ownerId,
raterId,
entityId,
type = 'billboard_rating'
type = "billboard_rating",
}) => {
try {
if (!ownerId || !raterId || !entityId) return
if (String(ownerId) === String(raterId)) return
const owner = normalizeId(ownerId);
const rater = normalizeId(raterId);
const entity = normalizeId(entityId);
await NotificationModel.create({
user_id: ownerId,
project_post_id: entityId,
if (!owner || !rater || !entity) return;
if (owner === rater) return;
await notifyUser(io, owner, {
type,
title: RATING_TITLE,
description: RATING_DESCRIPTION
})
description: RATING_DESCRIPTION,
project_post_id: entity,
actor_id: rater,
});
} catch (error) {
console.error('Error creating rating notification:', error)
console.error("Error creating rating notification:", error);
}
}
};
module.exports = {
createCommentNotification,
createRatingNotification,
resolveCourseOwnerId
}
};