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

Strange behaviour with findByCustomerId in contact entity. #273

Open
ShafiKh opened this issue Nov 11, 2022 · 1 comment
Open

Strange behaviour with findByCustomerId in contact entity. #273

ShafiKh opened this issue Nov 11, 2022 · 1 comment

Comments

@ShafiKh
Copy link

ShafiKh commented Nov 11, 2022

Package version: ^0.15.0
Laravel/lumen version: 5.8

Expected behavior

Moneybird API needs to return 404 if a contact doesn’t exist upon checking, if 404 then create a contact for the consumer otherwise return an already existing contact for that consumer.

Current behavior

Moneybird API returns 404 when checking a contact via consumer ID but when we try to create a contact then it says it already exists.

Sample code

          try {
                $checkCustomer = $this->findUser($user);
                if($checkCustomer) {
                    $user->money_bird_id = $checkCustomer->id;
                    $user->save();
                    Log::info("customer already exists" .json_encode($checkCustomer));
                    return;
                }
            } catch (ApiException $exception){
                if($exception->getCode() == 404){
                    $company = Company::find($user->company_id);
                    Log::info("customer not found, creating....:");
                    return $moneybird->contact([
                        'customer_id'  => $user->id,
                        'company_name' => is_null($company) ? '' : $company->name,
                        'firstname'    => $user->first_name,
                        'lastname'     => $user->last_name,
                        'attention'    => $user->name,
                        'address1'     => "{$user->address->street} {$user->address->suffix}",
                        'zipcode'      => $user->address->postal_code,
                        'city'         => $user->address->city,
                        'country'      => $user->address->country,
                        'email'        => $user->email
                    ]);
                }
            }
            
            public function findUser($user)
            {
                $moneybird = new Moneybird($this->api->connection);

                return $moneybird->contact()
                        ->findByCustomerId($user->id);
             }

Note:

This is happening for some of the consumers not all and due to this we are getting failed jobs, I have replicated this on my local with moneybird sandbox account.

Seems like something is wrong in the package's codebase, or maybe the moneybird APIs,

Anyone came across such a thing?

@martijnimhoff
Copy link

martijnimhoff commented Mar 11, 2023

I'm having an issue that might be related. The get requests seems to be cached for 1 minute or something.

My code is a bit different. I'm getting a contact by it's email. If the email is unknown, then I create it:

$contacts = $moneybird->contact()->get(['query' => $email]);

if (count($contacts) === 0) {
  return $moneybird->contact([
          'firstname' => $user->name,
          'send_invoices_to_email' => $email,
          'send_estimates_to_email' => $email,
          'delivery_method' => 'Manual',
          'company_name' => $user->company,
      ])
      ->save();
}

If i run this code twice (via a command on the CLI) then the $contacts var will be an empty array in BOTH cases. Which is weird. I've tested this endpoint directly with cURL and there the contacts get request does return a contact.

EDIT: If I do the get request via cURL before the contact create and THEN do the get request again. Then I get an empty array. So I think Moneybird is caching responses when the request is the same/similar. I'm contacting Moneybird. I'll keep you posted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants