/// <inheritdoc />
        /// <summary>
        /// Send Notification
        /// </summary>
        /// <param name="usersIds"></param>
        /// <param name="notification"></param>
        /// <returns></returns>
        public virtual async Task SendNotificationAsync(IEnumerable <Guid> usersIds, Notification notification)
        {
            if (notification == null)
            {
                throw new NullReferenceException();
            }
            try
            {
                var users = usersIds.ToList();
                if (!users.Any())
                {
                    return;
                }
                if (!notification.SendLocal && !notification.SendEmail)
                {
                    return;
                }
                var emails = new HashSet <string>();
                foreach (var userId in users)
                {
                    var user = await _userManager.UserManager.FindByIdAsync(userId.ToString());

                    if (user == null)
                    {
                        continue;
                    }
                    //send email only if email was confirmed
                    if (notification.SendEmail && user.EmailConfirmed)
                    {
                        emails.Add(user.Email);
                    }

                    if (!notification.SendLocal)
                    {
                        continue;
                    }
                    notification.Id     = Guid.NewGuid();
                    notification.UserId = userId;
                    var tenant = await _userManager.IdentityContext.Tenants.FirstOrDefaultAsync(x => x.Id.Equals(user.TenantId));

                    var response = await _dataService.Add <SystemNotifications>(_dataService.GetDictionary(notification), tenant.MachineName);

                    if (!response.IsSuccess)
                    {
                        _logger.LogError("Fail to add new notification in database");
                    }
                }
                if (notification.SendLocal)
                {
                    _hub.SendNotification(users, NotificationMapper.Map(notification));
                }
                if (notification.SendEmail)
                {
                    await _emailSender.SendEmailAsync(emails, notification.Subject, notification.Content);
                }
            }
            catch (Exception e)
            {
                _logger.LogCritical(e.Message);
            }
        }
Esempio n. 2
0
        public static ObjectId DeepCreate(DynamicRecord parentRecord, DynamicRecord record, SchemaView view, ObjectId tenantId, ObjectId loggedUser, string roleCode, IDynamicService <DynamicRecord> dynamicService, ISchemaService schemaService, bool allowUpdate = false)
        {
            var complexRecords = record.Values().Where(v => view.Schema.Attributes.ContainsKey(v.Key) && (view.Schema.Attributes[v.Key].IsComplex || view.Schema.Attributes[v.Key].IsSubCatalog));

            complexRecords.ToList().ForEach(r =>
            {
                var subcatalogView = schemaService.GetView(view.Schema.Attributes[r.Key].DataType, ViewCategory.Create, tenantId, roleCode);  //this should go to cache all of the time, it would be expensive not to.

                // When manually creating an invoice, we need to check if Count > 0, otherwise FindExisting throws an exception
                if (r.Value is Dictionary <string, object> && ((Dictionary <string, object>)r.Value).Count > 0)
                {
                    var subRecord     = new DynamicRecord().Initialize <DynamicRecord>((Dictionary <string, object>)r.Value, null, ViewCategory.Create);
                    bool isValidInput = true;
                    subRecord._id     = FindExisting(subRecord, subcatalogView, tenantId, loggedUser, dynamicService, out isValidInput);
                    if (subcatalogView != null && subcatalogView.Schema.AllowQuickCreate && subRecord._id == ObjectId.Empty && isValidInput)
                    {
                        var fieldId     = DeepCreate(parentRecord ?? record, subRecord, subcatalogView, tenantId, loggedUser, roleCode, dynamicService, schemaService);
                        var field       = ((Dictionary <string, object>)r.Value);
                        var arrayOfKeys = field.Keys.ToArray();
                        foreach (var k in arrayOfKeys)
                        {
                            if (subRecord.ContainsKey(k))
                            {
                                field[k] = subRecord[k];
                            }
                        }
                    }
                    else if (subRecord._id != ObjectId.Empty)
                    {
                        ((Dictionary <string, object>)r.Value)["_id"] = subRecord._id;
                    }
                }
                else if (subcatalogView?.Schema != null && subcatalogView.Schema.AllowQuickCreate && r.Value is List <Dictionary <string, object> > && ((List <Dictionary <string, object> >)r.Value).Count > 0)
                {
                    ((List <Dictionary <string, object> >)r.Value).ForEach(v =>
                    {
                        var subRecord   = new DynamicRecord().Initialize <DynamicRecord>(v, subcatalogView, ViewCategory.Create);
                        var fieldId     = DeepCreate(parentRecord ?? record, subRecord, subcatalogView, tenantId, loggedUser, roleCode, dynamicService, schemaService, true);
                        var field       = v;
                        var arrayOfKeys = field.Keys.ToArray();
                        foreach (var k in arrayOfKeys)
                        {
                            if (subRecord.ContainsKey(k))
                            {
                                field[k] = subRecord[k];
                            }
                        }
                    });
                }
            });

            if (record._id != ObjectId.Empty && allowUpdate)
            {
                record.ParentRecord = parentRecord;
                dynamicService.Update(record, view.Schema.CollectionName, view.Schema.Name, tenantId, loggedUser, roleCode);
                return(record._id);
            }
            else
            {
                var isValidInput = false;
                record._id = FindExisting(record, view, tenantId, loggedUser, dynamicService, out isValidInput);
                if (record._id == ObjectId.Empty)
                {
                    record.ParentRecord = parentRecord;
                    return(dynamicService.Add(record, view.Schema.Name, tenantId, loggedUser, roleCode));
                }
                else if (allowUpdate)
                {
                    record.ParentRecord = parentRecord;
                    dynamicService.Update(record, view.Schema.CollectionName, view.Schema.Name, tenantId, loggedUser, roleCode);
                    return(record._id);
                }
                else
                {
                    return(ObjectId.Empty);
                }
            }
        }