Exemplo n.º 1
0
        public async Task <CreateProfileResponse> Handle(CreateProfileCommand request, CancellationToken cancellationToken)
        {
            var existingProfile = await repository.GetProfileAsync(request.DeviceId, cancellationToken);

            if (existingProfile != null)
            {
                if (existingProfile.PushToken != request.PushToken)
                {
                    existingProfile.UpdatePushToken(request.PushToken);
                }

                await repository.UnitOfWork.SaveChangesAsync(cancellationToken);

                return(new CreateProfileResponse {
                    ProfileId = existingProfile.Id, DeviceId = existingProfile.DeviceId
                });
            }

            string token = tokenGenerator.Generate();

            var profile = new Profile(request.DeviceId, request.PushToken, request.Locale, token);

            var os = request.AppOperationSystem switch
            {
                "anr" => Platform.Android,
                "ios" => Platform.Ios,
                _ => Platform.Undefined
            };

            profile.AddClientInfo(new ClientInfo(request.AppName, request.AppIntegrator, request.AppVersion, os));

            await repository.CreateProfileAsync(profile, cancellationToken);

            await repository.UnitOfWork.SaveChangesAsync(cancellationToken);

            profile.ChangeMedicalId(hashGenerator.Generate(profile.Id));
            await repository.UnitOfWork.SaveChangesAsync(cancellationToken);

            return(new CreateProfileResponse {
                ProfileId = profile.Id, DeviceId = profile.DeviceId
            });
        }
    }