Files
modstagram-back/utils/commentNotification.js
payacom 3f416a7a60 full
2026-08-31 20:45:41 +03:30

82 lines
2.4 KiB
JavaScript

const { notifyUser } = require("./notifyUser");
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",
}) => {
try {
const owner = normalizeId(ownerId);
const commenter = normalizeId(commenterId);
const entity = normalizeId(entityId);
if (!owner || !commenter || !entity) return;
if (owner === commenter) return;
await notifyUser(io, owner, {
type,
title: COMMENT_TITLE,
description: COMMENT_DESCRIPTION,
project_post_id: entity,
actor_id: commenter,
});
} catch (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",
}) => {
try {
const owner = normalizeId(ownerId);
const rater = normalizeId(raterId);
const entity = normalizeId(entityId);
if (!owner || !rater || !entity) return;
if (owner === rater) return;
await notifyUser(io, owner, {
type,
title: RATING_TITLE,
description: RATING_DESCRIPTION,
project_post_id: entity,
actor_id: rater,
});
} catch (error) {
console.error("Error creating rating notification:", error);
}
};
module.exports = {
createCommentNotification,
createRatingNotification,
};