* feat: add membership creation date to Organization Member List table (CAL-5406) Co-Authored-By: keith@cal.com <keith@cal.com> * feat: add migration for membership creation date (CAL-5406) Co-Authored-By: keith@cal.com <keith@cal.com> * feat: make createdAt and updatedAt nullable (CAL-5406) Co-Authored-By: keith@cal.com <keith@cal.com> * feat: add updatedAt column to Organization Member List table (CAL-5406) Co-Authored-By: keith@cal.com <keith@cal.com> * fix: use type assertion to access createdAt and updatedAt fields (CAL-5406) Co-Authored-By: keith@cal.com <keith@cal.com> * fix: display N/A for null date values (CAL-5406) Co-Authored-By: keith@cal.com <keith@cal.com> * fix: use proper type assertions for createdAt and updatedAt fields (CAL-5406) Co-Authored-By: keith@cal.com <keith@cal.com> * fix: add createdAt and updatedAt to UserTableUser mock in test (CAL-5406) Co-Authored-By: keith@cal.com <keith@cal.com> * feat: add PostgreSQL trigger for membership timestamps (CAL-5406) Co-Authored-By: keith@cal.com <keith@cal.com> * fix: use empty string instead of N/A and add translations for column headers (CAL-5406) Co-Authored-By: keith@cal.com <keith@cal.com> * add i18n text * clean up type issue * feat: add translation keys for column headers (CAL-5406) Co-Authored-By: keith@cal.com <keith@cal.com> * disable sort * remove duplicated i18n texts * feat: add filters for lastActiveAt, createdAt, and updatedAt (CAL-5406) Co-Authored-By: keith@cal.com <keith@cal.com> * support date range filter * fix date range for end date * hide columns by default * revert wrong change * add missing selects * fix e2e test * fix: remove PostgreSQL trigger and let application handle timestamps (CAL-5406) Co-Authored-By: keith@cal.com <keith@cal.com> * feat: add application-level timestamp handling for Membership model (CAL-5406) Co-Authored-By: keith@cal.com <keith@cal.com> * add more timestamp handling * refactor: use Prisma's built-in decorators for Membership timestamps (CAL-5406) Co-Authored-By: keith@cal.com <keith@cal.com> * refactor: remove application-level timestamp handling in favor of Prisma decorators (CAL-5406) Co-Authored-By: keith@cal.com <keith@cal.com> * refactor: remove more application-level timestamp handling in favor of Prisma decorators (CAL-5406) Co-Authored-By: keith@cal.com <keith@cal.com> * fix e2e test --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: keith@cal.com <keith@cal.com> Co-authored-by: Anik Dhabal Babu <81948346+anikdhabal@users.noreply.github.com> Co-authored-by: Eunjae Lee <hey@eunjae.dev>
174 lines
4.5 KiB
TypeScript
174 lines
4.5 KiB
TypeScript
import type { FilterValue, SortingState } from "./types";
|
|
import {
|
|
isSingleSelectFilterValue,
|
|
isMultiSelectFilterValue,
|
|
isTextFilterValue,
|
|
isNumberFilterValue,
|
|
isDateRangeFilterValue,
|
|
} from "./utils";
|
|
|
|
type MakeWhereClauseProps = {
|
|
columnName: string;
|
|
filterValue: FilterValue;
|
|
json?: true | { path: string[] };
|
|
};
|
|
|
|
export function makeOrderBy(sorting: SortingState) {
|
|
if (!sorting || !sorting.length) return undefined;
|
|
|
|
return sorting.map((sort) => ({
|
|
[sort.id]: sort.desc ? ("desc" as const) : ("asc" as const),
|
|
}));
|
|
}
|
|
|
|
export function makeWhereClause(props: MakeWhereClauseProps) {
|
|
const { columnName, filterValue } = props;
|
|
const isJson = props.json === true || (typeof props.json === "object" && props.json.path?.length > 0);
|
|
const jsonPath = isJson && typeof props.json === "object" ? props.json.path : undefined;
|
|
|
|
const jsonPathObj = isJson && jsonPath ? { path: jsonPath } : {};
|
|
if (isMultiSelectFilterValue(filterValue)) {
|
|
return {
|
|
[columnName]: {
|
|
...jsonPathObj,
|
|
...(isJson ? { array_contains: filterValue.data } : { in: filterValue.data }),
|
|
},
|
|
};
|
|
} else if (isSingleSelectFilterValue(filterValue)) {
|
|
return {
|
|
[columnName]: {
|
|
...jsonPathObj,
|
|
equals: filterValue.data,
|
|
},
|
|
};
|
|
} else if (isTextFilterValue(filterValue)) {
|
|
const { operator, operand } = filterValue.data;
|
|
switch (operator) {
|
|
case "equals":
|
|
return {
|
|
[columnName]: {
|
|
...jsonPathObj,
|
|
equals: operand,
|
|
},
|
|
};
|
|
case "notEquals":
|
|
return {
|
|
[columnName]: {
|
|
...jsonPathObj,
|
|
not: operand,
|
|
},
|
|
};
|
|
case "contains":
|
|
return {
|
|
[columnName]: {
|
|
...jsonPathObj,
|
|
...(isJson ? { string_contains: operand } : { contains: operand, mode: "insensitive" }),
|
|
},
|
|
};
|
|
case "notContains":
|
|
return {
|
|
NOT: {
|
|
[columnName]: {
|
|
...jsonPathObj,
|
|
...(isJson ? { string_contains: operand } : { contains: operand, mode: "insensitive" }),
|
|
},
|
|
},
|
|
};
|
|
case "startsWith":
|
|
return {
|
|
[columnName]: {
|
|
...jsonPathObj,
|
|
...(isJson ? { string_starts_with: operand } : { startsWith: operand, mode: "insensitive" }),
|
|
},
|
|
};
|
|
case "endsWith":
|
|
return {
|
|
[columnName]: {
|
|
...jsonPathObj,
|
|
...(isJson ? { string_ends_with: operand } : { endsWith: operand, mode: "insensitive" }),
|
|
},
|
|
};
|
|
case "isEmpty":
|
|
return {
|
|
[columnName]: {
|
|
...jsonPathObj,
|
|
equals: "",
|
|
},
|
|
};
|
|
case "isNotEmpty":
|
|
return {
|
|
NOT: {
|
|
[columnName]: {
|
|
...jsonPathObj,
|
|
equals: "",
|
|
},
|
|
},
|
|
};
|
|
default:
|
|
throw new Error(`Invalid operator for text filter: ${operator}`);
|
|
}
|
|
} else if (isNumberFilterValue(filterValue)) {
|
|
const { operator, operand } = filterValue.data;
|
|
switch (operator) {
|
|
case "eq":
|
|
return {
|
|
[columnName]: {
|
|
...jsonPathObj,
|
|
equals: operand,
|
|
},
|
|
};
|
|
case "neq":
|
|
return {
|
|
[columnName]: {
|
|
...jsonPathObj,
|
|
not: operand,
|
|
},
|
|
};
|
|
case "gt":
|
|
return {
|
|
[columnName]: {
|
|
...jsonPathObj,
|
|
gt: operand,
|
|
},
|
|
};
|
|
case "gte":
|
|
return {
|
|
[columnName]: {
|
|
...jsonPathObj,
|
|
gte: operand,
|
|
},
|
|
};
|
|
case "lt":
|
|
return {
|
|
[columnName]: {
|
|
...jsonPathObj,
|
|
lt: operand,
|
|
},
|
|
};
|
|
case "lte":
|
|
return {
|
|
[columnName]: {
|
|
...jsonPathObj,
|
|
lte: operand,
|
|
},
|
|
};
|
|
default:
|
|
throw new Error(`Invalid operator for number filter: ${operator}`);
|
|
}
|
|
} else if (isDateRangeFilterValue(filterValue)) {
|
|
const { startDate, endDate } = filterValue.data;
|
|
if (!startDate || !endDate) {
|
|
throw new Error(`Invalid date range filter: ${JSON.stringify({ columnName, startDate, endDate })}`);
|
|
}
|
|
|
|
return {
|
|
[columnName]: {
|
|
...jsonPathObj,
|
|
gte: startDate,
|
|
lte: endDate,
|
|
},
|
|
};
|
|
}
|
|
throw new Error(`Invalid filter type: ${JSON.stringify({ columnName, filterValue })}`);
|
|
}
|