fix: Introduce formattedData for connections to separate types

This commit is contained in:
Faruk AYDIN
2022-03-04 15:16:09 +03:00
committed by Ömer Faruk Aydın
parent 39b50fc0d3
commit 719a948134
8 changed files with 28 additions and 23 deletions

View File

@@ -22,15 +22,15 @@ const createAuthDataResolver = async (
const appData = App.findOneByKey(connection.key); const appData = App.findOneByKey(connection.key);
const appInstance = new appClass(appData, { const appInstance = new appClass(appData, {
consumerKey: connection.data.consumerKey, consumerKey: connection.formattedData.consumerKey,
consumerSecret: connection.data.consumerSecret, consumerSecret: connection.formattedData.consumerSecret,
}); });
const authLink = await appInstance.authenticationClient.createAuthData(); const authLink = await appInstance.authenticationClient.createAuthData();
await connection.$query().patch({ await connection.$query().patch({
data: { formattedData: {
...connection.data, ...connection.formattedData,
...authLink, ...authLink,
}, },
}); });

View File

@@ -4,10 +4,11 @@ import connectionType from '../types/connection';
import availableAppsEnumType from '../types/available-apps-enum-type'; import availableAppsEnumType from '../types/available-apps-enum-type';
import RequestWithCurrentUser from '../../types/express/request-with-current-user'; import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import { GraphQLJSONObject } from 'graphql-type-json'; import { GraphQLJSONObject } from 'graphql-type-json';
import { IJSONObject } from '@automatisch/types';
type Params = { type Params = {
key: string; key: string;
data: object; formattedData: IJSONObject;
}; };
const createConnectionResolver = async ( const createConnectionResolver = async (
params: Params, params: Params,
@@ -17,7 +18,7 @@ const createConnectionResolver = async (
const connection = await req.currentUser.$relatedQuery('connections').insert({ const connection = await req.currentUser.$relatedQuery('connections').insert({
key: params.key, key: params.key,
data: params.data, formattedData: params.formattedData,
}); });
return { return {
@@ -30,7 +31,7 @@ const createConnection = {
type: connectionType, type: connectionType,
args: { args: {
key: { type: GraphQLNonNull(availableAppsEnumType) }, key: { type: GraphQLNonNull(availableAppsEnumType) },
data: { type: GraphQLNonNull(GraphQLJSONObject) }, formattedData: { type: GraphQLNonNull(GraphQLJSONObject) },
}, },
resolve: (_: any, params: Params, req: RequestWithCurrentUser) => resolve: (_: any, params: Params, req: RequestWithCurrentUser) =>
createConnectionResolver(params, req), createConnectionResolver(params, req),

View File

@@ -18,7 +18,7 @@ const resetConnectionResolver = async (
.throwIfNotFound(); .throwIfNotFound();
connection = await connection.$query().patchAndFetch({ connection = await connection.$query().patchAndFetch({
data: { screenName: connection.data.screenName }, formattedData: { screenName: connection.formattedData.screenName },
}); });
return connection; return connection;

View File

@@ -2,10 +2,11 @@ import { GraphQLString, GraphQLNonNull } from 'graphql';
import { GraphQLJSONObject } from 'graphql-type-json'; import { GraphQLJSONObject } from 'graphql-type-json';
import connectionType from '../types/connection'; import connectionType from '../types/connection';
import RequestWithCurrentUser from '../../types/express/request-with-current-user'; import RequestWithCurrentUser from '../../types/express/request-with-current-user';
import { IJSONObject } from '@automatisch/types';
type Params = { type Params = {
id: string; id: string;
data: object; formattedData: IJSONObject;
}; };
const updateConnectionResolver = async ( const updateConnectionResolver = async (
@@ -20,9 +21,9 @@ const updateConnectionResolver = async (
.throwIfNotFound(); .throwIfNotFound();
connection = await connection.$query().patchAndFetch({ connection = await connection.$query().patchAndFetch({
data: { formattedData: {
...connection.data, ...connection.formattedData,
...params.data, ...params.formattedData,
}, },
}); });

View File

@@ -21,13 +21,13 @@ const verifyConnectionResolver = async (
const appClass = (await import(`../../apps/${connection.key}`)).default; const appClass = (await import(`../../apps/${connection.key}`)).default;
const appData = App.findOneByKey(connection.key); const appData = App.findOneByKey(connection.key);
const appInstance = new appClass(appData, connection.data); const appInstance = new appClass(appData, connection.formattedData);
const verifiedCredentials = const verifiedCredentials =
await appInstance.authenticationClient.verifyCredentials(); await appInstance.authenticationClient.verifyCredentials();
connection = await connection.$query().patchAndFetch({ connection = await connection.$query().patchAndFetch({
data: { formattedData: {
...connection.data, ...connection.formattedData,
...verifiedCredentials, ...verifiedCredentials,
}, },
verified: true, verified: true,

View File

@@ -21,12 +21,12 @@ const testConnectionResolver = async (
const appClass = (await import(`../../apps/${connection.key}`)).default; const appClass = (await import(`../../apps/${connection.key}`)).default;
const appData = App.findOneByKey(connection.key); const appData = App.findOneByKey(connection.key);
const appInstance = new appClass(appData, connection.data); const appInstance = new appClass(appData, connection.formattedData);
const isStillVerified = const isStillVerified =
await appInstance.authenticationClient.isStillVerified(); await appInstance.authenticationClient.isStillVerified();
connection = await connection.$query().patchAndFetch({ connection = await connection.$query().patchAndFetch({
data: connection.data, formattedData: connection.formattedData,
verified: isStillVerified, verified: isStillVerified,
}); });

View File

@@ -10,7 +10,7 @@ const connectionType = new GraphQLObjectType({
return { return {
id: { type: GraphQLString }, id: { type: GraphQLString },
key: { type: GraphQLString }, key: { type: GraphQLString },
data: { type: connectionDataType }, formattedData: { type: connectionDataType },
verified: { type: GraphQLBoolean }, verified: { type: GraphQLBoolean },
app: { type: appType }, app: { type: appType },
createdAt: { type: GraphQLString }, createdAt: { type: GraphQLString },

View File

@@ -4,11 +4,13 @@ import { AES, enc } from 'crypto-js';
import Base from './base'; import Base from './base';
import User from './user'; import User from './user';
import appConfig from '../config/app'; import appConfig from '../config/app';
import { IJSONObject } from '@automatisch/types';
class Connection extends Base { class Connection extends Base {
id!: string; id!: string;
key!: string; key!: string;
data!: any; data: string;
formattedData!: IJSONObject;
userId!: string; userId!: string;
verified: boolean; verified: boolean;
count: number; count: number;
@@ -22,7 +24,8 @@ class Connection extends Base {
properties: { properties: {
id: { type: 'string', format: 'uuid' }, id: { type: 'string', format: 'uuid' },
key: { type: 'string', minLength: 1, maxLength: 255 }, key: { type: 'string', minLength: 1, maxLength: 255 },
data: { type: 'object' }, data: { type: 'string' },
formattedData: { type: 'object' },
userId: { type: 'string', format: 'uuid' }, userId: { type: 'string', format: 'uuid' },
verified: { type: 'boolean' }, verified: { type: 'boolean' },
}, },
@@ -42,20 +45,20 @@ class Connection extends Base {
encryptData(): void { encryptData(): void {
if (!this.eligibleForEncryption()) return; if (!this.eligibleForEncryption()) return;
this.data = AES.encrypt( this.data = AES.encrypt(
JSON.stringify(this.data), JSON.stringify(this.formattedData),
appConfig.encryptionKey appConfig.encryptionKey
).toString(); ).toString();
} }
decryptData(): void { decryptData(): void {
if (!this.eligibleForEncryption()) return; if (!this.eligibleForEncryption()) return;
this.data = JSON.parse( this.formattedData = JSON.parse(
AES.decrypt(this.data, appConfig.encryptionKey).toString(enc.Utf8) AES.decrypt(this.data, appConfig.encryptionKey).toString(enc.Utf8)
); );
} }
eligibleForEncryption(): boolean { eligibleForEncryption(): boolean {
return this.data ? true : false; return this.formattedData ? true : false;
} }
// TODO: Make another abstraction like beforeSave instead of using // TODO: Make another abstraction like beforeSave instead of using