46 lines
1.4 KiB
JavaScript
46 lines
1.4 KiB
JavaScript
const UserModel = require("../models/UserModel");
|
|
const { notifyUser } = require("./notifyUser");
|
|
const { buildStorageUrl } = require("./storageUrl");
|
|
|
|
const PURCHASE_DESCRIPTION = "دورهی آموزشی شما را خریداری کرد";
|
|
|
|
// همون الگوی tagNotification.js/notifyFollowers.js — چون این تابع مستقیم از کنترلر
|
|
// صدا زده میشه (نه یه جاب زمانبندیشده)، io از index.js با همین setIo تزریق میشه
|
|
let ioInstance = null;
|
|
function setIo(io) {
|
|
ioInstance = io;
|
|
}
|
|
|
|
const createCoursePurchaseNotification = async ({
|
|
ownerId,
|
|
buyerId,
|
|
courseId,
|
|
}) => {
|
|
try {
|
|
if (!ownerId || !buyerId || !courseId) return;
|
|
if (String(ownerId) === String(buyerId)) return;
|
|
|
|
const buyer = await UserModel.findById(buyerId).select(
|
|
"user_name profile_image"
|
|
);
|
|
const buyerUsername = buyer?.user_name || "کاربر";
|
|
const buyerImage = buildStorageUrl(buyer?.profile_image) || undefined;
|
|
|
|
await notifyUser(ioInstance, ownerId, {
|
|
type: "academy_purchase",
|
|
title: buyerUsername,
|
|
description: PURCHASE_DESCRIPTION,
|
|
project_post_id: courseId,
|
|
actor_id: buyerId,
|
|
image: buyerImage,
|
|
});
|
|
} catch (error) {
|
|
console.error("Error creating course purchase notification:", error);
|
|
}
|
|
};
|
|
|
|
module.exports = {
|
|
createCoursePurchaseNotification,
|
|
setIo,
|
|
};
|