@@ -7,8 +7,7 @@ import { HttpException } from "@/exceptions/http-exception";
77import { LocaleService } from "@/i18n/ctx" ;
88import { ChannelExtend } from "@/interfaces/channels.interface" ;
99import { Paging } from "@/interfaces/paging.interface" ;
10- import { Helper } from "@/utils/helper" ;
11- import { asc , desc , eq , isNotNull , like } from "drizzle-orm" ;
10+ import { and , asc , desc , eq , like , sql } from "drizzle-orm" ;
1211import { StatusCodes } from "http-status-codes" ;
1312import { Inject , Service } from "typedi" ;
1413
@@ -126,27 +125,67 @@ export class ChannelService {
126125 } ) ;
127126 }
128127
129- public async getChannelsPaging ( paging : PagingDTO ) : Promise < Paging < ChannelExtend > > {
130- const query = await db
131- . select ( )
128+ public async getChannelsPaging ( paging : PagingDTO ) : Promise < Paging < any > > {
129+ const offset = paging . page && ( ( paging . page - 1 ) * paging . limit ) || 0 ;
130+ // const [query, count] =
131+ // await Promise.all([
132+ // db
133+ // .select({
134+ // id: channels.id,
135+ // contactId: channels.contactId,
136+ // name: channels.contactName,
137+ // channelType: channelTypes.description,
138+ // credentials: channels.credentials,
139+ // active: channels.active,
140+ // createdAt: channels.createdAt,
141+ // updateAt: channels.updatedAt,
142+ // })
143+ // .from(channels)
144+ // .where(
145+ // and(
146+ // like(channels.contactId, `%${paging.q}%`),
147+ // eq(channels.deleted, false)
148+ // )
149+ // )
150+ // .innerJoin(channelTypes, eq(channels.channelTypeId, channelTypes.id))
151+ // .orderBy(paging.sortType === 'asc' ? asc(channels[paging.orderBy]) : desc(channels[paging.orderBy]))
152+ // .offset(offset)
153+ // .limit(paging.limit),
154+ // db
155+ // .select({ count: sql<number>`cast(count(${channels.id}) as integer)` })
156+ // .from(channels)
157+ // .where(eq(channels.deleted, false)),
158+ // ]);
159+
160+ const result : ChannelExtend [ ] = await db
161+ . select ( {
162+ id : channels . id ,
163+ contactId : channels . contactId ,
164+ contactName : channels . contactName ,
165+ name : channels . contactName ,
166+ channelType : channelTypes . description ,
167+ credentials : channels . credentials ,
168+ active : channels . active ,
169+ createdAt : channels . createdAt ,
170+ updateAt : channels . updatedAt ,
171+ } )
132172 . from ( channels )
133- . where ( paging . q ? like ( channels . contactId , `%${ paging . q } %` ) : isNotNull ( channels . contactId ) )
173+ . where (
174+ and (
175+ like ( channels . contactId , `%${ paging . q } %` ) ,
176+ eq ( channels . deleted , false )
177+ )
178+ )
134179 . innerJoin ( channelTypes , eq ( channels . channelTypeId , channelTypes . id ) )
135180 . orderBy ( paging . sortType === 'asc' ? asc ( channels [ paging . orderBy ] ) : desc ( channels [ paging . orderBy ] ) )
136- . offset ( paging . page * paging . limit )
181+ . offset ( offset )
137182 . limit ( paging . limit ) ;
138183
139- const data : ChannelExtend [ ] = query . map ( ( rs : any ) => ( {
140- id : rs . channels ?. id ,
141- contactId : rs . channels ?. contactId ,
142- contactName : rs . channels ?. contactName ,
143- channelType : rs . channel_types ?. description ,
144- credentials : rs . channels ?. credentials ?? null ,
145- active : rs . channels ?. active ,
146- createdAt : Helper . formatDate ( rs . channels ?. createdAt ) ,
147- updateAt : Helper . formatDate ( rs . channels ?. updateAt ) ,
148- } ) ) ;
149-
150- return { items : data , totalItems : data . length } ;
184+ const count = await db
185+ . select ( { count : sql < number > `cast(count(${ channels . id } ) as integer)` } )
186+ . from ( channels )
187+ . where ( eq ( channels . deleted , false ) ) ;
188+
189+ return { items : result , totalItems : count [ 0 ] . count } ;
151190 }
152191}
0 commit comments