duncanmcclean / simple-commerce

A simple, yet powerful e-commerce addon for Statamic.
https://statamic.com/addons/duncanmcclean/simple-commerce
Other
146 stars 40 forks source link

Orders linked to deleted customers breaks REST API response #988

Closed jacksleight closed 7 months ago

jacksleight commented 8 months ago

Description

David's run into an issue with the orders REST API endpoint as a customer/user that's linked to an order has been deleted, so it now throws:

Customer [ID] could not be found. in file /vendor/doublethreedigital/simple-commerce/src/Customers/UserCustomerRepository.php on line 23

Could this be handled gracefully so that orders without valid customers just exclude that data?

Steps to reproduce

  1. Create an order
  2. Delete the associated customer
  3. Fetch the orders REST API endpoint

Environment

Statamic Addons: 9 Antlers: runtime Sites: 1 Stache Watcher: Enabled Static Caching: Disabled Version: 4.48.0 PRO

Statamic Addons doublethreedigital/runway: 5.6.1 doublethreedigital/simple-commerce: 5.9.2 duncanmcclean/guest-entries: 3.1.3 jacksleight/statamic-bard-texstyle: 3.1.5 stillat/relationships: 2.1.3 studio1902/statamic-peak-browser-appearance: 3.3.4 studio1902/statamic-peak-commands: 3.2.0 studio1902/statamic-peak-seo: 7.5.0 studio1902/statamic-peak-tools: 4.4.0

duncanmcclean commented 8 months ago

I think I'll do away with the exceptions when "things" can't be found and instead return null.

Although, since it'd be breaking, I'll do it in v6.

jacksleight commented 8 months ago

OK great.

In the meantime do you think a composer patch would be the best workaround? Or is there a better temporary fix?

duncanmcclean commented 8 months ago

If you need a fix right now, then yeah, a composer patch would probably be best. v6 won't be out for another few weeks at this point.

jacksleight commented 8 months ago

Created a custom repository in the end:

namespace App\Customers;

use DoubleThreeDigital\SimpleCommerce\Contracts\Customer;
use DoubleThreeDigital\SimpleCommerce\Customers\UserCustomerRepository as BaseUserCustomerRepository;
use DoubleThreeDigital\SimpleCommerce\Exceptions\CustomerNotFound;

class UserCustomerRepository extends BaseUserCustomerRepository
{
    public function find($id): ?Customer
    {
        try {
            return parent::find($id);
        } catch (CustomerNotFound $e) {
            return null;
        }
    }
}
duncanmcclean commented 7 months ago

This will be fixed in v6 (#990).