88 lines
2.8 KiB
JavaScript
88 lines
2.8 KiB
JavaScript
const { normalizeCurrencyForShahoo } = require("./mapMarket");
|
|
const { mapDepositStatus, mapWithdrawStatus } = require("./mapWithdrawal");
|
|
|
|
function parseTime(value) {
|
|
if (!value) return new Date().toISOString();
|
|
const ms = Date.parse(value);
|
|
return Number.isFinite(ms) ? new Date(ms).toISOString() : String(value);
|
|
}
|
|
|
|
function buildDepositNotification(deposit) {
|
|
const currency = normalizeCurrencyForShahoo(deposit.currency || deposit.currencyName);
|
|
const amount = parseFloat(deposit.amount) || 0;
|
|
const status = mapDepositStatus(deposit.state || deposit.status);
|
|
|
|
return {
|
|
id: `deposit-${deposit.id}`,
|
|
text: `واریز ${amount} ${currency} — ${status}`,
|
|
created: parseTime(deposit.created_at || deposit.created),
|
|
status: "UNREAD",
|
|
_sortTime: Date.parse(deposit.created_at || deposit.created) || 0,
|
|
};
|
|
}
|
|
|
|
function buildWithdrawNotification(withdraw) {
|
|
const currency = normalizeCurrencyForShahoo(withdraw.currency || withdraw.currencyName);
|
|
const amount = parseFloat(withdraw.amount) || 0;
|
|
const status = mapWithdrawStatus(withdraw.state || withdraw.status);
|
|
|
|
return {
|
|
id: `withdraw-${withdraw.id}`,
|
|
text: `برداشت ${amount} ${currency} — ${status}`,
|
|
created: parseTime(withdraw.created_at || withdraw.created),
|
|
status: "UNREAD",
|
|
_sortTime: Date.parse(withdraw.created_at || withdraw.created) || 0,
|
|
};
|
|
}
|
|
|
|
function buildActivityNotification(activity) {
|
|
const action = activity.action || activity.topic || "activity";
|
|
const result = activity.result || activity.state || "succeed";
|
|
|
|
return {
|
|
id: `activity-${activity.id || `${action}-${activity.created_at}`}`,
|
|
text: `${action} (${result})`,
|
|
created: parseTime(activity.created_at || activity.created),
|
|
status: "UNREAD",
|
|
_sortTime: Date.parse(activity.created_at || activity.created) || 0,
|
|
};
|
|
}
|
|
|
|
function mergeNotifications(items) {
|
|
return items
|
|
.filter(Boolean)
|
|
.sort((a, b) => b._sortTime - a._sortTime)
|
|
.map(({ _sortTime, ...row }) => row);
|
|
}
|
|
|
|
function applyNotificationPrefs(notifications, prefs = {}) {
|
|
const deleted = new Set((prefs.deletedNotificationIds || []).map(String));
|
|
const read = new Set((prefs.readNotificationIds || []).map(String));
|
|
|
|
return notifications
|
|
.filter((row) => !deleted.has(String(row.id)))
|
|
.map((row) => ({
|
|
...row,
|
|
status: read.has(String(row.id)) ? "READ" : row.status,
|
|
}));
|
|
}
|
|
|
|
function paginateNotifications(rows, pageSize, pageNumber) {
|
|
const limit = Math.max(1, Math.min(Number(pageSize) || 10, 100));
|
|
const page = Math.max(0, Number(pageNumber) || 0);
|
|
const start = page * limit;
|
|
return {
|
|
content: rows.slice(start, start + limit),
|
|
count: rows.length,
|
|
};
|
|
}
|
|
|
|
module.exports = {
|
|
buildDepositNotification,
|
|
buildWithdrawNotification,
|
|
buildActivityNotification,
|
|
mergeNotifications,
|
|
applyNotificationPrefs,
|
|
paginateNotifications,
|
|
};
|