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 appInstance = new appClass(appData, {
consumerKey: connection.data.consumerKey,
consumerSecret: connection.data.consumerSecret,
consumerKey: connection.formattedData.consumerKey,
consumerSecret: connection.formattedData.consumerSecret,
});
const authLink = await appInstance.authenticationClient.createAuthData();
await connection.$query().patch({
data: {
...connection.data,
formattedData: {
...connection.formattedData,
...authLink,
},
});

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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