From c48c9058058d48048604db835a3a64fdd5dcb18f Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Sat, 6 Nov 2021 23:38:37 +0100 Subject: [PATCH] feat: Implement draft version of Step model --- .../migrations/20211106214730_create_steps.ts | 18 ++++++++ .../src/graphql/mutations/create-step.ts | 43 +++++++++++++++++++ packages/backend/src/graphql/root-mutation.ts | 4 +- packages/backend/src/graphql/types/step.ts | 21 +++++++++ packages/backend/src/models/step.ts | 33 ++++++++++++++ 5 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 packages/backend/src/db/migrations/20211106214730_create_steps.ts create mode 100644 packages/backend/src/graphql/mutations/create-step.ts create mode 100644 packages/backend/src/graphql/types/step.ts create mode 100644 packages/backend/src/models/step.ts diff --git a/packages/backend/src/db/migrations/20211106214730_create_steps.ts b/packages/backend/src/db/migrations/20211106214730_create_steps.ts new file mode 100644 index 00000000..eaa06eb9 --- /dev/null +++ b/packages/backend/src/db/migrations/20211106214730_create_steps.ts @@ -0,0 +1,18 @@ +import { Knex } from "knex"; + +export async function up(knex: Knex): Promise { + return knex.schema.createTable('steps', (table) => { + table.increments('id'); + table.string('key').notNullable(); + table.string('app_key').notNullable(); + table.string('type').notNullable(); + table.integer('connection_id').references('id').inTable('connections'); + table.text('parameters') + + table.timestamps(true, true); + }); +}; + +export async function down(knex: Knex): Promise { + return knex.schema.dropTable('steps'); +} diff --git a/packages/backend/src/graphql/mutations/create-step.ts b/packages/backend/src/graphql/mutations/create-step.ts new file mode 100644 index 00000000..53c3d30b --- /dev/null +++ b/packages/backend/src/graphql/mutations/create-step.ts @@ -0,0 +1,43 @@ +import { GraphQLString, GraphQLNonNull, GraphQLInt, GraphQLEnumType } from 'graphql'; +import Step from '../../models/step'; +import stepType from '../types/step'; +import RequestWithCurrentUser from '../../types/express/request-with-current-user'; + +type Params = { + key: string, + appKey: string, + type: string + connectionId: number +} + +const createStepResolver = async (params: Params, req: RequestWithCurrentUser) => { + const step = await Step.query().insertAndFetch({ + key: params.key, + appKey: params.appKey, + type: params.type, + connectionId: params.connectionId, + }); + + return step; +} + +const createStep = { + type: stepType, + args: { + key: { type: GraphQLNonNull(GraphQLString) }, + appKey: { type: GraphQLNonNull(GraphQLString) }, + type: { + type: new GraphQLEnumType({ + name: 'StepInputEnumType', + values: { + trigger: { value: 'trigger' }, + action: { value: 'action' }, + } + }) + }, + connectionId: { type: GraphQLNonNull(GraphQLInt) } + }, + resolve: (_: any, params: Params, req: RequestWithCurrentUser) => createStepResolver(params, req) +}; + +export default createStep; diff --git a/packages/backend/src/graphql/root-mutation.ts b/packages/backend/src/graphql/root-mutation.ts index 34892723..a50b4be5 100644 --- a/packages/backend/src/graphql/root-mutation.ts +++ b/packages/backend/src/graphql/root-mutation.ts @@ -5,6 +5,7 @@ import updateConnection from './mutations/update-connection'; import resetConnection from './mutations/reset-connection'; import verifyConnection from './mutations/verify-connection'; import deleteConnection from './mutations/delete-connection'; +import createStep from './mutations/create-step'; const rootMutation = new GraphQLObjectType({ name: 'Mutation', @@ -14,7 +15,8 @@ const rootMutation = new GraphQLObjectType({ updateConnection, resetConnection, verifyConnection, - deleteConnection + deleteConnection, + createStep, } }); diff --git a/packages/backend/src/graphql/types/step.ts b/packages/backend/src/graphql/types/step.ts new file mode 100644 index 00000000..71f9820b --- /dev/null +++ b/packages/backend/src/graphql/types/step.ts @@ -0,0 +1,21 @@ +import { GraphQLObjectType, GraphQLString, GraphQLNonNull, GraphQLEnumType, GraphQLInt } from 'graphql'; + +const stepType = new GraphQLObjectType({ + name: 'Step', + fields: { + key: { type: GraphQLNonNull(GraphQLString) }, + appKey: { type: GraphQLNonNull(GraphQLString) }, + type: { + type: new GraphQLEnumType({ + name: 'StepEnumType', + values: { + trigger: { value: 'trigger' }, + action: { value: 'action' }, + } + }) + }, + connectionId: { type: GraphQLNonNull(GraphQLInt) } + } +}) + +export default stepType; diff --git a/packages/backend/src/models/step.ts b/packages/backend/src/models/step.ts new file mode 100644 index 00000000..6dc01156 --- /dev/null +++ b/packages/backend/src/models/step.ts @@ -0,0 +1,33 @@ +import Base from './base' + +enum StepEnumType { + 'trigger', + 'action', +} + +class Step extends Base { + id!: number + key!: string + appKey!: string + type!: StepEnumType + connectionId!: number + parameters: any + + static tableName = 'steps'; + + static jsonSchema = { + type: 'object', + required: ['key', 'appKey', 'type', 'connectionId'], + + properties: { + id: { type: 'integer' }, + key: { type: 'string', minLength: 1, maxLength: 255 }, + appKey: { type: 'string', minLength: 1, maxLength: 255 }, + type: { type: "string", enum: ["action", "trigger"] }, + connectionId: { type: 'integer' }, + parameters: { type: 'object' }, + } + } +} + +export default Step;