This commit completes the migration from storing bitmap barcodes to using decoded HUB-3A text strings, removing all legacy code while maintaining backward compatibility during the transition period. Database & Server Actions: - billActions: Removed commented legacy barcodeImage code - locationActions: Updated field references in projections - monthActions: Use hub3aText when copying bills to new months - printActions: Support both hub3aText and barcodeImage during migration - Added @deprecated annotation to barcodeImage field - Filter includes bills with either field - Pass both fields to support gradual migration Barcode Decoder: - Removed barcodeImage field from DecodeResult type - Deleted copyBarcodeImage() function (58 lines) - No longer generating bitmaps during decode - Barcodes now generated on-demand from hub3aText - Cleaner separation: decoder extracts text, component renders barcode UI Components: - Pdf417Barcode: Added optional className prop for styling flexibility - Removed unnecessary wrapper div - Conditional styling (use className or default dimensions) - PrintPreview: Use Pdf417Barcode component with fallback to legacy barcodeImage - ViewBillCard: Major cleanup and migration support - Removed unused imports (React, updateOrAddBill, useLocale) - Removed unused middleware function - Removed unused variables and hidden input - Prefer hub3aText with Pdf417Barcode, fallback to barcodeImage - Clear legacy support comments Migration Strategy: All rendering code now follows the pattern: 1. Prefer hub3aText (new field) when available 2. Fallback to barcodeImage (legacy field) if needed 3. Clear comments marking legacy support code 4. Allows gradual migration without breaking existing bills Benefits: - More efficient storage (text vs base64 bitmap) - Barcodes generated on-demand (not stored) - Cleaner, more maintainable code - Consistent use of Pdf417Barcode component - Removed ~60 lines of unused code 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
74 lines
2.5 KiB
TypeScript
74 lines
2.5 KiB
TypeScript
'use server';
|
|
|
|
import { getDbClient } from '../dbClient';
|
|
import { BillingLocation } from '../db-types';
|
|
import { AuthenticatedUser } from '../types/next-auth';
|
|
import { withUser } from '../auth';
|
|
import { unstable_noStore as noStore } from 'next/cache';
|
|
|
|
export interface PrintBarcodeData {
|
|
locationName: string;
|
|
billName: string;
|
|
/**
|
|
* LEGACY SUPPORT ... untill all bills have been migrated
|
|
* @deprecated Use `hub3aText` instead.
|
|
*/
|
|
barcodeImage?: string;
|
|
hub3aText?: string;
|
|
payedAmount?: number | null;
|
|
}
|
|
|
|
/**
|
|
* Fetches all bills with barcode images for a specific month for printing
|
|
* @param year - Year to fetch data for
|
|
* @param month - Month to fetch data for (1-12)
|
|
* @returns Array of barcode data for printing
|
|
*/
|
|
export const fetchBarcodeDataForPrint = withUser(async (user: AuthenticatedUser, year: number, month: number): Promise<PrintBarcodeData[]> => {
|
|
noStore();
|
|
|
|
const { id: userId } = user;
|
|
const dbClient = await getDbClient();
|
|
|
|
const yearMonth = `${year}-${month.toString().padStart(2, '0')}`;
|
|
|
|
// Fetch all locations for the specific month
|
|
const locations = await dbClient.collection<BillingLocation>("lokacije")
|
|
.find({
|
|
userId, // ensure data belongs to authenticated user
|
|
"yearMonth.year": year,
|
|
"yearMonth.month": month
|
|
})
|
|
.toArray();
|
|
|
|
// Extract and flatten barcode data
|
|
const printData: PrintBarcodeData[] = [];
|
|
|
|
for (const location of locations) {
|
|
for (const bill of location.bills) {
|
|
// Only include bills that have barcode images
|
|
if ( ( bill.hub3aText && bill.hub3aText.trim() !== "") ||
|
|
// LEGACY SUPPORT ... untill all bills have been migrated
|
|
(bill.barcodeImage && bill.barcodeImage.trim() !== "")
|
|
) {
|
|
printData.push({
|
|
locationName: location.name,
|
|
billName: bill.name,
|
|
barcodeImage: bill.barcodeImage, // LEGACY SUPPORT ... untill all bills have been migrated
|
|
hub3aText: bill.hub3aText,
|
|
payedAmount: bill.payedAmount
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
// Sort by location name, then by bill name for consistent ordering
|
|
printData.sort((a, b) => {
|
|
if (a.locationName !== b.locationName) {
|
|
return a.locationName.localeCompare(b.locationName);
|
|
}
|
|
return a.billName.localeCompare(b.billName);
|
|
});
|
|
|
|
return printData;
|
|
}); |