add: gui sep requests
This commit is contained in:
108
index.js
108
index.js
@@ -986,6 +986,7 @@ app.post("/all-payments/send", async (req, res) => {
|
|||||||
} else {
|
} else {
|
||||||
await taavonSendDataZarinPal(province, data);
|
await taavonSendDataZarinPal(province, data);
|
||||||
}
|
}
|
||||||
|
await coll.deleteOne({ _id: new ObjectId(id) });
|
||||||
return res.json({ ok: true, message: "ارسال شد" });
|
return res.json({ ok: true, message: "ارسال شد" });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("all-payments send error", err);
|
console.error("all-payments send error", err);
|
||||||
@@ -993,6 +994,49 @@ app.post("/all-payments/send", async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// all-payments/remove: remove one payment from MongoDB
|
||||||
|
app.post("/all-payments/remove", async (req, res) => {
|
||||||
|
const { id } = req.body;
|
||||||
|
if (!id) {
|
||||||
|
return res.status(400).json({ error: "id is required" });
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const coll = await getSepPayCollection();
|
||||||
|
const result = await coll.deleteOne({ _id: new ObjectId(id) });
|
||||||
|
if (result.deletedCount === 0) {
|
||||||
|
return res.status(404).json({ error: "Record not found" });
|
||||||
|
}
|
||||||
|
return res.json({ ok: true, message: "حذف شد" });
|
||||||
|
} catch (err) {
|
||||||
|
console.error("all-payments remove error", err);
|
||||||
|
return res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// all-payments/remove-all: remove all payments from MongoDB
|
||||||
|
app.post("/all-payments/remove-all", async (req, res) => {
|
||||||
|
try {
|
||||||
|
const coll = await getSepPayCollection();
|
||||||
|
const result = await coll.deleteMany({});
|
||||||
|
return res.json({
|
||||||
|
ok: true,
|
||||||
|
message: "همه حذف شد",
|
||||||
|
deletedCount: result.deletedCount,
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
console.error("all-payments remove-all error", err);
|
||||||
|
return res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Province code (first 2 digits) -> name for all-payments
|
||||||
|
const PROVINCE_NAMES = {
|
||||||
|
10: "تست",
|
||||||
|
18: "همدان",
|
||||||
|
47: "مرکزی",
|
||||||
|
51: "کردستان",
|
||||||
|
};
|
||||||
|
|
||||||
// all-payments: list of saved SEP pay requests (from MongoDB)
|
// all-payments: list of saved SEP pay requests (from MongoDB)
|
||||||
app.get("/all-payments", async (req, res) => {
|
app.get("/all-payments", async (req, res) => {
|
||||||
let list = [];
|
let list = [];
|
||||||
@@ -1003,10 +1047,15 @@ app.get("/all-payments", async (req, res) => {
|
|||||||
.sort({ createdAt: -1 })
|
.sort({ createdAt: -1 })
|
||||||
.limit(500)
|
.limit(500)
|
||||||
.toArray();
|
.toArray();
|
||||||
list = raw.map((doc) => ({
|
list = raw.map((doc) => {
|
||||||
...doc,
|
const code = (doc.provincecode || "").toString().substring(0, 2);
|
||||||
_id: doc._id ? doc._id.toString() : doc._id,
|
const provinceName = PROVINCE_NAMES[code] || doc.provincecode || "-";
|
||||||
}));
|
return {
|
||||||
|
...doc,
|
||||||
|
_id: doc._id ? doc._id.toString() : doc._id,
|
||||||
|
provinceName,
|
||||||
|
};
|
||||||
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("all-payments list error", err);
|
console.error("all-payments list error", err);
|
||||||
}
|
}
|
||||||
@@ -1033,9 +1082,14 @@ app.get("/all-payments", async (req, res) => {
|
|||||||
th, td { padding: 10px 12px; text-align: right; border-bottom: 1px solid #eee; }
|
th, td { padding: 10px 12px; text-align: right; border-bottom: 1px solid #eee; }
|
||||||
th { background: #fafafa; font-weight: bold; color: #555; }
|
th { background: #fafafa; font-weight: bold; color: #555; }
|
||||||
tr:hover { background: #f9f9f9; }
|
tr:hover { background: #f9f9f9; }
|
||||||
.btn-send { background: #1976d2; color: #fff; border: none; padding: 6px 12px; border-radius: 6px; cursor: pointer; font-size: 13px; }
|
.btn-send { background: #1976d2; color: #fff; border: none; padding: 6px 12px; border-radius: 6px; cursor: pointer; font-size: 13px; margin-left: 6px; }
|
||||||
.btn-send:hover { background: #1565c0; }
|
.btn-send:hover { background: #1565c0; }
|
||||||
.btn-send:disabled { background: #9e9e9e; cursor: not-allowed; }
|
.btn-send:disabled { background: #9e9e9e; cursor: not-allowed; }
|
||||||
|
.btn-remove { background: #c62828; color: #fff; border: none; padding: 6px 12px; border-radius: 6px; cursor: pointer; font-size: 13px; }
|
||||||
|
.btn-remove:hover { background: #b71c1c; }
|
||||||
|
.btn-remove:disabled { background: #9e9e9e; cursor: not-allowed; }
|
||||||
|
.btn-remove-all { background: #c62828; color: #fff; border: none; padding: 8px 16px; border-radius: 6px; cursor: pointer; font-size: 14px; margin-bottom: 12px; }
|
||||||
|
.btn-remove-all:hover { background: #b71c1c; }
|
||||||
.cell-msg { font-size: 12px; padding: 4px 0; }
|
.cell-msg { font-size: 12px; padding: 4px 0; }
|
||||||
.cell-msg.ok { color: #2e7d32; }
|
.cell-msg.ok { color: #2e7d32; }
|
||||||
.cell-msg.err { color: #c62828; }
|
.cell-msg.err { color: #c62828; }
|
||||||
@@ -1043,21 +1097,24 @@ app.get("/all-payments", async (req, res) => {
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>همه پرداختها</h1>
|
<h1>همه پرداختها</h1>
|
||||||
|
<div id="toolbar"></div>
|
||||||
<div id="list"></div>
|
<div id="list"></div>
|
||||||
<script>
|
<script>
|
||||||
(function() {
|
(function() {
|
||||||
var list = JSON.parse("${listJson}");
|
var list = JSON.parse("${listJson}");
|
||||||
var listEl = document.getElementById('list');
|
var listEl = document.getElementById('list');
|
||||||
|
var toolbarEl = document.getElementById('toolbar');
|
||||||
if (!list || list.length === 0) {
|
if (!list || list.length === 0) {
|
||||||
listEl.innerHTML = '<p>موردی یافت نشد.</p>';
|
listEl.innerHTML = '<p>موردی یافت نشد.</p>';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
toolbarEl.innerHTML = '<button type="button" class="btn-remove-all" id="btn-remove-all">حذف همه</button>';
|
||||||
var rows = list.map(function(item) {
|
var rows = list.map(function(item) {
|
||||||
var createdAt = item.createdAt ? new Date(item.createdAt).toLocaleString('fa-IR') : '-';
|
var createdAt = item.createdAt ? new Date(item.createdAt).toLocaleString('fa-IR') : '-';
|
||||||
var id = item._id;
|
var id = item._id;
|
||||||
return '<tr><td>' + (item.amountRaw || item.amount) + '</td><td>' + (item.provincecode || '-') + '</td><td>' + (item.isLink ? 'بله' : 'خیر') + '</td><td>' + (item.phone || '-') + '</td><td>' + createdAt + '</td><td><button type="button" class="btn-send" data-id="' + id + '">ارسال به سرور</button><div class="cell-msg" id="msg-' + id + '"></div></td></tr>';
|
return '<tr><td>' + (item.amountRaw || item.amount) + '</td><td>' + (item.provinceName || '-') + '</td><td>' + (item.isLink ? 'بله' : 'خیر') + '</td><td>' + (item.phone || '-') + '</td><td>' + createdAt + '</td><td><button type="button" class="btn-send" data-id="' + id + '">ارسال به سرور</button><button type="button" class="btn-remove" data-id="' + id + '">حذف</button><div class="cell-msg" id="msg-' + id + '"></div></td></tr>';
|
||||||
}).join('');
|
}).join('');
|
||||||
listEl.innerHTML = '<table><thead><tr><th>مبلغ</th><th>استان</th><th>لینک</th><th>موبایل</th><th>تاریخ</th><th>ارسال به سرور</th></tr></thead><tbody>' + rows + '</tbody></table>';
|
listEl.innerHTML = '<table><thead><tr><th>مبلغ</th><th>استان</th><th>لینک</th><th>موبایل</th><th>تاریخ</th><th>عملیات</th></tr></thead><tbody>' + rows + '</tbody></table>';
|
||||||
listEl.querySelectorAll('.btn-send').forEach(function(btn) {
|
listEl.querySelectorAll('.btn-send').forEach(function(btn) {
|
||||||
btn.addEventListener('click', function() {
|
btn.addEventListener('click', function() {
|
||||||
if (!confirm('آیا مطمئن هستید؟')) return;
|
if (!confirm('آیا مطمئن هستید؟')) return;
|
||||||
@@ -1068,7 +1125,12 @@ app.get("/all-payments", async (req, res) => {
|
|||||||
fetch('/all-payments/send', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id: id }) })
|
fetch('/all-payments/send', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id: id }) })
|
||||||
.then(function(r) { return r.json(); })
|
.then(function(r) { return r.json(); })
|
||||||
.then(function(j) {
|
.then(function(j) {
|
||||||
if (msgEl) { msgEl.textContent = j.error || j.message || 'ارسال شد'; msgEl.className = 'cell-msg ' + (j.error ? 'err' : 'ok'); }
|
if (j.error) {
|
||||||
|
if (msgEl) { msgEl.textContent = j.error; msgEl.className = 'cell-msg err'; }
|
||||||
|
} else {
|
||||||
|
var row = btn.closest('tr');
|
||||||
|
if (row) row.remove();
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch(function(e) {
|
.catch(function(e) {
|
||||||
if (msgEl) { msgEl.textContent = e.message; msgEl.className = 'cell-msg err'; }
|
if (msgEl) { msgEl.textContent = e.message; msgEl.className = 'cell-msg err'; }
|
||||||
@@ -1076,6 +1138,36 @@ app.get("/all-payments", async (req, res) => {
|
|||||||
.finally(function() { btn.disabled = false; });
|
.finally(function() { btn.disabled = false; });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
listEl.querySelectorAll('.btn-remove').forEach(function(btn) {
|
||||||
|
btn.addEventListener('click', function() {
|
||||||
|
if (!confirm('آیا از حذف این مورد مطمئن هستید؟')) return;
|
||||||
|
var id = btn.getAttribute('data-id');
|
||||||
|
var msgEl = document.getElementById('msg-' + id);
|
||||||
|
if (msgEl) { msgEl.textContent = ''; msgEl.className = 'cell-msg'; }
|
||||||
|
btn.disabled = true;
|
||||||
|
fetch('/all-payments/remove', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id: id }) })
|
||||||
|
.then(function(r) { return r.json(); })
|
||||||
|
.then(function(j) {
|
||||||
|
if (j.error && msgEl) { msgEl.textContent = j.error; msgEl.className = 'cell-msg err'; }
|
||||||
|
else { var row = btn.closest('tr'); if (row) row.remove(); }
|
||||||
|
})
|
||||||
|
.catch(function(e) { if (msgEl) { msgEl.textContent = e.message; msgEl.className = 'cell-msg err'; } })
|
||||||
|
.finally(function() { btn.disabled = false; });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
document.getElementById('btn-remove-all').addEventListener('click', function() {
|
||||||
|
if (!confirm('آیا از حذف همه موارد مطمئن هستید؟')) return;
|
||||||
|
var btn = this;
|
||||||
|
btn.disabled = true;
|
||||||
|
fetch('/all-payments/remove-all', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: '{}' })
|
||||||
|
.then(function(r) { return r.json(); })
|
||||||
|
.then(function(j) {
|
||||||
|
if (j.error) { alert(j.error); }
|
||||||
|
else { listEl.innerHTML = '<p>موردی یافت نشد.</p>'; toolbarEl.innerHTML = ''; }
|
||||||
|
})
|
||||||
|
.catch(function(e) { alert(e.message); })
|
||||||
|
.finally(function() { btn.disabled = false; });
|
||||||
|
});
|
||||||
})();
|
})();
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
Reference in New Issue
Block a user