From a17e01793e780c0dbc256720ba384f44f2b5841b Mon Sep 17 00:00:00 2001 From: mic704b Date: Sun, 15 Mar 2020 23:07:01 +1100 Subject: [PATCH] All: Fix format of note timestamps (#2672) * Correct format of timestamps during unserialization. * Add tests. --- CliClient/tests/models_BaseItem.js | 12 ++++++++++++ ReactNativeClient/lib/models/BaseItem.js | 8 ++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/CliClient/tests/models_BaseItem.js b/CliClient/tests/models_BaseItem.js index cd0181b8c..205f1be97 100644 --- a/CliClient/tests/models_BaseItem.js +++ b/CliClient/tests/models_BaseItem.js @@ -57,4 +57,16 @@ describe('models_BaseItem', function() { expect(unserialized2.title).toBe(folder2.title); })); + it('should correctly unserialize note timestamps', asyncTest(async () => { + let folder = await Folder.save({ title: 'folder' }); + let note = await Note.save({ title: 'note', parent_id: folder.id }); + + let serialized = await Note.serialize(note); + let unserialized = await Note.unserialize(serialized); + + expect(unserialized.created_time).toEqual(note.created_time); + expect(unserialized.updated_time).toEqual(note.updated_time); + expect(unserialized.user_created_time).toEqual(note.user_created_time); + expect(unserialized.user_updated_time).toEqual(note.user_updated_time); + })); }); diff --git a/ReactNativeClient/lib/models/BaseItem.js b/ReactNativeClient/lib/models/BaseItem.js index e2c830ab1..a196bfa5c 100644 --- a/ReactNativeClient/lib/models/BaseItem.js +++ b/ReactNativeClient/lib/models/BaseItem.js @@ -254,13 +254,13 @@ class BaseItem extends BaseModel { const ItemClass = this.itemClass(type); - if (['created_time', 'updated_time', 'user_created_time', 'user_updated_time'].indexOf(propName) >= 0) { - if (!propValue) return 0; - propValue = moment(propValue, 'YYYY-MM-DDTHH:mm:ss.SSSZ').format('x'); - } else if (['title_diff', 'body_diff'].indexOf(propName) >= 0) { + if (['title_diff', 'body_diff'].indexOf(propName) >= 0) { if (!propValue) return ''; propValue = JSON.parse(propValue); } else { + if (['created_time', 'updated_time', 'user_created_time', 'user_updated_time'].indexOf(propName) >= 0) { + propValue = (!propValue) ? '0' : moment(propValue, 'YYYY-MM-DDTHH:mm:ss.SSSZ').format('x'); + } propValue = Database.formatValue(ItemClass.fieldType(propName), propValue); }