Example #1
0
        public static LeaveRequest CompleteLeaveRequest(HrmContext ctx, int id)
        {
            var leaveRequst = ctx.DbContext.LeaveRequests.Find(id);
            if (!leaveRequst.CanBalance())
                throw new Exception($"the leave request can't be balanced");


            var balance = new LeaveBalance()
            {
                Value = -ctx.Holidays.GetWorkHours(leaveRequst.Start.Value, leaveRequst.End.Value),
                OwnerId = leaveRequst.RequestIssuerId,
                Description = $"A completed Leave Request Start from {leaveRequst.Start.Value}",
                SnapShotData = JsonConvert.SerializeObject(new
                {
                    requestId = leaveRequst.Id,
                    requestFrom = leaveRequst.Start,
                    requestTo = leaveRequst.End,
                    type = leaveRequst.Type.ToString(),
                    approves = leaveRequst.Comments
                            .Select(x => new
                            {
                                x.Content,
                                x.CreatedOn,
                                x.CreatedBy,
                                Type = x.Type.ToString()
                            })
                            .ToList(),

                })
            };
            balance.RequestRelations.Add(new RequestBalanceRelation()
            {
                Balance = balance,
                RequestId = id
            });
            ctx.DbContext.LeaveBalances.Add(balance);
            ctx.DbContext.LeaveRequestComments.Add(new LeaveRequestComment()
            {
                Content = "Completed",
                Type = CommentType.Comment,
                RequestId = leaveRequst.Id
            });
            leaveRequst.State = LeaveRequestState.Completed;
            return leaveRequst;
        }
Example #2
0
 public static LeaveBalance CreateLeaveBalance(HrmContext ctx, LeaveBalance balance)
 {
     ctx.DbContext.Add(balance);
     return balance;
 }