public async Task DeleteRecordByRecordIdAsync(string recordId)
        {
            using (var dbContext = new AppDbContext(builder.Options))
            {
                var record = await dbContext.CheckRecord.FirstOrDefaultAsync(s => s.Id == recordId);

                if (record == null)
                {
                    return;
                }
                if (TypeEnum.LimitedLeaves.Contains(record.OffType) &&
                    record.StatusOfApproval == StatusOfApprovalEnum.APPROVED())
                {
                    var user = await dbContext.UserInfo.FirstOrDefaultAsync(s => s.Id == record.UserId);

                    if (user == null)
                    {
                        return;
                    }
                    user = TypeEnum.CalcLeaves(user, record.OffTimeStart, record.OffTimeEnd, record.OffType,
                                               inverseOp: true);
                    dbContext.Entry(user).State = EntityState.Modified;
                }
                dbContext.Entry(record).State = EntityState.Deleted;
                await dbContext.SaveChangesAsync();
            }
        }
        public async Task <bool> SetStatusOfApprovalAsync(string recordId, string status, bool OT = false)
        {
            using (var dbContext = new AppDbContext(builder.Options))
            {
                try
                {
                    dbContext.ChangeTracker.AutoDetectChangesEnabled = false;
                    var record = await dbContext.CheckRecord.FirstOrDefaultAsync(s => s.Id == recordId);

                    if (record == null)
                    {
                        return(false);
                    }
                    if (status == StatusOfApprovalEnum.APPROVED())
                    {
                        if (OT)
                        {
                            record.StatusOfApprovalForOvertime = status;
                        }
                        else
                        {
                            var user = await dbContext.UserInfo.FirstOrDefaultAsync(s => s.Id == record.UserId);

                            user = TypeEnum.CalcLeaves(user, record.OffTimeStart, record.OffTimeEnd, record.OffType);
                            if (user == null)
                            {
                                record.StatusOfApproval = StatusOfApprovalEnum.REJECTED();
                            }
                            else
                            {
                                dbContext.Entry(user).State = EntityState.Modified;
                                record.StatusOfApproval     = status;
                            }
                        }
                    }
                    else
                    {
                        if (OT)
                        {
                            record.StatusOfApprovalForOvertime = status;
                        }
                        else
                        {
                            record.StatusOfApproval = status;
                        }
                    }
                    dbContext.Entry(record).State = EntityState.Modified;
                    await dbContext.SaveChangesAsync();

                    return(true);
                }
                catch (Exception)
                {
                    return(false);
                }
            }
        }
        public async Task <bool> DeleteLeaveAsync(string id, DateTime date)
        {
            using (var dbContext = new AppDbContext(builder.Options))
            {
                dbContext.ChangeTracker.AutoDetectChangesEnabled = false;
                var result =
                    await dbContext.CheckRecord.FirstOrDefaultAsync(s => s.UserId == id && s.CheckedDate == date);

                if (result == null)
                {
                    return(false);
                }
                if (result.StatusOfApproval == StatusOfApprovalEnum.APPROVED())
                {
                    var user = await dbContext.UserInfo.FirstOrDefaultAsync(s => s.Id == result.UserId);

                    user = TypeEnum.CalcLeaves(user, result.OffTimeStart, result.OffTimeEnd,
                                               result.OffType, inverseOp: true);
                    dbContext.Entry(user).State = EntityState.Modified;
                }
                if (result.CheckInTime == null && result.CheckOutTime == null &&
                    string.IsNullOrWhiteSpace(result.OvertimeEndTime))
                {
                    dbContext.Entry(result).State = EntityState.Deleted;
                }
                else
                {
                    result.OffApplyDate           = null;
                    result.OffTimeStart           = null;
                    result.OffTimeEnd             = null;
                    result.OffEndDate             = null;
                    result.OffReason              = null;
                    result.OffType                = null;
                    result.StatusOfApproval       = StatusOfApprovalEnum.PENDING();
                    dbContext.Entry(result).State = EntityState.Modified;
                }
                await dbContext.SaveChangesAsync();

                return(true);
            }
        }
        public async Task InsertOrUpdateRecordAsync(EditRecordModel model)
        {
            using (var dbContext = new AppDbContext(builder.Options))
            {
                dbContext.ChangeTracker.AutoDetectChangesEnabled = false;
                bool isNewRecordNeedCalc = TypeEnum.LimitedLeaves.Contains(model.OffType) &&
                                           model.StatusOfApproval == StatusOfApprovalEnum.APPROVED();
                if (string.IsNullOrWhiteSpace(model.RecordId))
                {
                    DateTime?offApplyDate = null;
                    if (!string.IsNullOrWhiteSpace(model.OffType))
                    {
                        offApplyDate = DateTime.Today;
                    }
                    if (isNewRecordNeedCalc)
                    {
                        var user = await dbContext.UserInfo.FirstOrDefaultAsync(s => s.Id == model.UserId);

                        user = TypeEnum.CalcLeaves(user, model.OffTimeStart, model.OffTimeEnd, model.OffType);
                        if (user == null)
                        {
                            return;
                        }
                        dbContext.Entry(user).State = EntityState.Modified;
                    }
                    var r = new CheckRecord
                    {
                        UserId           = model.UserId,
                        CheckedDate      = model.CheckedDate,
                        CheckInTime      = model.CheckInTime,
                        CheckOutTime     = model.CheckOutTime,
                        GeoLocation1     = model.GeoLocation1,
                        GeoLocation2     = model.GeoLocation2,
                        OvertimeEndTime  = model.OvertimeEndTime,
                        OffType          = model.OffType,
                        OffTimeStart     = model.OffTimeStart,
                        OffTimeEnd       = model.OffTimeEnd,
                        OffReason        = model.OffReason,
                        StatusOfApproval = model.StatusOfApproval,
                        OffApplyDate     = offApplyDate
                    };
                    await dbContext.CheckRecord.AddAsync(r);
                }
                else
                {
                    var record = await dbContext.CheckRecord.FirstOrDefaultAsync(s => s.Id == model.RecordId);

                    if (record != null)
                    {
                        bool isOldRecordNeedCalc = TypeEnum.LimitedLeaves.Contains(record.OffType) &&
                                                   record.StatusOfApproval == StatusOfApprovalEnum.APPROVED();
                        if (isOldRecordNeedCalc || isNewRecordNeedCalc)
                        {
                            // new data is special, and approved -
                            var user = await dbContext.UserInfo.FirstOrDefaultAsync(s => s.Id == record.UserId);

                            if (user == null)
                            {
                                return;
                            }
                            if (isNewRecordNeedCalc)
                            {
                                var newCalc = TypeEnum.CalcLeaves(user, model.OffTimeStart, model.OffTimeEnd,
                                                                  model.OffType);
                                if (newCalc != null)
                                {
                                    user = newCalc;
                                    dbContext.Entry(user).State = EntityState.Modified;
                                }
                                else
                                {
                                    return;
                                }
                            }
                            // old data is special and approved +
                            if (isOldRecordNeedCalc)
                            {
                                user = TypeEnum.CalcLeaves(user, record.OffTimeStart, record.OffTimeEnd, record.OffType,
                                                           inverseOp: true);
                                dbContext.Entry(user).State = EntityState.Modified;
                            }
                        }
                        if (string.IsNullOrWhiteSpace(record.OffType) && !string.IsNullOrWhiteSpace(model.OffType))
                        {
                            record.OffApplyDate = DateTime.Today;
                        }
                        record.CheckInTime            = model.CheckInTime;
                        record.CheckOutTime           = model.CheckOutTime;
                        record.GeoLocation1           = model.GeoLocation1;
                        record.GeoLocation2           = model.GeoLocation2;
                        record.OvertimeEndTime        = model.OvertimeEndTime;
                        record.OffType                = model.OffType;
                        record.OffTimeStart           = model.OffTimeStart;
                        record.OffTimeEnd             = model.OffTimeEnd;
                        record.OffReason              = model.OffReason;
                        record.StatusOfApproval       = model.StatusOfApproval;
                        dbContext.Entry(record).State = EntityState.Modified;
                    }
                }
                await dbContext.SaveChangesAsync();
            }
        }