Exemplo n.º 1
0
        public Task <JsonResult> WithingsAuth(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "withings/auth")] HttpRequest req,
            [HttpBody(Required = true)] HttpParam <AuthDto> authDto,
            [HttpHeader(Name = "Authorization")] HttpParam <string> authorization,
            CancellationToken cancellationToken)
        {
            return(exceptionFilter.FilterExceptions(async() =>
            {
                var withingsAccessCode = authDto.Value.WithingsAccessCode;
                var withingsRedirectUri = authDto.Value.WithingsRedirectUri;

                var(userId, _, _) = await auth.ValidateUser(authorization, cancellationToken);
                var consent = await consentStore.FetchConsent(userId, cancellationToken);

                var newWithingsUserId = await withingsClient.CreateAccount(userId, withingsAccessCode, withingsRedirectUri, cancellationToken);

                if (consent == null || !consent.ExternalIds.TryGetValue(withingsToFhirConverter.System, out var withingsUserId) || withingsUserId != newWithingsUserId)
                {
                    consent ??= ConsentFactory.WithId(userId);
                    consent.ExternalIds[withingsToFhirConverter.System] = newWithingsUserId;
                    await consentStore.WriteConsent(consent, cancellationToken);
                }

                return new JsonResult(await userFactory.CreateUser(consent, jobId: null, cancellationToken));
            }));
        }
Exemplo n.º 2
0
        public Task <JsonResult> CreateOrUpdateUser(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "user")] HttpRequest req,
            [HttpBody(Required = true)] HttpParam <UserDto> userDto,
            [HttpHeader(Name = "Authorization")] HttpParam <string> authorization,
            [DurableClient] IDurableOrchestrationClient jobClient,
            CancellationToken cancellationToken)
        {
            return(exceptionFilter.FilterExceptions(async() =>
            {
                var connectedDevices = userDto.Value.ConnectedDevices ?? Array.Empty <Identifier>();
                var disconnectedDevices = userDto.Value.DisconnectedDevices ?? Array.Empty <Identifier>();
                var mobileDevice = userDto.Value.MobileDevice;

                var(userId, givenName, familyName) = await auth.ValidateUser(authorization, cancellationToken);
                var consent = await consentStore.FetchConsent(userId, cancellationToken);
                consent ??= ConsentFactory.WithId(userId);

                var statusCode = HttpStatusCode.NoContent;
                var jobId = (string?)null;

                if (consent.FhirId == null)
                {
                    var patient = await fhirClient.CreatePatient(userId, familyName, givenName, cancellationToken);
                    consent.FhirId = patient.Id;
                    statusCode = HttpStatusCode.OK;
                }

                if (mobileDevice != null)
                {
                    consent.MobileDevices.Add(mobileDevice);
                    statusCode = HttpStatusCode.OK;
                }

                if (connectedDevices.Length > 0 || disconnectedDevices.Length > 0)
                {
                    consent.Devices.RemoveAll(disconnectedDevices.Contains);
                    consent.Devices.AddRange(connectedDevices);
                    statusCode = HttpStatusCode.Accepted;
                }

                if (statusCode != HttpStatusCode.NoContent)
                {
                    await consentStore.WriteConsent(consent, cancellationToken);
                }

                if (connectedDevices.Length > 0 || disconnectedDevices.Length > 0)
                {
                    jobId = await jobClient.RunSingleton(
                        userId,
                        workflow: nameof(DeviceImportJob.DeviceImportWorkflow),
                        jobArguments: new StartDeviceManagementMessage
                    {
                        UserId = userId,
                        ConnectedDevices = connectedDevices,
                        DisconnectedDevices = disconnectedDevices,
                    },
                        log,
                        guidFactory);
                }

                return new JsonResult(await userFactory.CreateUser(consent, jobId, cancellationToken))
                {
                    StatusCode = (int?)statusCode,
                };
            }));
        }