예제 #1
0
        public AssociationUserTravelAgencyRole SaveTravelAgencySupervisor(int managerId, AssociationUserTravelAgencyRole supervisorRole)
        {
            using (var db = new LomsContext())
            {

                AssociationEmail emailProvider;

                if (supervisorRole.UserId == 0)
                {
                    var user = (from u in db.AssociationUsers
                                where u.AssociationId == CurrentAssociationId && u.Email == supervisorRole.UserEmail
                                select u)
                                .SingleOrDefault();

                    if (user == null || !user.HasOnlineAccess)
                    {
                        supervisorRole.Errors.AddError("UserEmail", "User is not registered!");
                        return supervisorRole;
                    }
                    else if (!user.IsTravelAgency)
                    {
                        supervisorRole.Errors.AddError("UserEmail", "User is not a travel agent!");
                        return supervisorRole;
                    }
                    else
                    {
                        var currenntRole = (from r in db.AssociationUserTravelAgencyRoles
                                            where r.UserId == user.Id
                                            select r).SingleOrDefault();

                        if (currenntRole != null)
                        {
                            if (currenntRole.AgencyId != supervisorRole.AgencyId)
                            {
                                supervisorRole.Errors.AddError("UserEmail", "User is a travel agent in another travel agency!");
                                return supervisorRole;
                            }
                            else if (currenntRole.AgencyId == supervisorRole.AgencyId &&
                                    (currenntRole.Role == TravelAgencyRole.Manager || currenntRole.Role == TravelAgencyRole.SupervisorAsManager || currenntRole.Role == TravelAgencyRole.Supervisor))
                            {
                                supervisorRole.Errors.AddError("UserEmail", "User is a existing travel team supervisor!");
                                return supervisorRole;
                            }
                            else
                            {
                                //upgrade
                                currenntRole.Role = supervisorRole.Role;
                                db.AssociationUserTravelAgencyRoles.ApplyChanges(supervisorRole);
                                db.SaveChanges();

                                //send upgrade email
                                emailProvider = (from e in db.AssociationEmails
                                                 where e.AssociationId == CurrentAssociationId
                                                 select e)
                                                    .FirstOrDefault();

                                if (emailProvider != null)
                                {
                                    var association = (from a in db.Associations
                                                       where a.Id == CurrentAssociationId
                                                       select a)
                                                     .SingleOrDefault();

                                    var request = HttpContext.Current.Request;
                                    var uri = request.Url;
                                    string baseUrl = String.Format("{0}://{1}:{2}", uri.Scheme, uri.Host ?? "80", uri.Port);
                                    string contactUsLink = Path.Combine(baseUrl + "/#Contact");

                                    var txtContent = MailTemplateHelper.GetTravelAgentIsUpgradedToTravelSupervisorTxtContent(association.Name.ToUpper(), user.FullName.ToUpper(), contactUsLink, "TRAVEL AGENCY");
                                    var htmlContent = MailTemplateHelper.GetTravelAgentIsUpgradedToTravelSupervisorHtmlContent(association.Name.ToUpper(), user.FullName.ToUpper(), contactUsLink, "TRAVEL AGENCY");
                                    var avBody = AlternateView.CreateAlternateViewFromString(htmlContent, null, MediaTypeNames.Text.Html);

                                    emailProvider.SendMail(user.Email, association.Name.ToUpper() + " Supervisor Upgrade", txtContent, null, avBody, true);
                                }

                                return currenntRole;
                            }
                        }
                    }

                    supervisorRole.UserId = user.Id;
                    supervisorRole.Status = TravelAgencyStatus.Pending;
                    db.AssociationUserTravelAgencyRoles.ApplyChanges(supervisorRole);

                    //start activation process
                    var activation = new AssociationUserTravelAgencyRoleActivation();
                    activation.UserId = user.Id;
                    activation.AgencyId = supervisorRole.AgencyId;
                    activation.Guid = Guid.NewGuid();
                    activation.ExpiryTime = DateTime.UtcNow.AddDays(7.0);
                    db.AssociationUserTravelAgencyRoleActivations.ApplyChanges(activation);
                    db.SaveChanges();

                    //send activation email
                    emailProvider = (from e in db.AssociationEmails
                                     where e.AssociationId == CurrentAssociationId
                                     select e)
                                        .FirstOrDefault();

                    if (emailProvider != null)
                    {
                        var association = (from a in db.Associations
                                           where a.Id == CurrentAssociationId
                                           select a)
                                         .SingleOrDefault();

                        var manager = (from u in db.AssociationUsers
                                       where u.Id == managerId
                                       select u)
                                        .SingleOrDefault();


                        var request = HttpContext.Current.Request;
                        var uri = request.Url;
                        string baseUrl = String.Format("{0}://{1}:{2}", uri.Scheme, uri.Host ?? "80", uri.Port);
                        string activtionLink = Path.Combine(baseUrl + string.Format("/#SupervisorActivation/{0}", activation.Guid.ToString("D")));
                        string contactUsLink = Path.Combine(baseUrl + "/#Contact");
                        string termAndConditionsLink = Path.Combine(baseUrl + "/terms");

                        var txtContent = MailTemplateHelper.GetTravelAgentIsAddedAsTravelSupervisorTxtContent(association.Name.ToUpper(), user.FullName.ToUpper(), activtionLink, contactUsLink, "TRAVEL AGENCY", "Group", manager.FullName, manager.Email, termAndConditionsLink);
                        var htmlContent = MailTemplateHelper.GetTravelAgentIsAddedAsTravelSupervisorHtmlContent(association.Name.ToUpper(), user.FullName.ToUpper(), activtionLink, contactUsLink, "TRAVEL AGENCY", "Group", manager.FullName, manager.Email, termAndConditionsLink);
                        var avBody = AlternateView.CreateAlternateViewFromString(htmlContent, null, MediaTypeNames.Text.Html);

                        emailProvider.SendMail(user.Email, association.Name.ToUpper() + " Supervisor Add", txtContent, null, avBody, true);
                    }


                    supervisorRole.UserFirstName = user.FirstName;
                    supervisorRole.UserLastName = user.LastName;
                    return supervisorRole;
                }
                else
                {
                    //only role can be changed
                    db.AssociationUserTravelAgencyRoles.ApplyChanges(supervisorRole);
                    db.SaveChanges();
                    return supervisorRole;
                }
            }
        }
     public bool Equals(AssociationUserTravelAgencyRoleActivation other)
     {
         if (ReferenceEquals(null, other)) return false;
         if (ReferenceEquals(this, other)) return true;
 		if (other.UserId == 0 && UserId == 0)
 			return false;
 		else
 			return other.UserId == UserId;
     }