diff --git a/src/components/ContentComponents/Konnector/KonnectorLoginForm.tsx b/src/components/ContentComponents/Konnector/KonnectorLoginForm.tsx index bf40b1667c42556fefe1fc965bb1396be60a548c..2731c89ebac345b696fcf9ac04eee3eccd8ce215 100644 --- a/src/components/ContentComponents/Konnector/KonnectorLoginForm.tsx +++ b/src/components/ContentComponents/Konnector/KonnectorLoginForm.tsx @@ -8,8 +8,9 @@ import StyledIconButton from 'components/CommonKit/IconButton/StyledIconButton' import StyledButton from 'components/CommonKit/Button/StyledButton' import TrailingIcon from 'assets/icons/ico/trailing-icon.svg' import { ConnectionService } from 'services/connectionService' -import { AccountService } from 'services/accountService' -import { Account, AuthLoginData, Trigger } from 'doctypes' +import { Account, AccountAuthData } from 'models/account.model' +import { AccountService } from 'services/account.service' +import { Trigger } from 'doctypes' import iconGRDFLogo from 'assets/icons/visu/grdf-logo.svg' import iconEGLLogo from 'assets/icons/visu/egl-logo.svg' @@ -98,7 +99,8 @@ const KonnectorLoginForm: React.FC<KonnectorLoginFormProps> = ({ password: password, } account.auth = auth - const updatedAccount = await AccountService.updateAccount(client, account) + const accountService = new AccountService(client) + const updatedAccount = await accountService.updateAccount(account) onSuccess(updatedAccount, trigger) } @@ -124,7 +126,7 @@ const KonnectorLoginForm: React.FC<KonnectorLoginFormProps> = ({ useEffect(() => { if (account) { - const auth: AuthLoginData = account.auth + const auth: AccountAuthData = account.auth if (auth.login) { setLogin(auth.login) } diff --git a/src/components/ContentComponents/Konnector/KonnectorOAuthForm.tsx b/src/components/ContentComponents/Konnector/KonnectorOAuthForm.tsx index 2716f218e4dfb0540e45f8f2ac99ce310c7699ce..1955d01a4b73e12b4c9cd0eef2d53752cec3c5eb 100644 --- a/src/components/ContentComponents/Konnector/KonnectorOAuthForm.tsx +++ b/src/components/ContentComponents/Konnector/KonnectorOAuthForm.tsx @@ -5,7 +5,7 @@ import { translate } from 'cozy-ui/react/I18n' import { Konnector, Trigger } from 'doctypes' import OAuthForm from 'components/ContentComponents/OAuth/OAuthForm' import StyledButton from 'components/CommonKit/Button/StyledButton' -import { AccountService } from 'services/accountService' +import { AccountService } from 'services/account.service' import { TriggerService } from 'services/triggersService' interface KonnectorOAuthFormProps { @@ -26,7 +26,8 @@ const KonnectorOAuthForm: React.FC<KonnectorOAuthFormProps> = ({ t, }: KonnectorOAuthFormProps) => { const handleSuccess = async (accountId: string) => { - const account = await AccountService.getAccount(client, accountId) + const accountService = new AccountService(client) + const account = await accountService.getAccount(accountId) if (!account) { onSuccess(null, null) } diff --git a/src/components/ContentComponents/Konnector/KonnectorResult.tsx b/src/components/ContentComponents/Konnector/KonnectorResult.tsx index c80e0ce336d405a1aeaef8fd53b5f29391a62a5e..fa257b02ef8597cee61d6f1d320d0b9f9bdb7ad2 100644 --- a/src/components/ContentComponents/Konnector/KonnectorResult.tsx +++ b/src/components/ContentComponents/Konnector/KonnectorResult.tsx @@ -9,7 +9,7 @@ import StyledBlackSpinner from 'components/CommonKit/Spinner/StyledBlackSpinner' import { Account, Trigger, Konnector } from 'doctypes' import { TriggerService } from 'services/triggersService' -import { AccountService } from 'services/accountService' +import { AccountService } from 'services/account.service' import warningWhite from 'assets/icons/ico/warning-white.svg' import { isKonnectorRunning } from 'cozy-harvest-lib/dist/helpers/triggers' @@ -94,7 +94,8 @@ const KonnectorResult: React.FC<KonnectorResultProps> = ({ setUpdating(true) try { if (account) { - await AccountService.deleteAccount(client, account) + const accountService = new AccountService(client) + await accountService.deleteAccount(account) await context.refreshFluidTypes() } } catch (error) { diff --git a/src/components/ContentComponents/KonnectorViewer/KonnectorViewerCard.tsx b/src/components/ContentComponents/KonnectorViewer/KonnectorViewerCard.tsx index 64697319a0226a1fea27cede5d75034273e9213a..cb32c937f5ae087d60d5dc2cb1eaf1914e03121b 100644 --- a/src/components/ContentComponents/KonnectorViewer/KonnectorViewerCard.tsx +++ b/src/components/ContentComponents/KonnectorViewer/KonnectorViewerCard.tsx @@ -1,5 +1,5 @@ import React, { useState, useEffect, useRef } from 'react' -import { AccountService } from 'services/accountService' +import { AccountService } from 'services/account.service' import { FluidType } from 'enum/fluid.enum' @@ -115,10 +115,8 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({ async function getData() { setLoading(true) if (konnector) { - const _account = await AccountService.getAccountByType( - client, - konnector.slug - ) + const accountService = new AccountService(client) + const _account = await accountService.getAccountByType(konnector.slug) if (subscribed && _account) { setAccount(_account) const _trigger = await TriggerService.fetchTriggerFromAccount( diff --git a/src/doctypes/io-cozy-accounts.ts b/src/doctypes/io-cozy-accounts.ts index 5969812143d8b01562adda58a08295cf2ee46d22..c5df0948773f317671043491481993966516aad5 100644 --- a/src/doctypes/io-cozy-accounts.ts +++ b/src/doctypes/io-cozy-accounts.ts @@ -1,32 +1 @@ export const ACCOUNTS_DOCTYPE = 'io.cozy.accounts' - -export type AuthLoginData = { - login: string - credentials_encrypted?: string - password?: string -} - -export type OAuthData = { - access_token: string - refresh_token: string - scope: string | null -} - -export type Account = { - _id: string - account_type: string - auth: AuthLoginData | OAuthData - identifier: string - state?: string | null -} - -export function isAccount(account: any): account is Account { - return ( - account && - '_id' in account && - 'account_type' in account && - 'auth' in account && - 'identifier' in account && - 'state' in account - ) -} diff --git a/src/models/account.model.ts b/src/models/account.model.ts new file mode 100644 index 0000000000000000000000000000000000000000..7f9ad023f1d7d92fe1145e146dcde078cd00253e --- /dev/null +++ b/src/models/account.model.ts @@ -0,0 +1,19 @@ +export interface Account { + _id: string + account_type: string + auth: AccountAuthData | AccountOAuthData + identifier: string + state?: string | null +} + +export interface AccountAuthData { + login: string + credentials_encrypted?: string + password?: string +} + +export interface AccountOAuthData { + access_token: string + refresh_token: string + scope: string | null +} diff --git a/src/services/account.service.ts b/src/services/account.service.ts new file mode 100644 index 0000000000000000000000000000000000000000..6b50d0e69133d9f0fba29d6f758daaf98ef90efa --- /dev/null +++ b/src/services/account.service.ts @@ -0,0 +1,75 @@ +import { Client } from 'cozy-client' +import { Account, AccountAuthData } from 'models/account.model' +import { Konnector } from 'doctypes' +import { build } from 'cozy-harvest-lib/dist/helpers/accounts' +import { + createAccount, + findAccount, + deleteAccount, + updateAccount, +} from 'cozy-harvest-lib/dist/connections/accounts' + +export class AccountService { + private _client: Client + + constructor(_client: Client) { + this._client = _client + } + + private buildAccountAttributes = ( + konnector: Konnector, + authData: AccountAuthData + ) => { + return build(konnector, authData) + } + + public createAccount = async ( + konnector: Konnector, + accountAuthData: AccountAuthData + ) => { + const accountAttributes = this.buildAccountAttributes( + konnector, + accountAuthData + ) + const account = await createAccount( + this._client, + konnector, + accountAttributes + ) + return account + } + + public getAccount = async (id: string) => { + const account: Account = await findAccount(this._client, id) + return account + } + + public getAccountByType = async (type: string) => { + const query = this._client + .find('io.cozy.accounts') + // eslint-disable-next-line @typescript-eslint/camelcase + .where({ account_type: type }) + .limitBy(1) + const result = await this._client.query(query) + return result.data[0] ? result.data[0] : null + } + + public updateAccount = async (account: Account) => { + const updatedAccount: Account = await updateAccount(this._client, account) + return updatedAccount + } + + public deleteAccount = async (account: Account) => { + const del = await deleteAccount(this._client, account) + return del + } + + public createIndexAccount = async () => { + const query = this._client + .find('io.cozy.accounts') + // eslint-disable-next-line @typescript-eslint/camelcase + .where({ account_type: 'index' }) + .limitBy(1) + return await this._client.query(query) + } +} diff --git a/src/services/accountService.ts b/src/services/accountService.ts deleted file mode 100644 index 03fad17337149e9653a91a5af7e7b496a73131aa..0000000000000000000000000000000000000000 --- a/src/services/accountService.ts +++ /dev/null @@ -1,78 +0,0 @@ -import { Client } from 'cozy-client' -import { Account, Konnector } from 'doctypes' -import { build } from 'cozy-harvest-lib/dist/helpers/accounts' -import { - createAccount, - findAccount, - deleteAccount, - updateAccount, -} from 'cozy-harvest-lib/dist/connections/accounts' - -export interface AuthData { - login: string - password: string -} - -export interface AccountAttributes { - account_type: string - auth: AuthData - identifier: string - state: string | null -} - -export class AccountService { - static buildAccountAttributes = ( - konnector: Konnector, - authData: AuthData - ) => { - return build(konnector, authData) - } - - static createAccount = async ( - client: Client, - konnector: Konnector, - accountAttributes: AccountAttributes - ) => { - const account = await createAccount(client, konnector, accountAttributes) - return account - } - - static getAccount = async (client: Client, id: string) => { - const account: Account = await findAccount(client, id) - return account - } - - static deleteAccount = async (client: Client, account: Account) => { - const del = await deleteAccount(client, account) - return del - } - - static updateAccount = async (client: Client, account: Account) => { - const updatedAccount: Account = await updateAccount(client, account) - return updatedAccount - } - - static getAccountByType = async (client: Client, type: string) => { - const query = client - .find('io.cozy.accounts') - // eslint-disable-next-line @typescript-eslint/camelcase - .where({ account_type: type }) - .limitBy(1) - const result = await client.query(query) - return result.data[0] ? result.data[0] : null - } - - static createIndexAccount = async (client: Client) => { - try { - const query = client - .find('io.cozy.accounts') - // eslint-disable-next-line @typescript-eslint/camelcase - .where({ account_type: 'index' }) - // .sortBy([{ 'cozyMetadata.updatedAt': 'desc' }]) - .limitBy(1) - return await client.query(query) - } catch (error) { - throw error - } - } -} diff --git a/src/services/connectionService.ts b/src/services/connectionService.ts index a4b6d13f82d9f292d34688ff5a358d24c7ca6800..a23c4a4d0a7d0d4703636e87814592b77714ae77 100644 --- a/src/services/connectionService.ts +++ b/src/services/connectionService.ts @@ -1,6 +1,7 @@ import { Client } from 'cozy-client' -import { Account, Konnector, Trigger } from 'doctypes' -import { AccountService, AuthData } from './accountService' +import { Account, AccountAuthData } from 'models/account.model' +import { Konnector, Trigger } from 'doctypes' +import { AccountService } from './account.service' import { TriggerService } from './triggersService' import KonnectorService from './konnectorService' @@ -28,15 +29,14 @@ export class ConnectionService { throw new Error(`Could not find konnector for ${this._konnectorId}`) } // Creation of the account linked to the konnector retrieved - const authData: AuthData = { login: this._login, password: this._password } - const accountAttributes = AccountService.buildAccountAttributes( - konnector, - authData - ) - const account: Account = await AccountService.createAccount( - this._client, + const accountAuthData: AccountAuthData = { + login: this._login, + password: this._password, + } + const accountService = new AccountService(this._client) + const account: Account = await accountService.createAccount( konnector, - accountAttributes + accountAuthData ) if (!account || !account._id) { throw new Error(`Error during account creation`) @@ -55,11 +55,5 @@ export class ConnectionService { account: account, trigger: trigger, } - //Launch the creation trigger - // const job = await triggersServices.launchTrigger() - // if (!job) { - // throw new Error(`Error during trigger launching`) - // } - // return job } } diff --git a/src/services/fluidService.ts b/src/services/fluidService.ts index 88fbb17b18b97e783628957cb68cd967cce85067..fda691c98d676d0bb5df11b8d2e902ed54f81763 100644 --- a/src/services/fluidService.ts +++ b/src/services/fluidService.ts @@ -4,7 +4,7 @@ import { DateTime } from 'luxon' import FluidConfigService from 'services/fluidConfigService' import KonnectorService from 'services/konnectorService' import ConsumptionDataManager from 'services/consumptionDataManagerService' -import { AccountService } from 'services/accountService' +import { AccountService } from 'services/account.service' // eslint-disable-next-line @typescript-eslint/interface-name-prefix export interface IFluidStatus { @@ -22,17 +22,15 @@ export class FluidService { public getFluidStatus = async () => { const fluidConfig = new FluidConfigService().getFluidConfig() + const accountService = new AccountService(this._client) const [elecAccount, gasAccount, waterAccount] = await Promise.all([ - AccountService.getAccountByType( - this._client, + accountService.getAccountByType( fluidConfig[FluidType.ELECTRICITY].konnectorConfig.slug ), - AccountService.getAccountByType( - this._client, + accountService.getAccountByType( fluidConfig[FluidType.GAS].konnectorConfig.slug ), - AccountService.getAccountByType( - this._client, + accountService.getAccountByType( fluidConfig[FluidType.WATER].konnectorConfig.slug ), ]) diff --git a/src/services/initDataManagerService.ts b/src/services/initDataManagerService.ts index a625af150b2a4e6a0a4fde43486df19ae1d4d0fe..1355a231e813ce07c4bc6d66eff6893ea85c70b0 100644 --- a/src/services/initDataManagerService.ts +++ b/src/services/initDataManagerService.ts @@ -26,7 +26,7 @@ import UserProfileDataManager from 'services/userProfileDataManagerService' import userProfileData from 'db/userProfileData.json' import KonnectorStatusService from 'services/konnectorStatusService' import KonnectorService from 'services/konnectorService' -import { AccountService } from 'services/accountService' +import { AccountService } from 'services/account.service' import { hashFile } from 'utils/hash' import { @@ -399,6 +399,7 @@ export default class InitDataManager { */ public async initIndex(): Promise<boolean | null> { try { + const accountService = new AccountService(this._client) await Promise.all([ this.createIndex(EGL_DAY_DOCTYPE), this.createIndex(EGL_MONTH_DOCTYPE), @@ -412,7 +413,7 @@ export default class InitDataManager { this.createIndex(GRDF_MONTH_DOCTYPE), this.createIndex(GRDF_YEAR_DOCTYPE), KonnectorService.createIndexKonnector(this._client), - AccountService.createIndexAccount(this._client), + accountService.createIndexAccount(), ]) return true } catch (error) { diff --git a/src/services/konnectorStatusService.ts b/src/services/konnectorStatusService.ts index 87645a5e0ae1d229dbb9cdd508248f667634a795..a796ac6254ada31c4569c6ba3eb77ba2720a994d 100644 --- a/src/services/konnectorStatusService.ts +++ b/src/services/konnectorStatusService.ts @@ -2,7 +2,7 @@ import { Client } from 'cozy-client' import { Konnector } from 'doctypes' import triggersMutations from 'cozy-harvest-lib/dist/connections/triggers' import FluidConfigService from 'services/fluidConfigService' -import { AccountService } from 'services/accountService' +import { AccountService } from 'services/account.service' import { FluidType } from 'enum/fluid.enum' export default class KonnectorStatusService { @@ -106,17 +106,15 @@ export default class KonnectorStatusService { async getKonnectorAccountStatus(): Promise<FluidType[]> { try { const fluidConfig = new FluidConfigService().getFluidConfig() + const accountService = new AccountService(this._client) const [elecData, gasData, waterData] = await Promise.all([ - AccountService.getAccountByType( - this._client, + accountService.getAccountByType( fluidConfig[FluidType.ELECTRICITY].konnectorConfig.slug ), - AccountService.getAccountByType( - this._client, + accountService.getAccountByType( fluidConfig[FluidType.GAS].konnectorConfig.slug ), - AccountService.getAccountByType( - this._client, + accountService.getAccountByType( fluidConfig[FluidType.WATER].konnectorConfig.slug ), ])