|
| 1 | +# Part of OpenG2P. See LICENSE file for full copyright and licensing details. |
| 2 | + |
| 3 | + |
| 4 | +from odoo import api, models |
| 5 | + |
| 6 | + |
| 7 | +class G2PProgramMembership(models.Model): |
| 8 | + _inherit = "g2p.program_membership" |
| 9 | + |
| 10 | + def _push_to_rabbitmq(self): |
| 11 | + """Push the record to RabbitMQ after applying JQ transformation.""" |
| 12 | + configs = self.env["g2p.datashare.config.rabbitmq"].search( |
| 13 | + [("active", "=", True), ("data_source", "=", "beneficiary_registry")] |
| 14 | + ) |
| 15 | + for rec in self: |
| 16 | + if rec.is_registrant: |
| 17 | + rec_data = rec.read()[0] |
| 18 | + rec_data.update( |
| 19 | + {"program_id": self.env["g2p.program"].browse(rec_data["program_id"][0]).read()[0]} |
| 20 | + ) |
| 21 | + for config in configs: |
| 22 | + self.process_reg_id(config.id_type, rec_data) |
| 23 | + transformed = config.transform_data(rec_data) |
| 24 | + if transformed is not None: |
| 25 | + config.publish(transformed) |
| 26 | + |
| 27 | + def process_reg_id(self, id_type, rec_data): |
| 28 | + reg_id = self.env["g2p.reg.id"].search( |
| 29 | + [("id_type", "=", id_type.id), ("partner_id", "=", rec_data["partner_id"][0])], limit=1 |
| 30 | + ) |
| 31 | + if reg_id: |
| 32 | + return rec_data.update({"reg_id_value": reg_id.value}) |
| 33 | + |
| 34 | + @api.model_create_multi |
| 35 | + def create(self, vals_list): |
| 36 | + """Override create to push new records to RabbitMQ.""" |
| 37 | + records = super().create(vals_list) |
| 38 | + records._push_to_rabbitmq() |
| 39 | + return records |
| 40 | + |
| 41 | + def write(self, vals): |
| 42 | + """Override write to push updated records to RabbitMQ.""" |
| 43 | + res = super().write(vals) |
| 44 | + self._push_to_rabbitmq() |
| 45 | + return res |
0 commit comments