public void DistrubuteEvent(Event @event, DistributionPolicy distributionPolicy)
        {
            var session = _sessionProvider.GetCurrentSession();

            var eventId = (int) session.Save(@event);
            foreach (var receiverId in distributionPolicy.ReceiverIds)
            {
                var id = session.Save(new Delivery(receiverId, eventId));
            }
        }
        public override void ConsumeEvent(IEventInfo eventInfo)
        {
            Require.NotNull(eventInfo, nameof(eventInfo));

            var @event = new Event(eventInfo);

            var distributionPolicy = GetDistributionPolicyForEvent((dynamic) eventInfo);

            EventRepository.DistrubuteEvent(@event, distributionPolicy);

            SendOutEmailsAboutEvent(distributionPolicy.ReceiverIds, eventInfo);
        }
        public override void ConsumeEvent(IEventInfo eventInfo)
        {
            Require.NotNull(eventInfo, nameof(eventInfo));

            var @event = new Event(eventInfo);

            var distributionPolicy = DistributionPolicyFactory.GetAdminRelatedPolicy();

            EventRepository.DistrubuteEvent(@event, distributionPolicy);

            SendOutEmailsAboutEvent(distributionPolicy.ReceiverIds, eventInfo);
        }
        public void EventIsStoredSuccessfully()
        {
            var provider = new DatabaseSessionProvider();
            var repository = new EventRepository(provider);
            var eventInfo = new NewEmailConfirmedDeveloper(1);
            var @event = new Event(eventInfo);
            var receivers = new[] {30, 31, 32};
            var distributionPolicy = new DistributionPolicy(receivers);

            repository.DistrubuteEvent(@event, distributionPolicy);
            var receivedEvent = repository.GetEventsByUser(receivers[0], false).Single();

            Assert.AreEqual(@event.EventInfo, receivedEvent.EventInfo);
            Assert.AreEqual(@event.EventType, receivedEvent.EventType);
        }
        //, string[] reccurrence)
        public string MoveEvent(DateTime Start, DateTime End, string Creator, string Organizer, string Location, string Title, string eventId, string destinationCalendar, string UserCalendar, int sequence, string description, string reccurrenceRule, bool isAllDay, string ColorId = "")
        {
            string movedEventId = string.Empty;
            try
            {
                int orgId = 0;
                orgId = GetOrgId(UserCalendar, orgId);
                if (orgId == 0)
                {
                    return movedEventId;
                }
                //if (service == null)
                //{
                SetCalendar(orgId);
                //}

                Event evnt = new Event();

                //if (ColorId == "")
                //{ evnt.ColorId = "5"; }
                if (ColorId != "")
                { evnt.ColorId = ColorId; }

                if (isAllDay)
                {
                    evnt.Start = new EventDateTime
                    {

                        Date = Start.ToString("yyyy-MM-dd"),
                        TimeZone = "Australia/Melbourne"//"Etc/UTC"
                    };
                    evnt.End = new EventDateTime
                    {
                        Date = End.ToString("yyyy-MM-dd"),
                        TimeZone = "Australia/Melbourne"//"Etc/UTC"
                    };
                    evnt.Transparency = "transparent";
                }
                else
                {
                    evnt.Start = new EventDateTime
                    {
                        DateTime = Start,
                        TimeZone = "Australia/Melbourne"//"Etc/UTC"
                    };
                    evnt.End = new EventDateTime
                    {
                        DateTime = End,
                        TimeZone = "Australia/Melbourne"//"Etc/UTC"
                    };
                }

                evnt.Created = DateTime.Today;
                evnt.Creator = new Event.CreatorData
                {
                    DisplayName = Creator
                };

                evnt.Organizer = new Event.OrganizerData
                {
                    DisplayName = Organizer,
                };
                //evnt.Location = Location;

                //evnt.Description = "SalesContact : " + SalesContact + Environment.NewLine + Environment.NewLine + "Required On :" + Convert.ToDateTime(RequiredDate).ToShortDateString() + Environment.NewLine + Environment.NewLine + "Product Description :" + ProductDescription + Environment.NewLine + Environment.NewLine + "Supplier Instructions :" + Comments.Replace("Supplier Instructions", "") + Environment.NewLine + Environment.NewLine + "Order Item (Please donot delete or update OrderItemId) :" + OrderId;
                evnt.Summary = Title;
                evnt.Description = description;
                //evnt.Id = eventViewModel.EventId;
                evnt.Location = Location;
                evnt.Sequence = sequence;
                if (reccurrenceRule.Trim() != string.Empty)
                {
                    string recc = "RRULE:" + reccurrenceRule;// +"\r\n";//"DTSTART;VALUE=DATE:" + DateTime.Parse(StartDate).ToString("yyyyMMdd").Replace("-","") + "\r\n" + "DTEND;VALUE=DATE:" + DateTime.Parse(EndDate).ToString("yyyyMMdd").Replace("-", "") + "\r\n" +
                    List<string> reccurrence = new List<string>();
                    reccurrence.Add(recc);
                    evnt.Recurrence = reccurrence;
                    //evnt.RecurringEventId = "1234";
                    //evnt.OriginalStartTime = evnt.Start;
                }
                var test = service.Events.Move(UserCalendar, eventId, destinationCalendar).Execute();
                var updatedEvent = service.Events.Update(evnt, destinationCalendar, eventId).Execute();
                movedEventId = updatedEvent.Id;
                UpdateEventInDB(eventId, UserCalendar);
                InsertReturnedEventDetailsInDB(destinationCalendar, updatedEvent);

            }
            catch (Exception ex)
            {

                if (ex.Message.Contains("Cannot change the organizer of an instance. [400]"))
                {
                    movedEventId = "CustomError400";
                }

                File.AppendAllText("C:\\Google.txt", ex.Message + Environment.NewLine + ex.InnerException + Environment.NewLine + ex.StackTrace);
            }
            return movedEventId;
        }
        public string CreateEvent(string Title, string Location, string SalesContact, DateTime StartDate, DateTime EndDate, string Comments, string ProductDescription, string OrderId, string RequiredDate, string ColorId, string UserCalendar, string description, string reccurrenceRule, bool isAllDay)
        {
            string eventIdAndSequence = string.Empty;
            try
            {
                int orgId = 0;
                orgId = GetOrgId(UserCalendar, orgId);
                if (orgId == 0)
                {
                    return eventIdAndSequence;
                }
                SetCalendar(orgId);
                Event evnt = new Event();
                if (ColorId != "")
                { evnt.ColorId = ColorId; }

                if (isAllDay)
                {
                    evnt.Start = new EventDateTime
                    {

                        Date = StartDate.ToString("yyyy-MM-dd"),
                        TimeZone = "Australia/Melbourne"
                    };
                    evnt.End = new EventDateTime
                    {
                        Date = EndDate.AddDays(1).ToString("yyyy-MM-dd"),
                        TimeZone = "Australia/Melbourne"//TimeZoneInfo.Local.Id//"Etc/UTC"
                    };
                    evnt.Transparency = "transparent";
                }
                else
                {
                    //string startFormat = StartDate;
                    evnt.Start = new EventDateTime
                    {

                        DateTime = StartDate,
                        TimeZone = "Australia/Melbourne"
                    };

                    evnt.End = new EventDateTime
                    {
                        DateTime = EndDate,
                        TimeZone = "Australia/Melbourne"
                    };
                }

                evnt.Description = description;
                evnt.Summary = Title;
                evnt.Location = Location;

                if (reccurrenceRule.Trim() != string.Empty)
                {
                    string recc = "RRULE:" + reccurrenceRule;
                    List<string> reccurrence = new List<string>();
                    reccurrence.Add(recc);
                    evnt.Recurrence = reccurrence;
                }

                var returnedEventDetailes = service.Events.Insert(evnt, UserCalendar).Execute();
                eventIdAndSequence = returnedEventDetailes.Id + "," + returnedEventDetailes.Sequence;
                InsertReturnedEventDetailsInDB(UserCalendar, returnedEventDetailes);

            }
            catch (Exception ex)
            {
                File.AppendAllText("C:\\Google.txt", ex.Message + Environment.NewLine + ex.InnerException + Environment.NewLine + ex.StackTrace);
            }
            return eventIdAndSequence;
        }
        private void InsertReturnedEventDetailsInDB(string UserCalendar, Event returnedEventDetailes, bool isAllDay = false)
        {
            string Calendar = UserCalendar;
            string EventId = returnedEventDetailes.Id;
            string RecurringEventId = returnedEventDetailes.RecurringEventId;
            if (RecurringEventId == null) { RecurringEventId = ""; };
            var Attendees = returnedEventDetailes.Attendees;
            string Attendes = "";
            if (Attendees != null)
            {

                for (int j = 0; j < Attendees.Count; j++)
                { Attendes = Attendes + "~" + Attendees[j]; };
            }
            string ColorId = returnedEventDetailes.ColorId;
            if (ColorId == null) { ColorId = ""; };
            bool? AttendeesOmitted = returnedEventDetailes.AttendeesOmitted;
            if (AttendeesOmitted == null) { AttendeesOmitted = false; };
            bool? AnyoneCanAddSelf = returnedEventDetailes.AnyoneCanAddSelf;
            if (AnyoneCanAddSelf == null) { AnyoneCanAddSelf = false; };
            string Title = returnedEventDetailes.Summary;
            if (Title == null) { Title = ""; }
            string Location = returnedEventDetailes.Location;
            if (Location == null) { Location = ""; }
            string Description = returnedEventDetailes.Description;
            if (Description == null) { Description = ""; }
            string Kind = returnedEventDetailes.Kind;
            if (Kind == null) { Kind = ""; }

            DateTime StartDate = Convert.ToDateTime("01/01/1900");

            if (returnedEventDetailes.Start != null)
            {
                if (returnedEventDetailes.Start.DateTime != null)
                    StartDate = Convert.ToDateTime(returnedEventDetailes.Start.DateTime);
                else
                {
                    StartDate = Convert.ToDateTime(returnedEventDetailes.Start.Date);
                }
            }

            //{
            //    StartDate = Convert.ToDateTime(returnedEventDetailes.Start.DateTime);

            //}
            if (StartDate != null && StartDate.ToString().Contains("0001")) { StartDate = Convert.ToDateTime("01/01/1900"); }
            DateTime EndDate = Convert.ToDateTime("01/01/1900");
            if (returnedEventDetailes.End != null)
            {
                if (returnedEventDetailes.End.DateTime != null)
                    EndDate = Convert.ToDateTime(returnedEventDetailes.End.DateTime);
                else
                {
                    EndDate = Convert.ToDateTime(returnedEventDetailes.End.Date);
                }

            }
            if (EndDate != null && EndDate.ToString().Contains("0001")) { EndDate = Convert.ToDateTime("01/01/1900"); }

            if (isAllDay)
            {
                EndDate = EndDate.AddDays(-1);
            }

            DateTime MovedOn = Convert.ToDateTime("01/01/1900");
            if (returnedEventDetailes.Created != null)
            { MovedOn = Convert.ToDateTime(returnedEventDetailes.Created); }
            if (MovedOn != null && MovedOn.ToString().Contains("0001")) { MovedOn = Convert.ToDateTime("01/01/1900"); }
            string Status = returnedEventDetailes.Status;
            var Recurrence = returnedEventDetailes.Recurrence;
            string Recurrences = "";
            if (Recurrence != null)
            {

                for (int j = 0; j < Recurrence.Count; j++)
                { Recurrences = Recurrences + "~" + Recurrence[j]; };
            }
            string Organizer = "";
            if (returnedEventDetailes.Organizer != null)
            {
                Organizer = returnedEventDetailes.Organizer.DisplayName;
            }
            if (Organizer == null) { Organizer = ""; }
            string Creator = "";
            if (returnedEventDetailes.Creator != null)
            {
                Creator = returnedEventDetailes.Creator.DisplayName;
            }
            if (Creator == null) { Creator = ""; }
            bool? Locked = false;
            try
            {
                Locked = returnedEventDetailes.Locked;
            }
            catch { };
            Locked = false;
            //if (Locked == null) { Locked = false; };
            int Sequence = 0;
            if (returnedEventDetailes.Sequence != null)
            { Sequence = (int)returnedEventDetailes.Sequence; }
            string Transperancy = returnedEventDetailes.Transparency;
            if (Transperancy == null) { Transperancy = ""; }
            DateTime Updated = Convert.ToDateTime("01/01/1900");
            if (returnedEventDetailes.Updated != null)
            { Updated = Convert.ToDateTime(returnedEventDetailes.Updated); }
            if (Updated != null && Updated.ToString().Contains("0001")) { Updated = Convert.ToDateTime("01/01/1900"); }
            string Visibility = returnedEventDetailes.Visibility;
            if (Visibility == null) { Visibility = ""; }
            InsertOtherEvents(Calendar, EventId, RecurringEventId, ColorId, Attendes, AttendeesOmitted, AnyoneCanAddSelf, Locked, Title, Location, Description, Kind, StartDate, EndDate, MovedOn, Status, Recurrences, Organizer, Creator, Sequence, Transperancy, Updated, Visibility, isAllDay);
        }
        //, string[] reccurrence)
        public string UpdateEvent(DateTime Start, DateTime End, string Creator, string Organizer, string Location, string Title, string eventId, string UserCalendar, int sequence, string description, string reccurrenceRule, bool isAllDay, string ColorId = "")
        {
            string updatedEventId = string.Empty;
            try
            {
                int orgId = 0;
                orgId = GetOrgId(UserCalendar, orgId);
                if (orgId == 0)
                {
                    return updatedEventId;
                }
                //if (service == null)
                //{
                SetCalendar(orgId);
                //}

                // CheckCalendarEvents("*****@*****.**");

                Event evnt = new Event();

                //if (ColorId == "")
                //{ evnt.ColorId = "5"; }
                if (ColorId != "")
                { evnt.ColorId = ColorId; }

                if (isAllDay)
                {
                    evnt.Start = new EventDateTime
                    {

                        Date = Start.ToString("yyyy-MM-dd"),
                        TimeZone = "Australia/Melbourne"//"Etc/UTC"
                    };

                    evnt.End = new EventDateTime
                    {
                        Date = End.AddDays(1).ToString("yyyy-MM-dd"),
                        // Date = End.ToString("yyyy-MM-dd"), here we are adding one day becoz at google take less one day in all day event so we explicity adding one day
                        TimeZone = "Australia/Melbourne"//"Etc/UTC"
                    };

                    evnt.Transparency = "transparent";
                }
                else
                {
                    evnt.Start = new EventDateTime
                    {
                        DateTime = Start,
                        TimeZone = "Australia/Melbourne"//"Etc/UTC"
                    };
                    evnt.End = new EventDateTime
                    {
                        DateTime = End,
                        TimeZone = "Australia/Melbourne"//"Etc/UTC"
                    };
                }

                evnt.Created = DateTime.Today;
                evnt.Creator = new Event.CreatorData
                {
                    DisplayName = Creator
                };

                evnt.Organizer = new Event.OrganizerData
                {
                    DisplayName = Organizer,
                };
                //evnt.Location = Location;

                evnt.Description = description;//"SalesContact : " + SalesContact + Environment.NewLine + Environment.NewLine + "Supplier Instructions :" ;
                evnt.Summary = Title;
                //evnt.Description = Title + "Desc";
                evnt.Id = eventId;
                evnt.Location = Location;
                evnt.Sequence = sequence;
                if (reccurrenceRule.Trim() != string.Empty)
                {
                    string recc = "RRULE:" + reccurrenceRule;// +"\r\n";//"DTSTART;VALUE=DATE:" + DateTime.Parse(StartDate).ToString("yyyyMMdd").Replace("-","") + "\r\n" + "DTEND;VALUE=DATE:" + DateTime.Parse(EndDate).ToString("yyyyMMdd").Replace("-", "") + "\r\n" +
                    List<string> reccurrence = new List<string>();
                    reccurrence.Add(recc);
                    evnt.Recurrence = reccurrence;

                    //evnt.RecurringEventId = "1234";
                    //evnt.OriginalStartTime = evnt.Start;
                }

                var updatedEvent = service.Events.Update(evnt, UserCalendar, eventId).Execute();
                eventId = updatedEvent.Id;
                InsertReturnedEventDetailsInDB(UserCalendar, updatedEvent, isAllDay);
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("Cannot turn an instance of a recurring event into a recurring event itself. [400]"))
                {
                    updatedEventId = "CustomError401";
                }

                File.AppendAllText("C:\\Google.txt", ex.Message + Environment.NewLine + ex.InnerException + Environment.NewLine + ex.StackTrace);
            }
            return updatedEventId;
        }
        //, string[] reccurrence)
        public string UpdateEvent(DateTime Start, DateTime End, string Creator, string Organizer, string Location, string Title, string eventId, string UserCalendar, int sequence, string description, string reccurrenceRule,bool isAllDay,string ColorId="")
        {
            string updatedEventId = string.Empty;
            SetCalendar();

            Event evnt = new Event();

            if (ColorId == "")
            { evnt.ColorId = "5"; }
            if (ColorId != "")
            { evnt.ColorId = ColorId; }

            if (isAllDay)
            {
                evnt.Start = new EventDateTime
                {

                    Date = Start.ToString("yyyy-MM-dd"),
                    TimeZone = "Etc/UTC"
                };
                evnt.End = new EventDateTime
                {
                    Date = End.ToString("yyyy-MM-dd"),
                    TimeZone = "Etc/UTC"
                };
                evnt.Transparency = "transparent";
            }
            else
            {
                evnt.Start = new EventDateTime
                {
                    DateTime = Start,
                    TimeZone = "Etc/UTC"
                };
                evnt.End = new EventDateTime
                {
                    DateTime = End,
                    TimeZone = "Etc/UTC"
                };
            }

            evnt.Created = DateTime.Today;
            evnt.Creator = new Event.CreatorData
            {
                DisplayName = Creator
            };

            evnt.Organizer = new Event.OrganizerData
            {
                DisplayName = Organizer,
            };
            //evnt.Location = Location;

            evnt.Description = description;//"SalesContact : " + SalesContact + Environment.NewLine + Environment.NewLine + "Supplier Instructions :" ;
            evnt.Summary = Title;
            //evnt.Description = Title + "Desc";
            evnt.Id = eventId;
            evnt.Location = Location;
            evnt.Sequence = sequence;
            if (reccurrenceRule.Trim() != string.Empty)
            {
                string recc = "RRULE:" + reccurrenceRule;// +"\r\n";//"DTSTART;VALUE=DATE:" + DateTime.Parse(StartDate).ToString("yyyyMMdd").Replace("-","") + "\r\n" + "DTEND;VALUE=DATE:" + DateTime.Parse(EndDate).ToString("yyyyMMdd").Replace("-", "") + "\r\n" +
                List<string> reccurrence = new List<string>();
                reccurrence.Add(recc);
                evnt.Recurrence = reccurrence;
                //evnt.RecurringEventId = "1234";
                //evnt.OriginalStartTime = evnt.Start;
            }
            var updatedEvent = service.Events.Update(evnt, UserCalendar, eventId).Execute();
            eventId = updatedEvent.Id;
            InsertReturnedEventDetailsInDB(UserCalendar, updatedEvent);
            return updatedEventId;
        }
        public string CreateEvent(string Title, string Location, string SalesContact, DateTime StartDate, DateTime EndDate, string Comments, string ProductDescription, string OrderId, string RequiredDate, string ColorId, string UserCalendar,string description, string reccurrenceRule,bool isAllDay)
        {
            string eventId = string.Empty;
            try
            {
                string text = File.ReadAllText("C://Google.txt");
                text = text + Environment.NewLine + "Set Calendar Called";
                File.WriteAllText("C://Google.txt", text);
                SetCalendar();
                text = text + Environment.NewLine + "Set Calendar Passed";
                File.WriteAllText("C://Google.txt", text);
                Event evnt = new Event();
                if (ColorId == "")
                { evnt.ColorId = "5"; }
                if (ColorId != "")
                { evnt.ColorId = ColorId; }

                if (isAllDay)
                {
                    evnt.Start = new EventDateTime
                    {

                        Date = StartDate.ToString("yyyy-MM-dd"),
                        TimeZone = "Etc/UTC"
                    };
                    evnt.End = new EventDateTime
                    {
                        Date = EndDate.ToString("yyyy-MM-dd"),
                        TimeZone = "Etc/UTC"
                    };
                    evnt.Transparency = "transparent";
                }
                else
                {
                    //string startFormat = StartDate;
                    evnt.Start = new EventDateTime
                    {

                        DateTime = StartDate,
                        TimeZone = "Etc/UTC"
                    };

                    evnt.End = new EventDateTime
                    {
                        DateTime = EndDate,
                        TimeZone = "Etc/UTC"
                    };
                }

                evnt.Description = description;// "SalesContact : " + SalesContact + Environment.NewLine + Environment.NewLine + "Required On :" + Environment.NewLine + Environment.NewLine + "Product Description :" + ProductDescription + Environment.NewLine + Environment.NewLine + "Supplier Instructions :" + Comments.Replace("Supplier Instructions", "") + Environment.NewLine + Environment.NewLine + "Order Item (Please donot delete or update OrderItemId) :" + OrderId;
                evnt.Summary = Title;
                evnt.Location = Location;

                if (reccurrenceRule.Trim() != string.Empty)
                {
                    string recc = "RRULE:" + reccurrenceRule;// +"\r\n";//"DTSTART;VALUE=DATE:" + DateTime.Parse(StartDate).ToString("yyyyMMdd").Replace("-","") + "\r\n" + "DTEND;VALUE=DATE:" + DateTime.Parse(EndDate).ToString("yyyyMMdd").Replace("-", "") + "\r\n" +
                    List<string> reccurrence = new List<string>();
                    reccurrence.Add(recc);
                    evnt.Recurrence = reccurrence;
                    //evnt.RecurringEventId = "1234";
                    //evnt.OriginalStartTime = evnt.Start;
                }

                var returnedEventDetailes = service.Events.Insert(evnt, UserCalendar).Execute();
                eventId = returnedEventDetailes.Id;
                InsertReturnedEventDetailsInDB(UserCalendar, returnedEventDetailes);
                File.WriteAllText("C://Google.txt", "Event added");
                //service.Events.Insert(evnt, "*****@*****.**").Execute();
                //  service.Events.Insert(evnt, "*****@*****.**").Execute();

            }
            catch (Exception ex)
            {
                File.AppendAllText("C:\\Google.txt",ex.Message + Environment.NewLine + ex.InnerException + Environment.NewLine + ex.StackTrace);
            }
            return eventId;
        }