I need to change the status of a property from true to false depending on whether in the property called “outstandingBalance” its value is equal to 0. For now I receive the data to make the change of “outstandingBalance” correctly but I would like it to be evaluated if its value it goes to 0 to be able to make the change automatically.
This is my code:
const previousBalance = parseFloat(lastPayment.previousBalance);
const newAmount = parseFloat(data.adjustmentAmount);
let newOutstandingBalance = 0;
newOutstandingBalance = parseFloat(previousBalance) + parseFloat(newAmount);
const updateOutstandingBalanceSale = await Sale.findOneAndUpdate( {'_id': req.creditSaleToAdjust.saleId },{
$set: {
'outstandingBalance': newOutstandingBalance
},
});
In my model I have the status property that I need to change if outstandingBalance has the value of 0
My model:
const SaleSchema = Schema (
{
saleId: {
type: Number,
unique: true
},
user:{
type: Schema.Types.ObjectId,
ref:'Users',
required: [true, 'El Usuario es obligatorio']
},
clientId:{
type: Schema.Types.ObjectId,
ref:'Client',
required: [true, 'El Cliente es obligatorio']
},
notes: {
type: String,
maxlength:200,
},
subtotal: {
type: Number,
default: 0,
required: [true, 'El Subtotal es obligatorio']
},
discount: {
type: Number,
default:0
},
tax: {
type: Number,
default:0
},
totalBeforeTax: {
type: Number,
default: 0,
required: [true, 'El Total sin Impuestos es obligatorio']
},
total: {
type: Number,
default: 0,
required: [true, 'El Total es obligatorio']
},
paymentType: {
type: String,
required: true,
enum: ['CASH', 'CREDIT', 'TRANSFER']
},
outstandingBalance: {
type: Number,
default: 0,
required: [true, 'El Adeudo Pendiente es obligatorio']
},
createdAt: {
type: Date,
default: Date.now
},
status: {
type: Boolean,
default: true,
required: [true, 'El Estatus es obligatorio']
}
}
);
Thanks for your help.