示例#1
0
 public async Task <IActionResult> Create([FromBody] CreatePushNotificationModel model)
 {
     return(await MakeActionCallAsync(async() => await _service.Create(model)));
 }
        public async Task <bool> Create(CreatePushNotificationModel model)
        {
            if (model.AnnouncementId.HasValue && model.PushNotificationUserType != null)
            {
                throw new Exception("You can set value or AnnouncementId or PushNotificationUserType!");
            }

            var pushNotification = _repository.Create(new PushNotification
            {
                Description = model.Description,
                Title       = model.Title,
                PushNotificationUserType   = model.PushNotificationUserType,
                PushNotificationActionType = model.PushNotificationActionType,
                PushNotificationStatusType = model.SendingDate > DateTime.UtcNow
                    ? PushNotificationStatusType.Scheduled
                    : PushNotificationStatusType.Sent,
                SendingDate = model.SendingDate
            });

            _repository.Create(new PushNotificationTranslate
            {
                Language           = Language.Arabian,
                PushNotificationId = pushNotification.Id,
                Title       = model.ArabianTitle,
                Description = model.ArabianDescription
            });
            await _repository.SaveChangesAsync();

            if (pushNotification.PushNotificationStatusType == PushNotificationStatusType.Scheduled)
            {
                var date = new DateTime(pushNotification.SendingDate.Year, pushNotification.SendingDate.Month,
                                        pushNotification.SendingDate.Day, pushNotification.SendingDate.Hour,
                                        pushNotification.SendingDate.Minute, pushNotification.SendingDate.Second);
                var sendPushTriggerBuilder = TriggerBuilder.Create()
                                             .WithIdentity($"{nameof(PushNotificationSendingJob)}Trigger#{PushNotificationSendingJob.Name}{pushNotification.Id}")
                                             .StartAt(date);
                dynamic sendPushProp = new ExpandoObject();
                sendPushProp.promoId = pushNotification.Id;
                await SchedulerHelper.Schedule <PushNotificationSendingJob, IJobListener>(new QuartzScheduleModel
                {
                    Name = $"{PushNotificationSendingJob.Name}{pushNotification.Id}",
                    IsListenerRequested = false,
                    DataMap             = sendPushProp,
                    TriggerBuilder      = sendPushTriggerBuilder
                });
            }
            if (model.PushNotificationUserType.HasValue)
            {
                switch (pushNotification.PushNotificationUserType)
                {
                case PushNotificationUserType.All:
                    await SendNotificationUsers(pushNotification, model.AnnouncementId);
                    await SendNotificationGuests(pushNotification, model.AnnouncementId);

                    break;

                case PushNotificationUserType.Guest:
                    await SendNotificationGuests(pushNotification, model.AnnouncementId);

                    break;

                case PushNotificationUserType.Registered:
                    await SendNotificationUsers(pushNotification, model.AnnouncementId);

                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            if (model.AnnouncementId.HasValue)
            {
                await SendNotificationToAnnoucmentCreator(model.AnnouncementId.Value, pushNotification);
            }

            await _repository.SaveChangesAsync();

            return(true);
        }