11const moment = require ( 'moment' ) ;
22const passwordTester = require ( 'simple-password-tester' ) ;
33const phoneNumber = require ( '@medic/phone-number' ) ;
4- const CHT = require ( '@medic/cht-datasource' ) ;
54const constants = require ( '@medic/constants' ) ;
65const USER_ROLES = constants . USER_ROLES ;
76const PASSWORD_MINIMUM_LENGTH = 8 ;
@@ -40,9 +39,8 @@ angular
4039 'use strict' ;
4140 'ngInject' ;
4241
43- const datasource = DataContext . getDatasource ( ) ;
42+ const datasourcePromise = DataContext . then ( dataContext => dataContext . getDatasource ( ) ) ;
4443 $scope . cancel = ( ) => $uibModalInstance . dismiss ( ) ;
45- const getContact = DataContext . bind ( CHT . Contact . v1 . get ) ;
4644
4745 const getRoles = roles => {
4846 if ( ! roles || ! roles . length ) {
@@ -61,9 +59,10 @@ angular
6159 } ) ;
6260 } ;
6361
64- const validateSkipPasswordPermission = ( ) => {
62+ const validateSkipPasswordPermission = datasource => {
6563 $scope . skipPasswordChange = datasource . v1 . hasPermissions (
66- [ 'can_skip_password_change' ] , $scope . editUserModel . roles , $scope . permissions
64+ [ 'can_skip_password_change' ] ,
65+ $scope . editUserModel . roles
6766 ) ;
6867 } ;
6968
@@ -112,7 +111,7 @@ angular
112111 // If $scope.model === {}, we're creating a new user.
113112 return $q . all ( [ Settings ( ) , getOidcUsername ( ) ] )
114113 . then ( ( [ settings , oidcUsername ] ) => {
115- $scope . permissions = settings . permissions ;
114+ $scope . settings = settings ;
116115 $scope . roles = settings . roles ;
117116 $scope . allowTokenLogin = allowTokenLogin ( settings ) ;
118117 $scope . allowSSOLogin = allowSSOLogin ( settings ) ;
@@ -177,10 +176,10 @@ angular
177176 . map ( ( row ) => row . doc ) ;
178177 } ;
179178
180- this . setupPromise = determineEditUserModel ( )
181- . then ( model => {
179+ this . setupPromise = $q . all ( [ determineEditUserModel ( ) , datasourcePromise ] )
180+ . then ( ( [ model , datasource ] ) => {
182181 $scope . editUserModel = model ;
183- validateSkipPasswordPermission ( ) ;
182+ validateSkipPasswordPermission ( datasource ) ;
184183 populateFacilitynContact ( ) ;
185184 } )
186185 . catch ( err => {
@@ -324,13 +323,14 @@ angular
324323 return true ;
325324 } ;
326325
327- const validatePlacesPermission = ( ) => {
326+ const validatePlacesPermission = datasource => {
328327 if ( ! $scope . editUserModel . place || $scope . editUserModel . place . length <= 1 ) {
329328 return true ;
330329 }
331330
332331 const userHasPermission = datasource . v1 . hasPermissions (
333- [ 'can_have_multiple_places' ] , $scope . editUserModel . roles , $scope . permissions
332+ [ 'can_have_multiple_places' ] ,
333+ $scope . editUserModel . roles
334334 ) ;
335335
336336 if ( ! userHasPermission ) {
@@ -388,16 +388,16 @@ angular
388388 } ) ;
389389 } ;
390390
391- const validateContactIsInPlace = ( ) => {
391+ const validateContactIsInPlace = datasource => {
392392 const placeIds = $scope . editUserModel . place ;
393393 const contactId = $scope . editUserModel . contact ;
394394 if ( ! placeIds || ! contactId ) {
395395 return $q . resolve ( true ) ;
396396 }
397397
398- const getParent = ( contactId ) => {
399- return getContact ( CHT . Qualifier . byUuid ( contactId ) ) . then ( contact => contact . parent ) ;
400- } ;
398+ const getParent = contactId => datasource . v1 . contact
399+ . getByUuid ( contactId )
400+ . then ( contact => contact . parent ) ;
401401
402402 const checkParent = ( parent , placeIds ) => {
403403 if ( ! parent ) {
@@ -584,41 +584,44 @@ angular
584584 'password' ? 'text' : 'password' ;
585585 } ;
586586
587- // #edit-user-profile is the admin view, which has additional fields.
588- $scope . editUser = ( ) => {
589- $scope . setProcessing ( ) ;
590- $scope . errors = { } ;
591- computeFields ( ) ;
592-
587+ const runValidations = datasource => {
593588 const synchronousValidations = validateName ( ) &&
594589 validateRole ( ) &&
595590 validateContactAndFacility ( ) &&
596591 validatePasswordForEditUser ( ) &&
597592 validateEmailAddress ( ) &&
598- validatePlacesPermission ( ) ;
593+ validatePlacesPermission ( datasource ) ;
599594
600595 if ( ! synchronousValidations ) {
601596 $scope . setError ( ) ;
602597 return ;
603598 }
604599
605- const asynchronousValidations = $q
600+ return $q
606601 . all ( [
607602 validateFacilityHierarchy ( ) ,
608- validateContactIsInPlace ( ) ,
603+ validateContactIsInPlace ( datasource ) ,
609604 validateTokenLogin ( ) ,
610605 ] )
611- . then ( responses => responses . every ( response => response ) ) ;
612-
613- return asynchronousValidations
606+ . then ( responses => responses . every ( Boolean ) )
614607 . then ( valid => {
615608 if ( ! valid ) {
616609 $scope . setError ( ) ;
617610 return ;
618611 }
619612
620613 return validateReplicationLimit ( ) . then ( ( ) => updateUser ( ) ) ;
621- } )
614+ } ) ;
615+ } ;
616+
617+ // #edit-user-profile is the admin view, which has additional fields.
618+ $scope . editUser = ( ) => {
619+ $scope . setProcessing ( ) ;
620+ $scope . errors = { } ;
621+ computeFields ( ) ;
622+
623+ return datasourcePromise
624+ . then ( runValidations )
622625 . catch ( err => {
623626 if ( err . key ) {
624627 $translate ( err . key , err . params ) . then ( value => $scope . setError ( err , value , err . severity ) ) ;
0 commit comments