Skip to content

When Resource Booking is removed all related Work Periods and Payments should be removed #203

Open
@maxceem

Description

@maxceem

If we delete Resource Booking we should remove all related Work Periods and payments related to these WorkPeriods.
But we would like to make it automatic if possible. So we don't have to implement this logic in every service or every place.

Automatic deletion

  • To avoid any possible data inconsistency, we would like to implement it on the model level. When we destroy (soft-delete) any ResourceBooking, all associated WorkPeriods should be also deleted automatically. And all the WorkPeriodPayemnts associated with these WorkPeriods also deleted automatically. For example, we could utilize afterDestroy or beforeDestroy hook.
  • This should happen using a DB transaction to ensure data consistency. So if it failed to delete some WorkPeriodPayment for some WorkPeriod then ResourceBooking deletion should also get reverted and deletion of WorkPeriods get reverted.
  • 🤔 The trickiest part is: after deleting ResourceBooking with all WorkPeriods and all WorkPeriodPayments we have to postEvent to Kafka for each deleted object (RB, WP, WPP) so other services like ES Processor know about it. Is it possible to achieve this in an elegant way?
  • The same should work when delete WorkPeriod directly, we should not write custom logic in service https://github.com/topcoder-platform/taas-apis/blob/dev/src/services/WorkPeriodService.js#L337-L341 it should work automatically on the model level as part of all the logic above. And we again should take care about sending Kafka events for deleted payments await Promise.all(workPeriod.payments.map(({ id }) => helper.postEvent(config.TAAS_WORK_PERIOD_PAYMENT_DELETE_TOPIC, { id }))) and deleted Work Period await helper.postEvent(config.TAAS_WORK_PERIOD_DELETE_TOPIC, { id }). Such events should be send only if everything was deleted good.

Automatic constraints

  • We have some important constrain: we cannot delete WorkPeriod which is partially or completely paid (which actually means that there is some WorkPeriodPayment with status completed. At the moment we implemented it on the service level in a bit different way: WP delete endpoint and RB delete endpoint.
  • Can we put this in the model level?
    • remove these checks for WorkPeriod.paymentStatus
    • instead of it WorkPeriodPayment model should not allow deleting payments in completed status automatically (using beforeDestroy hook and throw an error)
    • when we would try to delete WorkPeriod it would try to delete WorkPeriodPayment and if there is at least one completed payment it would throw error, and as a result we could not delete neither WorkPeriod not payments
    • the same for Resrouce Booking: we try to delete Resource Bookings, it tried to delete associated Work Periods it tires to delete Work Periods Payments, and if got error because some Work Periods Payment is completed then everything is reverted and user gets error

Unit Tests

  • Write good unit tests to check all this logic (like below or more):
    • should successfully delete Resource Booking with all associated Work Periods and corresponding Work Period Payments
      • check that all objects are really deleted in DB
      • check that events are posted to Kafka for all objects
    • should successfully delete Work Period with all associated Work Period Payments
      • check that all objects are really deleted in DB
      • check that events are posted to Kafka for all objects
    • should fail to delete Resource Booking with all associated Work Periods and corresponding Work Period Payments if there is a Work Period Payment with status "completed"
      • check that all data is reverted or not deleted in DB
      • check that events are NOT posted to Kafka for ANY objects

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions