Skip to content

Commit

Permalink
Paginate list connections with filters
Browse files Browse the repository at this point in the history
  • Loading branch information
hassan254-prog committed Jan 29, 2024
1 parent b3f84fd commit 2f7ecfd
Show file tree
Hide file tree
Showing 5 changed files with 294 additions and 37 deletions.
23 changes: 21 additions & 2 deletions packages/server/lib/controllers/connection.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,18 @@ class ConnectionController {

async listConnections(req: Request, res: Response, next: NextFunction) {
try {
const limit = req.query['limit'] ? parseInt(req.query['limit'] as string) : 20;
const offset = req.query['offset'] ? parseInt(req.query['offset'] as string) : 0;
const integration = req.query['integration']?.toString();
const connection = req.query['connection']?.toString();
const { success, error, response } = await getEnvironmentAndAccountId(res, req);
if (!success || response === null) {
errorManager.errResFromNangoErr(res, error);
return;
}
const { accountId, environmentId, isWeb } = response;

const { connectionId } = req.query;
const connections = await connectionService.listConnections(environmentId, connectionId as string);
const connections = await connectionService.listConnections(environmentId, limit, offset, connection as string, integration as string);

if (!isWeb) {
analytics.track(AnalyticsTypes.CONNECTION_LIST_FETCHED, accountId);
Expand Down Expand Up @@ -706,6 +709,22 @@ class ConnectionController {
next(err);
}
}
public async getPossibleFilters(req: Request, res: Response, next: NextFunction) {
try {
const { success: sessionSuccess, error: sessionError, response } = await getUserAccountAndEnvironmentFromSession(req);
if (!sessionSuccess || response === null) {
errorManager.errResFromNangoErr(res, sessionError);
return;
}
const { environment } = response;

const integrations = await configService.getAllNames(environment.id);
const connections = await connectionService.getAllNames(environment.id);
res.send({ integrations, connections });
} catch (error) {
next(error);
}
}
}

export default new ConnectionController();
1 change: 1 addition & 0 deletions packages/server/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ app.route('/api/v1/integration/:providerConfigKey').delete(webAuth, configContro
app.route('/api/v1/provider').get(connectionController.listProviders.bind(connectionController));

app.route('/api/v1/connection').get(webAuth, connectionController.listConnections.bind(connectionController));
app.route('/api/v1/connection-filters').get(webAuth, connectionController.getPossibleFilters.bind(connectionController));
app.route('/api/v1/connection/:connectionId').get(webAuth, connectionController.getConnectionWeb.bind(connectionController));
app.route('/api/v1/connection/:connectionId').delete(webAuth, connectionController.deleteConnection.bind(connectionController));
app.route('/api/v1/connection/admin/:connectionId').delete(webAuth, connectionController.deleteAdminConnection.bind(connectionController));
Expand Down
19 changes: 15 additions & 4 deletions packages/shared/lib/services/connection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,16 +504,27 @@ class ConnectionService {

public async listConnections(
environment_id: number,
connectionId?: string
limit = 20,
offset = 0,
connection?: string,
integration?: string
): Promise<{ id: number; connection_id: string; provider: string; created: string; metadata: Metadata }[]> {
const queryBuilder = db.knex
.withSchema(db.schema())
.from<Connection>(`_nango_connections`)
.select({ id: 'id' }, { connection_id: 'connection_id' }, { provider: 'provider_config_key' }, { created: 'created_at' }, 'metadata')
.where({ environment_id, deleted: false });
if (connectionId) {
queryBuilder.where({ connection_id: connectionId });
.where({ environment_id, deleted: false })
.offset(offset)
.limit(limit);

if (connection) {
queryBuilder.where({ connection_id: connection });
}

if (integration) {
queryBuilder.where({ provider_config_key: integration });
}

return queryBuilder;
}

Expand Down
Loading

0 comments on commit 2f7ecfd

Please sign in to comment.