Example #1
0
        public IActionResult PunchOut([FromQuery] string timeZone,
                                      [FromQuery] string latitude, [FromQuery] string longitude, [FromQuery] string sourceHardware,
                                      [FromQuery] string sourceOperatingSystem, [FromQuery] string sourceOperatingSystemVersion,
                                      [FromQuery] string sourceBrowser, [FromQuery] string sourceBrowserVersion)
        {
            var sourceHostname = ""; // Leave blank

            // Attempt to get the client IP address.
            var sourceIpAddress = $"{HttpContext.Connection.RemoteIpAddress}";

            var currentUser = CurrentUser();

            // Prevent double submission.
            var submission = _memoryCache.Get($"submission.punchout.{currentUser.Id}") as bool?;

            if (submission.HasValue)
            {
                return(BadRequest("Cannot punch out twice within 5 seconds."));
            }

            try
            {
                var repo = new PunchRepository(_context);

                var punch = repo.PunchOut(
                    currentUser,
                    "",
                    timeZone,
                    latitude,
                    longitude,
                    sourceHardware,
                    sourceHostname,
                    sourceIpAddress,
                    sourceOperatingSystem,
                    sourceOperatingSystemVersion,
                    sourceBrowser,
                    sourceBrowserVersion);

                // Record the submission.
                _memoryCache.Set($"submission.punchout.{currentUser.Id}", true, DateTime.UtcNow.AddSeconds(5));

                return(Ok(punch));
            }
            catch (DbUpdateException ex)
            {
                Trace.TraceError(ex.ToString());
                return(BadRequest(ex.Message));
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.ToString());
                return(BadRequest(ex.Message));
            }
        }
Example #2
0
 public UnitOfWork(TpContext context, ITimeService timeService)
 {
     _context      = context;
     AppUsers      = new AppUserRepository(_context);
     AppProfiles   = new AppProfileRepository(_context);
     RefreshTokens = new RefreshTokenRepository(_context);
     DayPunches    = new DayPunchRepository(_context, AppUsers);
     WeekPunches   = new WeekPunchRepository(_context, timeService, AppUsers);
     MonthPunches  = new MonthPunchRepository(_context, timeService, AppUsers);
     YearPunches   = new YearPunchRepository(_context, timeService, AppUsers);
     Punches       = new PunchRepository(_context, timeService, AppUsers);
     MonthStates   = new MonthStateRepository(_context);
     PunchStates   = new PunchStateRepository(_context);
 }
Example #3
0
        public IActionResult PunchIn([FromQuery] int taskId, [FromQuery] string timeZone,
                                     [FromQuery] string latitude, [FromQuery] string longitude, [FromQuery] string sourceHardware,
                                     [FromQuery] string sourceOperatingSystem, [FromQuery] string sourceOperatingSystemVersion,
                                     [FromQuery] string sourceBrowser, [FromQuery] string sourceBrowserVersion)
        {
            var sourceHostname = ""; // Leave blank

            // Attempt to get the client IP address.
            var sourceIpAddress = $"{HttpContext.Connection.RemoteIpAddress}";

            var currentUser = CurrentUser();

            var task = _context.Tasks
                       .Include(t => t.Job.Customer)
                       .Where(t => t.Job.Customer.OrganizationId == currentUser.OrganizationId)
                       .Where(t => t.Id == taskId)
                       .FirstOrDefault();

            // Ensure that task was found.
            if (task == null)
            {
                return(NotFound());
            }

            // Ensure job is open.
            if (task.Job.Status != "Open")
            {
                return(BadRequest("Cannot punch in on tasks for projects that are not open."));
            }

            // Prevent double submission.
            var submission = _memoryCache.Get($"submission.punchin.{currentUser.Id}") as bool?;

            if (submission.HasValue)
            {
                return(BadRequest("Cannot punch in twice within 5 seconds."));
            }

            try
            {
                var repo = new PunchRepository(_context);

                var punch = repo.PunchIn(
                    taskId,
                    CurrentUser(),
                    "",
                    timeZone,
                    latitude,
                    longitude,
                    sourceHardware,
                    sourceHostname,
                    sourceIpAddress,
                    sourceOperatingSystem,
                    sourceOperatingSystemVersion,
                    sourceBrowser,
                    sourceBrowserVersion);

                // Record the submission.
                _memoryCache.Set($"submission.punchin.{currentUser.Id}", true, DateTime.UtcNow.AddSeconds(5));

                return(Ok(punch));
            }
            catch (DbUpdateException ex)
            {
                Trace.TraceError(ex.ToString());
                return(BadRequest(new { Message = ex.Message }));
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.ToString());
                return(BadRequest(new { Message = ex.Message }));
            }
        }