Skip to content
Snippets Groups Projects
accountService.ts 3.25 KiB
Newer Older
Hugo NOUTS's avatar
Hugo NOUTS committed
import { Client } from 'cozy-client'
import { Account, Konnector } from 'doctypes'
import accountsMutations from 'cozy-harvest-lib/dist/connections/accounts'

export interface AccountAttributes {
  account_type: string
  auth: {
    login: string
    password: string
  }
  identifier: string
  state: string | null
}

export class AccountService {
  private _account: Account
  private _accountAttributes?: AccountAttributes

  constructor(
    private _client: Client,
    private _login: string,
    private _password: string,
    private _konnector: Konnector
  ) {
    this._account = {
      _id: '',
      // eslint-disable-next-line @typescript-eslint/camelcase
      account_type: '',
      auth: {
        // eslint-disable-next-line @typescript-eslint/camelcase
        credentials_encrypted: '',
        login: '',
      },
      identifier: '',
    }
    this._client = _client
    this._login = _login
    this._password = _password
    this._konnector = _konnector
  }

  get account(): Account {
    return this._account
  }

  createAccountAttributes = () => {
    if (!(this._login && this._password)) {
      throw new Error(
        'AccountService : createAccountAttributes - login or password not found'
      )
    }
    const accountAttributes: AccountAttributes = {
      // eslint-disable-next-line @typescript-eslint/camelcase
      account_type: this._konnector.slug,
      auth: {
        login: this._login,
        password: this._password,
      },
      identifier: 'login',
      state: null,
    }
    return accountAttributes
  }

  createAccount = async () => {
    this._accountAttributes = this.createAccountAttributes()
    if (!(this._accountAttributes && this._konnector)) {
      throw new Error(
        'AccountService : createAccount - accountAttributes or konnector not found'
      )
    }
    try {
      this._account = await accountsMutations(this._client).createAccount(
        this._konnector,
        this._accountAttributes
      )
      return this._account
    } catch (error) {
      throw error
    }
  }

  static getAccount = async (client: Client, id: string) => {
    try {
      const account: Account = await accountsMutations(client).findAccount(id)
      return account
    } catch (error) {
      throw error
    }
  }

  static deleteAccount = async (client: Client, account: Account) => {
    try {
      const del = await accountsMutations(client).deleteAccount(account)
      return del
    } catch (error) {
      throw error
    }
  }

  static getAccountByType = async (client: Client, type: string) => {
    try {
      const query = client
        .find('io.cozy.accounts')
        // eslint-disable-next-line @typescript-eslint/camelcase
        .where({ account_type: type })
        // .sortBy([{ 'cozyMetadata.updatedAt': 'desc' }])
Hugo NOUTS's avatar
Hugo NOUTS committed
        .limitBy(1)
      return await client.query(query)
    } catch (error) {
      throw error
    }
  }

  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' }])
Hugo NOUTS's avatar
Hugo NOUTS committed
        .limitBy(1)
      return await client.query(query)
    } catch (error) {
      throw error
    }
  }
}