I have a module for sales-person, where they can add their own managers. These will be displayed in ag-grid on UI. To add managers I need to select the manager, period start date, and period end date for a given manager. But simultaneously I also need to check the period of each manager, that any of the manager's dates should not overlap each other, based on it there Status is being decided. There can be N number of managers to a salesperson, but there should be only one Active manager to each sales-person at a time.
I am currently facing an issue while comparing the dates of the current manager to each of the given managers in the array.
I decide the status based on the following code:
decideStatus(): string {
let status = '';
let currentDate = new Date();
let fromDate = this.salesPersonForm.value.StartDate;
let tillDate = this.salesPersonForm.value.EndDate;
if(fromDate <= currentDate && (tillDate > currentDate || tillDate == null)) {
return status = 'Active'
} else if(fromDate > currentDate) {
return status = 'Onhold'
} else if(tillDate < currentDate) {
return status = 'Inactive'
}
}
I have the following piece of code for the adding the manager to the grid.
addManagers(){
if(this.salesPersonForm.value.SalesPerson) {
if(this.salesPersonForm.value.StartDate) {
if(this.salesPersonForm.value.EndDate == null || (this.salesPersonForm.value.EndDate > this.salesPersonForm.value.StartDate)) {
let loggedInUser = userName;
let manager = {
Name: someName
FromDate: this.salesPersonForm.value.StartDate
TillDate: this.salesPersonForm.value.EndDate
CreatedAt: new Date()
CreatedBy: loggedInUser,
Status: this.decideStatus()
}
this.managers.push(manager)
} else {
return;
}
} else {
return;
}
} else {
return
}
this.clear();
setTimeout(() => {
this.agGridReference.assignData(this.managers)
}, 200);
}
Idle UseCase: M1 - 01/01/2020 to 31/06/2020 - InActive M2 - 01/08/2020 to 31/12/2021 - InActive M3 - 01/01/2022 to somefuturevalue - Active
If dates are in the future than the current date, it will be considered as on hold.
InValid case: M1 - 01/01/2020 to 31/06/2020 - InActive M2 - 01/05/2020 to 28/02/2021 - InActive M3 - 01/01/2021 to 30/05/2021 - InActive
Ok, so you're going to push new manager into
managersarray only if new manager's interval is not overlapping with any existing manager's interval. Thus the check needs to be made before yourthis.managers.push(manager)line, something like this:replacing
this.managers.push(manager)with:this.compareDates(manager);adding these two methods:
And the helper method to return timestamp of each date in the previous method:
To see it in action: https://stackblitz.com/edit/angular-ivy-j9ldef