From eb61d1abbf31e1d4f84cf2ca20dec822be7ce7ed Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Fri, 18 Feb 2022 12:55:56 +0100 Subject: [PATCH] fix(core): update --- test/test.nonci.ts | 12 +++++++- ts/index.ts | 1 + ts/tink.classes.tinkaccount.ts | 37 ++++++++++++++++++----- ts/tink.classes.tinkuser.ts | 54 +++++++++++++++++++++++++++++++++- 4 files changed, 95 insertions(+), 9 deletions(-) diff --git a/test/test.nonci.ts b/test/test.nonci.ts index 88be4af..fd006e2 100644 --- a/test/test.nonci.ts +++ b/test/test.nonci.ts @@ -7,6 +7,16 @@ import * as tink from '../ts/index'; let tinkTestAccount: tink.TinkAccount; +tap.preTask('should delete existing users', async () => { + const preTinkAccount = tinkTestAccount = new tink.TinkAccount( + testQenv.getEnvVarOnDemand('TINK_CLIENT_ID'), + testQenv.getEnvVarOnDemand('TINK_CLIENT_SECRET') + ); + expect(tinkTestAccount).toBeInstanceOf(tink.TinkAccount); + const tinkUser = new tink.TinkUser(preTinkAccount, null, 'user_1234_abc'); + await tinkUser.delete(); +}) + tap.test('should create a valid tink account', async () => { tinkTestAccount = new tink.TinkAccount( testQenv.getEnvVarOnDemand('TINK_CLIENT_ID'), @@ -20,7 +30,7 @@ tap.test('should report tink as healthy', async () => { }); tap.test('should create a valid request', async () => { - await tinkTestAccount.request('', 'POST', '', {}); + await tinkTestAccount.createTinkUser("user_1234_abc"); }) tap.start(); diff --git a/ts/index.ts b/ts/index.ts index 16cda12..f303c4f 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -1 +1,2 @@ export * from './tink.classes.tinkaccount'; +export * from './tink.classes.tinkuser'; \ No newline at end of file diff --git a/ts/tink.classes.tinkaccount.ts b/ts/tink.classes.tinkaccount.ts index 97f0e44..af5e1a6 100644 --- a/ts/tink.classes.tinkaccount.ts +++ b/ts/tink.classes.tinkaccount.ts @@ -1,8 +1,13 @@ import * as plugins from './tink.plugins'; + +import { TinkUser } from './tink.classes.tinkuser' + export class TinkAccount { private clientId: string; private clientSecret: string; + private apiBaseUrl: string = 'https://api.tink.com'; + constructor(clientIdArg: string, clientSecretArg: string) { this.clientId = clientIdArg; this.clientSecret = clientSecretArg; @@ -18,12 +23,13 @@ export class TinkAccount { // the request method for tink respecting platform specific stuff // e.g. certain headers if needed - public async request( + public async request(optionsArg: { urlArg: string, methodArg: 'POST' | 'GET', scopeArg: string, - payloadArg: any - ) { + payloadArg: any, + externalUserId: string + }) { // check health if (!(await this.getTinkHealthyBoolean())) { throw new Error('TINK is not healthy right now. Please try again later.'); @@ -32,7 +38,7 @@ export class TinkAccount { } // lets get an accessToken for the request const response = await plugins.smartrequest.postFormDataUrlEncoded( - 'https://api.tink.com/api/v1/oauth/token', + `${this.apiBaseUrl}/api/v1/oauth/token`, {}, [ { @@ -49,11 +55,28 @@ export class TinkAccount { }, { key: 'scope', - content: 'user:read', + content: optionsArg.scopeArg, }, ] ); - console.log(response.statusCode); - console.log(response.body); + if (response.statusCode !== 200) { + throw new Error('there was an error aquiring an access token.'); + } + const accessToken = response.body.access_token; + + const response2 = await plugins.smartrequest.request(`${this.apiBaseUrl}${optionsArg.urlArg}`, { + headers: { + 'Authorization': `Bearer ${accessToken}`, + 'Content-Type': 'application/json' + }, + method: optionsArg.methodArg, + requestBody: JSON.stringify(optionsArg.payloadArg) + }) + console.log(response2.statusCode); + return response2.body; + } + + public async createTinkUser(externalUserIdArg: string) { + const tinkuser = await TinkUser.createNewTinkUser(this, externalUserIdArg); } } diff --git a/ts/tink.classes.tinkuser.ts b/ts/tink.classes.tinkuser.ts index 0ba5365..1226c22 100644 --- a/ts/tink.classes.tinkuser.ts +++ b/ts/tink.classes.tinkuser.ts @@ -1 +1,53 @@ -import * as plugins from './tink.plugins'; \ No newline at end of file +import * as plugins from './tink.plugins'; + +import { TinkAccount } from './tink.classes.tinkaccount'; + +export class TinkUser { + // STATIC + public static async createNewTinkUser (tinkaccountArg: TinkAccount, externalUserIdArg: string) { + const response = await tinkaccountArg.request({ + externalUserId: null, + urlArg: '/api/v1/user/create', + methodArg: 'POST', + scopeArg: 'user:create', + payloadArg: { + "external_user_id": externalUserIdArg, + "market": "GB", + "locale": "en_US" + } + }); + + console.log(response); + } + + // INSTANCE + public tinkAccountRef: TinkAccount; + public tinkUserId: string; + public externalUserIdArg: string; + + public authorizationToken: string + + constructor(tinkAccountrefArg: TinkAccount, tinkUserIdArg: string, externalUserIdArg: string) { + this.tinkAccountRef = tinkAccountrefArg; + this.tinkUserId = tinkUserIdArg; + this.externalUserIdArg = externalUserIdArg; + } + + public async authorize() { + + } + + /** + * deletes the user + */ + public async delete() { + const response = await this.tinkAccountRef.request({ + externalUserId: this.externalUserIdArg, + methodArg: 'POST', + payloadArg: {}, + scopeArg: 'user:delete', + urlArg: '/api/v1/user/delete' + }); + console.log(response); + } +} \ No newline at end of file