feat: Convert all query files to JS

This commit is contained in:
Faruk AYDIN
2023-12-28 13:39:31 +01:00
parent cd91f8c711
commit cbbb76a6c5
39 changed files with 151 additions and 342 deletions

View File

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

View File

@@ -1,12 +1,6 @@
import AppConfig from '../../models/app-config'; import AppConfig from '../../models/app-config';
import Context from '../../types/express/context';
type Params = { const getAppAuthClients = async (_parent, params, context) => {
appKey: string;
active: boolean;
};
const getAppAuthClients = async (_parent: unknown, params: Params, context: Context) => {
let canSeeAllClients = false; let canSeeAllClients = false;
try { try {
context.currentUser.can('read', 'App'); context.currentUser.can('read', 'App');
@@ -16,8 +10,7 @@ const getAppAuthClients = async (_parent: unknown, params: Params, context: Cont
// void // void
} }
const appConfig = await AppConfig const appConfig = await AppConfig.query()
.query()
.findOne({ .findOne({
key: params.appKey, key: params.appKey,
}) })
@@ -30,8 +23,8 @@ const getAppAuthClients = async (_parent: unknown, params: Params, context: Cont
if (!canSeeAllClients) { if (!canSeeAllClients) {
appAuthClients.where({ appAuthClients.where({
active: true active: true,
}) });
} }
return await appAuthClients; 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 App from '../../models/app';
import Connection from '../../models/connection'; import Connection from '../../models/connection';
import Context from '../../types/express/context';
type Params = { const getApp = async (_parent, params, context) => {
key: string;
};
const getApp = async (_parent: unknown, params: Params, context: Context) => {
const conditions = context.currentUser.can('read', 'Connection'); const conditions = context.currentUser.can('read', 'Connection');
const userConnections = context.currentUser.$relatedQuery('connections'); const userConnections = context.currentUser.$relatedQuery('connections');
const allConnections = Connection.query(); const allConnections = Connection.query();
const connectionBaseQuery = conditions.isCreator ? userConnections : allConnections; const connectionBaseQuery = conditions.isCreator
? userConnections
: allConnections;
const app = await App.findOneByKey(params.key); const app = await App.findOneByKey(params.key);
@@ -21,7 +18,7 @@ const getApp = async (_parent: unknown, params: Params, context: Context) => {
.select('connections.*') .select('connections.*')
.withGraphFetched({ .withGraphFetched({
appConfig: true, appConfig: true,
appAuthClient: true appAuthClient: true,
}) })
.fullOuterJoinRelated('steps') .fullOuterJoinRelated('steps')
.where({ .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 { DateTime } from 'luxon';
import { TSubscription } from '@automatisch/types';
import Context from '../../types/express/context';
import Billing from '../../helpers/billing/index.ee'; import Billing from '../../helpers/billing/index.ee';
import Execution from '../../models/execution';
import ExecutionStep from '../../models/execution-step'; import ExecutionStep from '../../models/execution-step';
import Subscription from '../../models/subscription.ee';
const getBillingAndUsage = async ( const getBillingAndUsage = async (_parent, _params, context) => {
_parent: unknown,
_params: unknown,
context: Context
) => {
const persistedSubscription = await context.currentUser.$relatedQuery( const persistedSubscription = await context.currentUser.$relatedQuery(
'currentSubscription' 'currentSubscription'
); );
@@ -28,7 +19,7 @@ const getBillingAndUsage = async (
}; };
}; };
const paidSubscription = (subscription: Subscription): TSubscription => { const paidSubscription = (subscription) => {
const currentPlan = Billing.paddlePlans.find( const currentPlan = Billing.paddlePlans.find(
(plan) => plan.productId === subscription.paddlePlanId (plan) => plan.productId === subscription.paddlePlanId
); );
@@ -63,7 +54,7 @@ const paidSubscription = (subscription: Subscription): TSubscription => {
}; };
}; };
const freeTrialSubscription = (): TSubscription => { const freeTrialSubscription = () => {
return { return {
status: null, status: null,
monthlyQuota: { monthlyQuota: {
@@ -85,15 +76,15 @@ const freeTrialSubscription = (): TSubscription => {
}; };
}; };
const executionIds = async (context: Context) => { const executionIds = async (context) => {
return ( return (
await context.currentUser await context.currentUser
.$relatedQuery('executions') .$relatedQuery('executions')
.select('executions.id') .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() const executionStepCount = await ExecutionStep.query()
.whereIn('execution_id', await executionIds(context)) .whereIn('execution_id', await executionIds(context))
.andWhere( .andWhere(

View File

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

View File

@@ -1,23 +1,15 @@
import { IConnection } from '@automatisch/types';
import App from '../../models/app'; import App from '../../models/app';
import Context from '../../types/express/context';
import Flow from '../../models/flow'; import Flow from '../../models/flow';
import Connection from '../../models/connection'; import Connection from '../../models/connection';
type Params = { const getConnectedApps = async (_parent, params, context) => {
name: string;
};
const getConnectedApps = async (
_parent: unknown,
params: Params,
context: Context
) => {
const conditions = context.currentUser.can('read', 'Connection'); const conditions = context.currentUser.can('read', 'Connection');
const userConnections = context.currentUser.$relatedQuery('connections'); const userConnections = context.currentUser.$relatedQuery('connections');
const allConnections = Connection.query(); const allConnections = Connection.query();
const connectionBaseQuery = conditions.isCreator ? userConnections : allConnections; const connectionBaseQuery = conditions.isCreator
? userConnections
: allConnections;
const userFlows = context.currentUser.$relatedQuery('flows'); const userFlows = context.currentUser.$relatedQuery('flows');
const allFlows = Flow.query(); const allFlows = Flow.query();
@@ -51,7 +43,7 @@ const getConnectedApps = async (
}) })
.map((app) => { .map((app) => {
const connection = connections.find( const connection = connections.find(
(connection) => (connection as IConnection).key === app.key (connection) => connection.key === app.key
); );
app.connectionCount = connection?.count || 0; 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 App from '../../models/app';
import Step from '../../models/step'; import Step from '../../models/step';
import ExecutionStep from '../../models/execution-step'; import ExecutionStep from '../../models/execution-step';
import globalVariable from '../../helpers/global-variable'; import globalVariable from '../../helpers/global-variable';
import computeParameters from '../../helpers/compute-parameters'; import computeParameters from '../../helpers/compute-parameters';
type Params = { const getDynamicData = async (_parent, params, context) => {
stepId: string;
key: string;
parameters: IJSONObject;
};
const getDynamicData = async (
_parent: unknown,
params: Params,
context: Context
) => {
const conditions = context.currentUser.can('update', 'Flow'); const conditions = context.currentUser.can('update', 'Flow');
const userSteps = context.currentUser.$relatedQuery('steps'); const userSteps = context.currentUser.$relatedQuery('steps');
const allSteps = Step.query(); const allSteps = Step.query();
@@ -40,9 +28,7 @@ const getDynamicData = async (
const app = await App.findOneByKey(step.appKey); const app = await App.findOneByKey(step.appKey);
const $ = await globalVariable({ connection, app, flow, step }); const $ = await globalVariable({ connection, app, flow, step });
const command = app.dynamicData.find( const command = app.dynamicData.find((data) => data.key === params.key);
(data: IDynamicData) => data.key === params.key
);
// apply run-time parameters that're not persisted yet // apply run-time parameters that're not persisted yet
for (const parameterKey in params.parameters) { for (const parameterKey in params.parameters) {
@@ -53,12 +39,17 @@ const getDynamicData = async (
const lastExecution = await flow.$relatedQuery('lastExecution'); const lastExecution = await flow.$relatedQuery('lastExecution');
const lastExecutionId = lastExecution?.id; const lastExecutionId = lastExecution?.id;
const priorExecutionSteps = lastExecutionId ? await ExecutionStep.query().where({ const priorExecutionSteps = lastExecutionId
execution_id: lastExecutionId, ? await ExecutionStep.query().where({
}) : []; execution_id: lastExecutionId,
})
: [];
// compute variables in parameters // compute variables in parameters
const computedParameters = computeParameters($.step.parameters, priorExecutionSteps); const computedParameters = computeParameters(
$.step.parameters,
priorExecutionSteps
);
$.step.parameters = computedParameters; $.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 App from '../../models/app';
import Step from '../../models/step'; import Step from '../../models/step';
import globalVariable from '../../helpers/global-variable'; import globalVariable from '../../helpers/global-variable';
type Params = { const getDynamicFields = async (_parent, params, context) => {
stepId: string;
key: string;
parameters: IJSONObject;
};
const getDynamicFields = async (
_parent: unknown,
params: Params,
context: Context
) => {
const conditions = context.currentUser.can('update', 'Flow'); const conditions = context.currentUser.can('update', 'Flow');
const userSteps = context.currentUser.$relatedQuery('steps'); const userSteps = context.currentUser.$relatedQuery('steps');
const allSteps = Step.query(); const allSteps = Step.query();
@@ -37,16 +25,14 @@ const getDynamicFields = async (
const app = await App.findOneByKey(step.appKey); const app = await App.findOneByKey(step.appKey);
const $ = await globalVariable({ connection, app, flow: step.flow, step }); const $ = await globalVariable({ connection, app, flow: step.flow, step });
const command = app.dynamicFields.find( const command = app.dynamicFields.find((data) => data.key === params.key);
(data: IDynamicFields) => data.key === params.key
);
for (const parameterKey in params.parameters) { for (const parameterKey in params.parameters) {
const parameterValue = params.parameters[parameterKey]; const parameterValue = params.parameters[parameterKey];
$.step.parameters[parameterKey] = parameterValue; $.step.parameters[parameterKey] = parameterValue;
} }
const additionalFields = await command.run($) || []; const additionalFields = (await command.run($)) || [];
return additionalFields; return additionalFields;
}; };

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,12 +1,9 @@
import Context from '../../types/express/context';
import Billing from '../../helpers/billing/index.ee'; import Billing from '../../helpers/billing/index.ee';
const getInvoices = async ( const getInvoices = async (_parent, _params, context) => {
_parent: unknown, const subscription = await context.currentUser.$relatedQuery(
_params: unknown, 'currentSubscription'
context: Context );
) => {
const subscription = await context.currentUser.$relatedQuery('currentSubscription');
if (!subscription) { if (!subscription) {
return; 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'; 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'); context.currentUser.can('read', 'Role');
return await Role.query().orderBy('name'); 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'; import SamlAuthProvider from '../../models/saml-auth-provider.ee';
type Params = { const getSamlAuthProviderRoleMappings = async (_parent, params, context) => {
id: string;
}
const getSamlAuthProviderRoleMappings = async (_parent: unknown, params: Params, context: Context) => {
context.currentUser.can('read', 'SamlAuthProvider'); context.currentUser.can('read', 'SamlAuthProvider');
const samlAuthProvider = await SamlAuthProvider const samlAuthProvider = await SamlAuthProvider.query()
.query()
.findById(params.id) .findById(params.id)
.throwIfNotFound(); .throwIfNotFound();
const roleMappings = await samlAuthProvider const roleMappings = await samlAuthProvider
.$relatedQuery('samlAuthProvidersRoleMappings') .$relatedQuery('samlAuthProvidersRoleMappings')
.orderBy('remote_role_name', 'asc') .orderBy('remote_role_name', 'asc');
return roleMappings; return roleMappings;
}; };

View File

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

View File

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

View File

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

View File

@@ -1,15 +1,11 @@
import appConfig from '../../config/app'; import appConfig from '../../config/app';
import Context from '../../types/express/context';
const getTrialStatus = async ( const getTrialStatus = async (_parent, _params, context) => {
_parent: unknown,
_params: unknown,
context: Context
) => {
if (!appConfig.isCloud) return; if (!appConfig.isCloud) return;
const inTrial = await context.currentUser.inTrial(); const inTrial = await context.currentUser.inTrial();
const hasActiveSubscription = await context.currentUser.hasActiveSubscription(); const hasActiveSubscription =
await context.currentUser.hasActiveSubscription();
if (!inTrial && hasActiveSubscription) return; 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 paginate from '../../helpers/pagination';
import User from '../../models/user'; import User from '../../models/user';
type Params = { const getUsers = async (_parent, params, context) => {
limit: number;
offset: number;
};
const getUsers = async (_parent: unknown, params: Params, context: Context) => {
context.currentUser.can('read', 'User'); context.currentUser.can('read', 'User');
const usersQuery = User.query() const usersQuery = User.query()

View File

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