diff --git a/energy_communities/services/crm_lead_service.py b/energy_communities/services/crm_lead_service.py index 77fec5eb2a33c4f4ca2915468d0ab29778bc9ea9..854028f990652b65ad684b11cb73d2685a97113c 100644 --- a/energy_communities/services/crm_lead_service.py +++ b/energy_communities/services/crm_lead_service.py @@ -17,32 +17,58 @@ class CRMLeadService(Component): create_dict = super().create(params) crm_lead = json.loads(create_dict.response[0].decode("utf-8")) + # get utm source from payload + target_source_xml_id = self._get_source_xml_id(params) + + if target_source_xml_id: + # setup utm source on crm lead + crm_lead_id = crm_lead.get('id', False) + self._setup_lead_utm_source(crm_lead_id, target_source_xml_id) + + # select autoresponder notification id based on utm source + template_external_id = self._get_autoresponder_email_template( + target_source_xml_id) + + # send auto responder email and notify admins + email_values = {"email_to": params["email_from"]} + if template_external_id: + template = self.env.ref( + "energy_communities.{}".format(template_external_id) + ) + template.sudo().send_mail( + crm_lead["id"], email_values=email_values) + # Add template to chatter message + self.env["crm.lead"].post_template_to_chatter(template.id) + return crm_lead + + def _setup_lead_utm_source(self, lead_id, source_xml_id): + lead = self.env['crm.lead'].browse(lead_id) + if lead: + utm_source = self.env.ref('energy_communities.'+source_xml_id) + lead.write({ + 'source_id': utm_source.id + }) + + def _get_source_xml_id(self, params): metadata = params["metadata"] target_source_xml_id = None for data in metadata: if data["key"] == "source_xml_id": target_source_xml_id = data["value"] + return target_source_xml_id + def _get_autoresponder_email_template(self, source_xml_id): template_external_id = None - if target_source_xml_id == "ce_source_creation_ce_proposal": + if source_xml_id == "ce_source_creation_ce_proposal": template_external_id = "email_templ_lead_ce_creation_receipt_confirm_id" - elif target_source_xml_id == "ce_source_existing_ce_contact": + elif source_xml_id == "ce_source_existing_ce_contact": template_external_id = "email_templ_lead_request_contact_confirm_id" - elif target_source_xml_id == "ce_source_existing_ce_info": + elif source_xml_id == "ce_source_existing_ce_info": template_external_id = "email_templ_lead_request_ce_news_confirm_id" - elif target_source_xml_id == "ce_source_future_location_ce_info": + elif source_xml_id == "ce_source_future_location_ce_info": template_external_id = ( "email_templ_lead_request_advise_future_ce_confirm_id" ) - elif target_source_xml_id == "ce_source_general_info": + elif source_xml_id == "ce_source_general_info": template_external_id = "email_templ_lead_request_platform_news_confirm_id" - - email_values = {"email_to": params["email_from"]} - - if template_external_id: - template = self.env.ref( - "energy_communities.{}".format(template_external_id) - ) - template.sudo().send_mail(crm_lead["id"], email_values=email_values) - # Add template to chatter message - self.env["crm.lead"].post_template_to_chatter(template.id) + return template_external_id