@@ -37,6 +37,7 @@ export default class PostgresInstrumentDataSource
3737 constructor (
3838 @inject ( Tokens . FapDataSource ) private fapDataSource : FapDataSource
3939 ) { }
40+
4041 private createInstrumentObject ( instrument : InstrumentRecord ) {
4142 return new Instrument (
4243 instrument . instrument_id ,
@@ -296,7 +297,22 @@ export default class PostgresInstrumentDataSource
296297 . whereIn ( 'tag_id' , tagIds )
297298 . select ( 'i.*' ) ;
298299
299- return instruments . map ( this . createInstrumentWithAvailabilityTimeObject ) ;
300+ const managerInstruments = await database < InstrumentRecord > (
301+ 'instruments as i'
302+ )
303+ . where ( 'i.manager_user_id' , userId )
304+ . select ( 'i.*' ) ;
305+
306+ const allInstruments = [ ...instruments , ...managerInstruments ] ;
307+ const uniqueInstruments = allInstruments . filter (
308+ ( inst , index , self ) =>
309+ self . findIndex ( ( i ) => i . instrument_id === inst . instrument_id ) ===
310+ index
311+ ) ;
312+
313+ return uniqueInstruments . map (
314+ this . createInstrumentWithAvailabilityTimeObject
315+ ) ;
300316 } else {
301317 const instruments =
302318 await database < InstrumentRecord > ( 'instruments as i' ) . select ( 'i.*' ) ;
@@ -314,11 +330,12 @@ export default class PostgresInstrumentDataSource
314330 'manager_user_id' ,
315331 ] )
316332 . from ( 'instruments as i' )
317- . join ( 'instrument_has_scientists as ihs' , {
333+ . leftJoin ( 'instrument_has_scientists as ihs' , {
318334 'i.instrument_id' : 'ihs.instrument_id' ,
319335 } )
320- . where ( 'ihs.user_id' , userId )
321- . orWhere ( 'i.manager_user_id' , userId )
336+ . where ( function ( ) {
337+ this . where ( 'ihs.user_id' , userId ) . orWhere ( 'i.manager_user_id' , userId ) ;
338+ } )
322339 . distinct ( 'i.instrument_id' )
323340 . then ( ( instruments : InstrumentRecord [ ] ) => {
324341 const result = instruments . map ( ( instrument ) =>
@@ -803,14 +820,20 @@ export default class PostgresInstrumentDataSource
803820 await database
804821 . count ( { count : '*' } )
805822 . from ( 'instruments as i' )
806- . join ( 'instrument_has_scientists as ihs' , {
807- 'i.instrument_id' : 'ihs.instrument_id' ,
823+ . leftJoin ( 'instrument_has_scientists as ihs' , function ( ) {
824+ this . on ( 'i.instrument_id' , '=' , 'ihs.instrument_id' ) . andOnVal (
825+ 'ihs.user_id' ,
826+ '=' ,
827+ userId
828+ ) ;
808829 } )
809- . where ( 'ihs.user_id' , userId )
810830 . where ( 'i.instrument_id' , instrumentId )
831+ . andWhere ( function ( ) {
832+ this . whereNotNull ( 'ihs.user_id' ) . orWhere ( 'i.manager_user_id' , userId ) ;
833+ } )
811834 . first ( ) ;
812835
813- return result ?. count === '1' ;
836+ return Number ( result ?. count ) >= 1 ;
814837 }
815838
816839 async hasInstrumentScientistAccess (
@@ -821,19 +844,27 @@ export default class PostgresInstrumentDataSource
821844 return database
822845 . select ( [ database . raw ( 'count(*) OVER() AS count' ) ] )
823846 . from ( 'proposals' )
824- . join ( 'instrument_has_scientists' , {
825- 'instrument_has_scientists.user_id' : scientistId ,
826- } )
827847 . join ( 'instrument_has_proposals' , {
828848 'instrument_has_proposals.proposal_pk' : 'proposals.proposal_pk' ,
829- 'instrument_has_proposals.instrument_id' :
830- 'instrument_has_scientists.instrument_id' ,
849+ } )
850+ . join ( 'instruments' , {
851+ 'instruments.instrument_id' : 'instrument_has_proposals.instrument_id' ,
852+ } )
853+ . leftJoin ( 'instrument_has_scientists' , {
854+ 'instrument_has_scientists.instrument_id' : 'instruments.instrument_id' ,
855+ 'instrument_has_scientists.user_id' : scientistId ,
831856 } )
832857 . where ( 'proposals.proposal_pk' , '=' , proposalPk )
833- . where ( 'instrument_has_scientists.instrument_id' , '=' , instrumentId )
858+ . where ( 'instrument_has_proposals.instrument_id' , '=' , instrumentId )
859+ . andWhere ( function ( ) {
860+ this . where ( 'instrument_has_scientists.user_id' , scientistId ) . orWhere (
861+ 'instruments.manager_user_id' ,
862+ scientistId
863+ ) ;
864+ } )
834865 . first ( )
835- . then ( ( result : undefined | { count : string } ) => {
836- return result ?. count === '1' ;
866+ . then ( ( result ) => {
867+ return Number ( result ?. count ) >= 1 ;
837868 } ) ;
838869 }
839870
0 commit comments