/** * Financial Plugin Test Helper * * Provides utilities for testing the financial plugin endpoints * Uses REAL PostgreSQL database - NO mocks */ const path = require('path'); const fs = require('fs'); /** * Create test fixtures for financial plugin testing */ class FinancialTestHelper { constructor() { this.siteId = null; this.unitId = null; this.userId = null; } /** * Create a test site */ async createTestSite() { const { siteRepository } = require('../../../src/repositories'); const siteData = { name: `Test Site ${Date.now()}`, address: '123 Test Street', email: `test-${Date.now()}@example.com`, owner_name: 'Test Owner', owner_email: `owner-${Date.now()}@example.com`, phone: '+1234567890', plan_id: null, subscription_type: 'monthly', is_active: true, operational_mode: 'dev' }; const site = await siteRepository.createSite(siteData); this.siteId = site.id; return site; } /** * Create a test building */ async createTestBuilding(siteId) { const { buildingRepository } = require('../../../src/repositories'); const buildingData = { site_id: siteId, name: 'Test Building', address: '123 Test Avenue', total_floors: 5, total_units: 10 }; const building = await buildingRepository.createBuilding(buildingData); return building; } /** * Create a test unit */ async createTestUnit(siteId, buildingId) { const { unitsRepository } = require('../../../src/repositories'); const unitData = { site_id: siteId, building_id: buildingId, unit_number: `U${Date.now()}`, type: 'residential', size: 100, monthly_service_fee: 500, location: 'Floor 3' }; const unit = await unitsRepository.createUnit(unitData); this.unitId = unit.id; return unit; } /** * Create a test user for authentication */ async createTestUser(siteId) { const { userRepository } = require('../../../src/repositories'); const bcrypt = require('bcryptjs'); const passwordHash = await bcrypt.hash('test123', 10); const userData = { email: `user-${Date.now()}@test.com`, name: 'Test User', role: 'hoa_admin', site_id: siteId, is_active: true, password_hash: passwordHash }; const user = await userRepository.createUser(userData); this.userId = user.id; return user; } /** * Create Chart of Accounts entries for a site */ async createTestAccounts(siteId) { const { chartOfAccountsRepository } = require('../../repositories'); const accounts = [ { site_id: siteId, account_code: '1000', account_name: 'Cash', account_type: 'asset', description: 'Cash on hand', is_active: true, created_by: this.userId || 'test-user' }, { site_id: siteId, account_code: '1100', account_name: 'Accounts Receivable', account_type: 'asset', description: 'Amounts owed by residents', is_active: true, created_by: this.userId || 'test-user' }, { site_id: siteId, account_code: '2000', account_name: 'Accounts Payable', account_type: 'liability', description: 'Amounts owed to vendors', is_active: true, created_by: this.userId || 'test-user' } ]; const createdAccounts = []; for (const account of accounts) { const created = await chartOfAccountsRepository.create(account); createdAccounts.push(created); } return createdAccounts; } /** * Create unit balance record */ async createTestUnitBalance(unitId, siteId, startingBalance = 1000) { const { unitBalanceRepository } = require('../../repositories'); const balanceData = { unit_id: unitId, site_id: siteId, starting_balance: startingBalance, current_balance: startingBalance, created_by: this.userId || 'test-user' }; return await unitBalanceRepository.create(balanceData); } /** * Create test invoice */ async createTestInvoice(unitId, siteId, amount = 500) { const { invoiceRepository } = require('../../repositories'); const today = new Date(); const nextMonth = new Date(today.getFullYear(), today.getMonth() + 1, today.getDate()); const invoiceData = { site_id: siteId, unit_id: unitId, invoice_number: `INV-${Date.now()}`, invoice_type: 'monthly_fee', description: 'Monthly maintenance fee', amount: amount, due_date: nextMonth.toISOString().split('T')[0], issue_date: today.toISOString().split('T')[0], status: 'pending', created_by: this.userId || 'test-user' }; return await invoiceRepository.create(invoiceData); } /** * Create test payment */ async createTestPayment(unitId, siteId, amount = 300, invoiceId = null) { const { paymentRepository } = require('../../repositories'); const paymentData = { site_id: siteId, unit_id: unitId, invoice_id: invoiceId, payment_amount: amount, payment_method: 'cash', payment_reference: `PAY-${Date.now()}`, created_by: this.userId || 'test-user' }; return await paymentRepository.create(paymentData); } /** * Cleanup test data */ async cleanup() { // Cleanup will be handled by the schema manager // This method is here for future use if needed } /** * Get mock request object for testing */ getMockRequest(siteId, userId) { return { user: { id: userId, site_id: siteId, role: 'hoa_admin' }, query: { site_id: siteId }, body: {} }; } /** * Get mock response object for testing */ getMockResponse() { const res = { status: jest.fn().mockReturnThis(), json: jest.fn().mockReturnThis(), send: jest.fn().mockReturnThis() }; return res; } } module.exports = FinancialTestHelper;