dyomedea/src/db/account.ts

44 lines
1.3 KiB
TypeScript

import { get, getFamily, put } from './lib';
import getUri from '../lib/ids';
import { getSettings, putSettings } from './settings';
export const initialAccount = {
id: 'initial',
name: '???',
localDb: '_dyomedea_',
};
export const getAccounts = async () => {
const accountDocs = await getFamily('account', { include_docs: true }, true);
return accountDocs.rows.map((row: any) => row.doc.doc);
};
export const getAccount = async (params: any) => {
const { id } = params;
console.log({ caller: 'getAccount', params });
const uri = getUri('account', { account: id });
return (await get(uri, true)).doc;
};
export const putAccount = async (params: any) => {
const { id, account } = params;
const uri = getUri('account', { account: id });
return await put(uri, 'account', (_: any) => account, {}, true);
};
export const getAccountById = (accounts: any, id: string) => {
const targetAccounts: any[] = accounts.filter((acc: any) => acc.id === id);
return targetAccounts[0];
};
export const getCurrentAccount = async (params: any) => {
const settings = await getSettings();
return await getAccount({ id: settings.currentAccountId });
};
export const putCurrentAccount = async (params: any) => {
const { account } = params;
const settings = await getSettings();
await putAccount({ id: settings.currentAccountId, account });
};