Skip to content

Commit

Permalink
Fix user filter types and optionality
Browse files Browse the repository at this point in the history
  • Loading branch information
pozylon committed Nov 26, 2024
1 parent 1acab50 commit 11f2fbf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
3 changes: 3 additions & 0 deletions packages/api/src/resolvers/queries/users/users.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { log } from '@unchainedshop/logger';
import { Context, Root, SortOption } from '@unchainedshop/types/api.js';
import { DateFilterInput } from '@unchainedshop/types/common.js';
import { UserQuery } from '@unchainedshop/types/user.js';

export default async function users(
Expand All @@ -9,6 +10,8 @@ export default async function users(
offset?: number;
sort?: Array<SortOption>;
includeGuests?: boolean;
emailVerified?: boolean;
lastLogin?: DateFilterInput;
},
{ modules, userId }: Context,
) {
Expand Down
15 changes: 10 additions & 5 deletions packages/core-users/src/module/configureUsersModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export const removeConfidentialServiceHashes = (rawUser: User): User => {
export const buildFindSelector = ({
includeGuests,
queryString,
emailVerified = null,
lastLogin = {},
emailVerified,
lastLogin,
...rest
}: UserQuery) => {
const selector: mongodb.Filter<User> = { ...rest, deleted: null };
Expand All @@ -43,13 +43,18 @@ export const buildFindSelector = ({
selector['emails.verified'] = true;
}
if (emailVerified === false) {
selector['emails.verified'] = false;
// We need to use $ne here else we'd also find users with many emails where one is
// unverified
selector['emails.verified'] = { $ne: true };
}
if (lastLogin?.start) {
selector['lastLogin.timestamp'] = { $exists: true };
}
if (lastLogin?.end) {
selector['lastLogin.timestamp'] = { $lte: lastLogin?.end };
selector['lastLogin.timestamp'].$lte = new Date(lastLogin.end);
}
if (lastLogin?.start) {
selector['lastLogin.timestamp'] = { $gte: lastLogin?.start };
selector['lastLogin.timestamp'].$gte = new Date(lastLogin.start);
}
if (queryString) {
(selector as any).$text = { $search: queryString };
Expand Down

0 comments on commit 11f2fbf

Please sign in to comment.