PostgreSQL stores all local application data — customers, products, payment methods, and various lookup tables. The connection is managed by a singleDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/tutosrive/factus_challenge/llms.txt
Use this file to discover all available pages before exploring further.
pg.Pool instance created in src/database.js and exported as the default export db. Every controller that touches the database imports this shared pool, ensuring connection reuse across concurrent requests without manual connection management.
Connection Configuration
The pool is configured entirely through environment variables. Theclient_encoding is hardcoded to utf8 to guarantee correct character handling for Spanish-language strings (names, addresses, observations).
src/database.js
Environment Variables
| Variable | Description |
|---|---|
DB_USER | PostgreSQL username |
DB_HOST | Hostname or IP of the PostgreSQL server |
DB_NAME | Name of the target database |
DB_PASSWORD | Password for DB_USER |
DB_SSL | Set to true to enable SSL/TLS for the connection |
Tables
The application reads and writes the following tables via the generic query routes (/get-data/:table, /add-data/:table, etc.):
customer
Invoice recipients. Stores identification details, names, address, and foreign keys to lookup tables (
municipality_id, type_id, id_org, tribute_id).products
Product and service catalog. Includes
code_reference, name, price, tax_rate, discount_rate, unit_measure_id, standard_code_id, tribute_id, and the is_excluded flag.payment_method
Payment method lookup table used when creating an invoice. Fields:
id, name.municipality
City/municipality lookup. Fields:
id, code, name. Referenced by the customer table.identification_document
Document type lookup (e.g., NIT, CC). Fields:
id, name. Used to identify customers.legal_organization
Legal organization type lookup (e.g., natural person, legal entity). Fields:
id, name.customer_tribute
Tax regime lookup. Fields:
id, name. Maps the tax treatment applicable to a customer.Helper Functions
src/helpers/helpers.mjs exports several utility functions used by the query controllers to build SQL statements dynamically from arbitrary request bodies, avoiding hardcoded column lists.
generate_str_of_dict(dict, key_value, restrict)
Builds a SQL fragment from a plain JavaScript object. When key_value is true it produces a SET clause for UPDATE statements (col = value, ...). When false it produces a comma-separated values list for INSERT statements.
The restrict parameter names a key that should be excluded from the output (typically the primary key id).
get_keys_dict(dict, restrict)
Returns an array of the keys of an object, optionally excluding one key. Used to build the column list for INSERT and UPDATE statements.
rd_key(limit)
Generates a random alphanumeric key of limit characters by alternating uppercase ASCII letters (A–Y) and single digits. Used to produce unique reference_code values for invoices (length 10) and code_reference values for line items (length 5).
validate_body(body)
Validates the request body of a POST /factura request before it is forwarded to the Factus API. Returns a validation result object with an ok boolean and, on failure, arrays of missing_properties and require conflicts.
Required fields and types:
| Field | Type | Additional constraint |
|---|---|---|
observation | string | Length must be ≤ 249 characters |
payment_method_code | number | — |
customer | object | — |
items | object / array | Each item receives an auto-generated code_reference |
body.numbering_range_id = 8body.reference_code— a 10-character random key generated byrd_key(10)
src/helpers/helpers.mjs — validate_body
