patch: create default payment methods

This commit is contained in:
akshayitzme
2024-12-26 15:20:32 +05:30
parent 9911d42da2
commit d37287dd3b
2 changed files with 60 additions and 0 deletions
+54
View File
@@ -0,0 +1,54 @@
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 };
+6
View File
@@ -7,6 +7,7 @@ import updateSchemas from './updateSchemas';
import setPaymentReferenceType from './setPaymentReferenceType';
import fixLedgerDateTime from './v0_21_0/fixLedgerDateTime';
import fixItemHSNField from './fixItemHSNField';
import createPaymentMethods from './createPaymentMethods';
export default [
{ name: 'testPatch', version: '0.5.0-beta.0', patch: testPatch },
@@ -42,4 +43,9 @@ export default [
patch: fixLedgerDateTime,
},
{ name: 'fixItemHSNField', version: '0.24.0', patch: fixItemHSNField },
{
name: 'createPaymentMethods',
version: '0.25.1',
patch: createPaymentMethods,
},
] as Patch[];