Skip to Content
Menu
This question has been flagged

Hello,


I need your help please, I have been trying to solve this issue by using methods like commit(), flush(), etc., without success.


To give some context, I have a MercadoLibre API and I have set up a webhook in Odoo using controllers. This webhook receives notifications that contain a field called "resource" which I use as an identifier. MercadoLibre sends a notification every time it makes an update, no matter how small. For example, a sale on MercadoLibre can generate 30 notifications where all of them share the same resource.


My goal is to make sure that every time a new notification arrives at Odoo via the webhook, it first checks if the resource already exists. If it does, it should update the existing record, if it does not, it should create a new record. I have provided a code snippet that should work normally:


@http.route('/ml_notifications', type='json', auth='public', methods=['POST'], cors='*', csrf=False)
def ml_notifications(self):
	​data_raw = request.jsonrequest
	​notification = request.env['ml.notifications'].with_user(SUPERUSER_ID)
	​data = self.process_raw(data_raw)
	​notification_id = notification.search([('resource', '=', data_raw['resource'])])
	​if notification_id:
		​notification_id.write(data)
	​else:
​notification_id = notification.create(data)


The problem I am facing is that the code is not consistently respecting the condition, which can lead to multiple notifications with the same resource. In theory, this should not happen because the code checks if the notification already exists before deciding to update or create a new record. 


Even though I have tried to use the commit() method after creating a new notification, the issue persists. I am not sure if this is a problem caused by multiple processes and how it could be avoided, or if there is something else causing it.


I hope someone can help me with this problem.


Avatar
Discard
Related Posts Replies Views Activity
2
Aug 23
4711
0
Aug 22
40
2
May 22
2217
2
Dec 24
3122
1
Dec 24
350