fix(magic-shelf): remove timestamp from magic shelf date checks (#2097)

This commit is contained in:
Alex
2026-07-24 22:57:10 +01:00
committed by GitHub
parent bf093b9506
commit 8edf1351b0
2 changed files with 14 additions and 17 deletions
@@ -534,7 +534,7 @@ public class BookRuleEvaluatorService {
if (value instanceof Boolean) {
return cb.equal(field, value);
} else if (value instanceof LocalDate) {
return cb.equal(field, value);
return cb.equal(field.cast(LocalDate.class), value);
} else if (value instanceof LocalDateTime) {
return cb.equal(field, value);
} else if (rule.getField() == RuleField.READ_STATUS) {
@@ -600,28 +600,28 @@ public class BookRuleEvaluatorService {
private Predicate buildGreaterThan(Rule rule, CriteriaBuilder cb, Root<BookEntity> root, Join<BookEntity, UserBookProgressEntity> progressJoin) {
return buildComparisonPredicate(rule, cb, root, progressJoin,
(field, dateValue) -> cb.greaterThan(field.as(LocalDateTime.class), dateValue),
(field, localDateValue) -> cb.greaterThan(field.as(LocalDate.class), localDateValue),
(field, localDateValue) -> cb.greaterThan(field.cast(LocalDate.class), localDateValue),
(field, numValue) -> cb.gt(toNumericExpression(field), numValue));
}
private Predicate buildGreaterThanEqual(Rule rule, CriteriaBuilder cb, Root<BookEntity> root, Join<BookEntity, UserBookProgressEntity> progressJoin) {
return buildComparisonPredicate(rule, cb, root, progressJoin,
(field, dateValue) -> cb.greaterThanOrEqualTo(field.as(LocalDateTime.class), dateValue),
(field, localDateValue) -> cb.greaterThanOrEqualTo(field.as(LocalDate.class), localDateValue),
(field, localDateValue) -> cb.greaterThanOrEqualTo(field.cast(LocalDate.class), localDateValue),
(field, numValue) -> cb.ge(toNumericExpression(field), numValue));
}
private Predicate buildLessThan(Rule rule, CriteriaBuilder cb, Root<BookEntity> root, Join<BookEntity, UserBookProgressEntity> progressJoin) {
return buildComparisonPredicate(rule, cb, root, progressJoin,
(field, dateValue) -> cb.lessThan(field.as(LocalDateTime.class), dateValue),
(field, localDateValue) -> cb.lessThan(field.as(LocalDate.class), localDateValue),
(field, localDateValue) -> cb.lessThan(field.cast(LocalDate.class), localDateValue),
(field, numValue) -> cb.lt(toNumericExpression(field), numValue));
}
private Predicate buildLessThanEqual(Rule rule, CriteriaBuilder cb, Root<BookEntity> root, Join<BookEntity, UserBookProgressEntity> progressJoin) {
return buildComparisonPredicate(rule, cb, root, progressJoin,
(field, dateValue) -> cb.lessThanOrEqualTo(field.as(LocalDateTime.class), dateValue),
(field, localDateValue) -> cb.lessThanOrEqualTo(field.as(LocalDate.class), localDateValue),
(field, localDateValue) -> cb.lessThanOrEqualTo(field.cast(LocalDate.class), localDateValue),
(field, numValue) -> cb.le(toNumericExpression(field), numValue));
}
@@ -655,7 +655,7 @@ public class BookRuleEvaluatorService {
if (start == null || end == null) return cb.conjunction();
if (start instanceof LocalDate && end instanceof LocalDate) {
return cb.between(field.as(LocalDate.class), (LocalDate) start, (LocalDate) end);
return cb.between(field.cast(LocalDate.class), (LocalDate) start, (LocalDate) end);
}
if (start instanceof LocalDateTime && end instanceof LocalDateTime) {
@@ -892,19 +892,12 @@ public class BookRuleEvaluatorService {
private Object normalizeValue(Object value, RuleField field) {
if (value == null) return null;
if (field == RuleField.PUBLISHED_DATE) {
if (field == RuleField.PUBLISHED_DATE || field == RuleField.DATE_FINISHED ||
field == RuleField.LAST_READ_TIME || field == RuleField.ADDED_ON) {
LocalDateTime parsed = parseDate(value);
return parsed != null ? parsed.toLocalDate() : null;
}
if (field == RuleField.DATE_FINISHED || field == RuleField.LAST_READ_TIME || field == RuleField.ADDED_ON) {
LocalDateTime parsed = parseDate(value);
if (parsed != null) {
return parsed.atZone(ZoneId.systemDefault()).toInstant();
}
return null;
}
if (field == RuleField.READ_STATUS) {
return value.toString();
}
@@ -27,14 +27,18 @@ export class BookRuleEvaluatorService {
}
const rawValue = this.extractBookValue(book, rule.field);
const isDateField = rule.field === 'publishedDate' || rule.field === 'dateFinished' ||
rule.field === 'lastReadTime' || rule.field === 'addedOn';
const withoutTime = (date: Date): Date =>
new Date(date.getFullYear(), date.getMonth(), date.getDate());
const normalize = (val: unknown): unknown => {
if (val === null || val === undefined) return val;
if (val instanceof Date) return val;
if (val instanceof Date) return isDateField ? withoutTime(val) : val;
if (typeof val === 'boolean') return String(val);
if (typeof val === 'string') {
const date = parseValue(val, 'date');
if (date != null) return date;
if (date instanceof Date) return isDateField ? withoutTime(date) : date;
return val.toLowerCase();
}
return val;