Skip to content

Commit f7081e6

Browse files
committed
fix emitting events for credit notas: only emit after db save + filtering only works with objectId.toString()
1 parent e711995 commit f7081e6

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

backend/src/controllers/invoices.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,15 @@ export const createInvoiceController = async (req: ConfacRequest, res: Response)
133133
.find({_id: {$in: linkedInvoiceIds}})
134134
.toArray();
135135

136-
linkedInvoices.forEach(invoice => {
137-
invoice.creditNotas = [...(invoice.creditNotas || []), createdInvoice._id];
138-
emitEntityEvent(req, SocketEventTypes.EntityUpdated, CollectionNames.INVOICES, invoice._id, invoice, 'everyone');
139-
});
140-
141136
await req.db.collection<IInvoice>(CollectionNames.INVOICES).updateMany(
142137
{_id: {$in: linkedInvoiceIds}},
143138
{$push: {creditNotas: createdInvoice._id.toString()}}
144139
);
140+
141+
linkedInvoices.forEach(invoice => {
142+
invoice.creditNotas = [...(invoice.creditNotas || []), createdInvoice._id];
143+
emitEntityEvent(req, SocketEventTypes.EntityUpdated, CollectionNames.INVOICES, invoice._id, invoice, 'everyone');
144+
});
145145
}
146146

147147
return res.send(createdInvoice);
@@ -258,15 +258,15 @@ export const deleteInvoiceController = async (req: ConfacRequest, res: Response)
258258
.find({_id: {$in: linkedInvoiceIds}})
259259
.toArray();
260260

261-
linkedInvoices.forEach(toUpdate => {
262-
toUpdate.creditNotas = toUpdate.creditNotas.filter(x => x !== invoice._id);
263-
emitEntityEvent(req, SocketEventTypes.EntityUpdated, CollectionNames.INVOICES, toUpdate._id, toUpdate, 'everyone');
264-
});
265-
266261
await req.db.collection<IInvoice>(CollectionNames.INVOICES).updateMany(
267262
{_id: {$in: linkedInvoiceIds}},
268263
{$pull: {creditNotas: invoice._id.toString()}}
269264
);
265+
266+
linkedInvoices.forEach(toUpdate => {
267+
toUpdate.creditNotas = toUpdate.creditNotas.filter(x => x !== invoice._id.toString());
268+
emitEntityEvent(req, SocketEventTypes.EntityUpdated, CollectionNames.INVOICES, toUpdate._id, toUpdate, 'everyone');
269+
});
270270
}
271271

272272
await req.db.collection(CollectionNames.INVOICES).findOneAndDelete({ _id: new ObjectID(invoiceId) });

0 commit comments

Comments
 (0)