예제 #1
0
        public async Task <IActionResult> ClockOut(string moniker, [FromQuery] string username)
        {
            try
            {
                //find jobsite
                var jobsite = await _repository.GetJobsiteAsync(moniker);

                if (jobsite == null)
                {
                    return(NotFound());
                }

                AppUser user;

                //code below is for managers to clock out other employees
                if (username != null)
                {
                    //manager status
                    var loggedInUser = await _userRepository.GetUser(_userAccessor.GetCurrentUsername());

                    if (loggedInUser.Manager == false)
                    {
                        return(Unauthorized(new RestError(HttpStatusCode.Unauthorized, new { Unauthorized = "Unauthorized to perform action" })));
                    }

                    user = await _userRepository.GetUser(username);
                }
                else
                {
                    //if not manager, clock-out functionality limited to self
                    user = await _userRepository.GetUser(_userAccessor.GetCurrentUsername());
                }


                //If not already clocked in, bad request
                var currentlyClockedin = await _timestampRepository.GetClockedInTimestamp(user);

                if (currentlyClockedin == null)
                {
                    return(BadRequest($"User must first be clocked in to {jobsite.Moniker} to clock out. "));
                }

                //if clocked in to another job, bad request
                if (currentlyClockedin.JobsiteId != jobsite.JobsiteId)
                {
                    return(BadRequest($"User currently clocked in to another job: {currentlyClockedin.Jobsite.Moniker}"));
                }

                //if clockedin to the correct jobsite, clock out
                var success = await _timestampRepository.ClockOut(user);

                if (success)
                {
                    return(Ok($"Successfully clocked out of {moniker}."));
                }
            }
            catch (Exception)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, "Server Error: Failed to clock out."));
            }
            return(BadRequest());
        }