Bug 29741

Summary: Add Koha::Patron->safe_to_delete
Product: Koha Reporter: Tomás Cohen Arazi (tcohen) <tomascohen>
Component: Architecture, internals, and plumbingAssignee: Tomás Cohen Arazi (tcohen) <tomascohen>
Status: CLOSED FIXED QA Contact: Testopia <testopia>
Severity: enhancement    
Priority: P5 - low CC: andrew, david, fridolin.somers, jonathan.druart, kyle, martin.renvoize, nick
Version: unspecified   
Hardware: All   
OS: All   
Change sponsored?: --- Patch complexity: ---
Documentation contact: Documentation submission:
Text to go in the release notes:
This enhancement adds a handy method for checking if a patron meets the conditions to be deleted. These conditions are: - Has no linked guarantees - Has no pending debts - Has no current checkouts - Is not the system-configured anonymous user It also adapts the DELETE /patrons route to use the newly introduced Koha::Patron->safe_to_delete method.
Version(s) released in:
22.05.00,21.11.02
Circulation function:
Bug Depends on: 29018    
Bug Blocks: 29742, 29765    
Attachments: Bug 29741: Unit tests
Bug 29741: Add Koha::Patron->safe_to_delete
Bug 29741: (follow-up) Make DELETE /patrons use the new validation method
Bug 29741: Unit tests
Bug 29741: Add Koha::Patron->safe_to_delete
Bug 29741: (follow-up) Make DELETE /patrons use the new validation method
Bug 29741: Unit tests
Bug 29741: Add Koha::Patron->safe_to_delete
Bug 29741: (follow-up) Make DELETE /patrons use the new validation method

Description Tomás Cohen Arazi (tcohen) 2021-12-20 17:16:07 UTC

    
Comment 1 Tomás Cohen Arazi (tcohen) 2021-12-20 17:22:24 UTC
Created attachment 128778 [details] [review]
Bug 29741: Unit tests

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Comment 2 Tomás Cohen Arazi (tcohen) 2021-12-20 17:22:32 UTC
Created attachment 128779 [details] [review]
Bug 29741: Add Koha::Patron->safe_to_delete

This patchset adds a handy method for checking if a patron meets the
conditions to be deleted. This conditions are:

- Has no linked guarantees
- Has no pending debts
- Has no current checkouts
- Is not the system-configured anonymous user

To test:
1. Apply the unit tests patch
2. Run:
   $ kshell
  k$ prove t/db_dependent/Koha/Patron.t
=> FAIL: Of course heh
3. Apply this patch
4. Repeat 2
=> SUCCESS: Tests pass, conditions are validated and the right string is
returned on each case
5. Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Comment 3 Tomás Cohen Arazi (tcohen) 2021-12-20 18:15:01 UTC
Created attachment 128784 [details] [review]
Bug 29741: (follow-up) Make DELETE /patrons use the new validation method

This patch adapts the route so it uses the newly introduced
Koha::Patron->safe_to_delete method.

To test:
1. Run:
   $ kshell
  k$ prove t/db_dependent/api/v1/patrons.t
=> SUCCESS: Tests pass
2. Apply this patch
3. Repeat 1
=> SUCCESS: Tests still pass!
4. Sign off :-D

Note: There's a trivial behavior change, in which the 'anonymous patron'
use case is caugh eariler than the ->delete call. I left the exception
catch block just in case, who knows :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Comment 4 Martin Renvoize (ashimema) 2021-12-20 21:46:31 UTC
Sorry dude, not 100% sure of the construct here... I feel like 'safe_to_delete' should return a Boolean given it's method name?

Perhaps a boolean in scalar context and a Boolean + hash or array of errors as the second slot of an array in list context?
Comment 5 Tomás Cohen Arazi (tcohen) 2021-12-20 21:59:13 UTC
(In reply to Martin Renvoize from comment #4)
> Sorry dude, not 100% sure of the construct here... I feel like
> 'safe_to_delete' should return a Boolean given it's method name?
> 
> Perhaps a boolean in scalar context and a Boolean + hash or array of errors
> as the second slot of an array in list context?

Maybe a Koha::Validation object that validates to a bool, and carries messages in it?
Comment 6 Tomás Cohen Arazi (tcohen) 2021-12-20 22:00:04 UTC
(In reply to Tomás Cohen Arazi from comment #5)
> (In reply to Martin Renvoize from comment #4)
> > Sorry dude, not 100% sure of the construct here... I feel like
> > 'safe_to_delete' should return a Boolean given it's method name?
> > 
> > Perhaps a boolean in scalar context and a Boolean + hash or array of errors
> > as the second slot of an array in list context?
> 
> Maybe a Koha::Validation object that validates to a bool, and carries
> messages in it?

I replicated Koha::Item's to limit the conflict surface, but...hey... Hehe
Comment 7 David Nind 2021-12-21 00:36:59 UTC
Created attachment 128814 [details] [review]
Bug 29741: Unit tests

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: David Nind <david@davidnind.com>
Comment 8 David Nind 2021-12-21 00:37:04 UTC
Created attachment 128815 [details] [review]
Bug 29741: Add Koha::Patron->safe_to_delete

This patchset adds a handy method for checking if a patron meets the
conditions to be deleted. This conditions are:

- Has no linked guarantees
- Has no pending debts
- Has no current checkouts
- Is not the system-configured anonymous user

To test:
1. Apply the unit tests patch
2. Run:
   $ kshell
  k$ prove t/db_dependent/Koha/Patron.t
=> FAIL: Of course heh
3. Apply this patch
4. Repeat 2
=> SUCCESS: Tests pass, conditions are validated and the right string is
returned on each case
5. Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: David Nind <david@davidnind.com>
Comment 9 David Nind 2021-12-21 00:37:08 UTC
Created attachment 128816 [details] [review]
Bug 29741: (follow-up) Make DELETE /patrons use the new validation method

This patch adapts the route so it uses the newly introduced
Koha::Patron->safe_to_delete method.

To test:
1. Run:
   $ kshell
  k$ prove t/db_dependent/api/v1/patrons.t
=> SUCCESS: Tests pass
2. Apply this patch
3. Repeat 1
=> SUCCESS: Tests still pass!
4. Sign off :-D

Note: There's a trivial behavior change, in which the 'anonymous patron'
use case is caugh eariler than the ->delete call. I left the exception
catch block just in case, who knows :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: David Nind <david@davidnind.com>
Comment 10 David Nind 2021-12-21 00:42:00 UTC
I've signed this off, but feel free to change the status if more work is still required (comments 4, 5 and 6).
Comment 11 Martin Renvoize (ashimema) 2021-12-21 07:22:53 UTC
(In reply to Tomás Cohen Arazi from comment #6)
> (In reply to Tomás Cohen Arazi from comment #5)
> > (In reply to Martin Renvoize from comment #4)
> > > Sorry dude, not 100% sure of the construct here... I feel like
> > > 'safe_to_delete' should return a Boolean given it's method name?
> > > 
> > > Perhaps a boolean in scalar context and a Boolean + hash or array of errors
> > > as the second slot of an array in list context?
> > 
> > Maybe a Koha::Validation object that validates to a bool, and carries
> > messages in it?
> 
> I replicated Koha::Item's to limit the conflict surface, but...hey... Hehe

I should have read more code for context, happy to stick with this as there's a precidence in Koha::Item.. I thought I'd seen something somewhere but couldn't place it.  In the transfers work I threw exceptions and allowed a force option to be passed, but the use case isn't the same.  I'd love to see us settle on something consistent.
Comment 12 Tomás Cohen Arazi (tcohen) 2021-12-21 12:56:17 UTC
(In reply to Martin Renvoize from comment #11)
> (In reply to Tomás Cohen Arazi from comment #6)
> > (In reply to Tomás Cohen Arazi from comment #5)
> > > (In reply to Martin Renvoize from comment #4)
> > > > Sorry dude, not 100% sure of the construct here... I feel like
> > > > 'safe_to_delete' should return a Boolean given it's method name?
> > > > 
> > > > Perhaps a boolean in scalar context and a Boolean + hash or array of errors
> > > > as the second slot of an array in list context?
> > > 
> > > Maybe a Koha::Validation object that validates to a bool, and carries
> > > messages in it?
> > 
> > I replicated Koha::Item's to limit the conflict surface, but...hey... Hehe
> 
> I should have read more code for context, happy to stick with this as
> there's a precidence in Koha::Item.. I thought I'd seen something somewhere
> but couldn't place it.  In the transfers work I threw exceptions and allowed
> a force option to be passed, but the use case isn't the same.  I'd love to
> see us settle on something consistent.

I filed bug 29746, and provided an implementation. If I get feedback on that, I can re-do this using that which will be much cleaner. It could be done on a follow-up bug if required, and definitely should cover Koha::Item->safe_to_delete as well.
Comment 13 Jonathan Druart 2022-01-04 15:19:11 UTC
Created attachment 128994 [details] [review]
Bug 29741: Unit tests

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Comment 14 Jonathan Druart 2022-01-04 15:19:16 UTC
Created attachment 128995 [details] [review]
Bug 29741: Add Koha::Patron->safe_to_delete

This patchset adds a handy method for checking if a patron meets the
conditions to be deleted. This conditions are:

- Has no linked guarantees
- Has no pending debts
- Has no current checkouts
- Is not the system-configured anonymous user

To test:
1. Apply the unit tests patch
2. Run:
   $ kshell
  k$ prove t/db_dependent/Koha/Patron.t
=> FAIL: Of course heh
3. Apply this patch
4. Repeat 2
=> SUCCESS: Tests pass, conditions are validated and the right string is
returned on each case
5. Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Comment 15 Jonathan Druart 2022-01-04 15:19:20 UTC
Created attachment 128996 [details] [review]
Bug 29741: (follow-up) Make DELETE /patrons use the new validation method

This patch adapts the route so it uses the newly introduced
Koha::Patron->safe_to_delete method.

To test:
1. Run:
   $ kshell
  k$ prove t/db_dependent/api/v1/patrons.t
=> SUCCESS: Tests pass
2. Apply this patch
3. Repeat 1
=> SUCCESS: Tests still pass!
4. Sign off :-D

Note: There's a trivial behavior change, in which the 'anonymous patron'
use case is caugh eariler than the ->delete call. I left the exception
catch block just in case, who knows :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Comment 16 Fridolin Somers 2022-01-05 23:35:20 UTC
Pushed to master for 22.05, thanks to everybody involved ﷐[U+1F984]﷑
Comment 17 Kyle M Hall (khall) 2022-01-07 11:28:22 UTC
Pushed to 21.11.x for 21.11.02
Comment 18 Kyle M Hall (khall) 2022-01-07 11:29:20 UTC
Backported as part of a combo with 29018. I'd rather not leave 21.11 in a half-finished state for this issue.
Comment 19 Andrew Fuerste-Henry 2022-01-11 14:26:19 UTC
Enhancement, not pushed to 21.05.x