Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create opportunity upon org validation in CRM #10222

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

4nt0ineB
Copy link
Collaborator

@4nt0ineB 4nt0ineB commented May 3, 2024

What

When an organization is validated on the pro platform, associated contact and org are created in the Odoo CRM and linked to a new opportunity.

Related issue(s)

@4nt0ineB 4nt0ineB added ✨ Feature Features or enhancements to Open Food Facts server 🏭 Producers Platform - Odoo OdooCRM is used by the Open Food Facts team to manage producers (and more) relationships labels May 3, 2024
@4nt0ineB 4nt0ineB self-assigned this May 3, 2024
@github-actions github-actions bot added dependencies Pull requests that update a dependency file 🏭 Producers Platform https://wiki.openfoodfacts.org/Platform_for_producers 🏭 Orgs labels May 3, 2024
@codecov-commenter
Copy link

codecov-commenter commented May 3, 2024

Codecov Report

Attention: Patch coverage is 15.11628% with 73 lines in your changes are missing coverage. Please review.

Project coverage is 49.75%. Comparing base (dc04d18) to head (5d2293a).
Report is 301 commits behind head on main.

Files Patch % Lines
lib/ProductOpener/CRM.pm 15.71% 59 Missing ⚠️
lib/ProductOpener/Orgs.pm 12.50% 13 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #10222      +/-   ##
==========================================
+ Coverage   49.54%   49.75%   +0.20%     
==========================================
  Files          67       72       +5     
  Lines       20650    21119     +469     
  Branches     4980     5057      +77     
==========================================
+ Hits        10231    10507     +276     
- Misses       9131     9317     +186     
- Partials     1288     1295       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

if not defined add_contact_to_company($crm_user_id, $crm_org_id);

die "Failed to create opportunity"
if not defined create_opportunity($user_ref, $org_ref);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe store the opportunity id as well, so that we can update it later, for instance if the org imports products.

Copy link
Member

@alexgarel alexgarel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work !

I did a quick review and added a comment !

};

# find country code id in odoo
my $user_country_code = uc $country_codes_reverse{$user_ref->{country}} || 'EN';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer the use of parenthesis for every function call (clearer for many contributors)


=cut

sub create_contact ($user_ref) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine it comes with another PR but it would be important to check user existence for the email in odoo.

Duplicates on contact are really annoying to the producers team.

Comment on lines +134 to +139
my $odoo_lang = odoo('res.lang', 'search_read', [[['iso_code', '=', $user_ref->{preferred_language}]]], { 'fields' => ['code']});
my $found_lang = $odoo_lang->[0];
$contact->{lang} = $found_lang->{code} // 'en_US'; # default to english
if(defined $found_lang->{id}) {
$contact->{x_off_languages} = [$found_lang->{id}];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a problem with Odoo because it will refuse to display a non-activated language in many to many fields (that's deep in the code), that's why I also created the x_off_languages_str field to store language values directly

=cut

sub create_opportunity ($user_ref, $org_ref) {
my $opportunity_id = odoo('crm.lead', 'create', [{name => "$org_ref->{name} - new org", partner_id => $user_ref->{crm_user_id}}]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to check titles, etc with Manon

Comment on lines +61 to +62
use Data::Dumper;
use feature 'say';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Data::Dumper and say aren't actually used, right? If they are, it might be better to use a proper $log method call instead


sub odoo(@params) {
if(not defined $xmlrpc) {
my $api_url = $ENV{ODOO_CRM_URL} . '//xmlrpc/2/';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't really use $ENV for dynamic configuration like this, outside of Config2_docker.pm. The "proper" way to add optional odoo configuration is probably adding it to Config.pm and to Config2.pm

$xmlrpc = XML::RPC->new($api_url . 'common');
};
if($@) {
$log->warn("odoo", {error => $@, reason => "Could not connect to Odoo CRM"});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For local development or some test environments, we won't always have "odoo" installed or configured. It might be a good idea to guard against the object being created again and again if no odoo connection is configured. For example during integration tests, this could avoid some noisy error logging (and possibly reduce execution duration)

Copy link

sonarcloud bot commented May 14, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@github-actions github-actions bot added the 💥 Merge Conflicts 💥 Merge Conflicts label May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file ✨ Feature Features or enhancements to Open Food Facts server 💥 Merge Conflicts 💥 Merge Conflicts 🏭 Orgs 🏭 Producers Platform - Odoo OdooCRM is used by the Open Food Facts team to manage producers (and more) relationships 🏭 Producers Platform https://wiki.openfoodfacts.org/Platform_for_producers
Projects
Status: 📋 Backlog
Status: In progress
6 participants