Files
evidencija-rezija/app/lib/actions/monthActions.ts
Knee Cola 7a5c503ce9 Refactor: Complete barcodeImage removal and hub3aText migration
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>
2025-11-23 09:03:53 +01:00

82 lines
2.6 KiB
TypeScript

'use server';
import { getDbClient } from '../dbClient';
import { ObjectId } from 'mongodb';
import { Bill, BillingLocation, YearMonth } from '../db-types';
import { AuthenticatedUser } from '../types/next-auth';
import { withUser } from '../auth';
import { unstable_noStore as noStore } from 'next/cache';
/**
* Server-side action which adds a new month to the database
* @param locationId location of the bill
* @param billId ID of the bill
* @param prevState previous state of the form
* @param formData form data
* @returns
*/
export const addMonth = withUser(async (user:AuthenticatedUser, { year, month }: YearMonth) => {
noStore();
const { id: userId } = user;
// update the bill in the mongodb
const dbClient = await getDbClient();
const prevYear = month === 1 ? year - 1 : year;
const prevMonth = month === 1 ? 12 : month - 1;
// find all locations for the previous month
const prevMonthLocations = await dbClient.collection<BillingLocation>("lokacije").find({
userId, // make sure that the locations belongs to the user
yearMonth: {
year: prevYear,
month: prevMonth,
}
});
const newMonthLocationsCursor = prevMonthLocations.map((prevLocation) => {
return({
// copy all the properties from the previous location
...prevLocation,
// assign a new ID
_id: (new ObjectId()).toHexString(),
yearMonth: {
year: year,
month: month,
},
// copy bill array, but set all bills to unpaid and remove attachments and notes
bills: prevLocation.bills.map((bill) => {
return {
...bill,
paid: false,
attachment: null,
notes: null,
payedAmount: null,
hub3aText: undefined,
} as Bill
})
} as BillingLocation);
});
const newMonthLocations = await newMonthLocationsCursor.toArray()
await dbClient.collection<BillingLocation>("lokacije").insertMany(newMonthLocations);
});
export const fetchAvailableYears = withUser(async (user:AuthenticatedUser) => {
noStore();
const { id: userId } = user;
const dbClient = await getDbClient();
// query mnogodb for all `yearMonth` values
const years:number[] = await dbClient.collection<BillingLocation>("lokacije")
.distinct("yearMonth.year", { userId })
// sort the years in descending order
const sortedYears = years.sort((a, b) => b - a);
return(sortedYears);
})