Repository: seratch/notion-sdk-jvm
Pull request #41 (2022-03-18 19:30:54)

Add timestamp filter to database queries
https://developers.notion.com/changelog/filter-databases-by-timestamp-even-if-they-dont-have-a-timestamp-property


Patch:
diff --git a/core/src/main/kotlin/notion/api/v1/model/databases/query/filter/PropertyFilter.kt b/core/src/main/kotlin/notion/api/v1/model/databases/query/filter/PropertyFilter.kt
index 66fa807..36f730f 100644
--- a/core/src/main/kotlin/notion/api/v1/model/databases/query/filter/PropertyFilter.kt
+++ b/core/src/main/kotlin/notion/api/v1/model/databases/query/filter/PropertyFilter.kt
@@ -16,8 +16,9 @@ constructor(
     var select: SelectFilter? = null,
     var multiSelect: MultiSelectFilter? = null,
     var date: DateFilter? = null,
-    var createdDate: DateFilter? = null,
-    var lastEditedDate: DateFilter? = null,
+    var timestamp: String? = null, // "created_time", "last_edited_time"
+    var createdTime: TimestampFilter? = null,
+    var lastEditedTime: TimestampFilter? = null,
     var createdBy: PeopleFilter? = null,
     var lastEditedBy: PeopleFilter? = null,
     var file: FilesFilter? = null,
diff --git a/core/src/main/kotlin/notion/api/v1/model/databases/query/filter/condition/TimestampFilter.kt b/core/src/main/kotlin/notion/api/v1/model/databases/query/filter/condition/TimestampFilter.kt
new file mode 100644
index 0000000..0b8aab5
--- /dev/null
+++ b/core/src/main/kotlin/notion/api/v1/model/databases/query/filter/condition/TimestampFilter.kt
@@ -0,0 +1,22 @@
+package notion.api.v1.model.databases.query.filter.condition
+
+open class TimestampFilter
+@JvmOverloads
+constructor(
+    var equals: String? = null,
+    var before: String? = null,
+    var after: String? = null,
+    var onOrBefore: String? = null,
+    var onOrAfter: String? = null,
+    var pastWeek: TimestampCondition? = null,
+    var pastMonth: TimestampCondition? = null,
+    var pastYear: TimestampCondition? = null,
+    var nextWeek: TimestampCondition? = null,
+    var nextMonth: TimestampCondition? = null,
+    var nextYear: TimestampCondition? = null,
+    var isEmpty: Boolean? = null,
+    var isNotEmpty: Boolean? = null,
+) {
+  open class TimestampCondition
+  object EmptyDateCondition : TimestampCondition()
+}


Test patch:
diff --git a/core/src/test/kotlin/integration_tests/DatabasesTest.kt b/core/src/test/kotlin/integration_tests/DatabasesTest.kt
index ddafaad..eea94c0 100644
--- a/core/src/test/kotlin/integration_tests/DatabasesTest.kt
+++ b/core/src/test/kotlin/integration_tests/DatabasesTest.kt
@@ -1,5 +1,7 @@
 package integration_tests
 
+import java.time.LocalDate
+import java.time.ZoneId
 import kotlin.test.assertEquals
 import kotlin.test.assertNotNull
 import kotlin.test.assertTrue
@@ -11,7 +13,9 @@ import notion.api.v1.model.databases.RichTextPropertySchema
 import notion.api.v1.model.databases.TitlePropertySchema
 import notion.api.v1.model.databases.query.filter.CompoundFilter
 import notion.api.v1.model.databases.query.filter.PropertyFilter
+import notion.api.v1.model.databases.query.filter.condition.DateFilter
 import notion.api.v1.model.databases.query.filter.condition.TextFilter
+import notion.api.v1.model.databases.query.filter.condition.TimestampFilter
 import notion.api.v1.model.databases.query.sort.QuerySort
 import notion.api.v1.model.databases.query.sort.QuerySortDirection
 import notion.api.v1.model.databases.query.sort.QuerySortTimestamp
@@ -139,6 +143,43 @@ class DatabasesTest {
     }
   }
 
+  @Test
+  fun query_date_timestamp() {
+    NotionClient(token = System.getenv("NOTION_TOKEN")).use { client ->
+      // Find the "Test Database" from the list
+      val database =
+          client
+              .search("Test Database")
+              .results
+              .find {
+                it.objectType == ObjectType.Database &&
+                    it.asDatabase().properties.containsKey("Severity")
+              }
+              ?.asDatabase()
+              ?: throw IllegalStateException(
+                  "Create a database named 'Test Database' and invite this app's user!")
+
+      val today = LocalDate.now(ZoneId.of("UTC")).toString()
+
+      // timestamp filter
+      assertNotNull(
+          client.queryDatabase(
+              databaseId = database.id,
+              filter =
+                  PropertyFilter(
+                      timestamp = "created_time", createdTime = TimestampFilter(equals = today)),
+              pageSize = 1,
+          ))
+      // date filter
+      assertNotNull(
+          client.queryDatabase(
+              databaseId = database.id,
+              filter = PropertyFilter(property = "Created Time", date = DateFilter(equals = today)),
+              pageSize = 1,
+          ))
+    }
+  }
+
   @Test
   fun query_compound() {
     NotionClient(token = System.getenv("NOTION_TOKEN")).use { client ->