Example #1
0
        public RelationshipEvent CreateItem(RelationshipEvent item)
        {
            _crmSecurity.DemandCreateOrUpdate(item);

            var htmlBody = String.Empty;

            if (item.CreateOn == DateTime.MinValue)
            {
                item.CreateOn = _tenantUtil.DateTimeNow();
            }

            item.CreateBy      = _securityContext.CurrentAccount.ID;
            item.LastModifedBy = _securityContext.CurrentAccount.ID;

            if (item.CategoryID == (int)HistoryCategorySystem.MailMessage)
            {
                //     var jsonObj = JsonDocument.Parse(item.Content).RootElement;
                //       var messageId = jsonObj.GetProperty("message_id").GetInt32();

                //var apiServer = new ApiServer();
                //var msg = apiServer.GetApiResponse(
                //    String.Format("{0}mail/messages/{1}.json?id={1}&loadImages=true&needSanitize=true", SetupInfo.WebApiBaseUrl, messageId), "GET");

                //        String msg = null;
                //                if (msg == null)
                throw new ArgumentException("Mail message cannot be found");

                //var msgResponseDto = JObject.Parse(Encoding.UTF8.GetString(Convert.FromBase64String(msg)));
                //var msgRequestObj = msgResponseDto.Value<JObject>("response");
                //string messageUrl;

                //htmlBody = msgRequestObj.Value<String>("htmlBody");

                //using (var fileStream = new MemoryStream(Encoding.UTF8.GetBytes(htmlBody)))
                //{
                //    var filePath = String.Format("folder_{0}/message_{1}.html", (messageId / 1000 + 1) * 1000, messageId);

                //    Global.GetStore().Save("mail_messages", filePath, fileStream);

                //    messageUrl = String.Format("{0}HttpHandlers/filehandler.ashx?action=mailmessage&message_id={1}", PathProvider.BaseAbsolutePath, messageId).ToLower();

                //}

                //var msg_date_created = msgRequestObj.Value<String>("date");
                //var message_id = msgRequestObj.Value<Int32>("id");
                //item.Content = JsonConvert.SerializeObject(new
                //{
                //    @from = msgRequestObj.Value<String>("from"),
                //    to = msgRequestObj.Value<String>("to"),
                //    cc = msgRequestObj.Value<String>("cc"),
                //    bcc = msgRequestObj.Value<String>("bcc"),
                //    subject = msgRequestObj.Value<String>("subject"),
                //    important = msgRequestObj.Value<Boolean>("important"),
                //    chain_id = msgRequestObj.Value<String>("chainId"),
                //    is_sended = msgRequestObj.Value<Int32>("folder") != 1,
                //    date_created = msg_date_created,
                //    introduction = msgRequestObj.Value<String>("introduction"),
                //    message_id = message_id,
                //    message_url = messageUrl
                //});

                //item.CreateOn = DateTime.Parse(msg_date_created, CultureInfo.InvariantCulture);

                //var sqlQueryFindMailsAlready = Query(CRMDbContext.RelationshipEvent)
                //                                    .Where(x => x.ContactId == item.ContactID)
                //                                    .Where(x => x.EntityType == item.EntityType)
                //                                    .Where(x => x.EntityId == item.EntityID)
                //                                    .Where(x => x.CategoryId == item.CategoryID)
                //                                    .Where(x => Microsoft.EntityFrameworkCore.EF.Functions.Like(x.Content, String.Format("\"message_id\":{0},", message_id)));

                //if (sqlQueryFindMailsAlready.Count() > 0)
                //    throw new Exception("Already exists");
            }

            var itemToInsert = new DbRelationshipEvent
            {
                ContactId     = item.ContactID,
                Content       = item.Content,
                CreateOn      = _tenantUtil.DateTimeToUtc(item.CreateOn),
                CreateBy      = item.CreateBy,
                EntityType    = item.EntityType,
                EntityId      = item.EntityID,
                CategoryId    = item.CategoryID,
                LastModifedOn = DateTime.UtcNow,
                LastModifedBy = item.LastModifedBy,
                TenantId      = TenantID,
                HaveFiles     = false
            };

            CrmDbContext.RelationshipEvent.Add(itemToInsert);
            CrmDbContext.SaveChanges();

            item.ID = itemToInsert.Id;

            if (item.CreateOn.Kind == DateTimeKind.Utc)
            {
                item.CreateOn = _tenantUtil.DateTimeFromUtc(item.CreateOn);
            }

            _factoryIndexer.Index(itemToInsert);

            return(item);
        }