seed stuff
This commit is contained in:
@@ -989,6 +989,7 @@ router.get('/logs', async (req, res, next) => {
|
||||
averageSpeed: parseFloat(log.average_speed),
|
||||
areaCovered: parseFloat(log.area_covered),
|
||||
notes: log.notes,
|
||||
seedingType: log.seeding_type,
|
||||
sectionName: log.section_name,
|
||||
sectionArea: parseFloat(log.section_area),
|
||||
propertyName: log.property_name,
|
||||
@@ -1020,7 +1021,8 @@ router.post('/logs', validateRequest(applicationLogSchema), async (req, res, nex
|
||||
averageSpeed,
|
||||
areaCovered,
|
||||
notes,
|
||||
products
|
||||
products,
|
||||
seedingType
|
||||
} = req.body;
|
||||
|
||||
// Start transaction
|
||||
@@ -1083,6 +1085,8 @@ router.post('/logs', validateRequest(applicationLogSchema), async (req, res, nex
|
||||
|
||||
// Add products to log
|
||||
console.log('Adding products to log:', products);
|
||||
// Track seed product names for lawn section update
|
||||
const seedProductNames = [];
|
||||
for (const product of products) {
|
||||
const {
|
||||
productId,
|
||||
@@ -1109,6 +1113,17 @@ router.post('/logs', validateRequest(applicationLogSchema), async (req, res, nex
|
||||
);
|
||||
|
||||
console.log('Product inserted successfully');
|
||||
|
||||
// Determine if this product is of type 'seed' and capture its name
|
||||
try {
|
||||
if (productId) {
|
||||
const pr = await client.query('SELECT name, product_type FROM products WHERE id=$1', [productId]);
|
||||
if (pr.rows[0]?.product_type === 'seed') seedProductNames.push(pr.rows[0].name);
|
||||
} else if (userProductId) {
|
||||
const upr = await client.query('SELECT custom_name, custom_product_type FROM user_products WHERE id=$1 AND user_id=$2', [userProductId, req.user.id]);
|
||||
if (upr.rows[0]?.custom_product_type === 'seed') seedProductNames.push(upr.rows[0].custom_name);
|
||||
}
|
||||
} catch (e) { console.warn('Seed product detection failed', e.message); }
|
||||
}
|
||||
|
||||
// If this was from a plan, mark the plan as completed
|
||||
@@ -1119,6 +1134,15 @@ router.post('/logs', validateRequest(applicationLogSchema), async (req, res, nex
|
||||
);
|
||||
}
|
||||
|
||||
// If seed was applied, update the lawn section grass type to reflect seeds
|
||||
if (seedProductNames.length > 0) {
|
||||
const blendLabel = seedProductNames.join(' + ');
|
||||
const suffix = seedingType === 'new_seed' ? ' (New Seed)' : (seedingType === 'overseed' ? ' (Overseed)' : '');
|
||||
try {
|
||||
await client.query('UPDATE lawn_sections SET grass_type=$1, updated_at=CURRENT_TIMESTAMP WHERE id=$2', [blendLabel + suffix, lawnSectionId]);
|
||||
} catch (e) { console.warn('Failed to update grass_type for section', lawnSectionId, e.message); }
|
||||
}
|
||||
|
||||
await client.query('COMMIT');
|
||||
|
||||
res.status(201).json({
|
||||
|
||||
Reference in New Issue
Block a user