@@ -86,35 +86,37 @@ export const searchEmployees = async (clientId: string, page: number, take: numb
8686 const offset = ( page - 1 ) * take ;
8787
8888 try {
89- const data = await db . query . employee . findMany ( {
90- with : {
91- employeeProfile : true ,
92- employeeCodes : {
93- where : ( code , { eq } ) => eq ( code . isActive , true ) ,
89+ return await db . transaction ( async ( tx ) => {
90+ const data = await tx . query . employee . findMany ( {
91+ with : {
92+ employeeProfile : true ,
93+ employeeCodes : {
94+ where : ( code , { eq } ) => eq ( code . isActive , true ) ,
95+ } ,
9496 } ,
95- } ,
96- where : ( employee , { eq , and , like , or } ) => search !== undefined
97- ? and (
98- or ( like ( employee . firstName , search ) , like ( employee . lastName , search ) ) ,
99- eq ( employee . clientId , clientId ) ,
100- eq ( employee . isCommissionable , true )
101- )
102- : and ( eq ( employee . clientId , clientId ) , eq ( employee . isCommissionable , true ) ) ,
103- offset ,
104- limit : take ,
105- } ) as Employee [ ] ;
106-
107- const count = ( await db . query . employee . findMany ( {
108- where : ( employee , { eq , and , like , or } ) => search !== undefined
109- ? and (
110- or ( like ( employee . firstName , search ) , like ( employee . lastName , search ) ) ,
111- eq ( employee . clientId , clientId ) ,
112- eq ( employee . isCommissionable , true )
113- )
114- : and ( eq ( employee . clientId , clientId ) , eq ( employee . isCommissionable , true ) ) ,
115- } ) ) . length ;
116-
117- return { data , count } ;
97+ where : ( employee , { eq , and , ilike , or } ) => search != null
98+ ? and (
99+ or ( ilike ( employee . firstName , `% ${ search } %` ) , ilike ( employee . lastName , `% ${ search } %` ) ) ,
100+ eq ( employee . clientId , clientId ) ,
101+ eq ( employee . isCommissionable , true )
102+ )
103+ : and ( eq ( employee . clientId , clientId ) , eq ( employee . isCommissionable , true ) ) ,
104+ offset ,
105+ limit : take ,
106+ } ) as Employee [ ] ;
107+
108+ const count = ( await tx . query . employee . findMany ( {
109+ where : ( employee , { eq , and , ilike , or } ) => search != null
110+ ? and (
111+ or ( ilike ( employee . firstName , `% ${ search } %` ) , ilike ( employee . lastName , `% ${ search } %` ) ) ,
112+ eq ( employee . clientId , clientId ) ,
113+ eq ( employee . isCommissionable , true )
114+ )
115+ : and ( eq ( employee . clientId , clientId ) , eq ( employee . isCommissionable , true ) ) ,
116+ } ) ) . length ;
117+
118+ return { data , count } ;
119+ } ) ;
118120 } catch ( ex ) {
119121 console . error ( ex ) ;
120122 return { data : [ ] as Employee [ ] , count : 0 } ;
0 commit comments