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

Refactor amounts calculation #1188

Merged
merged 15 commits into from
Nov 12, 2024
Merged

Refactor amounts calculation #1188

merged 15 commits into from
Nov 12, 2024

Conversation

pskl
Copy link
Collaborator

@pskl pskl commented Nov 1, 2024

Here be dragons 💀

Time to clean the way we calculate amounts, mere mortal

  • Kill PfmpAmountCalculator concern (-> PfmpManager) 🎯
  • Add new API for updating day_count 🎯
  • Kill after_save callback for Amount recalculation 🎯
  • Add validation on amount not being negative (😮)
  • Remove state transition on after save too
  • Add lock on Amounts calculation (prevent possible race)
  • Encapsulate calculation methods as private methods on the Manager object

About the removal of after_save callback check

Why remove it? Because it sucks, amounts ceilings can be exceeded by spamming requests, rebalancing could happily trigger other rebalancing (the fact that it doesnt currently is pure fluke), highway to Callback Hell 👿 and also just to simplify the mental model around this unnecessarily complex process.

Replaced by what? A new method on the PfmpManager called update -> PfmpManager.new(pfmp).update(day_count: X) with no side effects other than the one we want which is rebalancing of other pfmps for that same mef.

Retrocompatibility issues

What are the entry points of those callbacks? -> AFAIK:

  • Pfmp controller update -> replace with new API
  • Specs trying to check something -> replace with new API
  • In case of Pfmp destruction -> maybe change condition

Risks

  • Not enough tests?
  • Potential side effects with callbacks of callbacks that might go unnoticed?
  • Potential negative amounts Pfmps: I reset the ones in the db to 0 (77 records)

…ve the concern whilst keeping the callback (first step)
@pskl pskl linked an issue Nov 1, 2024 that may be closed by this pull request
@pskl pskl force-pushed the chore/refactor-amount-update branch 6 times, most recently from 9528b95 to 4fc0d77 Compare November 1, 2024 14:48
@pskl pskl force-pushed the chore/refactor-amount-update branch from 4fc0d77 to ee82f9f Compare November 1, 2024 15:15
@pskl pskl force-pushed the chore/refactor-amount-update branch from 15f9235 to 563f0da Compare November 4, 2024 12:00
@pskl pskl force-pushed the chore/refactor-amount-update branch from 563f0da to 60799fe Compare November 4, 2024 12:09
@pskl pskl force-pushed the chore/refactor-amount-update branch from 19cfc6a to e18305d Compare November 4, 2024 12:31
@pskl pskl force-pushed the chore/refactor-amount-update branch from a2fae19 to 30437f4 Compare November 4, 2024 15:24
@pskl pskl force-pushed the chore/refactor-amount-update branch 3 times, most recently from 6893ca7 to 6a0173d Compare November 5, 2024 16:43
@pskl pskl force-pushed the chore/refactor-amount-update branch from 6a0173d to 03ad732 Compare November 5, 2024 17:08
@pskl pskl force-pushed the chore/refactor-amount-update branch from c91024d to 72dfac2 Compare November 7, 2024 14:53
@pskl pskl force-pushed the chore/refactor-amount-update branch from 72dfac2 to d60db59 Compare November 7, 2024 15:14
@pskl pskl requested a review from tnicolas1 November 11, 2024 17:46
@@ -22,7 +22,7 @@ def edit; end
def create
@pfmp = Pfmp.new(pfmp_params.merge(schooling: @schooling))

if @pfmp.save
if PfmpManager.new(@pfmp).update(pfmp_params)
Copy link
Collaborator

Choose a reason for hiding this comment

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

La fonction 'create' et 'update' n'avaient pas la même condition initialement, mais maintenant elles vérifient toutes les 2 'PfmpManager.new(@PFMP).update(pfmp_params)', est-ce normal ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oui normalement c'est équivalent parce que #update et #save retournent true quand les validations sont passées et false quand ca n'est pas le cas. Et ici on veut juste déclencher explicitement le recalcul.

@pskl pskl force-pushed the chore/refactor-amount-update branch from 726b420 to d96b7c6 Compare November 12, 2024 13:24
@pskl pskl force-pushed the chore/refactor-amount-update branch 3 times, most recently from 3a2e779 to d826392 Compare November 12, 2024 16:54
@pskl pskl force-pushed the chore/refactor-amount-update branch from d826392 to fafc857 Compare November 12, 2024 16:55
@pskl pskl merged commit a1a4a55 into main Nov 12, 2024
5 checks passed
@pskl pskl deleted the chore/refactor-amount-update branch November 12, 2024 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refacto calcul des montants
2 participants