Quality-of-life enhancements: CSV import/export, opening balances, interest rates, mobile UX
- CSV import/export for Units, Projects, and Vendors with match-on-name/number upsert - Cash Flow report toggle for Cash Only vs Cash + Investments - Per-account and bulk opening balance setting with as-of date - Interest rate field on normal accounts with estimated monthly/annual interest display - Mobile sidebar auto-close on navigation - Shared CSV parsing/export utility extracted to frontend/src/utils/csv.ts DB migration needed for existing tenants: ALTER TABLE accounts ADD COLUMN IF NOT EXISTS interest_rate DECIMAL(6,4); Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { Controller, Get, Post, Put, Delete, Body, Param, UseGuards } from '@nestjs/common';
|
||||
import { Controller, Get, Post, Put, Delete, Body, Param, Res, UseGuards } from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { Response } from 'express';
|
||||
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
|
||||
import { UnitsService } from './units.service';
|
||||
|
||||
@@ -13,9 +14,19 @@ export class UnitsController {
|
||||
@Get()
|
||||
findAll() { return this.unitsService.findAll(); }
|
||||
|
||||
@Get('export')
|
||||
async exportCSV(@Res() res: Response) {
|
||||
const csv = await this.unitsService.exportCSV();
|
||||
res.set({ 'Content-Type': 'text/csv', 'Content-Disposition': 'attachment; filename="units.csv"' });
|
||||
res.send(csv);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) { return this.unitsService.findOne(id); }
|
||||
|
||||
@Post('import')
|
||||
importCSV(@Body() rows: any[]) { return this.unitsService.importCSV(rows); }
|
||||
|
||||
@Post()
|
||||
create(@Body() dto: any) { return this.unitsService.create(dto); }
|
||||
|
||||
|
||||
@@ -73,6 +73,90 @@ export class UnitsService {
|
||||
return rows[0];
|
||||
}
|
||||
|
||||
async exportCSV(): Promise<string> {
|
||||
const rows = await this.tenant.query(
|
||||
`SELECT unit_number, address_line1, city, state, zip_code, square_footage, lot_size,
|
||||
owner_name, owner_email, owner_phone, is_rented, monthly_assessment, status
|
||||
FROM units WHERE status != 'inactive' ORDER BY unit_number`,
|
||||
);
|
||||
const headers = ['unit_number', 'address_line1', 'city', 'state', 'zip_code', 'square_footage', 'lot_size',
|
||||
'owner_name', 'owner_email', 'owner_phone', 'is_rented', 'monthly_assessment', 'status'];
|
||||
const lines = [headers.join(',')];
|
||||
for (const r of rows) {
|
||||
lines.push(headers.map((h) => {
|
||||
const v = r[h] ?? '';
|
||||
const s = String(v);
|
||||
return s.includes(',') || s.includes('"') ? `"${s.replace(/"/g, '""')}"` : s;
|
||||
}).join(','));
|
||||
}
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
async importCSV(rows: any[]) {
|
||||
let created = 0, updated = 0;
|
||||
const errors: string[] = [];
|
||||
|
||||
// Resolve default assessment group for new units
|
||||
let defaultGroupId: string | null = null;
|
||||
const defaultGroup = await this.tenant.query(
|
||||
'SELECT id FROM assessment_groups WHERE is_default = true AND is_active = true LIMIT 1',
|
||||
);
|
||||
if (defaultGroup.length) defaultGroupId = defaultGroup[0].id;
|
||||
else {
|
||||
const anyGroup = await this.tenant.query('SELECT id FROM assessment_groups WHERE is_active = true LIMIT 1');
|
||||
if (anyGroup.length) defaultGroupId = anyGroup[0].id;
|
||||
}
|
||||
|
||||
for (let i = 0; i < rows.length; i++) {
|
||||
const row = rows[i];
|
||||
const unitNumber = (row.unit_number || '').trim();
|
||||
if (!unitNumber) { errors.push(`Row ${i + 1}: missing unit_number`); continue; }
|
||||
|
||||
try {
|
||||
const existing = await this.tenant.query('SELECT id FROM units WHERE unit_number = $1', [unitNumber]);
|
||||
if (existing.length) {
|
||||
await this.tenant.query(
|
||||
`UPDATE units SET
|
||||
address_line1 = COALESCE(NULLIF($2, ''), address_line1),
|
||||
city = COALESCE(NULLIF($3, ''), city),
|
||||
state = COALESCE(NULLIF($4, ''), state),
|
||||
zip_code = COALESCE(NULLIF($5, ''), zip_code),
|
||||
square_footage = COALESCE(NULLIF($6, '')::integer, square_footage),
|
||||
lot_size = COALESCE(NULLIF($7, '')::decimal, lot_size),
|
||||
owner_name = COALESCE(NULLIF($8, ''), owner_name),
|
||||
owner_email = COALESCE(NULLIF($9, ''), owner_email),
|
||||
owner_phone = COALESCE(NULLIF($10, ''), owner_phone),
|
||||
is_rented = COALESCE(NULLIF($11, '')::boolean, is_rented),
|
||||
monthly_assessment = COALESCE(NULLIF($12, '')::decimal, monthly_assessment),
|
||||
status = COALESCE(NULLIF($13, ''), status),
|
||||
updated_at = NOW()
|
||||
WHERE id = $1`,
|
||||
[existing[0].id, row.address_line1, row.city, row.state, row.zip_code,
|
||||
row.square_footage, row.lot_size, row.owner_name, row.owner_email,
|
||||
row.owner_phone, row.is_rented, row.monthly_assessment, row.status],
|
||||
);
|
||||
updated++;
|
||||
} else {
|
||||
if (!defaultGroupId) { errors.push(`Row ${i + 1}: no assessment group available for new unit`); continue; }
|
||||
await this.tenant.query(
|
||||
`INSERT INTO units (unit_number, address_line1, city, state, zip_code, square_footage, lot_size,
|
||||
owner_name, owner_email, owner_phone, is_rented, monthly_assessment, status, assessment_group_id)
|
||||
VALUES ($1, $2, $3, $4, $5, NULLIF($6, '')::integer, NULLIF($7, '')::decimal,
|
||||
$8, $9, $10, COALESCE(NULLIF($11, '')::boolean, false), COALESCE(NULLIF($12, '')::decimal, 0), COALESCE(NULLIF($13, ''), 'active'), $14)`,
|
||||
[unitNumber, row.address_line1 || null, row.city || null, row.state || null, row.zip_code || null,
|
||||
row.square_footage || '', row.lot_size || '', row.owner_name || null, row.owner_email || null,
|
||||
row.owner_phone || null, row.is_rented || '', row.monthly_assessment || '', row.status || '', defaultGroupId],
|
||||
);
|
||||
created++;
|
||||
}
|
||||
} catch (err: any) {
|
||||
errors.push(`Row ${i + 1} (${unitNumber}): ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
return { imported: created + updated, created, updated, errors };
|
||||
}
|
||||
|
||||
async delete(id: string) {
|
||||
await this.findOne(id);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user