(Model, id, current, change, conflicts, options, cb)
| 1579 | } |
| 1580 | |
| 1581 | function applyDelete(Model, id, current, change, conflicts, options, cb) { |
| 1582 | if (!current) { |
| 1583 | // The instance was either already deleted or not created at all, |
| 1584 | // we are done. |
| 1585 | return cb(); |
| 1586 | } |
| 1587 | |
| 1588 | const Change = Model.getChangeModel(); |
| 1589 | const rev = Change.revisionForInst(current); |
| 1590 | if (rev !== change.prev) { |
| 1591 | debug('Detected non-rectified change of %s %j', |
| 1592 | Model.modelName, id); |
| 1593 | debug('\tExpected revision: %s', change.rev); |
| 1594 | debug('\tActual revision: %s', rev); |
| 1595 | conflicts.push(change); |
| 1596 | return Change.rectifyModelChanges(Model.modelName, [id], cb); |
| 1597 | } |
| 1598 | |
| 1599 | Model.deleteAll(current.toObject(), options, function(err, result) { |
| 1600 | if (err) return cb(err); |
| 1601 | |
| 1602 | const count = result && result.count; |
| 1603 | switch (count) { |
| 1604 | case 1: |
| 1605 | // The happy path, exactly one record was updated |
| 1606 | return cb(); |
| 1607 | |
| 1608 | case 0: |
| 1609 | debug('DeleteAll detected non-rectified change of %s %j', |
| 1610 | Model.modelName, id); |
| 1611 | conflicts.push(change); |
| 1612 | // NOTE(bajtos) deleteAll triggers change rectification |
| 1613 | // for all model instances, even when no records were updated, |
| 1614 | // thus we don't need to rectify explicitly ourselves |
| 1615 | return cb(); |
| 1616 | |
| 1617 | case undefined: |
| 1618 | case null: |
| 1619 | return cb(new Error( |
| 1620 | g.f('Cannot apply bulk updates, ' + |
| 1621 | 'the connector does not correctly report ' + |
| 1622 | 'the number of deleted records.'), |
| 1623 | )); |
| 1624 | |
| 1625 | default: |
| 1626 | debug('%s.deleteAll modified unexpected number of instances: %j', |
| 1627 | Model.modelName, count); |
| 1628 | return cb(new Error( |
| 1629 | g.f('Bulk update failed, the connector has deleted unexpected ' + |
| 1630 | 'number of records: %s', JSON.stringify(count)), |
| 1631 | )); |
| 1632 | } |
| 1633 | }); |
| 1634 | } |
| 1635 | |
| 1636 | /** |
| 1637 | * Get the `Change` model. |
no outgoing calls
no test coverage detected
searching dependent graphs…