mirror of
https://github.com/frappe/books.git
synced 2025-01-08 01:14:39 +00:00
55 lines
1.4 KiB
TypeScript
55 lines
1.4 KiB
TypeScript
|
import { ModelNameEnum } from 'models/types';
|
||
|
import { DatabaseManager } from '../database/manager';
|
||
|
import { AccountTypeEnum } from 'models/baseModels/Account/types';
|
||
|
import { getDefaultMetaFieldValueMap } from 'backend/helpers';
|
||
|
|
||
|
type AccountTypeMap = Record<AccountTypeEnum, string[] | undefined>;
|
||
|
|
||
|
async function execute(dm: DatabaseManager) {
|
||
|
const accounts = (await dm.db?.getAll(ModelNameEnum.Account, {
|
||
|
fields: ['name', 'accountType'],
|
||
|
filters: {
|
||
|
accountType: [
|
||
|
'in',
|
||
|
[
|
||
|
AccountTypeEnum.Bank,
|
||
|
AccountTypeEnum.Cash,
|
||
|
AccountTypeEnum.Payable,
|
||
|
AccountTypeEnum.Receivable,
|
||
|
],
|
||
|
],
|
||
|
},
|
||
|
})) as { name: string; accountType: AccountTypeEnum }[];
|
||
|
|
||
|
const accountsMap = accounts.reduce((acc, ac) => {
|
||
|
acc[ac.accountType] ??= [];
|
||
|
acc[ac.accountType]!.push(ac.name);
|
||
|
return acc;
|
||
|
}, {} as AccountTypeMap);
|
||
|
|
||
|
const defaults = getDefaultMetaFieldValueMap();
|
||
|
|
||
|
const paymentMethods = [
|
||
|
{
|
||
|
name: 'Cash',
|
||
|
account: accountsMap[AccountTypeEnum.Cash]?.[0],
|
||
|
...defaults,
|
||
|
},
|
||
|
{
|
||
|
name: 'Bank',
|
||
|
account: accountsMap[AccountTypeEnum.Bank]?.[0],
|
||
|
...defaults,
|
||
|
},
|
||
|
{
|
||
|
name: 'Transfer',
|
||
|
account: accountsMap[AccountTypeEnum.Bank]?.[0],
|
||
|
...defaults,
|
||
|
},
|
||
|
];
|
||
|
|
||
|
for (const paymentMethod of paymentMethods) {
|
||
|
await dm.db?.insert(ModelNameEnum.PaymentMethod, paymentMethod);
|
||
|
}
|
||
|
}
|
||
|
export default { execute };
|