# Bug OP-12155 - Filtering with two timestamps for custom field values wrongfully requires a work package to match at both timestamps

Project: OpenProject
Status: closed
URL: https://community.openproject.org/work_packages/OP-12155

## Open points

_No work packages_

## Details

| Attribute | Value |
| --- | --- |
| Date | 2022-08-16 –  |
| Priority | High |
| Module |  |
| Target versions | 13.0.0 |
| Observed in versions |  |
| Sprint | 13.0.0 |
| Backlog bucket |  |
| Bug found in version (deprecated) |  |
| Found in mobile app version |  |
| Follow-up feedback e-mail |  |
| Requires doc change | No |
| Accessibility | No |
| Pull preview |  |
| List |  |
| Labels |  |

## People

| Attribute | Value |
| --- | --- |
| Assignee |  |
| Accountable |  |
| Product manager |  |
| Developers |  |
| Designer |  |
| Ordered by |  |
| Signed off by |  |
| Interest by |  |

## Planning

| Attribute | Value |
| --- | --- |
| Relevance |  |

## Costs

| Attribute | Value |
| --- | --- |
| Votes |  |
| Work |  |
| Labor costs |  |

## Description

### Steps to reproduce

*Please write down the steps to reproduce. Try to write down all necessary preconditions (what permissions do you have, are other users involved?). Example:*

1.  Have a work package
2.  Change a custom field value (e.g. change an integer CF from 1 to 2)
3.  Go to the work package list.
4.  Filter for the old value of the CF (e.g. integer CF set to 1 - which the work package no longer has)
5.  Enable baseline filtering and set the date to be at a time before the change to the custom field

### What is the buggy behavior?

*Please describe the bug in as much detail as possible. Example:*

  - The work package having had the value for the custom field at the baseline time will not show up in the work package list even though it had the filtered for value at that time. 

### What is the expected behavior?

*Describe how the application should behave like. Example:*

1.  The work package should be displayed in the list as it matches the custom field filter at the baseline time. 

### **Logs**

  - For custom fields, the current `custom_values` are always used for filtering even though the ones from the `customizable_journals` should be used.

<!-- end list -->

``` SQL
SELECT "work_packages"."id" FROM "work_packages" LEFT OUTER JOIN "projects" ON "projects"."id" = "work_packages"."project_id" WHERE "work_packages"."project_id" IN (SELECT "projects"."id" FROM "projects" INNER JOIN "enabled_modules" ON "projects"."id" = "enabled_modules"."project_id" AND "enabled_modules"."name" IN ('work_package_tracking') AND "projects"."active" = TRUE WHERE "projects"."active" = TRUE) AND (          work_packages.id IN
          (SELECT work_packages.id
          FROM work_packages
          LEFT OUTER JOIN custom_values
                 ON custom_values.customized_type = 'WorkPackage'
                 AND custom_values.customized_id = work_packages.id
                 AND custom_values.custom_field_id = 5
               JOIN custom_fields_types
                 ON custom_fields_types.type_id = work_packages.type_id
                 AND custom_fields_types.custom_field_id = 5
          WHERE custom_values.value IN ('517'))
) AND "work_packages"."id" IN (SELECT "work_packages"."id" FROM "work_packages" WHERE "work_packages"."id" IN (SELECT journals.journable_id as id FROM "work_package_journals" INNER JOIN "journals" ON "journals"."data_type" = 'Journal::WorkPackageJournal' AND "journals"."data_id" = "work_package_journals"."id" INNER JOIN (SELECT id, created_at, project_id FROM "work_packages") AS journables ON "journables"."id" = "journals"."journable_id" LEFT OUTER JOIN "projects" ON "projects"."id" = "journables"."project_id" WHERE "work_package_journals"."project_id" IN (SELECT "projects"."id" FROM "projects" INNER JOIN "enabled_modules" ON "projects"."id" = "enabled_modules"."project_id" AND "enabled_modules"."name" IN ('work_package_tracking') AND "projects"."active" = TRUE WHERE "projects"."active" = TRUE) AND (          journals.journable_id IN
          (SELECT journals.journable_id
          FROM work_packages
          LEFT OUTER JOIN custom_values
                 ON custom_values.customized_type = 'WorkPackage'
                 AND custom_values.customized_id = journals.journable_id
                 AND custom_values.custom_field_id = 5
               JOIN custom_fields_types
                 ON custom_fields_types.type_id = work_package_journals.type_id
                 AND custom_fields_types.custom_field_id = 5
          WHERE custom_values.value IN ('517'))
) AND ((          journals.journable_id IN
          (SELECT journals.journable_id
          FROM work_packages
          LEFT OUTER JOIN custom_values
                 ON custom_values.customized_type = 'WorkPackage'
                 AND custom_values.customized_id = journals.journable_id
                 AND custom_values.custom_field_id = 5
               JOIN custom_fields_types
                 ON custom_fields_types.type_id = work_package_journals.type_id
                 AND custom_fields_types.custom_field_id = 5
          WHERE custom_values.value IN ('517'))
) AND (journals.journable_id IN ('45414'))) AND "journals"."journable_type" = $1 AND "journals"."id" IN (SELECT distinct on (journable_type, journable_id) id FROM "journals" WHERE "journals"."journable_type" = $2 AND "journals"."created_at" <= $3 ORDER BY "journals"."journable_type" ASC, "journals"."journable_id" ASC, "journals"."created_at" DESC))) ORDER BY work_packages.id LIMIT $4 OFFSET $5  [["journable_type", "WorkPackage"], ["journable_type", "WorkPackage"], ["created_at", "2023-03-16 08:31:00.786501"], ["LIMIT", 500], ["OFFSET", 0]]
```

###
