Spaces:
Paused
Paused
File size: 2,829 Bytes
e3fefe8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | const { db } = require('../config/firebase');
const COMMISSION_RATE = 0.10;
function toMoney(value) {
return Math.round(Number(value || 0) * 100) / 100;
}
async function deductCommission({ professionalId, bookingId, amount }) {
if (!professionalId || !bookingId || amount === undefined) {
return {
success: false,
statusCode: 400,
message: 'professionalId, bookingId, and amount are required.',
};
}
const gross = Number(amount);
if (!Number.isFinite(gross) || gross <= 0) {
return {
success: false,
statusCode: 400,
message: 'amount must be a valid positive number.',
};
}
const bookingRef = db.ref(`bookings/${bookingId}`);
const bookingSnap = await bookingRef.once('value');
if (!bookingSnap.exists()) {
return {
success: false,
statusCode: 404,
message: 'Booking not found.',
};
}
const booking = bookingSnap.val() || {};
if (booking.status !== 'completed') {
return {
success: false,
statusCode: 409,
message: 'Commission can be deducted only after booking is completed.',
};
}
if (booking.commissionDeducted === true) {
return {
success: true,
statusCode: 200,
data: {
professionalId,
bookingId,
grossAmount: gross,
commission: Number(booking.commissionAmount || 0),
walletBalance: Number((await db.ref(`professionals/${professionalId}`).once('value')).val()?.wallet ?? 0),
},
};
}
const commission = toMoney(gross * COMMISSION_RATE);
const proRef = db.ref(`professionals/${professionalId}`);
const proSnap = await proRef.once('value');
if (!proSnap.exists()) {
return {
success: false,
statusCode: 404,
message: 'Professional not found.',
};
}
const pro = proSnap.val() || {};
const currentWallet = Number(pro.wallet ?? pro.walletBalance ?? 5000);
const newWallet = toMoney(Math.max(0, currentWallet - commission));
await proRef.update({
wallet: newWallet,
walletBalance: newWallet,
updatedAt: Date.now(),
});
const txRef = db.ref('transactions').push();
await txRef.set({
id: txRef.key,
professionalId,
bookingId,
type: 'commission_deduction',
amount: commission,
grossAmount: gross,
status: 'completed',
timestamp: Date.now(),
});
await bookingRef.update({
commissionAmount: commission,
professionalEarnings: toMoney(gross - commission),
commissionDeducted: true,
paymentStatus: 'commission_deducted',
updatedAt: Date.now(),
});
return {
success: true,
statusCode: 200,
data: {
professionalId,
bookingId,
grossAmount: gross,
commission,
walletBalance: newWallet,
},
};
}
module.exports = {
COMMISSION_RATE,
deductCommission,
};
|