public async Task <ActionResult> Put([FromRoute] ObjectId issueId, [FromRoute] ObjectId id,
                                             [FromBody] IssueTimeSheetDTO timeSheetDto)
        {
            await _timeSheetService.UpdateAsync(issueId, id, timeSheetDto);

            return(NoContent());
        }
Exemple #2
0
        public async Task TimeSheetTest3()
        {
            using var helper = await new SimpleTestHelperBuilder().Build();
            IssueTimeSheetDTO timeSheetDTO = new IssueTimeSheetDTO()
            {
                User  = helper.User,
                Start = DateTime.Now
            };

            var uri      = $"/api/issues/{helper.Issue.Id}/timesheets";
            var responce = await helper.client.PostAsync(uri, timeSheetDTO.ToStringContent());

            Assert.IsTrue(responce.IsSuccessStatusCode);
            var result = await responce.Parse <IssueTimeSheetDTO>();

            uri      = $"/api/issues/{helper.Issue.Id}/timesheets/{result.Id}";
            responce = await helper.client.GetAsync(uri);

            Assert.IsTrue(responce.IsSuccessStatusCode);
            var createdSheet = await responce.Parse <IssueTimeSheetDTO>();

            createdSheet.End = DateTime.Now.AddHours(-2);
            responce         = await helper.client.PutAsync(uri, createdSheet.ToStringContent());

            Assert.IsFalse(responce.IsSuccessStatusCode);
        }
        public async Task <IssueTimeSheetDTO> CreateAsync(ObjectId issueId, IssueTimeSheetDTO timeSheetDto)
        {
            ThrowErrorIfEndIsBeforStart(timeSheetDto);
            var issue = await _issueRepo.GetAsync(issueId);

            var user   = _httpContextAccessor.HttpContext.User;
            var userId = user.GetUserId();

            if ((await _authorizationService.AuthorizeAsync(user, issue, IssueOperationRequirments.CreateOwnTimeSheets)).Succeeded is false)
            {
                throw new HttpStatusException(StatusCodes.Status403Forbidden, "You are not allowed to create a time sheet.");
            }
            var timesheet = timeSheetDto.ToTimeSheet();

            timesheet.Id     = ObjectId.GenerateNewId();
            timesheet.UserId = userId;
            issue.TimeSheets.Add(timesheet);
            issue.IssueDetail.TotalWorkTime = CalculateWorkedTime(issue);

            await StopAllTimeSheetsOfUserAsync(userId);

            await _issueRepo.UpdateAsync(issue);

            await CreateTimeExccededMessage(issueId, timeSheetDto);

            return(await GetAsync(issueId, timesheet.Id));
        }
        public async Task UpdateAsync(ObjectId issueId, ObjectId id, IssueTimeSheetDTO timeSheetDto)
        {
            ThrowErrorIfEndIsBeforStart(timeSheetDto);
            var issue = await _issueRepo.GetAsync(issueId);

            // updating own timesheets require other requirements.
            if (_httpContextAccessor.HttpContext.User.GetUserId().Equals(timeSheetDto.User.Id))
            {
                await CanUserUpdateOwnTimeSheetAsync(issue);
            }
            else
            {
                await CanUserUpdateTimeSheetAsync(issue);

                // Send Message to User
                await CreateTimeChangedMessage(issue, timeSheetDto);
            }


            issue.TimeSheets.Replace(it => it.Id == timeSheetDto.Id, timeSheetDto.ToTimeSheet());
            issue.IssueDetail.TotalWorkTime = CalculateWorkedTime(issue);
            await _issueRepo.UpdateAsync(issue);

            await CreateTimeExccededMessage(issueId, timeSheetDto);
        }
        public async Task <ActionResult <IssueTimeSheetDTO> > Post([FromRoute] ObjectId issueId,
                                                                   [FromBody] IssueTimeSheetDTO timeSheetDto)
        {
            var res = await _timeSheetService.CreateAsync(issueId, timeSheetDto);

            return(CreatedAtAction(nameof(Get), new { issueId, id = res.Id }, res));
        }
        /// <summary>
        /// Checks if the ticket time could have been exceeded. If it was, a message is generated.
        /// The newly edited timesheet can be provided, in order to see if the timeSheet was just started.
        /// In that case, the check if the time was exceeded is not necessary and will be skipped.
        /// </summary>
        /// <param name="issueId"></param>
        /// <param name="timeSheetDto"></param>
        /// <returns></returns>
        private async Task CreateTimeExccededMessage(ObjectId issueId, IssueTimeSheetDTO timeSheetDto)
        {
            if (timeSheetDto.End == default)
            {
                return;
            }

            await CreateTimeExccededMessage(issueId);
        }
 private async Task CreateTimeChangedMessage(Issue issue, IssueTimeSheetDTO timeSheetDto)
 {
     await _messageService.CreateMessageAsync(new Message()
     {
         CompanyId      = (await _projectRepository.GetAsync(issue.ProjectId)).CompanyId,
         ProjectId      = issue.ProjectId,
         IssueId        = issue.Id,
         ReceiverUserId = timeSheetDto.User.Id,
         Type           = MessageType.RecordedTimeChanged,
         Consented      = false,
     });
 }
Exemple #8
0
        public async Task TimeSheetTest2()
        {
            using var helper = await new SimpleTestHelperBuilder().Build();
            IssueTimeSheetDTO timeSheetDTO = new IssueTimeSheetDTO()
            {
                User  = helper.User,
                Start = DateTime.Now.AddDays(-1),
                End   = DateTime.Now.AddDays(-2)
            };

            var uri      = $"/api/issues/{helper.Issue.Id}/timesheets";
            var responce = await helper.client.PostAsync(uri, timeSheetDTO.ToStringContent());

            Assert.IsFalse(responce.IsSuccessStatusCode);
        }
        private void ThrowErrorIfEndIsBeforStart(IssueTimeSheetDTO timeSheetDto)
        {
            if (timeSheetDto.Start == default(DateTime))
            {
                return;
            }

            if (timeSheetDto.End == default(DateTime))
            {
                return;
            }

            if (timeSheetDto.End < timeSheetDto.Start)
            {
                throw new HttpStatusException(StatusCodes.Status400BadRequest, "Das Enddatum darf nicht vor dem Startdatum sein");
            }
        }
        public async Task CustomerShouldNotBeAbleToStartTimeSheetTestAsync()
        {
            using var helper = await new SimpleTestHelperBuilderIssueAuthorization(Role.CustomerRole).Build();

            //_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", companyClientSignIn.Token);
            var uri = $"/api/issues/{helper.Issue.Id}/timesheets";

            var newItem = new IssueTimeSheetDTO()
            {
                User  = helper.User,
                Start = DateTime.Now,
                End   = DateTime.Now
            };

            var response = await helper.client.PostAsync(uri, newItem.ToStringContent());

            var r = await response.Content.Parse <IssueTimeSheetDTO>();

            Assert.AreEqual(HttpStatusCode.Forbidden, response.StatusCode, "Customer was able to create a timesheet!");
        }
        public async Task TimeSheetMessageTest4()
        {
            using var helper = await new SimpleTestHelperBuilderMessage().Build();
            var newUser = await helper.Helper.GenerateUserAndSetToProject(helper.Company.Id, helper.Project.Id, Role.EmployeeRole);

            IssueTimeSheetDTO timeSheetDTO = new IssueTimeSheetDTO()
            {
                User  = new UserDTO(await helper.Helper.UserRepository.GetAsync(newUser.Id)),
                Start = DateTime.Now
            };

            var uri      = $"/api/issues/{helper.Issue.Id}/timesheets";
            var responce = await helper.client.PostAsync(uri, timeSheetDTO.ToStringContent());

            Assert.IsTrue(responce.IsSuccessStatusCode);
            var result = await responce.Parse <IssueTimeSheetDTO>();

            helper.Helper.SetAuth(helper.SignIn);

            uri      = $"/api/issues/{helper.Issue.Id}/timesheets/{result.Id}";
            responce = await helper.client.GetAsync(uri);

            Assert.IsTrue(responce.IsSuccessStatusCode);
            var createdSheet = await responce.Parse <IssueTimeSheetDTO>();

            createdSheet.End = DateTime.Now.AddMinutes(30);
            responce         = await helper.client.PutAsync(uri, createdSheet.ToStringContent());

            Assert.IsTrue(responce.IsSuccessStatusCode);

            uri      = $"/api/messages/{newUser.Id}";
            responce = await helper.client.GetAsync(uri);

            Assert.IsTrue(responce.IsSuccessStatusCode);
            var messageList = await responce.Parse <IList <MessageDTO> >();

            Assert.AreEqual(1, messageList.Count);
        }
        public async Task TimeSheetMessageTest3()
        {
            using var helper = await new SimpleTestHelperBuilderMessage().Build();
            IssueTimeSheetDTO timeSheetDTO = new IssueTimeSheetDTO()
            {
                User  = helper.User,
                Start = DateTime.Now,
                End   = DateTime.Now.AddHours(2)
            };

            var uri      = $"/api/issues/{helper.Issue.Id}/timesheets";
            var responce = await helper.client.PostAsync(uri, timeSheetDTO.ToStringContent());

            Assert.IsTrue(responce.IsSuccessStatusCode);

            uri      = $"/api/messages/{helper.User.Id}";
            responce = await helper.client.GetAsync(uri);

            Assert.IsTrue(responce.IsSuccessStatusCode);
            var messageList = await responce.Parse <IList <MessageDTO> >();

            Assert.AreEqual(1, messageList.Count);
        }
Exemple #13
0
        public static async Task TimeSheetCancellation()
        {
            var now = new DateTime(2020, 5, 19, 12, 0, 0, DateTimeKind.Local);

            // Create all the timesheets
            using var helper = await new SimpleTestHelperBuilder().Build();
            var secondaryUser = helper.User;

            var issueId       = helper.Issue.Id;
            var timeSheetsUri = $"/api/issues/{issueId}/timesheets/";

            var firstTimeSheet = new IssueTimeSheetDTO()
            {
                User  = secondaryUser,
                Start = now,
            };

            var result = await helper.client.PostAsync(timeSheetsUri, firstTimeSheet.ToStringContent());

            Assert.AreEqual(HttpStatusCode.Created, result.StatusCode);

            var primaryUser = await helper.GenerateUserAndSetToProject(Role.EmployeeRole);

            Assert.AreNotEqual(secondaryUser.Id, primaryUser.Id);

            var secondTimeSheet = new IssueTimeSheetDTO()
            {
                User  = primaryUser,
                Start = now.AddDays(-2),
                End   = now.AddHours(-1),
            };

            result = await helper.client.PostAsync(timeSheetsUri, secondTimeSheet.ToStringContent());

            Assert.AreEqual(HttpStatusCode.Created, result.StatusCode);

            var thirdTimeSheet = new IssueTimeSheetDTO()
            {
                User  = primaryUser,
                Start = now.AddHours(-1),
                // Open ended
            };

            result = await helper.client.PostAsync(timeSheetsUri, thirdTimeSheet.ToStringContent());

            Assert.AreEqual(HttpStatusCode.Created, result.StatusCode);

            var content = await helper.CreateIssue();

            var otherIssue = await content.Parse <IssueDTO>();

            var timeSheetsOfOtherIssueUri = $"/api/issues/{otherIssue.Id}/timesheets/";

            var fourthTimeSheet = new IssueTimeSheetDTO()
            {
                User  = primaryUser,
                Start = now,
                // Open ended
            };

            result = await helper.client.PostAsync(timeSheetsOfOtherIssueUri, fourthTimeSheet.ToStringContent());

            Assert.AreEqual(HttpStatusCode.Created, result.StatusCode);


            // check the state of all timesheets
            result = await helper.client.GetAsync(timeSheetsUri);

            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);

            var timeSheets = await result.Parse <IList <IssueTimeSheetDTO> >();

            Assert.AreEqual(3, timeSheets.Count);

            AssertTimeSheetsAreEqualExceptId(firstTimeSheet, timeSheets[0]);
            AssertTimeSheetsAreEqualExceptId(secondTimeSheet, timeSheets[1]);
            Assert.AreEqual(thirdTimeSheet.User.Id, timeSheets[2].User.Id);
            Assert.AreEqual(thirdTimeSheet.Start, timeSheets[2].Start);
            Assert.AreNotEqual(default(DateTime), timeSheets[2].End); // end time must have been set

            // check other timesheet
            result = await helper.client.GetAsync(timeSheetsOfOtherIssueUri);

            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);

            var otherTimeSheets = await result.Parse <IList <IssueTimeSheetDTO> >();

            Assert.AreEqual(1, otherTimeSheets.Count);

            AssertTimeSheetsAreEqualExceptId(fourthTimeSheet, otherTimeSheets[0]);
        }
Exemple #14
0
 public static void AssertTimeSheetsAreEqualExceptId(IssueTimeSheetDTO expected, IssueTimeSheetDTO actual)
 {
     Assert.AreEqual(expected.User.Id, actual.User.Id);
     Assert.AreEqual(expected.Start, actual.Start);
     Assert.AreEqual(expected.End, actual.End);
 }
        public async Task TimeSheetMessageTest2()
        {
            using var helper = await new SimpleTestHelperBuilderMessage().Build();

            //Create and end one Sheet without Exceeding expacted time
            IssueTimeSheetDTO timeSheetDTO = new IssueTimeSheetDTO()
            {
                User  = helper.User,
                Start = DateTime.Now
            };

            var uri      = $"/api/issues/{helper.Issue.Id}/timesheets";
            var responce = await helper.client.PostAsync(uri, timeSheetDTO.ToStringContent());

            Assert.IsTrue(responce.IsSuccessStatusCode);
            var result = await responce.Parse <IssueTimeSheetDTO>();

            uri      = $"/api/issues/{helper.Issue.Id}/timesheets/{result.Id}";
            responce = await helper.client.GetAsync(uri);

            Assert.IsTrue(responce.IsSuccessStatusCode);
            var createdSheet = await responce.Parse <IssueTimeSheetDTO>();

            createdSheet.End = DateTime.Now.AddMinutes(30);
            responce         = await helper.client.PutAsync(uri, createdSheet.ToStringContent());

            Assert.IsTrue(responce.IsSuccessStatusCode);

            uri      = $"/api/messages/{helper.User.Id}";
            responce = await helper.client.GetAsync(uri);

            Assert.IsTrue(responce.IsSuccessStatusCode);
            var messageList = await responce.Parse <IList <MessageDTO> >();

            Assert.AreEqual(0, messageList.Count);

            //Create Second Sheet which accours the Time Exceeding
            var timeSheetDTO2 = new IssueTimeSheetDTO()
            {
                User  = helper.User,
                Start = DateTime.Now.AddHours(2)
            };

            uri      = $"/api/issues/{helper.Issue.Id}/timesheets";
            responce = await helper.client.PostAsync(uri, timeSheetDTO2.ToStringContent());

            Assert.IsTrue(responce.IsSuccessStatusCode);
            result = await responce.Parse <IssueTimeSheetDTO>();

            uri      = $"/api/issues/{helper.Issue.Id}/timesheets/{result.Id}";
            responce = await helper.client.GetAsync(uri);

            Assert.IsTrue(responce.IsSuccessStatusCode);
            var createdSheet2 = await responce.Parse <IssueTimeSheetDTO>();

            createdSheet2.End = DateTime.Now.AddHours(4);
            responce          = await helper.client.PutAsync(uri, createdSheet2.ToStringContent());

            Assert.IsTrue(responce.IsSuccessStatusCode);

            uri      = $"/api/messages/{helper.User.Id}";
            responce = await helper.client.GetAsync(uri);

            Assert.IsTrue(responce.IsSuccessStatusCode);
            var messageList2 = await responce.Parse <IList <MessageDTO> >();

            Assert.AreEqual(1, messageList2.Count);
        }