Skip to content
This repository has been archived by the owner on Sep 6, 2023. It is now read-only.

Commit

Permalink
Fix item custom field values when item custom field has a value
Browse files Browse the repository at this point in the history
When the custom field has a value for any item in the organization, it was no longer returned for any items that *do not* have a value for that custom field. This change makes it so that custom fields that apply to all categories will only be `join`ed with the item custom field table if the current item's barcode matches. Using a second `on` clause in the `join` is a better approach than using two `where` clauses.
  • Loading branch information
AdamVig committed Oct 9, 2017
1 parent d5b3686 commit 62807b8
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions controllers/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,10 @@ item.withCustomFields = (req, queryBuilder) => {
.from('customField')
// Join all custom fields with all categories (`categoryID = null` if no categories are specified)
.leftJoin('customFieldCategory', 'customField.customFieldID', 'customFieldCategory.customFieldID')
// Get values
.leftJoin('itemCustomField', 'customField.customFieldID', 'itemCustomField.customFieldID')
// Only get item custom fields for this item
.where(function () {
this.where('itemCustomField.barcode', req.params.barcode)
// Include unset item custom fields to avoid losing custom fields without values
.orWhere('itemCustomField.barcode', null)
// Get item custom fields for this item
.leftJoin('itemCustomField', function () {
this.on('customField.customFieldID', 'itemCustomField.customFieldID')
.on('itemCustomField.barcode', db.raw('?', [req.params.barcode]))
})
.andWhere('customFieldCategory.categoryID', null)
.andWhere('customField.organizationID', req.user.organizationID)
Expand Down

0 comments on commit 62807b8

Please sign in to comment.