@@ -76,64 +76,76 @@ class TrustRepository extends BaseRepository {
76
76
promise = promise . offset ( limitOptions . offset ) ;
77
77
}
78
78
79
+ let order = 'desc' ;
80
+ let column = 'wallet_trust.created_at' ;
81
+
82
+ if ( limitOptions ) {
83
+ if ( limitOptions . order ) {
84
+ order = limitOptions . order ;
85
+ }
86
+ if ( limitOptions . sort_by ) {
87
+ column = limitOptions . sort_by ;
88
+ }
89
+ }
90
+ promise = promise . orderBy ( column , order ) ;
91
+
79
92
return promise ;
80
93
}
81
94
82
- async getAllByFilter ( filter , limitOptions ) {
83
- let promise = this . _session
84
- . getDB ( )
85
- . select (
86
- 'wallet_trust.id' ,
87
- 'wallet_trust.actor_wallet_id' ,
88
- 'wallet_trust.target_wallet_id' ,
89
- 'wallet_trust.type' ,
90
- 'wallet_trust.originator_wallet_id' ,
91
- 'wallet_trust.request_type' ,
92
- 'wallet_trust.state' ,
93
- 'wallet_trust.created_at' ,
94
- 'wallet_trust.updated_at' ,
95
- 'wallet_trust.active' ,
96
- 'originator_wallet.name as originating_wallet' ,
97
- 'actor_wallet.name as actor_wallet' ,
98
- 'target_wallet.name as target_wallet' ,
99
- )
100
- . from ( 'wallet_trust' )
101
- . leftJoin (
102
- 'wallet as originator_wallet' ,
103
- 'wallet_trust.originator_wallet_id' ,
104
- '=' ,
105
- 'originator_wallet.id' ,
106
- )
107
- . leftJoin (
108
- 'wallet as actor_wallet' ,
109
- 'wallet_trust.actor_wallet_id' ,
110
- '=' ,
111
- 'actor_wallet.id' ,
112
- )
113
- . leftJoin (
114
- 'wallet as target_wallet' ,
115
- 'wallet_trust.target_wallet_id' ,
116
- '=' ,
117
- 'target_wallet.id' ,
118
- )
119
- . where ( ( builder ) => this . whereBuilder ( filter , builder ) )
120
- . distinctOn ( 'wallet_trust.id' ) ; // distinct on id to avoid duplicates
95
+ async getAllByFilter ( filter , limitOptions ) {
96
+ const subquery = this . _session . getDB ( )
97
+ . select (
98
+ 'wallet_trust.id' ,
99
+ 'wallet_trust.actor_wallet_id' ,
100
+ 'wallet_trust.target_wallet_id' ,
101
+ 'wallet_trust.type' ,
102
+ 'wallet_trust.originator_wallet_id' ,
103
+ 'wallet_trust.request_type' ,
104
+ 'wallet_trust.state' ,
105
+ 'wallet_trust.created_at' ,
106
+ 'wallet_trust.updated_at' ,
107
+ 'wallet_trust.active' ,
108
+ 'originator_wallet.name as originating_wallet' ,
109
+ 'actor_wallet.name as actor_wallet' ,
110
+ 'target_wallet.name as target_wallet' ,
111
+ )
112
+ . from ( 'wallet_trust' )
113
+ . leftJoin ( 'wallet as originator_wallet' , 'wallet_trust.originator_wallet_id' , '=' , 'originator_wallet.id' )
114
+ . leftJoin ( 'wallet as actor_wallet' , 'wallet_trust.actor_wallet_id' , '=' , 'actor_wallet.id' )
115
+ . leftJoin ( 'wallet as target_wallet' , 'wallet_trust.target_wallet_id' , '=' , 'target_wallet.id' )
116
+ . where ( ( builder ) => this . whereBuilder ( filter , builder ) )
117
+ . distinctOn ( 'wallet_trust.id' )
118
+ . orderBy ( 'wallet_trust.id' , 'asc' ) ;
121
119
122
- // get the total count (before applying limit and offset options)
123
- const count = await this . _session . getDB ( ) . from ( promise . as ( 'p' ) ) . count ( '*' ) ;
120
+ let derivedTable = this . _session . getDB ( ) . select ( '*' ) . from ( subquery . as ( 'subquery' ) ) ;
124
121
125
- if ( limitOptions && limitOptions . limit ) {
126
- promise = promise . limit ( limitOptions . limit ) ;
127
- }
122
+ let order = 'desc' ;
123
+ let column = 'created_at' ;
128
124
129
- if ( limitOptions && limitOptions . offset ) {
130
- promise = promise . offset ( limitOptions . offset ) ;
125
+ if ( limitOptions ) {
126
+ if ( limitOptions . order ) {
127
+ order = limitOptions . order ;
131
128
}
129
+ if ( limitOptions . sort_by ) {
130
+ column = limitOptions . sort_by ;
131
+ }
132
+ }
132
133
133
- const result = await promise ;
134
+ derivedTable = derivedTable . orderBy ( column , order ) ;
134
135
135
- return { result, count : + count [ 0 ] . count } ;
136
+ if ( limitOptions && limitOptions . limit ) {
137
+ derivedTable = derivedTable . limit ( limitOptions . limit ) ;
136
138
}
139
+ if ( limitOptions && limitOptions . offset ) {
140
+ derivedTable = derivedTable . offset ( limitOptions . offset ) ;
141
+ }
142
+
143
+ const result = await derivedTable ;
144
+
145
+ const { count} = result [ 0 ] ;
146
+
147
+ return { result, count : + count } ;
148
+ }
137
149
}
138
150
139
151
module . exports = TrustRepository ;
0 commit comments