I've a requirement for leave management system where user can apply for leaves depending upon their allotment and using Angular with ASP.NET Web Api. Example:
UserId - Leave - Allotment - Year
1001 - Casual - 20 - 2019
1001 - Sick - 20 - 2019
1002 - Annual - 10 - 2019
This is pretty basic. So what I am doing whenever a user tries to apply for leave, the system gets total days from start and end date, finally checks the allotment with respect to the total days. In the front-end, user has to select a type, for which the specific allotment no should be deducted as follows:
<label for="leaveType">Leave Type</label>
<select [(ngModel)]="leaveType" [ngModelOptions]="{standalone: true}" class="form-control">
<option value="">Please select leave</option>
<option *ngFor="let value of leaveType" [ngValue]="value.LeaveType">
{{value.LeaveType}}
</option>
</select>
I am getting the leave allotment details using ASP.NET Web Api for a specific user perfectly and this is what I did:
leaveType: string;
chkLeave: number = 0;
userLeave: any;
this.dataservice.GetLeaveInfo(selectedValue).subscribe(result => { //selectedValue is another drop down value where users are being selected with user id
this.userLeave = result;
}, error => console.error(error));
Then finally in another method using Angular, I try to verify whether there is available leave allotment for that specific leave type like this:
chkAllotment() {
var startDate = new Date(Result[0]);
var endDate = new Date(Result[1]);
const diff = endDate.getDate() - startDate.getDate();
debugger;
for(let i = 0; i < this.userLeave.length; i++) {
if(userLeave[i].LeaveName = this.leaveType && userLeave[i].LeaveRemains >= diff)
this.chkLeave = 1;
}
};
Finally I did this to check if the leave type allotment is available for the user:
if(this.chkLeave == 1) {
alert("You can have the leave.");
} else {
alert("Oops! You can't have the leave.");
}
Now the issue is for multiple leave allotment, the method works perfectly and verifies the allotment for single allotment like Annual. Say for user 1001, it has two leave type allotments. So when I select casual leave, for the first time it checks perfectly whether the user is eligible for the leave depending upon allotment but it stuck when I select the second leave type, say sick and the leave days exceeds the allotment no (Basically allotments are total enjoyable days). It keeps the state of the first leave and the variable chkLeave holds the number 0 or 1 depending upon the first chosen leave type. In the inspect element of the browser, I can see the details in an array for leave allotment but while verifying, the loop somehow doesn't give the expected output. Is there anything wrong with the for loop?