farmOS / farmOS.js

A JavaScript library for working with farmOS data structures and interacting with farmOS servers.
MIT License
15 stars 13 forks source link

Creating logs using subrequests give 1/20/1970 time stamp. #87

Open braughtg opened 10 months ago

braughtg commented 10 months ago

Describe the bug When a seeding log is created using a sub-request to set the log_category the timestamp and purchase_date are set to 01/20/1970 instead of the current date.

To Reproduce

The following code uses a farm instance to create a seeding log with the log_category set to seeding_tray, which exists in the taxonomy_term--log_category vocabulary of the farmOS instance being used:

const seeding2 = await farm.log.create({
  type: 'log--seeding',
  status: 'done',
  name: 'TestSeedingWithSubrequest',
});

const options = {
  subrequest: {
    category: {
      $find: {
        type: 'taxonomy_term--log_category',
        name: 'seeding_tray',
      },
      $limit: 1,
      $createIfNotFound: false,
    },
  },
};

const result2 = await farm.log.send(seeding2, options);
console.log(result2);

The output generated is:

[
  {
    id: 'ff30fda2-2371-445d-b995-b73e498fefe1',
    type: 'log--seeding',
    meta: {
      created: '2023-11-20T20:43:03.000Z',
      changed: '2023-11-20T20:43:03.000Z',
      fieldChanges: [Object],
      conflicts: [],
      remote: [Object]
    },
    attributes: {
      name: 'TestSeedingWithSubrequest',
      timestamp: '1970-01-20T16:21:52.000Z',
      status: 'done',
      data: null,
      notes: null,
      flag: [],
      geometry: null,
      is_movement: false,
      lot_number: null,
      purchase_date: '1970-01-20T16:21:52+00:00',
      source: null
    },
    relationships: {
      log_type: [Object],
      revision_user: [Object],
      uid: [Object],
      file: [],
      image: [],
      location: [],
      asset: [],
      category: [],
      quantity: [],
      owner: [Array],
      equipment: []
    }
  }
]

This can be compared to the results when creating a seeding log not using a subrequest:

const seeding1 = await farm.log.create({
  type: 'log--seeding',
  status: 'done',
  name: 'TestSeedingNoSubrequest',
});

const result1 = await farm.log.send(seeding1);
console.log(result1);

Which produces the output:

{
  id: '73083248-f8f8-4452-aa4a-2f044a2edc8e',
  type: 'log--seeding',
  meta: {
    created: '2023-11-20T20:43:03.000Z',
    changed: '2023-11-20T20:43:03.000Z',
    fieldChanges: {
      name: '2023-11-20T20:43:03.000Z',
      timestamp: '2023-11-20T20:43:03.000Z',
      status: '2023-11-20T20:43:03.000Z',
      data: '2023-11-20T20:43:03.000Z',
      notes: '2023-11-20T20:43:03.000Z',
      flag: '2023-11-20T20:43:03.000Z',
      geometry: '2023-11-20T20:43:03.000Z',
      is_movement: '2023-11-20T20:43:03.000Z',
      lot_number: '2023-11-20T20:43:03.000Z',
      purchase_date: '2023-11-20T20:43:03.000Z',
      source: '2023-11-20T20:43:03.000Z',
      log_type: undefined,
      file: undefined,
      image: undefined,
      location: undefined,
      asset: undefined,
      category: undefined,
      quantity: undefined,
      owner: undefined,
      equipment: undefined
    },
    conflicts: [],
    remote: {
      lastSync: '2023-11-20T20:43:03.073Z',
      url: '/log/32',
      meta: [Object]
    }
  },
  attributes: {
    name: 'TestSeedingNoSubrequest',
    timestamp: '2023-11-20T20:43:02.000Z',
    status: 'done',
    data: null,
    notes: null,
    flag: [],
    geometry: null,
    is_movement: false,
    lot_number: null,
    purchase_date: '2023-11-20T20:43:02+00:00',
    source: null
  },
  relationships: {
    log_type: {
      type: 'log_type--log_type',
      id: '360f865c-b85d-4df6-b0cf-2b4bcf6a4f4c',
      meta: [Object]
    },
    revision_user: {
      type: 'user--user',
      id: '7bbe33bd-3292-4a80-a078-36907dddd986',
      meta: [Object]
    },
    uid: {
      type: 'user--user',
      id: '7bbe33bd-3292-4a80-a078-36907dddd986',
      meta: [Object]
    },
    file: [],
    image: [],
    location: [],
    asset: [],
    category: [],
    quantity: [],
    owner: [ [Object] ],
    equipment: []
  }
}

The output in the second case has the correct timestamp as well as quite a bit of additional other information suggesting that these two types of creation are being handled differently.

Expected behavior The log created with the subrequest should have a timestamp of the current date and time.

Screenshots

Here are the two logs created as shown on the farmOS "logs" page:

image

Desktop (please complete the following information):

Additional context The code samples were run in Node 18.17.1