public IHttpActionResult PostInteractions([FromBody] List <MobileInteractionSession> sessions, Guid?personalDeviceGuid = null) { var person = GetPerson(); var ipAddress = System.Web.HttpContext.Current?.Request?.UserHostAddress; using (var rockContext = new Data.RockContext()) { var interactionChannelService = new InteractionChannelService(rockContext); var interactionComponentService = new InteractionComponentService(rockContext); var interactionSessionService = new InteractionSessionService(rockContext); var interactionService = new InteractionService(rockContext); var userLoginService = new UserLoginService(rockContext); var channelMediumTypeValue = DefinedValueCache.Get(SystemGuid.DefinedValue.INTERACTIONCHANNELTYPE_WEBSITE); var pageEntityTypeId = EntityTypeCache.Get(typeof(Model.Page)).Id; // // Check to see if we have a site and the API key is valid. // if (MobileHelper.GetCurrentApplicationSite() == null) { return(StatusCode(System.Net.HttpStatusCode.Forbidden)); } // // Get the personal device identifier if they provided it's unique identifier. // int?personalDeviceId = null; if (personalDeviceGuid.HasValue) { personalDeviceId = new PersonalDeviceService(rockContext).GetId(personalDeviceGuid.Value); } rockContext.WrapTransaction(() => { foreach (var mobileSession in sessions) { var interactionGuids = mobileSession.Interactions.Select(i => i.Guid).ToList(); var existingInteractionGuids = interactionService.Queryable() .Where(i => interactionGuids.Contains(i.Guid)) .Select(i => i.Guid) .ToList(); // // Loop through all interactions that don't already exist and add each one. // foreach (var mobileInteraction in mobileSession.Interactions.Where(i => !existingInteractionGuids.Contains(i.Guid))) { int?interactionComponentId = null; // // Lookup the interaction channel, and create it if it doesn't exist // if (mobileInteraction.AppId.HasValue && mobileInteraction.PageGuid.HasValue) { var site = SiteCache.Get(mobileInteraction.AppId.Value); var page = PageCache.Get(mobileInteraction.PageGuid.Value); if (site == null || page == null) { continue; } // // Try to find an existing interaction channel. // var interactionChannelId = interactionChannelService.Queryable() .Where(a => a.ChannelTypeMediumValueId == channelMediumTypeValue.Id && a.ChannelEntityId == site.Id) .Select(a => ( int? )a.Id) .FirstOrDefault(); // // If not found, create one. // if (!interactionChannelId.HasValue) { var interactionChannel = new InteractionChannel { Name = site.Name, ChannelTypeMediumValueId = channelMediumTypeValue.Id, ChannelEntityId = site.Id, ComponentEntityTypeId = pageEntityTypeId }; interactionChannelService.Add(interactionChannel); rockContext.SaveChanges(); interactionChannelId = interactionChannel.Id; } // // Get an existing or create a new component. // var interactionComponent = interactionComponentService.GetComponentByChannelIdAndEntityId(interactionChannelId.Value, page.Id, page.InternalName); rockContext.SaveChanges(); interactionComponentId = interactionComponent.Id; } else if (mobileInteraction.ChannelId.HasValue) { var interactionChannelId = mobileInteraction.ChannelId; if (mobileInteraction.ComponentId.HasValue) { interactionComponentId = mobileInteraction.ComponentId.Value; } else if (mobileInteraction.ComponentName.IsNotNullOrWhiteSpace()) { // // Get an existing or create a new component. // var interactionComponent = interactionComponentService.GetComponentByComponentName(interactionChannelId.Value, mobileInteraction.ComponentName); rockContext.SaveChanges(); interactionComponentId = interactionComponent.Id; } else { continue; } } else { continue; } // // Add the interaction // if (interactionComponentId.HasValue) { var interaction = interactionService.CreateInteraction(interactionComponentId.Value, mobileInteraction.EntityId, mobileInteraction.Operation, mobileInteraction.Summary, mobileInteraction.Data, person?.PrimaryAliasId, mobileInteraction.DateTime, mobileSession.Application, mobileSession.OperatingSystem, mobileSession.ClientType, null, ipAddress, mobileSession.Guid); interaction.Guid = mobileInteraction.Guid; interaction.PersonalDeviceId = personalDeviceId; interactionService.Add(interaction); rockContext.SaveChanges(); } } } }); } return(Ok()); }
public IHttpActionResult PostInteractions([FromBody] List <MobileInteractionSession> sessions, Guid?personalDeviceGuid = null) { var person = GetPerson(); var ipAddress = System.Web.HttpContext.Current?.Request?.UserHostAddress; using (var rockContext = new Data.RockContext()) { var interactionChannelService = new InteractionChannelService(rockContext); var interactionComponentService = new InteractionComponentService(rockContext); var interactionSessionService = new InteractionSessionService(rockContext); var interactionService = new InteractionService(rockContext); var userLoginService = new UserLoginService(rockContext); var channelMediumTypeValue = DefinedValueCache.Get(SystemGuid.DefinedValue.INTERACTIONCHANNELTYPE_WEBSITE); var pageEntityTypeId = EntityTypeCache.Get(typeof(Model.Page)).Id; // // Check to see if we have a site and the API key is valid. // if (MobileHelper.GetCurrentApplicationSite() == null) { return(StatusCode(System.Net.HttpStatusCode.Forbidden)); } // // Get the personal device identifier if they provided it's unique identifier. // int?personalDeviceId = null; if (personalDeviceGuid.HasValue) { personalDeviceId = new PersonalDeviceService(rockContext).GetId(personalDeviceGuid.Value); } // // Create a quick way to cache data since we have to loop twice. // var interactionComponentLookup = new Dictionary <string, int>(); // // Helper method to get a cache key for looking up the component Id. // string GetComponentCacheKey(MobileInteraction mi) { return($"{mi.AppId}:{mi.PageGuid}:{mi.ChannelGuid}:{mi.ChannelId}:{mi.ComponentId}:{mi.ComponentName}"); } // // Interactions Components will now try to load from cache which // causes problems if we are inside a transaction. So first loop through // everything and make sure all our components and channels exist. // var prePassInteractions = sessions.SelectMany(a => a.Interactions) .DistinctBy(a => GetComponentCacheKey(a)); // // It's safe to do this pre-pass outside the transaction since we are just creating // the channels and components (if necessary), which is going to have to be done at // at some point no matter what. // foreach (var mobileInteraction in prePassInteractions) { // // Lookup the interaction channel, and create it if it doesn't exist // if (mobileInteraction.AppId.HasValue && mobileInteraction.PageGuid.HasValue) { var site = SiteCache.Get(mobileInteraction.AppId.Value); var page = PageCache.Get(mobileInteraction.PageGuid.Value); if (site == null || page == null) { continue; } // // Try to find an existing interaction channel. // var interactionChannelId = InteractionChannelCache.GetChannelIdByTypeIdAndEntityId(channelMediumTypeValue.Id, site.Id, site.Name, pageEntityTypeId, null); // // Get an existing or create a new component. // var interactionComponentId = InteractionComponentCache.GetComponentIdByChannelIdAndEntityId(interactionChannelId, page.Id, page.InternalName); interactionComponentLookup.AddOrReplace(GetComponentCacheKey(mobileInteraction), interactionComponentId); } else if (mobileInteraction.ChannelId.HasValue || mobileInteraction.ChannelGuid.HasValue) { int?interactionChannelId = null; if (mobileInteraction.ChannelId.HasValue) { interactionChannelId = mobileInteraction.ChannelId.Value; } else if (mobileInteraction.ChannelGuid.HasValue) { interactionChannelId = InteractionChannelCache.Get(mobileInteraction.ChannelGuid.Value)?.Id; } if (interactionChannelId.HasValue) { if (mobileInteraction.ComponentId.HasValue) { // Use the provided component identifier. interactionComponentLookup.AddOrReplace(GetComponentCacheKey(mobileInteraction), mobileInteraction.ComponentId.Value); } else if (mobileInteraction.ComponentName.IsNotNullOrWhiteSpace()) { int interactionComponentId; // Get or create a new component with the details we have. if (mobileInteraction.ComponentEntityId.HasValue) { interactionComponentId = InteractionComponentCache.GetComponentIdByChannelIdAndEntityId(interactionChannelId.Value, mobileInteraction.ComponentEntityId, mobileInteraction.ComponentName); } else { var interactionComponent = interactionComponentService.GetComponentByComponentName(interactionChannelId.Value, mobileInteraction.ComponentName); rockContext.SaveChanges(); interactionComponentId = interactionComponent.Id; } interactionComponentLookup.AddOrReplace(GetComponentCacheKey(mobileInteraction), interactionComponentId); } } } } // // Now wrap the actual interaction creation inside a transaction. We should // probably move this so it uses the InteractionTransaction class for better // performance. This is so we can inform the client that either everything // saved or that nothing saved. No partial saves here. // rockContext.WrapTransaction(() => { foreach (var mobileSession in sessions) { var interactionGuids = mobileSession.Interactions.Select(i => i.Guid).ToList(); var existingInteractionGuids = interactionService.Queryable() .Where(i => interactionGuids.Contains(i.Guid)) .Select(i => i.Guid) .ToList(); // // Loop through all interactions that don't already exist and add each one. // foreach (var mobileInteraction in mobileSession.Interactions.Where(i => !existingInteractionGuids.Contains(i.Guid))) { string cacheKey = GetComponentCacheKey(mobileInteraction); if (!interactionComponentLookup.ContainsKey(cacheKey)) { // Shouldn't happen, but just in case. continue; } var interactionComponentId = interactionComponentLookup[cacheKey]; // // Add the interaction // var interaction = interactionService.CreateInteraction(interactionComponentId, mobileInteraction.EntityId, mobileInteraction.Operation, mobileInteraction.Summary, mobileInteraction.Data, person?.PrimaryAliasId, RockDateTime.ConvertLocalDateTimeToRockDateTime(mobileInteraction.DateTime.LocalDateTime), mobileSession.Application, mobileSession.OperatingSystem, mobileSession.ClientType, null, ipAddress, mobileSession.Guid); interaction.Guid = mobileInteraction.Guid; interaction.PersonalDeviceId = personalDeviceId; interaction.RelatedEntityTypeId = mobileInteraction.RelatedEntityTypeId; interaction.RelatedEntityId = mobileInteraction.RelatedEntityId; interaction.ChannelCustom1 = mobileInteraction.ChannelCustom1; interaction.ChannelCustom2 = mobileInteraction.ChannelCustom2; interaction.ChannelCustomIndexed1 = mobileInteraction.ChannelCustomIndexed1; interactionService.Add(interaction); // Attempt to process this as a communication interaction. ProcessCommunicationInteraction(mobileSession, mobileInteraction, rockContext); } } rockContext.SaveChanges(); }); } return(Ok()); }
public object UpdateProfile(MobilePerson profile) { var user = UserLoginService.GetCurrentUser(false); if (user == null) { return(ActionStatusCode(System.Net.HttpStatusCode.Unauthorized)); } var personId = user.PersonId.Value; var rockContext = new Data.RockContext(); var personService = new PersonService(rockContext); var phoneNumberService = new PhoneNumberService(rockContext); var person = personService.Get(personId); person.NickName = person.NickName == person.FirstName ? profile.FirstName : person.NickName; person.FirstName = profile.FirstName; person.LastName = profile.LastName; var gender = (Model.Gender)profile.Gender; if (GenderVisibility != VisibilityTriState.Hidden) { person.Gender = gender; } if (GetAttributeValue(AttributeKeys.BirthDateShow).AsBoolean()) { person.SetBirthDate(profile.BirthDate?.Date); } if (GetAttributeValue(AttributeKeys.CampusShow).AsBoolean()) { person.PrimaryFamily.CampusId = profile.CampusGuid.HasValue ? CampusCache.Get(profile.CampusGuid.Value)?.Id : null; } if (GetAttributeValue(AttributeKeys.EmailShow).AsBoolean()) { person.Email = profile.Email; } if (GetAttributeValue(AttributeKeys.MobilePhoneShow).AsBoolean()) { int phoneNumberTypeId = DefinedValueCache.Get(SystemGuid.DefinedValue.PERSON_PHONE_TYPE_MOBILE).Id; var phoneNumber = person.PhoneNumbers.FirstOrDefault(n => n.NumberTypeValueId == phoneNumberTypeId); if (phoneNumber == null) { phoneNumber = new PhoneNumber { NumberTypeValueId = phoneNumberTypeId }; person.PhoneNumbers.Add(phoneNumber); } // TODO: What to do with country code? phoneNumber.CountryCode = PhoneNumber.CleanNumber("+1"); phoneNumber.Number = PhoneNumber.CleanNumber(profile.MobilePhone); if (string.IsNullOrWhiteSpace(phoneNumber.Number)) { person.PhoneNumbers.Remove(phoneNumber); phoneNumberService.Delete(phoneNumber); } } if (GetAttributeValue(AttributeKeys.AddressShow).AsBoolean()) { var addressTypeGuid = SystemGuid.DefinedValue.GROUP_LOCATION_TYPE_HOME.AsGuid(); var groupLocationService = new GroupLocationService(rockContext); var dvHomeAddressType = DefinedValueCache.Get(addressTypeGuid); var familyAddress = groupLocationService.Queryable().Where(l => l.GroupId == person.PrimaryFamily.Id && l.GroupLocationTypeValueId == dvHomeAddressType.Id).FirstOrDefault(); if (familyAddress != null && string.IsNullOrWhiteSpace(profile.HomeAddress.Street1)) { // delete the current address groupLocationService.Delete(familyAddress); } else { if (!string.IsNullOrWhiteSpace(profile.HomeAddress.Street1)) { if (familyAddress == null) { familyAddress = new GroupLocation(); groupLocationService.Add(familyAddress); familyAddress.GroupLocationTypeValueId = dvHomeAddressType.Id; familyAddress.GroupId = person.PrimaryFamily.Id; familyAddress.IsMailingLocation = true; familyAddress.IsMappedLocation = true; } else if (familyAddress.Location.Street1 != profile.HomeAddress.Street1) { // user clicked move so create a previous address var previousAddress = new GroupLocation(); groupLocationService.Add(previousAddress); var previousAddressValue = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.GROUP_LOCATION_TYPE_PREVIOUS.AsGuid()); if (previousAddressValue != null) { previousAddress.GroupLocationTypeValueId = previousAddressValue.Id; previousAddress.GroupId = person.PrimaryFamily.Id; Location previousAddressLocation = new Location { Street1 = familyAddress.Location.Street1, Street2 = familyAddress.Location.Street2, City = familyAddress.Location.City, State = familyAddress.Location.State, PostalCode = familyAddress.Location.PostalCode, Country = familyAddress.Location.Country }; previousAddress.Location = previousAddressLocation; } } // TODO: ??? // familyAddress.IsMailingLocation = cbIsMailingAddress.Checked; // familyAddress.IsMappedLocation = cbIsPhysicalAddress.Checked; familyAddress.Location = new LocationService(rockContext).Get( profile.HomeAddress.Street1, string.Empty, profile.HomeAddress.City, profile.HomeAddress.State, profile.HomeAddress.PostalCode, profile.HomeAddress.Country, person.PrimaryFamily, true); // since there can only be one mapped location, set the other locations to not mapped if (familyAddress.IsMappedLocation) { var groupLocations = groupLocationService.Queryable() .Where(l => l.GroupId == person.PrimaryFamily.Id && l.Id != familyAddress.Id).ToList(); foreach (var groupLocation in groupLocations) { groupLocation.IsMappedLocation = false; } } rockContext.SaveChanges(); } } } rockContext.SaveChanges(); var mobilePerson = MobileHelper.GetMobilePerson(person, MobileHelper.GetCurrentApplicationSite()); mobilePerson.AuthToken = MobileHelper.GetAuthenticationToken(user.UserName); return(ActionOk(mobilePerson)); }
public PersonalDevice UpdateByMACAddress(string macAddress, string deviceIdentifier = "", string devicePlatform = "", string deviceVersion = "", int?personAliasId = null) { var rockContext = new Data.RockContext(); var service = new PersonalDeviceService(rockContext); // MAC address var personalDevice = service.GetByMACAddress(macAddress); if (personalDevice == null) { personalDevice = new PersonalDevice(); personalDevice.MACAddress = macAddress; service.Add(personalDevice); } // unique identifier if (deviceIdentifier.IsNotNullOrWhitespace()) { personalDevice.DeviceUniqueIdentifier = deviceIdentifier; } // Platform if (devicePlatform.IsNotNullOrWhitespace()) { var dt = DefinedTypeCache.Read(Rock.SystemGuid.DefinedType.PERSONAL_DEVICE_PLATFORM.AsGuid()); DefinedValueCache dv = null; if (dt != null) { dv = dt.DefinedValues.FirstOrDefault(v => v.Value == devicePlatform); } if (dv == null) { dv = DefinedValueCache.Read(Rock.SystemGuid.DefinedValue.PERSONAL_DEVICE_PLATFORM_OTHER.AsGuid()); } personalDevice.PlatformValueId = dv != null ? dv.Id : (int?)null; } // Version if (deviceVersion.IsNotNullOrWhitespace()) { personalDevice.DeviceVersion = deviceVersion; } // Person if (personAliasId.HasValue) { var person = new PersonAliasService(rockContext).GetPerson(personAliasId.Value); if (person != null) { if (personalDevice.PersonAlias == null || personalDevice.PersonAlias.PersonId != person.Id) { personalDevice.PersonAliasId = personAliasId.Value; } // Update any associated interaction records for the device that do not have an associated person. if (personalDevice.Id != 0) { var interactionService = new InteractionService(rockContext); foreach (var interaction in interactionService.Queryable() .Where(i => i.PersonalDeviceId == personalDevice.Id && !i.PersonAliasId.HasValue)) { interaction.PersonAliasId = personAliasId.Value; } } } } rockContext.SaveChanges(); return(GetById(personalDevice.Id)); }