From a4659c40147bafe84a2177d5343055e7dd4b500d Mon Sep 17 00:00:00 2001 From: shreddedbacon Date: Tue, 7 Jan 2025 09:38:07 +1100 Subject: [PATCH] feat: more filters for deploymentsByFilter --- .../api/src/resources/deployment/resolvers.ts | 20 +++++++++++++++++-- services/api/src/typeDefs.js | 2 +- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/services/api/src/resources/deployment/resolvers.ts b/services/api/src/resources/deployment/resolvers.ts index 28908dbc9c..3c00065ea5 100644 --- a/services/api/src/resources/deployment/resolvers.ts +++ b/services/api/src/resources/deployment/resolvers.ts @@ -151,7 +151,7 @@ export const getDeploymentsByFilter: ResolverFn = async ( { sqlClientPool, hasPermission, models, keycloakGrant, keycloakUsersGroups, adminScopes } ) => { - const { openshifts, deploymentStatus = ["NEW", "PENDING", "RUNNING", "QUEUED"] } = input; + const { openshifts, deploymentStatus = ["NEW", "PENDING", "RUNNING", "QUEUED"], month, includeDeleted } = input; /* use the same mechanism for viewing all projects @@ -181,13 +181,29 @@ export const getDeploymentsByFilter: ResolverFn = async ( queryBuilder = queryBuilder.whereIn('environment.project', userProjectIds); } + // collect builds for a specific year/month + if (month) { + queryBuilder = queryBuilder.andWhere( + knex.raw(`YEAR(deployment.created) = YEAR(STR_TO_DATE(?, '%Y-%m'))`, month), + ) + .andWhere( + knex.raw(`MONTH(deployment.created) = MONTH(STR_TO_DATE(?, '%Y-%m'))`, month), + ); + } + if(openshifts) { queryBuilder = queryBuilder.whereIn('environment.openshift', openshifts); } queryBuilder = queryBuilder.whereIn('deployment.status', deploymentStatus); - queryBuilder = queryBuilder.where('environment.deleted', '=', '0000-00-00 00:00:00'); + // if includeDeleted is false, exclude deleted environments in the results (default) + if (!includeDeleted) { + queryBuilder = queryBuilder.where('environment.deleted', '=', '0000-00-00 00:00:00'); + } + + // exclude results where a project doesn't exist + queryBuilder = queryBuilder.whereRaw('environment.project IN (SELECT id FROM project)') const queryBuilderString = queryBuilder.toString(); diff --git a/services/api/src/typeDefs.js b/services/api/src/typeDefs.js index b3fb60c37c..433f641188 100644 --- a/services/api/src/typeDefs.js +++ b/services/api/src/typeDefs.js @@ -1350,7 +1350,7 @@ const typeDefs = gql` deploymentByRemoteId(id: String): Deployment deploymentByName(input: DeploymentByNameInput): Deployment deploymentsByBulkId(bulkId: String): [Deployment] - deploymentsByFilter(openshifts: [Int], deploymentStatus: [DeploymentStatusType]): [Deployment] + deploymentsByFilter(openshifts: [Int], deploymentStatus: [DeploymentStatusType], month: Date, includeDeleted: Boolean): [Deployment] taskByTaskName(taskName: String): Task taskByRemoteId(id: String): Task taskById(id: Int): Task