Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions client/play/geoword/admin/invoice.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ <h1 class="display-6">Invoice</h1>
<p><b>Packet id (or set id):</b> <span id="packet-id"></span></p>
<p><b>Username:</b> <span id="username"></span></p>
<p><b>User id:</b> <span id="user-id"></span></p>
<p class="d-none" id="recorded-by-row"><b>Manually recorded by:</b> <span id="recorded-by"></span></p>
<p><b>Amount:</b> $<span id="amount"></span></p>
</div>

Expand Down
6 changes: 5 additions & 1 deletion client/play/geoword/admin/invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ fetch('/api/geoword/admin/invoice?' + new URLSearchParams({ _id, packetName }))
.then(response => response.json())
.then(data => {
const { amount, payment } = data;
const { packet, set, user_id: userId, username, createdAt } = payment;
const { packet, set, user_id: userId, username, createdAt, recordedBy, recordedByUsername } = payment;
document.getElementById('date').textContent = new Date(createdAt).toLocaleString();
document.getElementById('packet-name').textContent = packet?.name ?? `All packets in ${set.name}`;
document.getElementById('packet-id').textContent = packet?._id ?? set._id;
document.getElementById('user-id').textContent = userId;
document.getElementById('username').textContent = username;
if (recordedBy) {
document.getElementById('recorded-by').textContent = `${recordedByUsername} (${recordedBy})`;
document.getElementById('recorded-by-row').classList.remove('d-none');
}
document.getElementById('amount').textContent = (amount / 100).toFixed(2);
});
3 changes: 3 additions & 0 deletions database/geoword/admin/get-invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export default async function getInvoice ({ _id }) {

if (payment) {
payment.username = await getUsername(payment.user_id);
if (payment.recordedBy) {
payment.recordedByUsername = await getUsername(payment.recordedBy);
}
}

return payment;
Expand Down
8 changes: 4 additions & 4 deletions database/geoword/record-payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ import getSetPayment from './get-set-payment.js';
/**
* @param {String} packetName
* @param {ObjectId} userId
* @param {{ manual?: boolean, amountInCents?: number }} [options]
* @param {{ manual?: boolean, amountInCents?: number, recordedBy?: ObjectId }} [options]
*/
async function recordPayment (packetName, userId, { manual = false, amountInCents = null } = {}) {
async function recordPayment (packetName, userId, { manual = false, amountInCents = null, recordedBy = null } = {}) {
const setPayment = await getSetPayment(packetName);
if (setPayment) {
return await payments.replaceOne(
{ user_id: userId, 'set._id': setPayment.set._id, packet: { $exists: false } },
{ user_id: userId, set: setPayment.set, createdAt: new Date(), manual, amountInCents },
{ user_id: userId, set: setPayment.set, createdAt: new Date(), manual, amountInCents, recordedBy },
{ upsert: true }
);
}

const packet = await packets.findOne({ name: packetName });
return await payments.replaceOne(
{ user_id: userId, 'packet.name': packetName },
{ user_id: userId, packet: { _id: packet._id, name: packetName }, set: packet.set, createdAt: new Date(), manual, amountInCents },
{ user_id: userId, packet: { _id: packet._id, name: packetName }, set: packet.set, createdAt: new Date(), manual, amountInCents, recordedBy },
{ upsert: true }
);
}
Expand Down
2 changes: 1 addition & 1 deletion routes/api/geoword/admin/record-payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ router.put('/', async (req, res) => {
if (!userId) { return res.status(400).send('User not found'); }

const amountInCents = await getCost(packetName);
const result = await recordPayment(packetName, userId, { manual: true, amountInCents });
const result = await recordPayment(packetName, userId, { manual: true, amountInCents, recordedBy: res.locals.userId });

console.log(userId, result);

Expand Down
Loading