@@ -811,6 +811,47 @@ describe('MedplumNotificationBackend', () => {
811811 'contextName lookup with caseSensitive=false is not supported by MedplumNotificationBackend. Only case-sensitive exact matching is supported.' ,
812812 ) ;
813813 } ) ;
814+
815+ it ( 'should map supported orderBy fields and directions to FHIR _sort' , async ( ) => {
816+ const searchResourcesSpy = jest
817+ . spyOn ( medplumClient , 'searchResources' )
818+ . mockResolvedValue ( [ ] as any ) ;
819+
820+ const testCases = [
821+ { orderBy : { field : 'sendAfter' , direction : 'asc' } as const , expectedSort : 'sent' } ,
822+ { orderBy : { field : 'sendAfter' , direction : 'desc' } as const , expectedSort : '-sent' } ,
823+ { orderBy : { field : 'sentAt' , direction : 'asc' } as const , expectedSort : 'sent' } ,
824+ { orderBy : { field : 'sentAt' , direction : 'desc' } as const , expectedSort : '-sent' } ,
825+ { orderBy : { field : 'createdAt' , direction : 'asc' } as const , expectedSort : '_lastUpdated' } ,
826+ { orderBy : { field : 'createdAt' , direction : 'desc' } as const , expectedSort : '-_lastUpdated' } ,
827+ { orderBy : { field : 'updatedAt' , direction : 'asc' } as const , expectedSort : '_lastUpdated' } ,
828+ { orderBy : { field : 'updatedAt' , direction : 'desc' } as const , expectedSort : '-_lastUpdated' } ,
829+ ] ;
830+
831+ for ( const testCase of testCases ) {
832+ searchResourcesSpy . mockClear ( ) ;
833+ await backend . filterNotifications ( { } , 0 , 10 , testCase . orderBy ) ;
834+
835+ expect ( searchResourcesSpy ) . toHaveBeenCalledWith ( 'Communication' , [
836+ [ '_count' , '10' ] ,
837+ [ '_offset' , '0' ] ,
838+ [ '_sort' , testCase . expectedSort ] ,
839+ [ '_tag' , 'notification' ] ,
840+ ] ) ;
841+ }
842+ } ) ;
843+
844+ it ( 'should throw for unsupported orderBy.readAt' , async ( ) => {
845+ const searchResourcesSpy = jest
846+ . spyOn ( medplumClient , 'searchResources' )
847+ . mockResolvedValue ( [ ] as any ) ;
848+
849+ await expect (
850+ backend . filterNotifications ( { } , 0 , 10 , { field : 'readAt' , direction : 'asc' } ) ,
851+ ) . rejects . toThrow ( "orderBy field 'readAt' is not supported by MedplumNotificationBackend." ) ;
852+
853+ expect ( searchResourcesSpy ) . not . toHaveBeenCalled ( ) ;
854+ } ) ;
814855 } ) ;
815856
816857 describe ( 'getFilterCapabilities' , ( ) => {
@@ -877,5 +918,18 @@ describe('MedplumNotificationBackend', () => {
877918 expect ( capabilities [ 'stringLookups.includes' ] ) . toBe ( false ) ;
878919 expect ( capabilities [ 'stringLookups.caseInsensitive' ] ) . toBe ( false ) ;
879920 } ) ;
921+
922+ it ( 'should mark orderBy.readAt as unsupported' , ( ) => {
923+ const capabilities = backend . getFilterCapabilities ( ) ;
924+ expect ( capabilities [ 'orderBy.readAt' ] ) . toBe ( false ) ;
925+ } ) ;
926+
927+ it ( 'should mark supported orderBy fields as supported' , ( ) => {
928+ const capabilities = backend . getFilterCapabilities ( ) ;
929+ expect ( capabilities [ 'orderBy.sendAfter' ] ) . toBe ( true ) ;
930+ expect ( capabilities [ 'orderBy.sentAt' ] ) . toBe ( true ) ;
931+ expect ( capabilities [ 'orderBy.createdAt' ] ) . toBe ( true ) ;
932+ expect ( capabilities [ 'orderBy.updatedAt' ] ) . toBe ( true ) ;
933+ } ) ;
880934 } ) ;
881935} ) ;
0 commit comments