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

fix: fix crm sync #10747

Merged
merged 5 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions lib/ProductOpener/CRM.pm
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ sub sync_org_with_crm($org_ref, $salesperson_user_id) {
my $my_admin = retrieve_user($salesperson_user_id);
$log->debug("store_org", {myuser => $my_admin}) if $log->is_debug();

$partner_id ||= $company_id;
my $opportunity_id
= create_onboarding_opportunity("$org_ref->{name} - new", $company_id, $partner_id, $my_admin->{email});
defined $opportunity_id or die "Failed to create opportunity";
Expand All @@ -147,10 +148,10 @@ sub sync_org_with_crm($org_ref, $salesperson_user_id) {
}
1;
} or do {
$org_ref->{valid_org} = 'unreviewed';
$log->error("store_org", {error => $@}) if $log->is_error();
return;
};
return;
return 1;
}

=head2 find_or_create_contact ($user_ref)
Expand All @@ -173,8 +174,6 @@ sub find_or_create_contact($user_ref) {
my $contact_id = find_contact($user_ref);
if (defined $contact_id) {
return if not link_user_with_contact($user_ref, $contact_id);
add_category_to_partner($user_ref, 'Producer');
update_partner_country($user_ref);
}
else {
$contact_id = create_contact($user_ref);
Expand Down Expand Up @@ -209,6 +208,8 @@ sub link_user_with_contact($user_ref, $contact_id) {
[$contact_id],
{
x_off_username => $user_ref->{userid},
email => $user_ref->{email},
phone => $user_ref->{phone},
category_id => [[$commands{link}, $crm_data->{category}{Producer}]],
$country_id ? (country_id => $country_id) : ()
}
Expand Down Expand Up @@ -347,8 +348,6 @@ sub find_or_create_company($org_ref, $contact_id = undef) {
my $company_id = find_company($org_ref, $contact_id);
if (defined $company_id) {
return if not link_org_with_company($org_ref, $company_id);
add_category_to_partner($org_ref, 'Producer');
update_partner_country($org_ref);
}
else {
$company_id = create_company($org_ref);
Expand Down Expand Up @@ -383,6 +382,9 @@ sub link_org_with_company($org_ref, $company_id) {
{
x_off_org_id => $org_ref->{org_id},
category_id => [[$commands{link}, $crm_data->{category}{Producer}]],
phone => $org_ref->{commercial_service}{phone},
email => $org_ref->{commercial_service}{email},
website => $org_ref->{link},
x_off_main_contact => $user_ref->{crm_user_id},
$country_id ? (country_id => $country_id) : ()
}
Expand Down Expand Up @@ -480,9 +482,9 @@ sub create_company ($org_ref) {

my $company = {
name => $org_ref->{name},
phone => $org_ref->{phone},
email => $org_ref->{email},
website => $org_ref->{website},
phone => $org_ref->{commercial_service}{phone},
email => $org_ref->{commercial_service}{email},
website => $org_ref->{link},
category_id => [$crm_data->{category}{Producer}], # "Producer" category id in Odoo
is_company => 1,
x_off_org_id => $org_ref->{org_id},
Expand Down
13 changes: 7 additions & 6 deletions lib/ProductOpener/Orgs.pm
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,11 @@ sub store_org ($org_ref) {
my $previous_org_ref = retrieve("$BASE_DIRS{ORGS}/$org_ref->{org_id}.sto");

if ( (defined $previous_org_ref)
&& $previous_org_ref->{valid_org} ne 'accepted'
&& $org_ref->{valid_org} eq 'accepted')
&& ($previous_org_ref->{valid_org} ne 'accepted')
&& ($org_ref->{valid_org} eq 'accepted')
&& (not sync_org_with_crm($org_ref, $User_id)))
{
sync_org_with_crm($org_ref, $User_id);
$org_ref->{valid_org} = 'unreviewed';
}

if ( defined $org_ref->{crm_org_id}
Expand All @@ -185,13 +186,13 @@ sub store_org ($org_ref) {
$org_ref->{main_contact} = $previous_org_ref->{main_contact};
}

# Store to file
store("$BASE_DIRS{ORGS}/" . $org_ref->{org_id} . ".sto", $org_ref);

# Store to MongoDB
my $orgs_collection = get_orgs_collection();
$orgs_collection->replace_one({"org_id" => $org_ref->{org_id}}, $org_ref, {upsert => 1});

# Store to file
store("$BASE_DIRS{ORGS}/" . $org_ref->{org_id} . ".sto", $org_ref);

return;
}

Expand Down
6 changes: 5 additions & 1 deletion scripts/migrations/2024_08_validate_and_sync_crm_existing_orgs.pl
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
use ProductOpener::Orgs qw( list_org_ids retrieve_org store_org send_rejection_email);
use ProductOpener::CRM qw( init_crm_data sync_org_with_crm );
use ProductOpener::Store qw( retrieve store );
use ProductOpener::Data qw( :all );
use Log::Any::Adapter 'TAP';
use Encode;

binmode(STDOUT, ":encoding(UTF-8)");
Expand Down Expand Up @@ -60,7 +62,7 @@
open($checkpoint, '+<:encoding(UTF-8)', $checkpoint_file) or die "Could not open file: $!";
my %orgs_processed = map {chomp; $_ => 1} <$checkpoint>;

foreach my $org_id (keys %orgs_to_accept) {
foreach my $org_id (sort(list_org_ids())) {

$org_id = decode utf8 => $org_id;
my $org_ref = retrieve_org($org_id);
Expand Down Expand Up @@ -91,6 +93,8 @@
}

store("$BASE_DIRS{ORGS}/" . $org_ref->{org_id} . ".sto", $org_ref);
my $orgs_collection = get_orgs_collection();
$orgs_collection->replace_one({"org_id" => $org_ref->{org_id}}, $org_ref, {upsert => 1});

print $checkpoint "$org_id\n";
}
Loading