private void ValidateFinalize(Job job, FinalizeJobPaymentSM finalize)
        {
            if (job == null)
                throw new Exception(string.Format("Job with id '{0}' not found.", finalize.JobId));

            if (job.ParentId != finalize.UserId)
                throw new Exception("Unable to finalize job, parentId is not equal to authenticated user.");
            if (finalize.Bonus < 0)
                throw new Exception("bonus is less than 0");
            if (finalize.Bonus > 200)
                throw new Exception("bonus is greater than 200");
            if (finalize.Duration > 24)
                throw new Exception("duration is greater than 24 hours");
        }
        private PostJobResultSM ValidateJobForInsert(Job job)
        {
            var result = new PostJobResultSM();
            var now = TimeUtil.GetCurrentUtcTime();
            if (job.Start < now)
            {
                result.Error += "Job Start is in the past";
                return result;
            }
            if (job.Duration <= 0)
            {
                result.Error += "Job Duration is not greater than zero";
                return result;
            }
            if (job.JobInvites == null || job.JobInvites.Count == 0)
            {
                result.Error += "Must invite at least one sitter";
                return result;
            }

            return result;
        }
        public bool ProcessSitterResponse(SitterJobInviteResponseSM response, Job jobIn)
        {
            Job job = jobIn ?? _jobDal.GetById(response.JobId);
            JobInvite invite = job.JobInvites.FirstOrDefault(m => m.SitterId == response.SitterId);
            if (invite == null)
            {
                throw new AppException(string.Format("Unable to find invite on job {0} for sitterId {1}", job.Id, response.SitterId));
            }
            invite.LatestResponseDate = TimeUtil.GetCurrentUtcTime();
            invite.LatestResponseMessage = response.Message;

            if (response.Response == SitterResponse.Accept)
            {
                job.State = JobState.Accepted;
                job.AcceptedSitterId = response.SitterId;

                // STEP - Send Text to parent and other sitters of job closed.
                try
                {
                    // STEP - Send confirm to parent, and closed notice to other sitters
                    _omm.SendNoticeOfSitterAccept(job);
                }
                catch (Exception ex)
                {
                    _log.Error(job.ParentId.ToString(), "error while SendParentNoticeOfSitterAccept()." , ex);
                    throw;
                }

            }
            else if (response.Response == SitterResponse.Decline)
            {
                invite.State = InvitationState.Declined;
                bool allSittersDeclined = job.JobInvites.All(m => m.State == InvitationState.Declined);
                job.State = JobState.Closed;
                job.CloseReason = CloseReason.AllSittersDeclined;
                _omm.SendParentNoticeOfSitterDecline(job, response.SitterId, allSittersDeclined);
            }
            else
            {
                invite.State = InvitationState.InvalidResponse;
            }
            _jobDal.Update(job);

            return true;
        }
        public void InsertJobReadyForPayment()
        {
            Parent parent = _parentRepo.GetAll().FirstOrDefault(m => m.Id == 1);
            List<MySitterHub.Model.Core.Sitter> sitters = new SitterRepository().GetAll();

            MySitterHub.Model.Core.Sitter sitter = sitters[0];

            // Create Job.
            var job = new Job
            {
                ParentId = parent.Id,
                Duration = 3,
                Start = new DateTime(2015, 1, 1),
                State = JobState.Accepted
            };

            var invite = new JobInvite {SitterId = sitter.Id, RatePerHour = 10, InvitedDate = job.Start.AddDays(-3), LatestResponseDate = job.Start.AddDays(-2)};
            job.JobInvites = new List<JobInvite>();
            job.JobInvites.Add(invite);
            job.AcceptedSitterId = sitter.Id;

            invite.State = InvitationState.Accepted;
            invite.LatestResponseMessage = "yes";

           // var finalize = new FinalizeJobPaymentSM();
           //// finalize.JobId = job.Id;
           // finalize.UserId = job.ParentId;
           // finalize.Duration = job.Duration;
           // finalize.Bonus = 5;
           // job.SetFinalPayment(finalize);

            _jobDal.Insert(job);
        }
        public PostJobResultSM PostJob(PostJobSM jobSm)
        {
            foreach (var invite in jobSm.JobInvites)
            {
                invite.InvitedDate = TimeUtil.GetCurrentUtcTime();
            }

            var job = new Job();
            job.ParentId = jobSm.ParentId;
            job.Notes = jobSm.Notes;
            job.Start = jobSm.DateTimeStartDte != null ? jobSm.DateTimeStartDte.Value : DateTime.Parse(jobSm.DateTimeStart);
            job.Duration = jobSm.Duration;
            job.JobInvites = jobSm.JobInvites;
            job.State = JobState.Posted;
            
            PostJobResultSM result = ValidateJobForInsert(job);
            if (!result.HasError)
            {
                _jobDal.Insert(job);
            }
            return result;
        }
        private void generateJobs(int parentId, List<AppUser> sitters)
        {
            DateTime currentDate = TimeUtil.GetCurrentUtcTime();
            DateTime currentDate5pm = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day, 17, 0, 0).ToUniversalTime();

            int countJobs = 5;
            var jobs = new List<Job>();
            for (int i = 0; i < countJobs; i++)
            {
                // Create Job.
                var job = new Job
                {
                    ParentId = parentId,
                    Notes = "note " + i,
                };
                jobs.Add(job);
            }
            List<int> sitterIds = sitters.Select(m => m.Id).ToList();

            Job j1 = jobs[0];
            j1.State = JobState.Closed;
            j1.Start = currentDate5pm.AddDays(-3);
            j1.Duration = 5.5m;
            j1.Notes = "Closed, 3 days in past, paid successfully with finalized payment";
            AddInvites(j1, sitterIds, sitterIds[0]);
            j1.CloseReason = CloseReason.Paid;
            var finalize = new FinalizeJobPaymentSM();
            //finalize.JobId = j1.Id;
            finalize.UserId = j1.ParentId;
            finalize.Duration = j1.Duration;
            finalize.Bonus = 5;
            j1.SetFinalPayment(finalize);

            Job j2 = jobs[1];
            j2.State = JobState.Accepted;
            j2.Start = currentDate5pm.AddDays(-2);
            j2.Notes = "accepted by SitterId " + sitterIds[1];
            AddInvites(j2, sitterIds, sitterIds[1]);
            j2.Duration = 3m;
            j2.CloseReason = CloseReason.PaidOffline;

            Job j3 = jobs[2];
            j3.State = JobState.Closed;
            j3.Start = currentDate5pm.AddDays(-1);
            j3.Duration = 5;
            j2.Notes = "Closed, parent cancelled. Accepted by SitterId " + sitterIds[0];
            AddInvites(j3, sitterIds, sitterIds[0]);
            j3.CloseReason = CloseReason.ParentCancelled;
            j3.AcceptedSitterId = sitterIds[0];

            Job j4 = jobs[3];
            j4.State = JobState.Accepted;
            j4.Start = currentDate5pm.AddDays(0);
            j4.Duration = 2.5m;
            j4.Notes = "Accepted by sitterId " + sitterIds[0];
            AddInvites(j4, sitterIds, sitterIds[0]);

            Job j5 = jobs[4];
            j5.State = JobState.Posted;
            j5.Start = currentDate5pm.AddDays(1);
            j5.Duration = 2;
            j5.Notes = "Posted, pending invite Sitter1 (results in real text message), 2nd sitter pending invite, 3rd sitter declines";
            j5.JobInvites = new List<JobInvite>();
            j5.JobInvites.Add(new JobInvite {SitterId = sitterIds[0], RatePerHour = 8, InvitedDate = j5.Start.AddDays(-1)});
            j5.JobInvites.Add(new JobInvite {SitterId = sitterIds[1], RatePerHour = 9, InvitedDate = j5.Start.AddDays(-1)});
            j5.JobInvites[1].State = InvitationState.Declined;

            // STEP Invites, and Insert into DAL
            for (int i = 0; i < countJobs; i++)
            {
                _jobDal.Insert(jobs[i]);
            }
        }
        private void AddInvites(Job job, IEnumerable<int> sitterIds, int? acceptedSitterId = null)
        {
            job.JobInvites = new List<JobInvite>();
            foreach (int id in sitterIds)
            {
                var invite = new JobInvite {SitterId = id, RatePerHour = 5 + id, InvitedDate = job.Start.AddDays(-3), LatestResponseDate = job.Start.AddDays(-2)};
                job.JobInvites.Add(invite);
                if (id == acceptedSitterId)
                {
                    invite.State = InvitationState.Accepted;
                    invite.LatestResponseMessage = "yes";
                }
                else
                {
                    invite.State = InvitationState.Declined; //Futuredev: make some responses blank
                    invite.LatestResponseMessage = "no";
                }
            }
            if (acceptedSitterId != null && job.JobInvites.All(m => m.SitterId != acceptedSitterId.Value))
                throw new Exception();

            job.AcceptedSitterId = acceptedSitterId;
        }
Example #8
0
        public void Update(Job job)
        {
            Debug.Assert(job.Id != 0);

            MongoCnn.GetJobCollection().Save(job);
        }
Example #9
0
 public void Insert(Job job)
 {
     job.Id = new CounterDal().GetNextJobSlugId();
     MongoCnn.GetJobCollection().Save(job);
 }
        public void SendParentNoticeOfSitterRenege(Job job, int renegeingSitterId)
        {
            AppUser parent = _appUserRepo.GetById(job.ParentId);

            DateTime date = parent.ToLocalTime(job.Start);

            AppUser sitter = _appUserRepo.GetById(renegeingSitterId);

            // STEP - Notify parent
            var txtMsg = new TxtMsgOutbound
            {
                MobilePhone = parent.MobilePhone,
                Message = MessageTemplates.FormatSendParentNoticeOfSitterRenege(sitter.FullName(), date),
                OutboundMessageType = OutboundMessageType.ParentNotifySitterRenege,
                ReceipientId = parent.Id,
                SenderId = renegeingSitterId,
                JobId = job.Id
            };

            _smsOutboundRepo.QueueSmsForSend(txtMsg);
        }
        public void SendParentNoticeOfSitterDecline(Job job, int decliningSitterId, bool allSittersDeclined)
        {
            AppUser parent = _appUserRepo.GetById(job.ParentId);

            AppUser sitter = _appUserRepo.GetById(decliningSitterId);

            // STEP - Notify parent
            var txtMsg = new TxtMsgOutbound
            {
                MobilePhone = parent.MobilePhone,
                Message = MessageTemplates.FormatSendParentNoticeOfSitterDecline(sitter.FullName(), allSittersDeclined),
                OutboundMessageType = OutboundMessageType.ParentNotifySitterDecline,
                ReceipientId = parent.Id,
                SenderId = decliningSitterId,
                JobId = job.Id
            };

            _smsOutboundRepo.QueueSmsForSend(txtMsg);
        }
        public void SendNoticeOfSitterAccept(Job jobA)
        {
            AppUser parent = _appUserRepo.GetById(jobA.ParentId);
            if (jobA.AcceptedSitterId == null)
            {
                throw new Exception("Accepted sitter is null for SendParentNoticeOfSitterAccept()");
            }

            AppUser sitter = _appUserRepo.GetById(jobA.AcceptedSitterId.Value);

            // STEP - Notify parent
            var txtMsg = new TxtMsgOutbound
            {
                MobilePhone = parent.MobilePhone,
                Message = MessageTemplates.FormatSendParentNoticeOfSitterAccept(sitter.FullName()),
                OutboundMessageType = OutboundMessageType.ParentNotifySitterAccept,
                ReceipientId = parent.Id,
                SenderId = jobA.AcceptedSitterId.Value,
                JobId = jobA.Id
            };

            _smsOutboundRepo.QueueSmsForSend(txtMsg);

            // STEP - Notify other sitters
            foreach (JobInvite s in jobA.JobInvites)
            {
                if (s.SitterId == jobA.AcceptedSitterId)
                {
                    continue;
                }

                AppUser sitterOther = _appUserRepo.GetById(s.SitterId);
                var txtMsgSO = new TxtMsgOutbound
                {
                    MobilePhone = sitterOther.MobilePhone,
                    Message = MessageTemplates.FormatSendOtherSittersNoticeOfJobClose(parent.FullName(), sitterOther.ToLocalTime(jobA.Start)),
                    OutboundMessageType = OutboundMessageType.SitterNotifyJobClose,
                    ReceipientId = sitterOther.Id,
                    SenderId = jobA.ParentId,
                    JobId = jobA.Id
                };
                _smsOutboundRepo.QueueSmsForSend(txtMsgSO);

                //STEP - Stop waiting for response from other sitters
                TxtMsgAwaitingResponse awaiting = _txtMsgAwaitingResponseDal.GetByAwaitingUserMobile(sitterOther.MobilePhone);
                if (awaiting != null)
                    _txtMsgAwaitingResponseDal.DeleteAwaiting(awaiting.Id);
            }
        }