77 findApplicationById ,
88 findEnvironmentById ,
99 findGitProviderById ,
10+ findMemberById ,
1011 findProjectById ,
1112 getApplicationStats ,
1213 IS_CLOUD ,
@@ -32,7 +33,7 @@ import {
3233} from "@dokploy/server" ;
3334import { db } from "@dokploy/server/db" ;
3435import { TRPCError } from "@trpc/server" ;
35- import { eq } from "drizzle-orm" ;
36+ import { and , desc , eq , ilike , or , sql } from "drizzle-orm" ;
3637import { nanoid } from "nanoid" ;
3738import { z } from "zod" ;
3839import { createTRPCRouter , protectedProcedure } from "@/server/api/trpc" ;
@@ -53,6 +54,8 @@ import {
5354 apiSaveGitProvider ,
5455 apiUpdateApplication ,
5556 applications ,
57+ environments ,
58+ projects ,
5659} from "@/server/db/schema" ;
5760import { deploymentWorker } from "@/server/queues/deployments-queue" ;
5861import type { DeploymentJob } from "@/server/queues/queue-types" ;
@@ -1002,4 +1005,138 @@ export const applicationRouter = createTRPCRouter({
10021005 message : "Deployment cancellation only available in cloud version" ,
10031006 } ) ;
10041007 } ) ,
1008+
1009+ search : protectedProcedure
1010+ . input (
1011+ z . object ( {
1012+ q : z . string ( ) . optional ( ) ,
1013+ name : z . string ( ) . optional ( ) ,
1014+ appName : z . string ( ) . optional ( ) ,
1015+ description : z . string ( ) . optional ( ) ,
1016+ repository : z . string ( ) . optional ( ) ,
1017+ owner : z . string ( ) . optional ( ) ,
1018+ dockerImage : z . string ( ) . optional ( ) ,
1019+ projectId : z . string ( ) . optional ( ) ,
1020+ environmentId : z . string ( ) . optional ( ) ,
1021+ limit : z . number ( ) . min ( 1 ) . max ( 100 ) . default ( 20 ) ,
1022+ offset : z . number ( ) . min ( 0 ) . default ( 0 ) ,
1023+ } ) ,
1024+ )
1025+ . query ( async ( { ctx, input } ) => {
1026+ const baseConditions = [
1027+ eq ( projects . organizationId , ctx . session . activeOrganizationId ) ,
1028+ ] ;
1029+
1030+ if ( input . projectId ) {
1031+ baseConditions . push ( eq ( environments . projectId , input . projectId ) ) ;
1032+ }
1033+ if ( input . environmentId ) {
1034+ baseConditions . push (
1035+ eq ( applications . environmentId , input . environmentId ) ,
1036+ ) ;
1037+ }
1038+
1039+ if ( input . q ?. trim ( ) ) {
1040+ const term = `%${ input . q . trim ( ) } %` ;
1041+ baseConditions . push (
1042+ or (
1043+ ilike ( applications . name , term ) ,
1044+ ilike ( applications . appName , term ) ,
1045+ ilike ( applications . description ?? "" , term ) ,
1046+ ilike ( applications . repository ?? "" , term ) ,
1047+ ilike ( applications . owner ?? "" , term ) ,
1048+ ilike ( applications . dockerImage ?? "" , term ) ,
1049+ ) ! ,
1050+ ) ;
1051+ }
1052+
1053+ if ( input . name ?. trim ( ) ) {
1054+ baseConditions . push ( ilike ( applications . name , `%${ input . name . trim ( ) } %` ) ) ;
1055+ }
1056+ if ( input . appName ?. trim ( ) ) {
1057+ baseConditions . push (
1058+ ilike ( applications . appName , `%${ input . appName . trim ( ) } %` ) ,
1059+ ) ;
1060+ }
1061+ if ( input . description ?. trim ( ) ) {
1062+ baseConditions . push (
1063+ ilike (
1064+ applications . description ?? "" ,
1065+ `%${ input . description . trim ( ) } %` ,
1066+ ) ,
1067+ ) ;
1068+ }
1069+ if ( input . repository ?. trim ( ) ) {
1070+ baseConditions . push (
1071+ ilike ( applications . repository ?? "" , `%${ input . repository . trim ( ) } %` ) ,
1072+ ) ;
1073+ }
1074+ if ( input . owner ?. trim ( ) ) {
1075+ baseConditions . push (
1076+ ilike ( applications . owner ?? "" , `%${ input . owner . trim ( ) } %` ) ,
1077+ ) ;
1078+ }
1079+ if ( input . dockerImage ?. trim ( ) ) {
1080+ baseConditions . push (
1081+ ilike (
1082+ applications . dockerImage ?? "" ,
1083+ `%${ input . dockerImage . trim ( ) } %` ,
1084+ ) ,
1085+ ) ;
1086+ }
1087+
1088+ if ( ctx . user . role === "member" ) {
1089+ const { accessedServices } = await findMemberById (
1090+ ctx . user . id ,
1091+ ctx . session . activeOrganizationId ,
1092+ ) ;
1093+ if ( accessedServices . length === 0 ) return { items : [ ] , total : 0 } ;
1094+ baseConditions . push (
1095+ sql `${ applications . applicationId } IN (${ sql . join (
1096+ accessedServices . map ( ( id ) => sql `${ id } ` ) ,
1097+ sql `, ` ,
1098+ ) } )`,
1099+ ) ;
1100+ }
1101+
1102+ const where = and ( ...baseConditions ) ;
1103+
1104+ const [ items , countResult ] = await Promise . all ( [
1105+ db
1106+ . select ( {
1107+ applicationId : applications . applicationId ,
1108+ name : applications . name ,
1109+ appName : applications . appName ,
1110+ description : applications . description ,
1111+ environmentId : applications . environmentId ,
1112+ applicationStatus : applications . applicationStatus ,
1113+ sourceType : applications . sourceType ,
1114+ createdAt : applications . createdAt ,
1115+ } )
1116+ . from ( applications )
1117+ . innerJoin (
1118+ environments ,
1119+ eq ( applications . environmentId , environments . environmentId ) ,
1120+ )
1121+ . innerJoin ( projects , eq ( environments . projectId , projects . projectId ) )
1122+ . where ( where )
1123+ . orderBy ( desc ( applications . createdAt ) )
1124+ . limit ( input . limit )
1125+ . offset ( input . offset ) ,
1126+ db
1127+ . select ( { count : sql < number > `count(*)::int` } )
1128+ . from ( applications )
1129+ . innerJoin (
1130+ environments ,
1131+ eq ( applications . environmentId , environments . environmentId ) ,
1132+ )
1133+ . innerJoin ( projects , eq ( environments . projectId , projects . projectId ) )
1134+ . where ( where ) ,
1135+ ] ) ;
1136+
1137+ return {
1138+ items,
1139+ total : countResult [ 0 ] ?. count ?? 0 ,
1140+ } ;
1141+ } ) ,
10051142} ) ;
0 commit comments