Merge pull request #1512 from automatisch/queries-js

feat: Convert all query files to JS
This commit is contained in:
Ömer Faruk Aydın
2024-01-04 18:04:21 +01:00
committed by GitHub
39 changed files with 151 additions and 342 deletions

View File

@@ -1,11 +1,6 @@
import AppAuthClient from '../../models/app-auth-client';
import Context from '../../types/express/context';
type Params = {
id: string;
};
const getAppAuthClient = async (_parent: unknown, params: Params, context: Context) => {
const getAppAuthClient = async (_parent, params, context) => {
let canSeeAllClients = false;
try {
context.currentUser.can('read', 'App');
@@ -15,8 +10,7 @@ const getAppAuthClient = async (_parent: unknown, params: Params, context: Conte
// void
}
const appAuthClient = AppAuthClient
.query()
const appAuthClient = AppAuthClient.query()
.findById(params.id)
.throwIfNotFound();

View File

@@ -1,12 +1,6 @@
import AppConfig from '../../models/app-config';
import Context from '../../types/express/context';
type Params = {
appKey: string;
active: boolean;
};
const getAppAuthClients = async (_parent: unknown, params: Params, context: Context) => {
const getAppAuthClients = async (_parent, params, context) => {
let canSeeAllClients = false;
try {
context.currentUser.can('read', 'App');
@@ -16,8 +10,7 @@ const getAppAuthClients = async (_parent: unknown, params: Params, context: Cont
// void
}
const appConfig = await AppConfig
.query()
const appConfig = await AppConfig.query()
.findOne({
key: params.appKey,
})
@@ -30,8 +23,8 @@ const getAppAuthClients = async (_parent: unknown, params: Params, context: Cont
if (!canSeeAllClients) {
appAuthClients.where({
active: true
})
active: true,
});
}
return await appAuthClients;

View File

@@ -0,0 +1,17 @@
import AppConfig from '../../models/app-config';
const getAppConfig = async (_parent, params, context) => {
context.currentUser.can('create', 'Connection');
const appConfig = await AppConfig.query()
.withGraphFetched({
appAuthClients: true,
})
.findOne({
key: params.key,
});
return appConfig;
};
export default getAppConfig;

View File

@@ -1,23 +0,0 @@
import AppConfig from '../../models/app-config';
import Context from '../../types/express/context';
type Params = {
key: string;
};
const getAppConfig = async (_parent: unknown, params: Params, context: Context) => {
context.currentUser.can('create', 'Connection');
const appConfig = await AppConfig
.query()
.withGraphFetched({
appAuthClients: true
})
.findOne({
key: params.key
});
return appConfig;
};
export default getAppConfig;

View File

@@ -1,17 +1,14 @@
import App from '../../models/app';
import Connection from '../../models/connection';
import Context from '../../types/express/context';
type Params = {
key: string;
};
const getApp = async (_parent: unknown, params: Params, context: Context) => {
const getApp = async (_parent, params, context) => {
const conditions = context.currentUser.can('read', 'Connection');
const userConnections = context.currentUser.$relatedQuery('connections');
const allConnections = Connection.query();
const connectionBaseQuery = conditions.isCreator ? userConnections : allConnections;
const connectionBaseQuery = conditions.isCreator
? userConnections
: allConnections;
const app = await App.findOneByKey(params.key);
@@ -21,7 +18,7 @@ const getApp = async (_parent: unknown, params: Params, context: Context) => {
.select('connections.*')
.withGraphFetched({
appConfig: true,
appAuthClient: true
appAuthClient: true,
})
.fullOuterJoinRelated('steps')
.where({

View File

@@ -0,0 +1,17 @@
import App from '../../models/app';
const getApps = async (_parent, params) => {
const apps = await App.findAll(params.name);
if (params.onlyWithTriggers) {
return apps.filter((app) => app.triggers?.length);
}
if (params.onlyWithActions) {
return apps.filter((app) => app.actions?.length);
}
return apps;
};
export default getApps;

View File

@@ -1,24 +0,0 @@
import { IApp } from '@automatisch/types';
import App from '../../models/app';
type Params = {
name: string;
onlyWithTriggers: boolean;
onlyWithActions: boolean;
};
const getApps = async (_parent: unknown, params: Params) => {
const apps = await App.findAll(params.name);
if (params.onlyWithTriggers) {
return apps.filter((app: IApp) => app.triggers?.length);
}
if (params.onlyWithActions) {
return apps.filter((app: IApp) => app.actions?.length);
}
return apps;
};
export default getApps;

View File

@@ -1,17 +1,8 @@
import { DateTime } from 'luxon';
import { TSubscription } from '@automatisch/types';
import Context from '../../types/express/context';
import Billing from '../../helpers/billing/index.ee';
import Execution from '../../models/execution';
import ExecutionStep from '../../models/execution-step';
import Subscription from '../../models/subscription.ee';
const getBillingAndUsage = async (
_parent: unknown,
_params: unknown,
context: Context
) => {
const getBillingAndUsage = async (_parent, _params, context) => {
const persistedSubscription = await context.currentUser.$relatedQuery(
'currentSubscription'
);
@@ -28,7 +19,7 @@ const getBillingAndUsage = async (
};
};
const paidSubscription = (subscription: Subscription): TSubscription => {
const paidSubscription = (subscription) => {
const currentPlan = Billing.paddlePlans.find(
(plan) => plan.productId === subscription.paddlePlanId
);
@@ -63,7 +54,7 @@ const paidSubscription = (subscription: Subscription): TSubscription => {
};
};
const freeTrialSubscription = (): TSubscription => {
const freeTrialSubscription = () => {
return {
status: null,
monthlyQuota: {
@@ -85,15 +76,15 @@ const freeTrialSubscription = (): TSubscription => {
};
};
const executionIds = async (context: Context) => {
const executionIds = async (context) => {
return (
await context.currentUser
.$relatedQuery('executions')
.select('executions.id')
).map((execution: Execution) => execution.id);
).map((execution) => execution.id);
};
const executionStepCount = async (context: Context) => {
const executionStepCount = async (context) => {
const executionStepCount = await ExecutionStep.query()
.whereIn('execution_id', await executionIds(context))
.andWhere(

View File

@@ -1,11 +1,7 @@
import { hasValidLicense } from '../../helpers/license.ee';
import Config from '../../models/config';
type Params = {
keys: string[];
};
const getConfig = async (_parent: unknown, params: Params) => {
const getConfig = async (_parent, params) => {
if (!(await hasValidLicense())) return {};
const configQuery = Config.query();
@@ -22,7 +18,7 @@ const getConfig = async (_parent: unknown, params: Params) => {
computedConfig[key] = value?.data;
return computedConfig;
}, {} as Record<string, unknown>);
}, {});
};
export default getConfig;

View File

@@ -1,23 +1,15 @@
import { IConnection } from '@automatisch/types';
import App from '../../models/app';
import Context from '../../types/express/context';
import Flow from '../../models/flow';
import Connection from '../../models/connection';
type Params = {
name: string;
};
const getConnectedApps = async (
_parent: unknown,
params: Params,
context: Context
) => {
const getConnectedApps = async (_parent, params, context) => {
const conditions = context.currentUser.can('read', 'Connection');
const userConnections = context.currentUser.$relatedQuery('connections');
const allConnections = Connection.query();
const connectionBaseQuery = conditions.isCreator ? userConnections : allConnections;
const connectionBaseQuery = conditions.isCreator
? userConnections
: allConnections;
const userFlows = context.currentUser.$relatedQuery('flows');
const allFlows = Flow.query();
@@ -51,7 +43,7 @@ const getConnectedApps = async (
})
.map((app) => {
const connection = connections.find(
(connection) => (connection as IConnection).key === app.key
(connection) => connection.key === app.key
);
app.connectionCount = connection?.count || 0;

View File

@@ -0,0 +1,5 @@
const getCurrentUser = async (_parent, _params, context) => {
return context.currentUser;
};
export default getCurrentUser;

View File

@@ -1,11 +0,0 @@
import Context from '../../types/express/context';
const getCurrentUser = async (
_parent: unknown,
_params: unknown,
context: Context
) => {
return context.currentUser;
};
export default getCurrentUser;

View File

@@ -1,22 +1,10 @@
import { IDynamicData, IJSONObject } from '@automatisch/types';
import Context from '../../types/express/context';
import App from '../../models/app';
import Step from '../../models/step';
import ExecutionStep from '../../models/execution-step';
import globalVariable from '../../helpers/global-variable';
import computeParameters from '../../helpers/compute-parameters';
type Params = {
stepId: string;
key: string;
parameters: IJSONObject;
};
const getDynamicData = async (
_parent: unknown,
params: Params,
context: Context
) => {
const getDynamicData = async (_parent, params, context) => {
const conditions = context.currentUser.can('update', 'Flow');
const userSteps = context.currentUser.$relatedQuery('steps');
const allSteps = Step.query();
@@ -40,9 +28,7 @@ const getDynamicData = async (
const app = await App.findOneByKey(step.appKey);
const $ = await globalVariable({ connection, app, flow, step });
const command = app.dynamicData.find(
(data: IDynamicData) => data.key === params.key
);
const command = app.dynamicData.find((data) => data.key === params.key);
// apply run-time parameters that're not persisted yet
for (const parameterKey in params.parameters) {
@@ -53,12 +39,17 @@ const getDynamicData = async (
const lastExecution = await flow.$relatedQuery('lastExecution');
const lastExecutionId = lastExecution?.id;
const priorExecutionSteps = lastExecutionId ? await ExecutionStep.query().where({
execution_id: lastExecutionId,
}) : [];
const priorExecutionSteps = lastExecutionId
? await ExecutionStep.query().where({
execution_id: lastExecutionId,
})
: [];
// compute variables in parameters
const computedParameters = computeParameters($.step.parameters, priorExecutionSteps);
const computedParameters = computeParameters(
$.step.parameters,
priorExecutionSteps
);
$.step.parameters = computedParameters;

View File

@@ -1,20 +1,8 @@
import { IDynamicFields, IJSONObject } from '@automatisch/types';
import Context from '../../types/express/context';
import App from '../../models/app';
import Step from '../../models/step';
import globalVariable from '../../helpers/global-variable';
type Params = {
stepId: string;
key: string;
parameters: IJSONObject;
};
const getDynamicFields = async (
_parent: unknown,
params: Params,
context: Context
) => {
const getDynamicFields = async (_parent, params, context) => {
const conditions = context.currentUser.can('update', 'Flow');
const userSteps = context.currentUser.$relatedQuery('steps');
const allSteps = Step.query();
@@ -37,16 +25,14 @@ const getDynamicFields = async (
const app = await App.findOneByKey(step.appKey);
const $ = await globalVariable({ connection, app, flow: step.flow, step });
const command = app.dynamicFields.find(
(data: IDynamicFields) => data.key === params.key
);
const command = app.dynamicFields.find((data) => data.key === params.key);
for (const parameterKey in params.parameters) {
const parameterValue = params.parameters[parameterKey];
$.step.parameters[parameterKey] = parameterValue;
}
const additionalFields = await command.run($) || [];
const additionalFields = (await command.run($)) || [];
return additionalFields;
};

View File

@@ -1,22 +1,13 @@
import Context from '../../types/express/context';
import paginate from '../../helpers/pagination';
import Execution from '../../models/execution';
type Params = {
executionId: string;
limit: number;
offset: number;
};
const getExecutionSteps = async (
_parent: unknown,
params: Params,
context: Context
) => {
const getExecutionSteps = async (_parent, params, context) => {
const conditions = context.currentUser.can('read', 'Execution');
const userExecutions = context.currentUser.$relatedQuery('executions');
const allExecutions = Execution.query();
const executionBaseQuery = conditions.isCreator ? userExecutions : allExecutions;
const executionBaseQuery = conditions.isCreator
? userExecutions
: allExecutions;
const execution = await executionBaseQuery
.clone()

View File

@@ -1,19 +1,12 @@
import Context from '../../types/express/context';
import Execution from '../../models/execution';
type Params = {
executionId: string;
};
const getExecution = async (
_parent: unknown,
params: Params,
context: Context
) => {
const getExecution = async (_parent, params, context) => {
const conditions = context.currentUser.can('read', 'Execution');
const userExecutions = context.currentUser.$relatedQuery('executions');
const allExecutions = Execution.query();
const executionBaseQuery = conditions.isCreator ? userExecutions : allExecutions;
const executionBaseQuery = conditions.isCreator
? userExecutions
: allExecutions;
const execution = await executionBaseQuery
.clone()

View File

@@ -1,36 +1,18 @@
import { raw } from 'objection';
import { DateTime } from 'luxon';
import Context from '../../types/express/context';
import Execution from '../../models/execution';
import paginate from '../../helpers/pagination';
type Filters = {
flowId?: string;
status?: string;
createdAt?: {
from?: string;
to?: string;
};
}
type Params = {
limit: number;
offset: number;
filters?: Filters;
};
const getExecutions = async (
_parent: unknown,
params: Params,
context: Context
) => {
const getExecutions = async (_parent, params, context) => {
const conditions = context.currentUser.can('read', 'Execution');
const filters = params.filters;
const userExecutions = context.currentUser.$relatedQuery('executions');
const allExecutions = Execution.query();
const executionBaseQuery = conditions.isCreator ? userExecutions : allExecutions;
const executionBaseQuery = conditions.isCreator
? userExecutions
: allExecutions;
const selectStatusStatement = `
case
@@ -48,8 +30,7 @@ const getExecutions = async (
.groupBy('executions.id')
.orderBy('created_at', 'desc');
const computedExecutions = Execution
.query()
const computedExecutions = Execution.query()
.with('executions', executions)
.withSoftDeleted()
.withGraphFetched({
@@ -69,20 +50,16 @@ const getExecutions = async (
if (filters?.createdAt) {
const createdAtFilter = filters.createdAt;
if (createdAtFilter.from) {
const isoFromDateTime = DateTime
.fromMillis(
parseInt(createdAtFilter.from, 10)
)
.toISO();
const isoFromDateTime = DateTime.fromMillis(
parseInt(createdAtFilter.from, 10)
).toISO();
computedExecutions.where('executions.created_at', '>=', isoFromDateTime);
}
if (createdAtFilter.to) {
const isoToDateTime = DateTime
.fromMillis(
parseInt(createdAtFilter.to, 10)
)
.toISO();
const isoToDateTime = DateTime.fromMillis(
parseInt(createdAtFilter.to, 10)
).toISO();
computedExecutions.where('executions.created_at', '<=', isoToDateTime);
}
}

View File

@@ -1,11 +1,6 @@
import Context from '../../types/express/context';
import Flow from '../../models/flow';
type Params = {
id: string;
};
const getFlow = async (_parent: unknown, params: Params, context: Context) => {
const getFlow = async (_parent, params, context) => {
const conditions = context.currentUser.can('read', 'Flow');
const userFlows = context.currentUser.$relatedQuery('flows');
const allFlows = Flow.query();

View File

@@ -1,16 +1,7 @@
import Flow from '../../models/flow';
import Context from '../../types/express/context';
import paginate from '../../helpers/pagination';
type Params = {
appKey?: string;
connectionId?: string;
name?: string;
limit: number;
offset: number;
};
const getFlows = async (_parent: unknown, params: Params, context: Context) => {
const getFlows = async (_parent, params, context) => {
const conditions = context.currentUser.can('read', 'Flow');
const userFlows = context.currentUser.$relatedQuery('flows');
const allFlows = Flow.query();

View File

@@ -1,12 +1,9 @@
import Context from '../../types/express/context';
import Billing from '../../helpers/billing/index.ee';
const getInvoices = async (
_parent: unknown,
_params: unknown,
context: Context
) => {
const subscription = await context.currentUser.$relatedQuery('currentSubscription');
const getInvoices = async (_parent, _params, context) => {
const subscription = await context.currentUser.$relatedQuery(
'currentSubscription'
);
if (!subscription) {
return;

View File

@@ -0,0 +1,17 @@
import Role from '../../models/role';
const getRole = async (_parent, params, context) => {
context.currentUser.can('read', 'Role');
return await Role.query()
.leftJoinRelated({
permissions: true,
})
.withGraphFetched({
permissions: true,
})
.findById(params.id)
.throwIfNotFound();
};
export default getRole;

View File

@@ -1,23 +0,0 @@
import Context from '../../types/express/context';
import Role from '../../models/role';
type Params = {
id: string
};
const getRole = async (_parent: unknown, params: Params, context: Context) => {
context.currentUser.can('read', 'Role');
return await Role
.query()
.leftJoinRelated({
permissions: true
})
.withGraphFetched({
permissions: true
})
.findById(params.id)
.throwIfNotFound();
};
export default getRole;

View File

@@ -1,7 +1,6 @@
import Context from '../../types/express/context';
import Role from '../../models/role';
const getRoles = async (_parent: unknown, params: unknown, context: Context) => {
const getRoles = async (_parent, params, context) => {
context.currentUser.can('read', 'Role');
return await Role.query().orderBy('name');

View File

@@ -1,21 +1,15 @@
import Context from '../../types/express/context';
import SamlAuthProvider from '../../models/saml-auth-provider.ee';
type Params = {
id: string;
}
const getSamlAuthProviderRoleMappings = async (_parent: unknown, params: Params, context: Context) => {
const getSamlAuthProviderRoleMappings = async (_parent, params, context) => {
context.currentUser.can('read', 'SamlAuthProvider');
const samlAuthProvider = await SamlAuthProvider
.query()
const samlAuthProvider = await SamlAuthProvider.query()
.findById(params.id)
.throwIfNotFound();
const roleMappings = await samlAuthProvider
.$relatedQuery('samlAuthProvidersRoleMappings')
.orderBy('remote_role_name', 'asc')
.orderBy('remote_role_name', 'asc');
return roleMappings;
};

View File

@@ -1,11 +1,6 @@
import Context from '../../types/express/context';
import SamlAuthProvider from '../../models/saml-auth-provider.ee';
const getSamlAuthProvider = async (
_parent: unknown,
params: unknown,
context: Context
) => {
const getSamlAuthProvider = async (_parent, params, context) => {
context.currentUser.can('read', 'SamlAuthProvider');
const samlAuthProvider = await SamlAuthProvider.query()

View File

@@ -1,17 +1,8 @@
import { ref } from 'objection';
import ExecutionStep from '../../models/execution-step';
import Step from '../../models/step';
import Context from '../../types/express/context';
type Params = {
stepId: string;
};
const getStepWithTestExecutions = async (
_parent: unknown,
params: Params,
context: Context
) => {
const getStepWithTestExecutions = async (_parent, params, context) => {
const conditions = context.currentUser.can('update', 'Flow');
const userSteps = context.currentUser.$relatedQuery('steps');
const allSteps = Step.query();

View File

@@ -1,11 +1,6 @@
import appConfig from '../../config/app';
import Context from '../../types/express/context';
const getSubscriptionStatus = async (
_parent: unknown,
_params: unknown,
context: Context
) => {
const getSubscriptionStatus = async (_parent, _params, context) => {
if (!appConfig.isCloud) return;
const currentSubscription = await context.currentUser.$relatedQuery(

View File

@@ -1,15 +1,11 @@
import appConfig from '../../config/app';
import Context from '../../types/express/context';
const getTrialStatus = async (
_parent: unknown,
_params: unknown,
context: Context
) => {
const getTrialStatus = async (_parent, _params, context) => {
if (!appConfig.isCloud) return;
const inTrial = await context.currentUser.inTrial();
const hasActiveSubscription = await context.currentUser.hasActiveSubscription();
const hasActiveSubscription =
await context.currentUser.hasActiveSubscription();
if (!inTrial && hasActiveSubscription) return;

View File

@@ -0,0 +1,17 @@
import User from '../../models/user';
const getUser = async (_parent, params, context) => {
context.currentUser.can('read', 'User');
return await User.query()
.leftJoinRelated({
role: true,
})
.withGraphFetched({
role: true,
})
.findById(params.id)
.throwIfNotFound();
};
export default getUser;

View File

@@ -1,23 +0,0 @@
import Context from '../../types/express/context';
import User from '../../models/user';
type Params = {
id: string
};
const getUser = async (_parent: unknown, params: Params, context: Context) => {
context.currentUser.can('read', 'User');
return await User
.query()
.leftJoinRelated({
role: true
})
.withGraphFetched({
role: true
})
.findById(params.id)
.throwIfNotFound();
};
export default getUser;

View File

@@ -1,13 +1,7 @@
import Context from '../../types/express/context';
import paginate from '../../helpers/pagination';
import User from '../../models/user';
type Params = {
limit: number;
offset: number;
};
const getUsers = async (_parent: unknown, params: Params, context: Context) => {
const getUsers = async (_parent, params, context) => {
context.currentUser.can('read', 'User');
const usersQuery = User.query()

View File

@@ -1,22 +1,14 @@
import Context from '../../types/express/context';
import App from '../../models/app';
import Connection from '../../models/connection';
import globalVariable from '../../helpers/global-variable';
type Params = {
id: string;
data: object;
};
const testConnection = async (
_parent: unknown,
params: Params,
context: Context
) => {
const testConnection = async (_parent, params, context) => {
const conditions = context.currentUser.can('update', 'Connection');
const userConnections = context.currentUser.$relatedQuery('connections');
const allConnections = Connection.query();
const connectionBaseQuery = conditions.isCreator ? userConnections : allConnections;
const connectionBaseQuery = conditions.isCreator
? userConnections
: allConnections;
let connection = await connectionBaseQuery
.clone()