feat: Improve preparation method display and filtering

This commit is contained in:
2026-06-07 00:31:38 +05:30
parent 012db524cb
commit 681f892d63
8 changed files with 452 additions and 63 deletions

View File

@@ -50,9 +50,15 @@ const initDb = async () => {
rating INTEGER,
created_at BIGINT,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
is_deleted BOOLEAN DEFAULT FALSE
is_deleted BOOLEAN DEFAULT FALSE,
recipe_data JSONB DEFAULT '{}'
)
`);
// Ensure the recipe_data column exists on older db setups
await pool.query(`
ALTER TABLE brew_logs ADD COLUMN IF NOT EXISTS recipe_data JSONB DEFAULT '{}'
`);
console.log('Database initialized');
} catch (err) {
console.error('Error initializing database', err);

View File

@@ -191,9 +191,16 @@ app.post('/api/sync', authenticateToken, async (req, res) => {
// 2. Process incoming brew logs
for (const log of brewLogs) {
const { id, beanId, method, notes, rating, createdAt, updatedAt, isDeleted, ...recipeFields } = log;
const grind = log.grindSize || log.grind || '';
const waterTemp = log.waterTemp || '';
const ratio = log.brewRatio || log.ratio || '';
const yieldVal = log.yield || '';
const time = log.brewTime || log.time || '';
await client.query(`
INSERT INTO brew_logs (id, user_id, bean_id, method, grind, water_temp, ratio, yield, time, notes, rating, created_at, updated_at, is_deleted)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
INSERT INTO brew_logs (id, user_id, bean_id, method, grind, water_temp, ratio, yield, time, notes, rating, created_at, updated_at, is_deleted, recipe_data)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)
ON CONFLICT (id) DO UPDATE SET
bean_id = EXCLUDED.bean_id,
method = EXCLUDED.method,
@@ -206,24 +213,26 @@ app.post('/api/sync', authenticateToken, async (req, res) => {
rating = EXCLUDED.rating,
created_at = EXCLUDED.created_at,
updated_at = EXCLUDED.updated_at,
is_deleted = EXCLUDED.is_deleted
is_deleted = EXCLUDED.is_deleted,
recipe_data = EXCLUDED.recipe_data
WHERE (EXCLUDED.updated_at > brew_logs.updated_at OR brew_logs.user_id IS NULL)
AND (brew_logs.user_id = EXCLUDED.user_id OR brew_logs.user_id IS NULL)
`, [
log.id,
id,
userId,
log.beanId,
log.method || '',
log.grind || '',
log.waterTemp || '',
log.ratio || '',
log.yield || '',
log.time || '',
log.notes || '',
log.rating || 0,
log.createdAt ? BigInt(log.createdAt) : BigInt(Date.now()),
log.updatedAt ? new Date(log.updatedAt) : new Date(),
log.isDeleted || false
beanId,
method || '',
grind,
waterTemp,
ratio,
yieldVal,
time,
notes || '',
rating || 0,
createdAt ? BigInt(createdAt) : BigInt(Date.now()),
updatedAt ? new Date(updatedAt) : new Date(),
isDeleted || false,
JSON.stringify(recipeFields)
]);
}
@@ -256,21 +265,34 @@ app.post('/api/sync', authenticateToken, async (req, res) => {
isDeleted: b.is_deleted
}));
const mappedLogs = serverLogs.rows.map(l => ({
id: l.id,
beanId: l.bean_id,
method: l.method,
grind: l.grind,
waterTemp: l.water_temp,
ratio: l.ratio,
yield: l.yield,
time: l.time,
notes: l.notes,
rating: l.rating,
createdAt: Number(l.created_at),
updatedAt: l.updated_at.toISOString(),
isDeleted: l.is_deleted
}));
const mappedLogs = serverLogs.rows.map(l => {
const baseLog = {
id: l.id,
beanId: l.bean_id,
method: l.method,
notes: l.notes,
rating: l.rating,
createdAt: Number(l.created_at),
updatedAt: l.updated_at.toISOString(),
isDeleted: l.is_deleted
};
const recipeData = l.recipe_data || {};
// Fallback for old database rows that don't have recipe_data populated
const fallback = {};
if (!recipeData.grindSize && l.grind) fallback.grindSize = l.grind;
if (!recipeData.waterTemp && l.water_temp) fallback.waterTemp = l.water_temp;
if (!recipeData.brewRatio && l.ratio) fallback.brewRatio = l.ratio;
if (!recipeData.yield && l.yield) fallback.yield = l.yield;
if (!recipeData.brewTime && l.time) fallback.brewTime = l.time;
return {
...baseLog,
...fallback,
...recipeData
};
});
res.json({
serverTime,