I have a subscription-based application where users will be registered and get paid for the work they will do on daily basis. There are certain rules for payment.
- The user will only be able to withdraw the amount every 30 days from the day they have registered.
- Registration is subscription-based so there will be an expiry date for every user's plan.
- I need to calculate two things. Amount earned every 30 days until today and remaining amount for an incomplete month until today (current month or remaining days that is less than 30)
import {differenceInDays, parseISO} from "date-fns";
let differenceDays = differenceInDays(new Date(), parseISO(startDate)) // subscription start date
let multiplier = parseInt(differenceDays / 30) // integer value
let remainder = differenceDays % 30 // remainig days from above divition
This is as far as I could manage to go and can't seem to figure out a way to do it. I am using a timestamp to save each transaction and data is coming from PostgreSQL table to node.
The data block is something like this.
let data = [
{date:'2021-05-26 18:36:55.767', income:888, start_date:'2021-05-26 18:36:55.767', end_date:'2021-05-26 18:36:55.767'},
{date:'2022-05-01 18:36:55.767', income:200, ...}, // same as above
{date:'2021-07-26 18:36:55.767', income:888, ...},
{date:'2023-01-26 18:36:55.767', income:300, ...},
{date:'2021-02-26 18:36:55.767', income:888, ...},
{date:'2023-08-26 18:36:55.767', income:444, ...},
{date:'2023-09-26 18:36:55.767', income:888, ...},
{date:'2024-01-26 18:36:55.767', income:541, ...},
{date:'2024-12-26 18:36:55.767', income:888, ...},
{date:'2024-07-26 18:36:55.767', income:336, ...},
{date:'2025-01-26 18:36:55.767', income:888, ...},
{date:'2025-07-26 18:36:55.767', income:519, ...},
] // for demonstration purpose only
// start_date => subscription start date
// end_date => subscription expiry date
Data is coming from the PostgreSQL database having two table transactions joined with subscriptions and this is joined of those two tables.
By every 30 days I mean, upon completion of 30 days they will get the amount they are entitled to. this amount can be withdrawn any time after this 30-day period.
30 days => 4360 // Amount earned in first 30 days
31 - 59 days => they can withdraw the above amount and this monthly amount is accumulated in their account.
60 days => 3950 // Amount earned in 2nd 30 days
61 - 89 days => 4360 + 3950 can be withdrawn if user haven't already withdraw 4360