Exemplo n.º 1
0
        protected override async Task Handle(CreateAlertCommand request, CancellationToken cancellationToken)
        {
            var profiles = await repository.GetProfilesByCovidPassAsync(request.CovidPass, cancellationToken);

            foreach (var profile in profiles)
            {
                profile.AddAlert(new Alert(profile.DeviceId, profile.Id, DateTime.UtcNow, request.Content));

                await repository.UnitOfWork.SaveChangesAsync(cancellationToken);

                if (request.WithPushNotification.HasValue && request.WithPushNotification.Value)
                {
                    var message = new Notification
                    {
                        Priority            = "high",
                        ContentAvailable    = true,
                        NotificationContent = new NotificationContent
                        {
                            Title = request.PushSubject,
                            Body  = request.PushBody,
                            Sound = "default"
                        }
                    };
                    var sendNotificationCommand = new SendPushNotificationCommand(profile.Id, message);
                    await mediator.Send(sendNotificationCommand, cancellationToken);
                }
            }
        }
        public async Task <PushNonce> Handle(GeneratePushNonceCommand request, CancellationToken cancellationToken)
        {
            var profile = await repository.GetProfileAsync(request.ProfileId, request.DeviceId, cancellationToken);

            if (profile == null)
            {
                throw new DomainException("Profile not found");
            }

            if (string.IsNullOrEmpty(profile.PushToken))
            {
                throw new DomainException("Push token is required please update profile");
            }

            var instanceInfo = await instanceIdService.GetInstanceInfoAsync(profile.PushToken, cancellationToken);

            if (instanceInfo == null)
            {
                throw new DomainException("Push token is invalid");
            }

            if (instanceInfo.Platform != profile.ClientInfo.OperationSystem || instanceInfo.Application != profile.ClientInfo.Integrator)
            {
                throw new DomainException("Push token is invalid or from wrong platform");
            }

            var pushNonce = await context.PushNonces.SingleOrDefaultAsync(x => x.Id == profile.PushToken, cancellationToken);

            if (pushNonce != null)
            {
                pushNonce.Update(nonceGenerator.Generate(), DateTime.UtcNow, DateTime.UtcNow.Add(request.TokenExpiration));
            }
            else
            {
                pushNonce = new PushNonce(profile.PushToken, nonceGenerator.Generate(), DateTime.UtcNow, DateTime.UtcNow.Add(request.TokenExpiration));
                await context.PushNonces.AddAsync(pushNonce, cancellationToken);
            }

            await context.SaveChangesAsync(cancellationToken);

            var message = new Notification
            {
                Data = new Dictionary <string, object>
                {
                    { "type", "PUSH_NONCE" },
                    { "Nonce", pushNonce.Body }
                },
                Priority         = "high",
                ContentAvailable = true
            };
            var sendNonceCommand = new SendPushNotificationCommand(profile.Id, message);
            await mediator.Send(sendNonceCommand, cancellationToken);

            return(pushNonce);
        }
Exemplo n.º 3
0
        protected override async Task Handle(UpdateQuarantineCommand request, CancellationToken cancellationToken)
        {
            var profiles = (await repository.GetProfilesByCovidPassAsync(request.CovidPass, cancellationToken)).ToList();

            if (!profiles.Any())
            {
                throw new DomainException($"No profiles found with {nameof(request.CovidPass)}: '{request.CovidPass}'");
            }

            foreach (var profile in profiles)
            {
                var notification = CreateQuarantineUpdatedNotification(profile, request.NotificationTitle, request.NotificationBody);

                profile.UpdateQuarantine(request.QuarantineStart, request.QuarantineEnd, request.BorderCrossedAt, request.QuarantineAddress);
                var command = new SendPushNotificationCommand(profile.Id, notification);
                await mediator.Send(command, cancellationToken);
            }

            await repository.UnitOfWork.SaveChangesAsync(cancellationToken);
        }
        protected override async Task Handle(ConfirmInfectionCommand request, CancellationToken cancellationToken)
        {
            var profile = await repository.GetProfileAsync(request.ProfileId, request.DeviceId, cancellationToken);

            if (profile == null)
            {
                throw new DomainException("Profile not found.");
            }
            if (profile.AuthToken != request.MfaToken)
            {
                throw new DomainException("Wrong maf token.");
            }
            if (profile.AuthToken == request.MfaToken)
            {
                if (profile.ConfirmedInfection == false)
                {
                    profile.ConfirmInfection();
                    await repository.UnitOfWork.SaveChangesAsync(cancellationToken);

                    if (request.SendingNotificationsEnabled)
                    {
                        var query  = new GetContactsQuery(profile.DeviceId, profile.Id);
                        var result = await mediator.Send(query, cancellationToken);

                        var groupedContacts = result.Contacts.GroupBy(x => x.ProfileId);
                        foreach (var contact in groupedContacts)
                        {
                            var firstContactFromGroup = contact.First();
                            var data    = new { messageType = "CORONA_INFECTION_CONFIRMED" };
                            var command = new SendPushNotificationCommand(firstContactFromGroup.ProfileId, data);
                            await mediator.Send(command, cancellationToken);
                        }
                    }
                }
            }
        }