Keep earliest Date Added when merging items

Closes #1669
This commit is contained in:
Dan Stillman 2019-04-02 14:01:25 -04:00
parent 197d609f8e
commit 9ed48f3837
3 changed files with 23 additions and 3 deletions

View file

@ -1250,6 +1250,11 @@ Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) {
env.sqlValues.push({ int: itemTypeID });
}
if (isNew || (this._changed.primaryData && this._changed.primaryData.dateAdded)) {
env.sqlColumns.push('dateAdded');
env.sqlValues.push(this.dateAdded ? this.dateAdded : Zotero.DB.transactionDateTime);
}
// If a new item and Date Modified hasn't been provided, or an existing item and
// Date Modified hasn't changed from its previous value and skipDateModifiedUpdate wasn't
// passed, use the current timestamp
@ -1269,9 +1274,6 @@ Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) {
if (env.sqlColumns.length) {
if (isNew) {
env.sqlColumns.push('dateAdded');
env.sqlValues.push(this.dateAdded ? this.dateAdded : Zotero.DB.transactionDateTime);
env.sqlColumns.unshift('itemID');
env.sqlValues.unshift(parseInt(itemID));

View file

@ -759,7 +759,14 @@ Zotero.Items = function() {
var toSave = {};
toSave[item.id] = item;
var earliestDateAdded = item.dateAdded;
for (let otherItem of otherItems) {
// Use the earliest date added of all the items
if (otherItem.dateAdded < earliestDateAdded) {
earliestDateAdded = otherItem.dateAdded;
}
let otherItemURI = Zotero.URI.getItemURI(otherItem);
// Move child items to master
@ -824,6 +831,8 @@ Zotero.Items = function() {
yield otherItem.save();
}
item.setField('dateAdded', earliestDateAdded);
for (let i in toSave) {
yield toSave[i].save();
}

View file

@ -191,6 +191,15 @@ describe("Zotero.Items", function () {
assert.sameMembers(rels, [item2URI, item3URI]);
})
it("should use the earliest Date Added", async function () {
var item1 = await createDataObject('item', { dateAdded: '2019-01-02 00:00:00' });
var item2 = await createDataObject('item', { dateAdded: '2019-01-01 00:00:00' });
var item3 = await createDataObject('item', { dateAdded: '2019-01-03 00:00:00' });
await Zotero.Items.merge(item1, [item2, item3]);
assert.equal(item1.dateAdded, '2019-01-01 00:00:00');
});
it("should merge two items when servant is linked to an item absent from cache", function* () {
// two group libraries
var groupOneInfo = yield createGroup({