full
This commit is contained in:
@@ -1,47 +1,74 @@
|
||||
const NotificationModel = require('../models/NotificationModel')
|
||||
const AcademyModel = require('../models/AcademyModel')
|
||||
const { notifyUser } = require("./notifyUser");
|
||||
const AcademyModel = require("../models/AcademyModel");
|
||||
|
||||
const LIKE_TITLE = 'پست شما لایک شد'
|
||||
const LIKE_DESCRIPTION = 'پست شما لایک شد'
|
||||
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'
|
||||
type = "post_like",
|
||||
}) => {
|
||||
try {
|
||||
if (!ownerId || !likerId || !entityId) return
|
||||
if (String(ownerId) === String(likerId)) return
|
||||
const owner = normalizeId(ownerId);
|
||||
const liker = normalizeId(likerId);
|
||||
const entity = normalizeId(entityId);
|
||||
|
||||
await NotificationModel.create({
|
||||
user_id: ownerId,
|
||||
project_post_id: entityId,
|
||||
if (!owner || !liker || !entity) return;
|
||||
if (owner === liker) return;
|
||||
|
||||
await notifyUser(io, owner, {
|
||||
type,
|
||||
title: LIKE_TITLE,
|
||||
description: LIKE_DESCRIPTION
|
||||
})
|
||||
description: LIKE_DESCRIPTION,
|
||||
project_post_id: entity,
|
||||
actor_id: liker,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error creating like notification:', error)
|
||||
console.error("Error creating like notification:", error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const resolveCourseOwnerId = async (course) => {
|
||||
if (course?.user_id) {
|
||||
return course.user_id
|
||||
}
|
||||
if (!course) return null;
|
||||
|
||||
if (course?.academyId) {
|
||||
const directOwner = normalizeId(course.user_id);
|
||||
if (directOwner) return directOwner;
|
||||
|
||||
if (course.academyId) {
|
||||
const academy = await AcademyModel.findById(course.academyId)
|
||||
if (academy?.userId) {
|
||||
return academy.userId
|
||||
}
|
||||
.select("userId")
|
||||
.lean();
|
||||
const academyOwner = normalizeId(academy?.userId);
|
||||
if (academyOwner) return academyOwner;
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
const resolveAdvertisingOwnerId = (advertising) => {
|
||||
if (!advertising) return null;
|
||||
return normalizeId(advertising.creator_id);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
createLikeNotification,
|
||||
resolveCourseOwnerId
|
||||
}
|
||||
resolveCourseOwnerId,
|
||||
resolveAdvertisingOwnerId,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user