75 lines
2.1 KiB
JavaScript
75 lines
2.1 KiB
JavaScript
const { notifyUser } = require("./notifyUser");
|
|
const AcademyModel = require("../models/AcademyModel");
|
|
|
|
const LIKE_TITLE = "پست شما لایک شد";
|
|
const LIKE_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 {string|import('mongoose').Types.ObjectId} params.ownerId
|
|
* @param {string|import('mongoose').Types.ObjectId} params.likerId
|
|
* @param {string|import('mongoose').Types.ObjectId} params.entityId
|
|
* @param {'post_like'|'academy_like'|'billboard_like'} [params.type]
|
|
*/
|
|
const createLikeNotification = async ({
|
|
io,
|
|
ownerId,
|
|
likerId,
|
|
entityId,
|
|
type = "post_like",
|
|
}) => {
|
|
try {
|
|
const owner = normalizeId(ownerId);
|
|
const liker = normalizeId(likerId);
|
|
const entity = normalizeId(entityId);
|
|
|
|
if (!owner || !liker || !entity) return;
|
|
if (owner === liker) return;
|
|
|
|
await notifyUser(io, owner, {
|
|
type,
|
|
title: LIKE_TITLE,
|
|
description: LIKE_DESCRIPTION,
|
|
project_post_id: entity,
|
|
actor_id: liker,
|
|
});
|
|
} catch (error) {
|
|
console.error("Error creating like notification:", error);
|
|
}
|
|
};
|
|
|
|
const resolveCourseOwnerId = async (course) => {
|
|
if (!course) return null;
|
|
|
|
const directOwner = normalizeId(course.user_id);
|
|
if (directOwner) return directOwner;
|
|
|
|
if (course.academyId) {
|
|
const academy = await AcademyModel.findById(course.academyId)
|
|
.select("userId")
|
|
.lean();
|
|
const academyOwner = normalizeId(academy?.userId);
|
|
if (academyOwner) return academyOwner;
|
|
}
|
|
|
|
return null;
|
|
};
|
|
|
|
const resolveAdvertisingOwnerId = (advertising) => {
|
|
if (!advertising) return null;
|
|
return normalizeId(advertising.creator_id);
|
|
};
|
|
|
|
module.exports = {
|
|
createLikeNotification,
|
|
resolveCourseOwnerId,
|
|
resolveAdvertisingOwnerId,
|
|
};
|