@@ -192,7 +192,8 @@ func (a *App) handleCreateField(w http.ResponseWriter, r *http.Request) {
192192 CreatedAt : time .Now (),
193193 Status : models .ReportStatusProcessing ,
194194 }
195- if req .AreaHa != nil {
195+ // Create meta if any of its fields are provided
196+ if req .AreaHa != nil || strings .TrimSpace (req .Notes ) != "" || strings .TrimSpace (req .Crop ) != "" {
196197 f .Meta = & models.FieldMeta {AreaHa : req .AreaHa , Notes : req .Notes , Crop : req .Crop }
197198 }
198199
@@ -217,14 +218,17 @@ func (a *App) handleCreateField(w http.ResponseWriter, r *http.Request) {
217218 f .ID = res .InsertedID .(primitive.ObjectID )
218219 _ = json .NewEncoder (w ).Encode (f )
219220
220- if err == nil {
221- // send update to processor
222- a .PostProcessorReports (ctx , processorReportReq {
221+ // send update to processor only if we know the crop/yield type
222+ yt := strings .TrimSpace (req .Crop )
223+ if yt != "" {
224+ if _ , perr := a .PostProcessorReports (ctx , processorReportReq {
223225 FieldID : f .ID .Hex (),
224226 GeoJSON : req .Geometry ,
225- YieldType : req . Crop ,
227+ YieldType : yt ,
226228 Yields : req .Yields ,
227- })
229+ }); perr != nil {
230+ log .Println ("processor error (create)" , perr )
231+ }
228232 }
229233}
230234
@@ -274,7 +278,7 @@ func (a *App) handleGetField(w http.ResponseWriter, r *http.Request) {
274278 _ = json .NewEncoder (w ).Encode (f )
275279}
276280
277- // handleUpdateField updates name/geometry and meta.areaHa if provided.
281+ // handleUpdateField updates name/geometry/yields and meta.areaHa if provided.
278282func (a * App ) handleUpdateField (w http.ResponseWriter , r * http.Request ) {
279283 uid := mustUserID (r )
280284 idStr := chi .URLParam (r , "id" )
@@ -311,6 +315,20 @@ func (a *App) handleUpdateField(w http.ResponseWriter, r *http.Request) {
311315 if req .AreaHa != nil {
312316 set ["meta.areaHa" ] = req .AreaHa // store under nested meta
313317 }
318+ if strings .TrimSpace (req .Notes ) != "" {
319+ set ["meta.notes" ] = req .Notes
320+ }
321+ if strings .TrimSpace (req .Crop ) != "" {
322+ set ["meta.crop" ] = req .Crop
323+ }
324+ if len (req .Yields ) > 0 {
325+ // Convert to models.YieldEntry so correct bson field names are used
326+ ys := make ([]models.YieldEntry , len (req .Yields ))
327+ for i , y := range req .Yields {
328+ ys [i ] = models.YieldEntry {Year : y .Year , ValueTph : y .ValueTph , Unit : y .Unit , Notes : y .Notes }
329+ }
330+ set ["yields" ] = ys
331+ }
314332 if len (set ) == 0 {
315333 http .Error (w , "nothing to update" , http .StatusBadRequest )
316334 return
@@ -333,14 +351,23 @@ func (a *App) handleUpdateField(w http.ResponseWriter, r *http.Request) {
333351 enrichFieldWithLatestReport (ctx , a , & out )
334352 _ = json .NewEncoder (w ).Encode (out )
335353
336- if err == nil {
337- // send update to processor
338- a .PostProcessorReports (ctx , processorReportReq {
354+ // send update to processor only if geometry is provided and we know yieldType
355+ yt2 := strings .TrimSpace (req .Crop )
356+ if yt2 == "" && out .Meta != nil {
357+ yt2 = strings .TrimSpace (out .Meta .Crop )
358+ }
359+ if len (req .Geometry ) > 0 && yt2 != "" {
360+ log .Println ("req" , req )
361+ if _ , perr := a .PostProcessorReports (ctx , processorReportReq {
339362 FieldID : out .ID .Hex (),
340363 GeoJSON : req .Geometry ,
341- YieldType : req . Crop ,
364+ YieldType : yt2 ,
342365 Yields : req .Yields ,
343- })
366+ }); perr != nil {
367+ log .Println ("processor error (update)" , perr )
368+ }
369+ } else {
370+ log .Println ("skip PostProcessorReports: missing geometry or yieldType" )
344371 }
345372}
346373
0 commit comments