Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions arbitrage-tracker/src/app/inventory/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
calculatePremiumPaidPercent,
calculateProfitMarginPercent
} from '../../lib/calculations'; // Import calculation functions
import { Timestamp } from 'firebase/firestore'; // Import Timestamp

// Sample data for preloading
const samplePreloadedCoins: PreloadedCoinData[] = [
Expand Down Expand Up @@ -171,7 +172,7 @@ function InventoryPage() {
setRefreshKey(prevKey => prevKey + 1);
};

const formatDateForCSV = (timestamp: any): string => {
const formatDateForCSV = (timestamp: Timestamp | Date | undefined | null): string => {
if (!timestamp) return '';
if (timestamp instanceof Date) {
return timestamp.toISOString().split('T')[0];
Expand All @@ -182,7 +183,7 @@ function InventoryPage() {
return ''; // Fallback for unexpected format
};

const escapeCSVField = (field: any): string => {
const escapeCSVField = (field: unknown): string => {
if (field === null || field === undefined) return ''; // Return empty string for null/undefined
const stringField = String(field);
// Basic CSV escaping: if field contains comma, newline or double quote, wrap in double quotes
Expand Down
33 changes: 27 additions & 6 deletions arbitrage-tracker/src/lib/firestoreService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,19 +263,40 @@ export const updateCoin = async (
throw new Error('Coin ID is required to update a coin.');
}
const coinDocRef = doc(db, 'coins', coinId);
const updateData: any = {
// Use a more specific type if possible, combining Partial<Coin> and FieldValue for timestamps
// For a general approach to avoid 'any' here:
const updatePayload: Record<string, unknown> = {
...coinData,
updatedAt: serverTimestamp() as Timestamp,
updatedAt: serverTimestamp(), // serverTimestamp() is already a FieldValue, no need to cast to Timestamp
};

// If purchaseDate is being updated and is a string, convert it
if (coinData.purchaseDate && !(coinData.purchaseDate instanceof Timestamp)) {
updateData.purchaseDate = Timestamp.fromDate(new Date(coinData.purchaseDate as any));
// The `coinData.purchaseDate` should ideally be a Timestamp if it's part of the `Coin` type.
// If it can also be a string date passed to this function, the `Coin` type or this function's param type should reflect that.
// Assuming `coinData.purchaseDate` is `Timestamp | undefined` as per `Coin` type:
// The following block might be for cases where `coinData` comes from a less strictly typed source.
if (coinData.purchaseDate) {
if (!(coinData.purchaseDate instanceof Timestamp)) {
// This case implies coinData.purchaseDate is not conforming to Coin['purchaseDate'] type here.
// This could be an old string date from a form that wasn't converted.
console.warn("updateCoin received a purchaseDate that is not a Firestore Timestamp. Attempting conversion.");
try {
// Attempt conversion from string or Date object
updatePayload.purchaseDate = Timestamp.fromDate(new Date(coinData.purchaseDate as string | Date));
} catch (dateError) {
console.error("Failed to convert purchaseDate to Timestamp in updateCoin:", dateError);
// Decide how to handle: throw error, or remove from payload?
// For now, let's remove it to prevent a bad update.
delete updatePayload.purchaseDate;
}
} else {
// It's already a Timestamp, ensure it's in the payload if it was in coinData.
updatePayload.purchaseDate = coinData.purchaseDate;
}
}


try {
await updateDoc(coinDocRef, updateData);
await updateDoc(coinDocRef, updatePayload);
} catch (error) {
console.error('Error updating coin in Firestore: ', error);
throw error;
Expand Down