This commit is contained in:
payacom
2026-09-03 16:06:02 +03:30
parent 3f416a7a60
commit 90c5f94b57
31 changed files with 3968 additions and 4283 deletions

View File

@@ -1,7 +1,6 @@
const jwt = require('jsonwebtoken')
const PaymentModel = require('../../../models/PaymentModel')
const ProjectModel = require('../../../models/ProjectModel')
const moment = require('moment-jalaali')
const getFinancial = async (req, res) => {
try {
@@ -15,40 +14,29 @@ const getFinancial = async (req, res) => {
limit: req.query.limit || 10, // حداکثر تعداد موارد برای هر صفحه
sort: { createdAt: -1 }
}
// Find all payments associated with the user
// همه‌ی پرداخت‌های کاربر (پروژه، تبلیغات، هدیه، فروشگاه، رزرو، دوره و ...)، نه فقط
// پرداخت‌های مرتبط با پروژه — project_title فقط وقتی project_id واقعاً ست شده باشه پر می‌شه
const financial = await PaymentModel.paginate(
{ user_id: userId }, // فیلتر
options // گزینه‌های پیجینیشن
)
// Prepare the data to be sent back
const userPayments = await Promise.all(financial.docs.map(async (payment) => {
// Find the project associated with the payment
const project = await ProjectModel.findById(payment.project_id)
if (!project) {
return null
}
// استفاده از کتابخانه moment-jalaali برای تبدیل تاریخ و زمان به شمسی
const shamsiDate = moment(payment.createdAt).format('jYYYY-jMM-jDD')
const shamsiTime = moment(payment.createdAt).format('HH:mm')
// Extract the required information
const userPayment = {
_id: payment._id,
project_title: project.title,
project_id: project._id,
payment_date: shamsiDate, // Format date as YYYY-MM-DD
payment_time: shamsiTime,
iban: project.creator_id ? project.creator_id.iban : null, // Check if creator_id exists before accessing iban
amount: payment.amount,
payment_status: payment.status
}
return userPayment
}))
// Filter out null values
const validUserPayments = userPayments.filter(payment => payment !== null)
if (financial.docs.length) {
const projectIds = financial.docs
.map((payment) => payment.project_id)
.filter(Boolean)
const projects = projectIds.length
? await ProjectModel.find({ _id: { $in: projectIds } }).select('title').lean()
: []
const titleById = new Map(projects.map((p) => [String(p._id), p.title]))
financial.docs = financial.docs.map((payment) => {
const doc = typeof payment.toObject === 'function' ? payment.toObject() : payment
return {
...doc,
project_title: doc.project_id ? titleById.get(String(doc.project_id)) : undefined
}
})
}
// Send the payments back to the client
res.status(200).json({