2023-02-13 17:30:01 +00:00
|
|
|
import { get, getFamily, put } from './lib';
|
2022-12-25 15:30:47 +00:00
|
|
|
import getUri from '../lib/ids';
|
2023-02-13 17:30:01 +00:00
|
|
|
import { getSettings, putSettings } from './settings';
|
2022-12-25 15:30:47 +00:00
|
|
|
|
2022-12-25 20:37:59 +00:00
|
|
|
export const initialAccount = {
|
|
|
|
id: 'initial',
|
|
|
|
name: '???',
|
2023-02-11 10:31:32 +00:00
|
|
|
localDb: '_dyomedea_',
|
2022-12-25 20:37:59 +00:00
|
|
|
};
|
|
|
|
|
2022-12-25 15:30:47 +00:00
|
|
|
export const getAccounts = async () => {
|
2022-12-25 21:09:52 +00:00
|
|
|
const accountDocs = await getFamily('account', { include_docs: true }, true);
|
|
|
|
return accountDocs.rows.map((row: any) => row.doc.doc);
|
2022-12-25 15:30:47 +00:00
|
|
|
};
|
|
|
|
|
2023-02-13 17:30:01 +00:00
|
|
|
export const getAccount = async (params: any) => {
|
|
|
|
const { id } = params;
|
2023-03-04 10:59:34 +00:00
|
|
|
console.log({ caller: 'getAccount', params });
|
2023-02-13 17:30:01 +00:00
|
|
|
const uri = getUri('account', { account: id });
|
|
|
|
return (await get(uri, true)).doc;
|
|
|
|
};
|
|
|
|
|
2022-12-26 16:31:05 +00:00
|
|
|
export const putAccount = async (params: any) => {
|
|
|
|
const { id, account } = params;
|
2022-12-25 20:37:59 +00:00
|
|
|
const uri = getUri('account', { account: id });
|
2022-12-25 15:30:47 +00:00
|
|
|
return await put(uri, 'account', (_: any) => account, {}, true);
|
|
|
|
};
|
2022-12-25 21:09:52 +00:00
|
|
|
|
|
|
|
export const getAccountById = (accounts: any, id: string) => {
|
|
|
|
const targetAccounts: any[] = accounts.filter((acc: any) => acc.id === id);
|
|
|
|
return targetAccounts[0];
|
|
|
|
};
|
2023-02-13 17:30:01 +00:00
|
|
|
|
|
|
|
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 });
|
|
|
|
};
|