Merge pull request #874 from bigcapitalhq/feature/20251218134811

fix: import module bugs
This commit is contained in:
Ahmed Bouhuolia
2025-12-18 21:25:34 +02:00
committed by GitHub
37 changed files with 690 additions and 185 deletions

View File

@@ -167,6 +167,9 @@
"**/*.(t|j)s" "**/*.(t|j)s"
], ],
"coverageDirectory": "../coverage", "coverageDirectory": "../coverage",
"testEnvironment": "node" "testEnvironment": "node",
"moduleNameMapper": {
"^@/(.*)$": "<rootDir>/$1"
}
} }
} }

View File

@@ -3,6 +3,7 @@
"field.description": "Description", "field.description": "Description",
"field.slug": "Account slug", "field.slug": "Account slug",
"field.code": "Account code", "field.code": "Account code",
"field.code_hint": "Unique number to identify the account.",
"field.root_type": "Root type", "field.root_type": "Root type",
"field.normal": "Account normal", "field.normal": "Account normal",
"field.normal.credit": "Credit", "field.normal.credit": "Credit",
@@ -13,5 +14,6 @@
"field.balance": "Balance", "field.balance": "Balance",
"field.bank_balance": "Bank Balance", "field.bank_balance": "Bank Balance",
"field.parent_account": "Parent Account", "field.parent_account": "Parent Account",
"field.created_at": "Created at" "field.created_at": "Created at",
"field.account_hint": "Matches the account name or code."
} }

View File

@@ -0,0 +1,27 @@
{
"field.vendor": "Vendor",
"field.bill_number": "Bill No.",
"field.bill_date": "Date",
"field.due_date": "Due Date",
"field.reference_no": "Reference No.",
"field.exchange_rate": "Exchange Rate",
"field.note": "Note",
"field.open": "Open",
"field.entries": "Entries",
"field.item": "Item",
"field.item_hint": "Matches the item name or code.",
"field.rate": "Rate",
"field.quantity": "Quantity",
"field.description": "Line Description",
"field.amount": "Amount",
"field.payment_amount": "Payment Amount",
"field.status": "Status",
"field.status.paid": "Paid",
"field.status.partially-paid": "Partially Paid",
"field.status.overdue": "Overdue",
"field.status.unpaid": "Unpaid",
"field.status.opened": "Opened",
"field.status.draft": "Draft",
"field.created_at": "Created At"
}

View File

@@ -0,0 +1,15 @@
{
"field.vendor": "Vendor",
"field.payment_date": "Payment Date",
"field.payment_number": "Payment No.",
"field.payment_account": "Payment Account",
"field.exchange_rate": "Exchange Rate",
"field.note": "Note",
"field.reference": "Reference",
"field.entries": "Entries",
"field.entries.bill": "Bill",
"field.entries.payment_amount": "Payment Amount",
"field.payment_number_hint": "The payment number should be unique.",
"field.bill_hint": "Matches the bill number."
}

View File

@@ -0,0 +1,16 @@
{
"field.customer": "Customer",
"field.exchange_rate": "Exchange Rate",
"field.credit_note_date": "Credit Note Date",
"field.reference_no": "Reference No.",
"field.note": "Note",
"field.terms_conditions": "Terms & Conditions",
"field.credit_note_number": "Credit Note Number",
"field.open": "Open",
"field.entries": "Entries",
"field.item": "Item",
"field.rate": "Rate",
"field.quantity": "Quantity",
"field.description": "Description"
}

View File

@@ -0,0 +1,21 @@
{
"field.customer": "Customer",
"field.estimate_date": "Estimate Date",
"field.expiration_date": "Expiration Date",
"field.estimate_number": "Estimate No.",
"field.reference_no": "Reference No.",
"field.exchange_rate": "Exchange Rate",
"field.currency": "Currency",
"field.note": "Note",
"field.terms_conditions": "Terms & Conditions",
"field.delivered": "Delivered",
"field.entries": "Entries",
"field.amount": "Amount",
"field.status": "Status",
"field.status.draft": "Draft",
"field.status.delivered": "Delivered",
"field.status.rejected": "Rejected",
"field.status.approved": "Approved",
"field.created_at": "Created At"
}

View File

@@ -26,6 +26,7 @@
"field.due_amount": "Due amount", "field.due_amount": "Due amount",
"field.delivered": "Delivered", "field.delivered": "Delivered",
"field.item_name": "Item Name", "field.item_name": "Item Name",
"field.item_hint": "Matches the item name or code.",
"field.rate": "Rate", "field.rate": "Rate",
"field.quantity": "Quantity", "field.quantity": "Quantity",
"field.description": "Description", "field.description": "Description",
@@ -38,5 +39,7 @@
"field.status.draft": "Draft", "field.status.draft": "Draft",
"field.created_at": "Created at", "field.created_at": "Created at",
"field.currency": "Currency", "field.currency": "Currency",
"field.entries": "Entries" "field.entries": "Entries",
"field.branch": "Branch",
"field.warehouse": "Warehouse"
} }

View File

@@ -17,6 +17,7 @@
"field.quantity_on_hand": "Quantity on Hand", "field.quantity_on_hand": "Quantity on Hand",
"field.note": "Note", "field.note": "Note",
"field.category": "Category", "field.category": "Category",
"field.category_hint": "Matches the category name.",
"field.active": "Active", "field.active": "Active",
"field.created_at": "Created At" "field.created_at": "Created At"
} }

View File

@@ -0,0 +1,17 @@
{
"field.customer": "Customer",
"field.payment_date": "Payment Date",
"field.amount": "Amount",
"field.reference_no": "Reference No.",
"field.deposit_account": "Deposit Account",
"field.payment_receive_no": "Payment No.",
"field.statement": "Statement",
"field.entries": "Entries",
"field.exchange_rate": "Exchange Rate",
"field.invoice": "Invoice",
"field.entries.payment_amount": "Payment Amount",
"field.created_at": "Created At",
"field.payment_no_hint": "The payment number should be unique.",
"field.invoice_hint": "Matches the invoice number."
}

View File

@@ -10,5 +10,21 @@
"paper.receipt_amount": "Receipt amount", "paper.receipt_amount": "Receipt amount",
"paper.total": "Total", "paper.total": "Total",
"paper.balance_due": "Balance Due", "paper.balance_due": "Balance Due",
"paper.payment_amount": "Payment Amount" "paper.payment_amount": "Payment Amount",
"field.receipt_date": "Receipt Date",
"field.customer": "Customer",
"field.deposit_account": "Deposit Account",
"field.exchange_rate": "Exchange Rate",
"field.receipt_number": "Receipt Number",
"field.reference_no": "Reference No.",
"field.closed": "Closed",
"field.entries": "Entries",
"field.statement": "Statement",
"field.receipt_message": "Receipt Message",
"field.amount": "Amount",
"field.status": "Status",
"field.status.draft": "Draft",
"field.status.closed": "Closed",
"field.created_at": "Created At"
} }

View File

@@ -2,5 +2,18 @@
"view.draft": "Draft", "view.draft": "Draft",
"view.published": "Published", "view.published": "Published",
"view.open": "Open", "view.open": "Open",
"view.closed": "Closed" "view.closed": "Closed",
"field.vendor": "Vendor",
"field.vendor_credit_number": "Vendor Credit No.",
"field.vendor_credit_date": "Vendor Credit Date",
"field.reference_no": "Reference No.",
"field.exchange_rate": "Exchange Rate",
"field.note": "Note",
"field.open": "Open",
"field.entries": "Entries",
"field.item": "Item",
"field.rate": "Rate",
"field.quantity": "Quantity",
"field.description": "Description"
} }

View File

@@ -156,7 +156,7 @@ export const AccountMeta = {
minLength: 3, minLength: 3,
maxLength: 6, maxLength: 6,
unique: true, unique: true,
importHint: 'Unique number to identify the account.', importHint: 'account.field.code_hint',
}, },
accountType: { accountType: {
name: 'account.field.type', name: 'account.field.type',

View File

@@ -167,7 +167,7 @@ export const BillPaymentMeta = {
name: 'bill_payment.field.payment_number', name: 'bill_payment.field.payment_number',
fieldType: 'text', fieldType: 'text',
unique: true, unique: true,
importHint: 'The payment number should be unique.', importHint: 'bill_payment.field.payment_number_hint',
}, },
paymentAccountId: { paymentAccountId: {
name: 'bill_payment.field.payment_account', name: 'bill_payment.field.payment_account',
@@ -175,7 +175,7 @@ export const BillPaymentMeta = {
relationModel: 'Account', relationModel: 'Account',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],
required: true, required: true,
importHint: 'Matches the account name or code.', importHint: 'account.field.account_hint',
}, },
exchangeRate: { exchangeRate: {
name: 'bill_payment.field.exchange_rate', name: 'bill_payment.field.exchange_rate',
@@ -203,7 +203,7 @@ export const BillPaymentMeta = {
relationModel: 'Bill', relationModel: 'Bill',
relationImportMatch: 'billNumber', relationImportMatch: 'billNumber',
required: true, required: true,
importHint: 'Matches the bill number.', importHint: 'bill_payment.field.bill_hint',
}, },
paymentAmount: { paymentAmount: {
name: 'bill_payment.field.entries.payment_amount', name: 'bill_payment.field.entries.payment_amount',
@@ -213,7 +213,7 @@ export const BillPaymentMeta = {
}, },
}, },
branchId: { branchId: {
name: 'Branch', name: 'invoice.field.branch',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Branch', relationModel: 'Branch',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],

View File

@@ -184,76 +184,76 @@ export const BillMeta = {
}, },
fields2: { fields2: {
billNumber: { billNumber: {
name: 'Bill No.', name: 'bill.field.bill_number',
fieldType: 'text', fieldType: 'text',
required: true, required: true,
}, },
referenceNo: { referenceNo: {
name: 'Reference No.', name: 'bill.field.reference_no',
fieldType: 'text', fieldType: 'text',
}, },
billDate: { billDate: {
name: 'Date', name: 'bill.field.bill_date',
fieldType: 'date', fieldType: 'date',
required: true, required: true,
}, },
dueDate: { dueDate: {
name: 'Due Date', name: 'bill.field.due_date',
fieldType: 'date', fieldType: 'date',
required: true, required: true,
}, },
vendorId: { vendorId: {
name: 'Vendor', name: 'bill.field.vendor',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Contact', relationModel: 'Contact',
relationImportMatch: 'displayName', relationImportMatch: 'displayName',
required: true, required: true,
}, },
exchangeRate: { exchangeRate: {
name: 'Exchange Rate', name: 'bill.field.exchange_rate',
fieldType: 'number', fieldType: 'number',
}, },
note: { note: {
name: 'Note', name: 'bill.field.note',
fieldType: 'text', fieldType: 'text',
}, },
open: { open: {
name: 'Open', name: 'bill.field.open',
fieldType: 'boolean', fieldType: 'boolean',
}, },
entries: { entries: {
name: 'Entries', name: 'bill.field.entries',
fieldType: 'collection', fieldType: 'collection',
collectionOf: 'object', collectionOf: 'object',
collectionMinLength: 1, collectionMinLength: 1,
required: true, required: true,
fields: { fields: {
itemId: { itemId: {
name: 'Item', name: 'bill.field.item',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Item', relationModel: 'Item',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],
required: true, required: true,
importHint: 'Matches the item name or code.', importHint: 'bill.field.item_hint',
}, },
rate: { rate: {
name: 'Rate', name: 'bill.field.rate',
fieldType: 'number', fieldType: 'number',
required: true, required: true,
}, },
quantity: { quantity: {
name: 'Quantity', name: 'bill.field.quantity',
fieldType: 'number', fieldType: 'number',
required: true, required: true,
}, },
description: { description: {
name: 'Line Description', name: 'bill.field.description',
fieldType: 'text', fieldType: 'text',
}, },
}, },
}, },
branchId: { branchId: {
name: 'Branch', name: 'invoice.field.branch',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Branch', relationModel: 'Branch',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],
@@ -261,7 +261,7 @@ export const BillMeta = {
required: true, required: true,
}, },
warehouseId: { warehouseId: {
name: 'Warehouse', name: 'invoice.field.warehouse',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Warehouse', relationModel: 'Warehouse',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],

View File

@@ -164,73 +164,73 @@ export const CreditNoteMeta = {
}, },
fields2: { fields2: {
customerId: { customerId: {
name: 'Customer', name: 'credit_note.field.customer',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Contact', relationModel: 'Contact',
relationImportMatch: 'displayName', relationImportMatch: 'displayName',
required: true, required: true,
}, },
exchangeRate: { exchangeRate: {
name: 'Exchange Rate', name: 'credit_note.field.exchange_rate',
fieldType: 'number', fieldType: 'number',
}, },
creditNoteDate: { creditNoteDate: {
name: 'Credit Note Date', name: 'credit_note.field.credit_note_date',
fieldType: 'date', fieldType: 'date',
required: true, required: true,
}, },
referenceNo: { referenceNo: {
name: 'Reference No.', name: 'credit_note.field.reference_no',
fieldType: 'text', fieldType: 'text',
}, },
note: { note: {
name: 'Note', name: 'credit_note.field.note',
fieldType: 'text', fieldType: 'text',
}, },
termsConditions: { termsConditions: {
name: 'Terms & Conditions', name: 'credit_note.field.terms_conditions',
fieldType: 'text', fieldType: 'text',
}, },
creditNoteNumber: { creditNoteNumber: {
name: 'Credit Note Number', name: 'credit_note.field.credit_note_number',
fieldType: 'text', fieldType: 'text',
}, },
open: { open: {
name: 'Open', name: 'credit_note.field.open',
fieldType: 'boolean', fieldType: 'boolean',
}, },
entries: { entries: {
name: 'Entries', name: 'credit_note.field.entries',
fieldType: 'collection', fieldType: 'collection',
collectionOf: 'object', collectionOf: 'object',
collectionMinLength: 1, collectionMinLength: 1,
fields: { fields: {
itemId: { itemId: {
name: 'Item', name: 'credit_note.field.item',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Item', relationModel: 'Item',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],
required: true, required: true,
importHint: 'Matches the item name or code.', importHint: 'invoice.field.item_hint',
}, },
rate: { rate: {
name: 'Rate', name: 'credit_note.field.rate',
fieldType: 'number', fieldType: 'number',
required: true, required: true,
}, },
quantity: { quantity: {
name: 'Quantity', name: 'credit_note.field.quantity',
fieldType: 'number', fieldType: 'number',
required: true, required: true,
}, },
description: { description: {
name: 'Description', name: 'credit_note.field.description',
fieldType: 'text', fieldType: 'text',
}, },
}, },
}, },
branchId: { branchId: {
name: 'Branch', name: 'invoice.field.branch',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Branch', relationModel: 'Branch',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],
@@ -238,7 +238,7 @@ export const CreditNoteMeta = {
required: true, required: true,
}, },
warehouseId: { warehouseId: {
name: 'Warehouse', name: 'invoice.field.warehouse',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Warehouse', relationModel: 'Warehouse',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],

View File

@@ -5,10 +5,10 @@ import { ExpensesSampleData } from './constants';
import { CreateExpense } from './commands/CreateExpense.service'; import { CreateExpense } from './commands/CreateExpense.service';
import { CreateExpenseDto } from './dtos/Expense.dto'; import { CreateExpenseDto } from './dtos/Expense.dto';
import { ImportableService } from '../Import/decorators/Import.decorator'; import { ImportableService } from '../Import/decorators/Import.decorator';
import { ManualJournal } from '../ManualJournals/models/ManualJournal'; import { Expense } from './models/Expense.model';
@Injectable() @Injectable()
@ImportableService({ name: ManualJournal.name }) @ImportableService({ name: Expense.name })
export class ExpensesImportable extends Importable { export class ExpensesImportable extends Importable {
constructor(private readonly createExpenseService: CreateExpense) { constructor(private readonly createExpenseService: CreateExpense) {
super(); super();

View File

@@ -135,7 +135,7 @@ export const ExpenseMeta = {
relationModel: 'Account', relationModel: 'Account',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],
required: true, required: true,
importHint: 'Matches the account name or code.', importHint: 'account.field.account_hint',
}, },
referenceNo: { referenceNo: {
name: 'expense.field.reference_no', name: 'expense.field.reference_no',
@@ -169,7 +169,7 @@ export const ExpenseMeta = {
relationModel: 'Account', relationModel: 'Account',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],
required: true, required: true,
importHint: 'Matches the account name or code.', importHint: 'account.field.account_hint',
}, },
amount: { amount: {
name: 'expense.field.amount', name: 'expense.field.amount',
@@ -187,7 +187,7 @@ export const ExpenseMeta = {
fieldType: 'boolean', fieldType: 'boolean',
}, },
branchId: { branchId: {
name: 'Branch', name: 'invoice.field.branch',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Branch', relationModel: 'Branch',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],

View File

@@ -18,7 +18,7 @@ import { CurrencyParsingDTOs } from './_constants';
export class ImportFileDataTransformer { export class ImportFileDataTransformer {
constructor( constructor(
private readonly resource: ResourceService, private readonly resource: ResourceService,
) {} ) { }
/** /**
* Parses the given sheet data before passing to the service layer. * Parses the given sheet data before passing to the service layer.
@@ -55,9 +55,8 @@ export class ImportFileDataTransformer {
/** /**
* Aggregates parsed data based on resource metadata configuration. * Aggregates parsed data based on resource metadata configuration.
* @param {number} tenantId * @param {string} resourceName - The resource name.
* @param {string} resourceName * @param {Record<string, any>} parsedData - The parsed data to aggregate.
* @param {Record<string, any>} parsedData
* @returns {Record<string, any>[]} * @returns {Record<string, any>[]}
*/ */
public aggregateParsedValues( public aggregateParsedValues(
@@ -110,8 +109,11 @@ export class ImportFileDataTransformer {
valueDTOs: Record<string, any>[], valueDTOs: Record<string, any>[],
trx?: Knex.Transaction trx?: Knex.Transaction
): Promise<Record<string, any>[]> { ): Promise<Record<string, any>[]> {
// const tenantModels = this.tenancy.models(tenantId); // Create a model resolver function that uses ResourceService
const _valueParser = valueParser(fields, {}, trx); const modelResolver = (modelName: string) => {
return this.resource.getResourceModel(modelName)();
};
const _valueParser = valueParser(fields, modelResolver, trx);
const _keyParser = parseKey(fields); const _keyParser = parseKey(fields);
const parseAsync = async (valueDTO) => { const parseAsync = async (valueDTO) => {

View File

@@ -19,7 +19,8 @@ export class ImportFileDataValidator {
/** /**
* Validates the given mapped DTOs and returns errors with their index. * Validates the given mapped DTOs and returns errors with their index.
* @param {Record<string, any>} mappedDTOs * @param {ResourceMetaFieldsMap} importableFields - Already localized fields from ResourceService
* @param {Record<string, any>} data
* @returns {Promise<void | ImportInsertError[]>} * @returns {Promise<void | ImportInsertError[]>}
*/ */
public async validateData( public async validateData(

View File

@@ -24,7 +24,7 @@ export class ImportFileUploadService {
@Inject(ImportModel.name) @Inject(ImportModel.name)
private readonly importModel: typeof ImportModel, private readonly importModel: typeof ImportModel,
) {} ) { }
/** /**
* Imports the specified file for the given resource. * Imports the specified file for the given resource.

View File

@@ -6,7 +6,7 @@ import { ContextIdFactory, ModuleRef } from '@nestjs/core';
@Injectable() @Injectable()
export class ImportableRegistry { export class ImportableRegistry {
constructor(private readonly moduleRef: ModuleRef) {} constructor(private readonly moduleRef: ModuleRef) { }
/** /**
* Retrieves the importable service instance of the given resource name. * Retrieves the importable service instance of the given resource name.
* @param {string} name * @param {string} name
@@ -15,6 +15,12 @@ export class ImportableRegistry {
public async getImportable(name: string) { public async getImportable(name: string) {
const _name = this.sanitizeResourceName(name); const _name = this.sanitizeResourceName(name);
const importable = getImportableService(_name); const importable = getImportableService(_name);
if (!importable) {
throw new Error(
`No importable service found for resource "${_name}". Make sure the resource has an @ImportableService decorator registered.`,
);
}
const contextId = ContextIdFactory.create(); const contextId = ContextIdFactory.create();
const importableInstance = await this.moduleRef.resolve(importable, contextId, { const importableInstance = await this.moduleRef.resolve(importable, contextId, {

View File

@@ -0,0 +1,287 @@
import { aggregate } from './_utils';
describe('aggregate', () => {
describe('basic aggregation', () => {
it('should aggregate entries with matching comparator attribute', () => {
const input = [
{ id: 1, name: 'John', entries: ['entry1'] },
{ id: 2, name: 'Jane', entries: ['entry2'] },
{ id: 1, name: 'John', entries: ['entry3'] },
];
const result = aggregate(input, 'id', 'entries');
expect(result).toHaveLength(2);
expect(result[0]).toEqual({
id: 1,
name: 'John',
entries: ['entry1', 'entry3'],
});
expect(result[1]).toEqual({
id: 2,
name: 'Jane',
entries: ['entry2'],
});
});
it('should preserve order of first occurrence', () => {
const input = [
{ id: 2, entries: ['a'] },
{ id: 1, entries: ['b'] },
{ id: 2, entries: ['c'] },
];
const result = aggregate(input, 'id', 'entries');
expect(result[0].id).toBe(2);
expect(result[1].id).toBe(1);
});
});
describe('no matching entries', () => {
it('should return all entries unchanged when no comparator matches', () => {
const input = [
{ id: 1, name: 'John', entries: ['entry1'] },
{ id: 2, name: 'Jane', entries: ['entry2'] },
{ id: 3, name: 'Bob', entries: ['entry3'] },
];
const result = aggregate(input, 'id', 'entries');
expect(result).toHaveLength(3);
expect(result).toEqual(input);
});
});
describe('edge cases', () => {
it('should return empty array when input is empty', () => {
const result = aggregate([], 'id', 'entries');
expect(result).toEqual([]);
expect(result).toHaveLength(0);
});
it('should return single entry unchanged when input has one item', () => {
const input = [{ id: 1, name: 'John', entries: ['entry1'] }];
const result = aggregate(input, 'id', 'entries');
expect(result).toHaveLength(1);
expect(result[0]).toEqual({
id: 1,
name: 'John',
entries: ['entry1'],
});
});
it('should handle multiple entries with same comparator value', () => {
const input = [
{ id: 1, entries: ['a'] },
{ id: 1, entries: ['b'] },
{ id: 1, entries: ['c'] },
{ id: 1, entries: ['d'] },
];
const result = aggregate(input, 'id', 'entries');
expect(result).toHaveLength(1);
expect(result[0].entries).toEqual(['a', 'b', 'c', 'd']);
});
});
describe('different comparator attributes', () => {
it('should work with string comparator attribute', () => {
const input = [
{ name: 'Product A', category: 'Electronics', entries: ['item1'] },
{ name: 'Product B', category: 'Books', entries: ['item2'] },
{ name: 'Product C', category: 'Electronics', entries: ['item3'] },
];
const result = aggregate(input, 'category', 'entries');
expect(result).toHaveLength(2);
expect(result[0]).toEqual({
name: 'Product A',
category: 'Electronics',
entries: ['item1', 'item3'],
});
expect(result[1]).toEqual({
name: 'Product B',
category: 'Books',
entries: ['item2'],
});
});
it('should not aggregate items with undefined comparator values', () => {
const input = [
{ id: undefined, entries: ['a'] },
{ id: 1, entries: ['b'] },
{ id: undefined, entries: ['c'] },
];
const result = aggregate(input, 'id', 'entries');
expect(result).toHaveLength(3);
// Items with undefined id are NOT aggregated - each remains separate
expect(result[0].entries).toEqual(['a']);
expect(result[1].entries).toEqual(['b']);
expect(result[2].entries).toEqual(['c']);
});
it('should handle null comparator values separately', () => {
const input = [
{ id: null, entries: ['a'] },
{ id: 1, entries: ['b'] },
{ id: null, entries: ['c'] },
];
const result = aggregate(input, 'id', 'entries');
expect(result).toHaveLength(3);
expect(result[0].entries).toEqual(['a']);
expect(result[1].entries).toEqual(['b']);
expect(result[2].entries).toEqual(['c']);
});
it('should not aggregate items missing the comparatorAttr property', () => {
const input = [
{ id: 1, entries: ['a'] },
{ name: 'No ID', entries: ['b'] }, // missing 'id' property
{ id: 1, entries: ['c'] },
{ entries: ['d'] }, // also missing 'id' property
];
const result = aggregate(input, 'id', 'entries');
// 3 entries: aggregated id:1, and two separate items without 'id' property
expect(result).toHaveLength(3);
// Items with id: 1 are aggregated
expect(result[0]).toEqual({ id: 1, entries: ['a', 'c'] });
// Items missing 'id' are NOT aggregated - each remains separate
expect(result[1]).toEqual({ name: 'No ID', entries: ['b'] });
expect(result[2]).toEqual({ entries: ['d'] });
});
});
describe('different group attributes', () => {
it('should work with different groupOn attribute name', () => {
const input = [
{ id: 1, items: ['item1'] },
{ id: 1, items: ['item2'] },
{ id: 2, items: ['item3'] },
];
const result = aggregate(input, 'id', 'items');
expect(result).toHaveLength(2);
expect(result[0].items).toEqual(['item1', 'item2']);
expect(result[1].items).toEqual(['item3']);
});
});
describe('complex entries', () => {
it('should aggregate entries containing objects', () => {
const input = [
{ invoiceId: 'INV-001', entries: [{ itemId: 1, quantity: 2 }] },
{ invoiceId: 'INV-002', entries: [{ itemId: 2, quantity: 1 }] },
{ invoiceId: 'INV-001', entries: [{ itemId: 3, quantity: 5 }] },
];
const result = aggregate(input, 'invoiceId', 'entries');
expect(result).toHaveLength(2);
expect(result[0].entries).toEqual([
{ itemId: 1, quantity: 2 },
{ itemId: 3, quantity: 5 },
]);
expect(result[1].entries).toEqual([{ itemId: 2, quantity: 1 }]);
});
it('should aggregate entries with multiple items in each entry', () => {
const input = [
{ id: 1, entries: ['a', 'b'] },
{ id: 1, entries: ['c', 'd'] },
{ id: 2, entries: ['e'] },
];
const result = aggregate(input, 'id', 'entries');
expect(result).toHaveLength(2);
expect(result[0].entries).toEqual(['a', 'b', 'c', 'd']);
expect(result[1].entries).toEqual(['e']);
});
});
describe('numeric comparator values', () => {
it('should correctly compare numeric values', () => {
const input = [
{ id: 1, entries: ['a'] },
{ id: 2, entries: ['b'] },
{ id: 1, entries: ['c'] },
];
const result = aggregate(input, 'id', 'entries');
expect(result).toHaveLength(2);
expect(result.find((r) => r.id === 1).entries).toEqual(['a', 'c']);
expect(result.find((r) => r.id === 2).entries).toEqual(['b']);
});
it('should treat 0 as a valid comparator value', () => {
const input = [
{ id: 0, entries: ['a'] },
{ id: 1, entries: ['b'] },
{ id: 0, entries: ['c'] },
];
const result = aggregate(input, 'id', 'entries');
expect(result).toHaveLength(2);
expect(result[0].entries).toEqual(['a', 'c']);
expect(result[1].entries).toEqual(['b']);
});
});
describe('preserving other properties', () => {
it('should preserve all properties from the first matching entry', () => {
const input = [
{ id: 1, name: 'First', extra: 'data1', entries: ['a'] },
{ id: 1, name: 'Second', extra: 'data2', entries: ['b'] },
];
const result = aggregate(input, 'id', 'entries');
expect(result).toHaveLength(1);
expect(result[0].name).toBe('First');
expect(result[0].extra).toBe('data1');
expect(result[0].entries).toEqual(['a', 'b']);
});
});
describe('empty entries arrays', () => {
it('should handle empty entries arrays', () => {
const input = [
{ id: 1, entries: [] },
{ id: 1, entries: ['a'] },
];
const result = aggregate(input, 'id', 'entries');
expect(result).toHaveLength(1);
expect(result[0].entries).toEqual(['a']);
});
it('should handle all empty entries arrays', () => {
const input = [
{ id: 1, entries: [] },
{ id: 1, entries: [] },
];
const result = aggregate(input, 'id', 'entries');
expect(result).toHaveLength(1);
expect(result[0].entries).toEqual([]);
});
});
});

View File

@@ -253,28 +253,28 @@ export const getResourceColumns = (resourceColumns: {
}) => { }) => {
const mapColumn = const mapColumn =
(group: string) => (group: string) =>
([fieldKey, { name, importHint, required, order, ...field }]: [ ([fieldKey, { name, importHint, required, order, ...field }]: [
string, string,
IModelMetaField2, IModelMetaField2,
]) => { ]) => {
const extra: Record<string, any> = {}; const extra: Record<string, any> = {};
const key = fieldKey; const key = fieldKey;
if (group) { if (group) {
extra.group = group; extra.group = group;
} }
if (field.fieldType === 'collection') { if (field.fieldType === 'collection') {
extra.fields = mapColumns(field.fields, key); extra.fields = mapColumns(field.fields, key);
} }
return { return {
key, key,
name, name,
required, required,
hint: importHint, hint: importHint,
order, order,
...extra, ...extra,
};
}; };
};
const sortColumn = (a, b) => const sortColumn = (a, b) =>
a.order && b.order ? a.order - b.order : a.order ? -1 : b.order ? 1 : 0; a.order && b.order ? a.order - b.order : a.order ? -1 : b.order ? 1 : 0;
@@ -284,52 +284,54 @@ export const getResourceColumns = (resourceColumns: {
return R.compose(transformInputToGroupedFields, mapColumns)(resourceColumns); return R.compose(transformInputToGroupedFields, mapColumns)(resourceColumns);
}; };
export type ModelResolver = (modelName: string) => any;
// Prases the given object value based on the field key type. // Prases the given object value based on the field key type.
export const valueParser = export const valueParser =
(fields: ResourceMetaFieldsMap, tenantModels: any, trx?: Knex.Transaction) => (fields: ResourceMetaFieldsMap, modelResolver: ModelResolver, trx?: Knex.Transaction) =>
async (value: any, key: string, group = '') => { async (value: any, key: string, group = '') => {
let _value = value; let _value = value;
const fieldKey = key.includes('.') ? key.split('.')[0] : key; const fieldKey = key.includes('.') ? key.split('.')[0] : key;
const field = group ? fields[group]?.fields[fieldKey] : fields[fieldKey]; const field = group ? fields[group]?.fields[fieldKey] : fields[fieldKey];
// Parses the boolean value. // Parses the boolean value.
if (field.fieldType === 'boolean') { if (field.fieldType === 'boolean') {
_value = parseBoolean(value); _value = parseBoolean(value);
// Parses the enumeration value. // Parses the enumeration value.
} else if (field.fieldType === 'enumeration') { } else if (field.fieldType === 'enumeration') {
const option = get(field, 'options', []).find( const option = get(field, 'options', []).find(
(option) => option.label?.toLowerCase() === value?.toLowerCase(), (option) => option.label?.toLowerCase() === value?.toLowerCase(),
); );
_value = get(option, 'key'); _value = get(option, 'key');
// Parses the numeric value. // Parses the numeric value.
} else if (field.fieldType === 'number') { } else if (field.fieldType === 'number') {
_value = multiNumberParse(value); _value = multiNumberParse(value);
// Parses the relation value. // Parses the relation value.
} else if (field.fieldType === 'relation') { } else if (field.fieldType === 'relation') {
const RelationModel = tenantModels[field.relationModel]; const RelationModel = modelResolver(field.relationModel);
if (!RelationModel) { if (!RelationModel) {
throw new Error(`The relation model of ${key} field is not exist.`); throw new Error(`The relation model of ${key} field is not exist.`);
} }
const relationQuery = RelationModel.query(trx); const relationQuery = RelationModel.query(trx);
const relationKeys = castArray(field?.relationImportMatch); const relationKeys = castArray(field?.relationImportMatch);
relationQuery.where(function () { relationQuery.where(function () {
relationKeys.forEach((relationKey: string) => { relationKeys.forEach((relationKey: string) => {
this.orWhereRaw('LOWER(??) = LOWER(?)', [relationKey, value]); this.orWhereRaw('LOWER(??) = LOWER(?)', [relationKey, value]);
});
}); });
}); const result = await relationQuery.first();
const result = await relationQuery.first(); _value = get(result, 'id');
_value = get(result, 'id'); } else if (field.fieldType === 'collection') {
} else if (field.fieldType === 'collection') { const ObjectFieldKey = key.includes('.') ? key.split('.')[1] : key;
const ObjectFieldKey = key.includes('.') ? key.split('.')[1] : key; const _valueParser = valueParser(fields, modelResolver);
const _valueParser = valueParser(fields, tenantModels); _value = await _valueParser(value, ObjectFieldKey, fieldKey);
_value = await _valueParser(value, ObjectFieldKey, fieldKey); }
} return _value;
return _value; };
};
/** /**
* Parses the field key and detarmines the key path. * Parses the field key and detarmines the key path.
@@ -402,12 +404,17 @@ export function aggregate(
groupOn: string, groupOn: string,
): Array<Record<string, any>> { ): Array<Record<string, any>> {
return input.reduce((acc, curr) => { return input.reduce((acc, curr) => {
// Skip aggregation if the current item doesn't have the comparator attribute
if (curr[comparatorAttr] === undefined || curr[comparatorAttr] === null) {
acc.push({ ...curr });
return acc;
}
const existingEntry = acc.find( const existingEntry = acc.find(
(entry) => entry[comparatorAttr] === curr[comparatorAttr], (entry) => entry[comparatorAttr] === curr[comparatorAttr],
); );
if (existingEntry) { if (existingEntry) {
existingEntry[groupOn].push(...curr.entries); existingEntry[groupOn].push(...curr[groupOn]);
} else { } else {
acc.push({ ...curr }); acc.push({ ...curr });
} }

View File

@@ -267,28 +267,28 @@ export const ItemMeta = {
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Account', relationModel: 'Account',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],
importHint: 'Matches the account name or code.', importHint: 'account.field.account_hint',
}, },
sellAccountId: { sellAccountId: {
name: 'item.field.sell_account', name: 'item.field.sell_account',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Account', relationModel: 'Account',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],
importHint: 'Matches the account name or code.', importHint: 'account.field.account_hint',
}, },
inventoryAccountId: { inventoryAccountId: {
name: 'item.field.inventory_account', name: 'item.field.inventory_account',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Account', relationModel: 'Account',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],
importHint: 'Matches the account name or code.', importHint: 'account.field.account_hint',
}, },
sellDescription: { sellDescription: {
name: 'Sell Description', name: 'item.field.sell_description',
fieldType: 'text', fieldType: 'text',
}, },
purchaseDescription: { purchaseDescription: {
name: 'Purchase Description', name: 'item.field.purchase_description',
fieldType: 'text', fieldType: 'text',
}, },
note: { note: {
@@ -300,7 +300,7 @@ export const ItemMeta = {
fieldType: 'relation', fieldType: 'relation',
relationModel: 'ItemCategory', relationModel: 'ItemCategory',
relationImportMatch: ['name'], relationImportMatch: ['name'],
importHint: 'Matches the category name.', importHint: 'item.field.category_hint',
}, },
active: { active: {
name: 'item.field.active', name: 'item.field.active',

View File

@@ -165,12 +165,12 @@ export const PaymentReceivedMeta = {
relationModel: 'Account', relationModel: 'Account',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],
required: true, required: true,
importHint: 'Matches the account name or code.', importHint: 'account.field.account_hint',
}, },
paymentReceiveNo: { paymentReceiveNo: {
name: 'payment_receive.field.payment_receive_no', name: 'payment_receive.field.payment_receive_no',
fieldType: 'text', fieldType: 'text',
importHint: 'The payment number should be unique.', importHint: 'payment_receive.field.payment_no_hint',
}, },
statement: { statement: {
name: 'payment_receive.field.statement', name: 'payment_receive.field.statement',
@@ -189,7 +189,7 @@ export const PaymentReceivedMeta = {
relationModel: 'SaleInvoice', relationModel: 'SaleInvoice',
relationImportMatch: 'invoiceNo', relationImportMatch: 'invoiceNo',
required: true, required: true,
importHint: 'Matches the invoice number.', importHint: 'payment_receive.field.invoice_hint',
}, },
paymentAmount: { paymentAmount: {
name: 'payment_receive.field.entries.payment_amount', name: 'payment_receive.field.entries.payment_amount',
@@ -199,7 +199,7 @@ export const PaymentReceivedMeta = {
}, },
}, },
branchId: { branchId: {
name: 'Branch', name: 'invoice.field.branch',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Branch', relationModel: 'Branch',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],

View File

@@ -1,5 +1,6 @@
import { ModuleRef } from '@nestjs/core'; import { ModuleRef } from '@nestjs/core';
import { pickBy } from 'lodash'; import { pickBy, mapValues } from 'lodash';
import { I18nService } from 'nestjs-i18n';
import { WarehousesSettings } from '../Warehouses/WarehousesSettings'; import { WarehousesSettings } from '../Warehouses/WarehousesSettings';
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { BranchesSettingsService } from '../Branches/BranchesSettings'; import { BranchesSettingsService } from '../Branches/BranchesSettings';
@@ -20,7 +21,8 @@ export class ResourceService {
private readonly branchesSettings: BranchesSettingsService, private readonly branchesSettings: BranchesSettingsService,
private readonly warehousesSettings: WarehousesSettings, private readonly warehousesSettings: WarehousesSettings,
private readonly moduleRef: ModuleRef, private readonly moduleRef: ModuleRef,
) {} private readonly i18nService: I18nService,
) { }
/** /**
* Retrieve resource model object. * Retrieve resource model object.
@@ -96,7 +98,45 @@ export class ResourceService {
}; };
/** /**
* Retrieve the resource fields. * Localizes a single field by translating its name and importHint.
* @param {IModelMetaField2} field - The field to localize.
* @returns {IModelMetaField2} - The localized field.
*/
private localizeField(field: IModelMetaField2): IModelMetaField2 {
const localizedField = {
...field,
name: this.i18nService.t(field.name, { defaultValue: field.name }),
} as IModelMetaField2;
if (field.importHint) {
localizedField.importHint = this.i18nService.t(field.importHint, {
defaultValue: field.importHint,
});
}
// Recursively localize nested fields (for collection types)
if (field.fields) {
localizedField.fields = this.localizeFields(
field.fields as unknown as Record<string, IModelMetaField2>,
) as unknown as typeof field.fields;
}
return localizedField;
}
/**
* Localizes all fields in a fields map.
* @param {Record<string, IModelMetaField2>} fields - The fields to localize.
* @returns {Record<string, IModelMetaField2>} - The localized fields.
*/
private localizeFields(
fields: Record<string, IModelMetaField2>,
): Record<string, IModelMetaField2> {
return mapValues(fields, (field) => this.localizeField(field));
}
/**
* Retrieve the resource fields with localized names and hints.
* @param {string} modelName * @param {string} modelName
* @returns {IModelMetaField2} * @returns {IModelMetaField2}
*/ */
@@ -104,8 +144,11 @@ export class ResourceService {
[key: string]: IModelMetaField2; [key: string]: IModelMetaField2;
} { } {
const meta = this.getResourceMeta(modelName); const meta = this.getResourceMeta(modelName);
const filteredFields = this.filterSupportFeatures(meta.fields2);
return this.filterSupportFeatures(meta.fields2); return this.localizeFields(
filteredFields as Record<string, IModelMetaField2>,
);
} }
/** /**

View File

@@ -191,52 +191,52 @@ export const SaleEstimateMeta = {
}, },
fields2: { fields2: {
customerId: { customerId: {
name: 'Customer', name: 'estimate.field.customer',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Contact', relationModel: 'Contact',
relationImportMatch: ['displayName'], relationImportMatch: ['displayName'],
required: true, required: true,
}, },
estimateDate: { estimateDate: {
name: 'Estimate Date', name: 'estimate.field.estimate_date',
fieldType: 'date', fieldType: 'date',
required: true, required: true,
}, },
expirationDate: { expirationDate: {
name: 'Expiration Date', name: 'estimate.field.expiration_date',
fieldType: 'date', fieldType: 'date',
required: true, required: true,
}, },
estimateNumber: { estimateNumber: {
name: 'Estimate No.', name: 'estimate.field.estimate_number',
fieldType: 'text', fieldType: 'text',
}, },
reference: { reference: {
name: 'Reference No.', name: 'estimate.field.reference_no',
fieldType: 'text', fieldType: 'text',
}, },
exchangeRate: { exchangeRate: {
name: 'Exchange Rate', name: 'estimate.field.exchange_rate',
fieldType: 'number', fieldType: 'number',
}, },
currencyCode: { currencyCode: {
name: 'Currency', name: 'estimate.field.currency',
fieldType: 'text', fieldType: 'text',
}, },
note: { note: {
name: 'Note', name: 'estimate.field.note',
fieldType: 'text', fieldType: 'text',
}, },
termsConditions: { termsConditions: {
name: 'Terms & Conditions', name: 'estimate.field.terms_conditions',
fieldType: 'text', fieldType: 'text',
}, },
delivered: { delivered: {
name: 'Delivered', name: 'estimate.field.delivered',
type: 'boolean', type: 'boolean',
}, },
entries: { entries: {
name: 'Entries', name: 'estimate.field.entries',
fieldType: 'collection', fieldType: 'collection',
collectionOf: 'object', collectionOf: 'object',
collectionMinLength: 1, collectionMinLength: 1,
@@ -248,7 +248,7 @@ export const SaleEstimateMeta = {
relationModel: 'Item', relationModel: 'Item',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],
required: true, required: true,
importHint: 'Matches the item name or code.', importHint: 'invoice.field.item_hint',
}, },
rate: { rate: {
name: 'invoice.field.rate', name: 'invoice.field.rate',
@@ -261,13 +261,13 @@ export const SaleEstimateMeta = {
required: true, required: true,
}, },
description: { description: {
name: 'Line Description', name: 'invoice.field.description',
fieldType: 'text', fieldType: 'text',
}, },
}, },
}, },
branchId: { branchId: {
name: 'Branch', name: 'invoice.field.branch',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Branch', relationModel: 'Branch',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],
@@ -275,7 +275,7 @@ export const SaleEstimateMeta = {
required: true, required: true,
}, },
warehouseId: { warehouseId: {
name: 'Warehouse', name: 'invoice.field.warehouse',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Warehouse', relationModel: 'Warehouse',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],

View File

@@ -5,10 +5,10 @@ import { Importable } from '@/modules/Import/Importable';
import { CreateSaleInvoiceDto } from '../dtos/SaleInvoice.dto'; import { CreateSaleInvoiceDto } from '../dtos/SaleInvoice.dto';
import { SaleInvoicesSampleData } from '../constants'; import { SaleInvoicesSampleData } from '../constants';
import { ImportableService } from '@/modules/Import/decorators/Import.decorator'; import { ImportableService } from '@/modules/Import/decorators/Import.decorator';
import { ManualJournal } from '@/modules/ManualJournals/models/ManualJournal'; import { SaleInvoice } from '../models/SaleInvoice';
@Injectable() @Injectable()
@ImportableService({ name: ManualJournal.name }) @ImportableService({ name: SaleInvoice.name })
export class SaleInvoicesImportable extends Importable { export class SaleInvoicesImportable extends Importable {
constructor(private readonly createInvoiceService: CreateSaleInvoice) { constructor(private readonly createInvoiceService: CreateSaleInvoice) {
super(); super();

View File

@@ -259,7 +259,7 @@ export const SaleInvoiceMeta = {
relationModel: 'Item', relationModel: 'Item',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],
required: true, required: true,
importHint: 'Matches the item name or code.', importHint: 'invoice.field.item_hint',
}, },
rate: { rate: {
name: 'invoice.field.rate', name: 'invoice.field.rate',
@@ -283,7 +283,7 @@ export const SaleInvoiceMeta = {
printable: false, printable: false,
}, },
branchId: { branchId: {
name: 'Branch', name: 'invoice.field.branch',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Branch', relationModel: 'Branch',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],
@@ -291,7 +291,7 @@ export const SaleInvoiceMeta = {
required: true, required: true,
}, },
warehouseId: { warehouseId: {
name: 'Warehouse', name: 'invoice.field.warehouse',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Warehouse', relationModel: 'Warehouse',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],

View File

@@ -186,42 +186,42 @@ export const SaleReceiptMeta = {
}, },
fields2: { fields2: {
receiptDate: { receiptDate: {
name: 'Receipt Date', name: 'receipt.field.receipt_date',
fieldType: 'date', fieldType: 'date',
required: true, required: true,
}, },
customerId: { customerId: {
name: 'Customer', name: 'receipt.field.customer',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Contact', relationModel: 'Contact',
relationImportMatch: 'displayName', relationImportMatch: 'displayName',
required: true, required: true,
}, },
depositAccountId: { depositAccountId: {
name: 'Deposit Account', name: 'receipt.field.deposit_account',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Account', relationModel: 'Account',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],
required: true, required: true,
}, },
exchangeRate: { exchangeRate: {
name: 'Exchange Rate', name: 'receipt.field.exchange_rate',
fieldType: 'number', fieldType: 'number',
}, },
receiptNumber: { receiptNumber: {
name: 'Receipt Number', name: 'receipt.field.receipt_number',
fieldType: 'text', fieldType: 'text',
}, },
referenceNo: { referenceNo: {
name: 'Reference No.', name: 'receipt.field.reference_no',
fieldType: 'text', fieldType: 'text',
}, },
closed: { closed: {
name: 'Closed', name: 'receipt.field.closed',
fieldType: 'boolean', fieldType: 'boolean',
}, },
entries: { entries: {
name: 'Entries', name: 'receipt.field.entries',
fieldType: 'collection', fieldType: 'collection',
collectionOf: 'object', collectionOf: 'object',
collectionMinLength: 1, collectionMinLength: 1,
@@ -233,7 +233,7 @@ export const SaleReceiptMeta = {
relationModel: 'Item', relationModel: 'Item',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],
required: true, required: true,
importHint: 'Matches the item name or code.', importHint: 'invoice.field.item_hint',
}, },
rate: { rate: {
name: 'invoice.field.rate', name: 'invoice.field.rate',
@@ -252,15 +252,15 @@ export const SaleReceiptMeta = {
}, },
}, },
statement: { statement: {
name: 'Statement', name: 'receipt.field.statement',
fieldType: 'text', fieldType: 'text',
}, },
receiptMessage: { receiptMessage: {
name: 'Receipt Message', name: 'receipt.field.receipt_message',
fieldType: 'text', fieldType: 'text',
}, },
branchId: { branchId: {
name: 'Branch', name: 'invoice.field.branch',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Branch', relationModel: 'Branch',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],
@@ -268,7 +268,7 @@ export const SaleReceiptMeta = {
required: true, required: true,
}, },
warehouseId: { warehouseId: {
name: 'Warehouse', name: 'invoice.field.warehouse',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Warehouse', relationModel: 'Warehouse',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],

View File

@@ -107,4 +107,4 @@ const modelProviders = models.map((model) => RegisterTenancyModel(model));
imports: [...modelProviders], imports: [...modelProviders],
exports: [...modelProviders], exports: [...modelProviders],
}) })
export class TenancyModelsModule {} export class TenancyModelsModule { }

View File

@@ -177,70 +177,70 @@ export const VendorCreditMeta = {
}, },
fields2: { fields2: {
vendorId: { vendorId: {
name: 'Vendor', name: 'vendor_credit.field.vendor',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Contact', relationModel: 'Contact',
relationImportMatch: 'displayName', relationImportMatch: 'displayName',
required: true, required: true,
}, },
exchangeRate: { exchangeRate: {
name: 'Echange Rate', name: 'vendor_credit.field.exchange_rate',
fieldType: 'text', fieldType: 'text',
}, },
vendorCreditNumber: { vendorCreditNumber: {
name: 'Vendor Credit No.', name: 'vendor_credit.field.vendor_credit_number',
fieldType: 'text', fieldType: 'text',
}, },
referenceNo: { referenceNo: {
name: 'Refernece No.', name: 'vendor_credit.field.reference_no',
fieldType: 'text', fieldType: 'text',
}, },
vendorCreditDate: { vendorCreditDate: {
name: 'Vendor Credit Date', name: 'vendor_credit.field.vendor_credit_date',
fieldType: 'date', fieldType: 'date',
required: true, required: true,
}, },
note: { note: {
name: 'Note', name: 'vendor_credit.field.note',
fieldType: 'text', fieldType: 'text',
}, },
open: { open: {
name: 'Open', name: 'vendor_credit.field.open',
fieldType: 'boolean', fieldType: 'boolean',
}, },
entries: { entries: {
name: 'Entries', name: 'vendor_credit.field.entries',
fieldType: 'collection', fieldType: 'collection',
collectionOf: 'object', collectionOf: 'object',
collectionMinLength: 1, collectionMinLength: 1,
required: true, required: true,
fields: { fields: {
itemId: { itemId: {
name: 'Item Name', name: 'vendor_credit.field.item',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Item', relationModel: 'Item',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],
required: true, required: true,
importHint: 'Matches the item name or code.', importHint: 'invoice.field.item_hint',
}, },
rate: { rate: {
name: 'Rate', name: 'vendor_credit.field.rate',
fieldType: 'number', fieldType: 'number',
required: true, required: true,
}, },
quantity: { quantity: {
name: 'Quantity', name: 'vendor_credit.field.quantity',
fieldType: 'number', fieldType: 'number',
required: true, required: true,
}, },
description: { description: {
name: 'Description', name: 'vendor_credit.field.description',
fieldType: 'text', fieldType: 'text',
}, },
}, },
}, },
branchId: { branchId: {
name: 'Branch', name: 'invoice.field.branch',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Branch', relationModel: 'Branch',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],
@@ -248,7 +248,7 @@ export const VendorCreditMeta = {
required: true required: true
}, },
warehouseId: { warehouseId: {
name: 'Warehouse', name: 'invoice.field.warehouse',
fieldType: 'relation', fieldType: 'relation',
relationModel: 'Warehouse', relationModel: 'Warehouse',
relationImportMatch: ['name', 'code'], relationImportMatch: ['name', 'code'],

View File

@@ -102,6 +102,7 @@ function AccountsSuggestFieldRoot({
return ( return (
<FSuggest <FSuggest
items={filteredAccounts} items={filteredAccounts}
itemPredicate={filterAccountsPredicater}
onCreateItemSelect={handleCreateItemSelect} onCreateItemSelect={handleCreateItemSelect}
valueAccessor="id" valueAccessor="id"
textAccessor="name" textAccessor="name"

View File

@@ -48,7 +48,7 @@ function ExpenseForm({
createExpenseMutate, createExpenseMutate,
expense, expense,
expenseId, expenseId,
submitPayload, submitPayloadRef,
} = useExpenseFormContext(); } = useExpenseFormContext();
const isNewMode = !expenseId; const isNewMode = !expenseId;
@@ -86,9 +86,12 @@ function ExpenseForm({
return; return;
} }
// Get submit payload from ref for synchronous access
const currentSubmitPayload = submitPayloadRef?.current || {};
const form = { const form = {
...transformFormValuesToRequest(values), ...transformFormValuesToRequest(values),
publish: submitPayload.publish, publish: currentSubmitPayload.publish,
}; };
// Handle request success. // Handle request success.
const handleSuccess = (response) => { const handleSuccess = (response) => {
@@ -103,10 +106,10 @@ function ExpenseForm({
}); });
setSubmitting(false); setSubmitting(false);
if (submitPayload.redirect) { if (currentSubmitPayload.redirect) {
history.push('/expenses'); history.push('/expenses');
} }
if (submitPayload.resetForm) { if (currentSubmitPayload.resetForm) {
resetForm(); resetForm();
} }
}; };

View File

@@ -59,8 +59,13 @@ function ExpenseFormPageProvider({ query, expenseId, ...props }) {
const { mutateAsync: createExpenseMutate } = useCreateExpense(); const { mutateAsync: createExpenseMutate } = useCreateExpense();
const { mutateAsync: editExpenseMutate } = useEditExpense(); const { mutateAsync: editExpenseMutate } = useEditExpense();
// Submit form payload. // Submit form payload - using ref for synchronous access.
const [submitPayload, setSubmitPayload] = React.useState({}); const submitPayloadRef = React.useRef({});
// Setter to update the ref.
const setSubmitPayload = React.useCallback((payload) => {
submitPayloadRef.current = payload;
}, []);
// Detarmines whether the form in new mode. // Detarmines whether the form in new mode.
const isNewMode = !expenseId; const isNewMode = !expenseId;
@@ -69,7 +74,7 @@ function ExpenseFormPageProvider({ query, expenseId, ...props }) {
const provider = { const provider = {
isNewMode, isNewMode,
expenseId, expenseId,
submitPayload, submitPayloadRef, // Expose ref for synchronous access
currencies, currencies,
customers, customers,

View File

@@ -7,6 +7,10 @@
color: #738091; color: #738091;
font-weight: 700; font-weight: 700;
padding: 0 11px; padding: 0 11px;
:global(.bp4-dark) &{
color: var(--color-light-gray1);
}
} }
.table { .table {
@@ -24,6 +28,11 @@
padding-bottom: 8px; padding-bottom: 8px;
color: #738091; color: #738091;
font-weight: 500; font-weight: 500;
:global(.bp4-dark) &{
color: var(--color-light-gray1);
border-top: 1px solid rgba(255, 255, 255, 0.2);
}
} }
} }

View File

@@ -7,6 +7,9 @@
border-top: 1px solid #d9d9da; border-top: 1px solid #d9d9da;
padding: 6px 0; padding: 6px 0;
:global(.bp4-dark) &{
border-top-color: rgba(255, 255, 255, 0.1);
}
&:last-child{ &:last-child{
padding-bottom: 0; padding-bottom: 0;
} }
@@ -31,6 +34,10 @@ table.skippedTable {
tr:hover td{ tr:hover td{
background: #F6F7F9; background: #F6F7F9;
:global(.bp4-dark) &{
background: var(--color-dark-gray2);
}
} }
} }
} }