Skip to content

Commit

Permalink
Improve parameter names and switch two state enum that will never hav…
Browse files Browse the repository at this point in the history
…e third state to optional boolean
  • Loading branch information
pozylon committed Nov 25, 2024
1 parent caf2185 commit 1acab50
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 39 deletions.
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Next
## Minor
- API: Extend `Query.users` to accept additional filter options `verificationStatus` & `loginWithinDays`
- API: Extend `Query.users` to accept additional filter options `emailVerified` & `lastLogin`

## Breaking
- Change argument format of `Query.workStatistics`, `Query.eventStatistics` & `Query.orderStatistics` from previous
Expand Down
107 changes: 98 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions packages/api/src/schema/inputTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,5 @@ export default [
start: DateTime
end: DateTime
}
enum UserVerificationFilter {
VERIFIED
UNVERIFIED
}
`,
];
8 changes: 4 additions & 4 deletions packages/api/src/schema/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export default [
includeGuests: Boolean = false
queryString: String
sort: [SortOptionInput!]
verificationStatus: UserVerificationFilter
loginWithinDays: DateFilterInput
emailVerified: Boolean
lastLogin: DateFilterInput
): [User!]!
"""
Expand All @@ -29,8 +29,8 @@ export default [
usersCount(
includeGuests: Boolean = false
queryString: String
verificationStatus: UserVerificationFilter
loginWithinDays: DateFilterInput
emailVerified: Boolean
lastLogin: DateFilterInput
): Int!
"""
Expand Down
23 changes: 10 additions & 13 deletions packages/core-users/src/module/configureUsersModule.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import localePkg from 'locale';
import { ModuleInput, UnchainedCore } from '@unchainedshop/types/core.js';
import { User, UserQuery, UsersModule, UserVerificationFilter } from '@unchainedshop/types/user.js';
import { User, UserQuery, UsersModule } from '@unchainedshop/types/user.js';
import { log, LogLevel } from '@unchainedshop/logger';
import { emit, registerEvents } from '@unchainedshop/events';
import { generateDbFilterById, buildSortOptions, mongodb } from '@unchainedshop/mongodb';
Expand Down Expand Up @@ -33,26 +33,23 @@ export const removeConfidentialServiceHashes = (rawUser: User): User => {
export const buildFindSelector = ({
includeGuests,
queryString,
verificationStatus = null,
loginWithinDays = {},
emailVerified = null,
lastLogin = {},
...rest
}: UserQuery) => {
const selector: mongodb.Filter<User> = { ...rest, deleted: null };
if (!includeGuests) selector.guest = { $in: [false, null] };
if (verificationStatus === UserVerificationFilter.VERIFIED) {
if (emailVerified === true) {
selector['emails.verified'] = true;
}
if (verificationStatus === UserVerificationFilter.UNVERIFIED) {
if (emailVerified === false) {
selector['emails.verified'] = false;
}
if (loginWithinDays?.end || loginWithinDays?.start) {
if (loginWithinDays?.end && loginWithinDays?.start)
selector['lastLogin.timestamp'] = {
$gte: loginWithinDays?.start,
$lte: loginWithinDays?.end,
};
else if (loginWithinDays?.end) selector['lastLogin.timestamp'] = { $lte: loginWithinDays?.end };
else if (loginWithinDays?.start) selector['lastLogin.timestamp'] = { $gte: loginWithinDays?.start };
if (lastLogin?.end) {
selector['lastLogin.timestamp'] = { $lte: lastLogin?.end };
}
if (lastLogin?.start) {
selector['lastLogin.timestamp'] = { $gte: lastLogin?.start };
}
if (queryString) {
(selector as any).$text = { $search: queryString };
Expand Down
9 changes: 2 additions & 7 deletions packages/types/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ export enum UserOrderFilter {
NO_ORDERS = 'NO_ORDERS',
}

export enum UserVerificationFilter {
VERIFIED = 'VERIFIED',
UNVERIFIED = 'UNVERIFIED',
}

export enum UserCartFilter {
HAS_CART = 'HAS_CART',
NO_CART = 'NO_CART',
Expand Down Expand Up @@ -102,8 +97,8 @@ export type User = {
export type UserQuery = Filter<User> & {
includeGuests?: boolean;
queryString?: string;
verificationStatus?: UserVerificationFilter;
loginWithinDays?: DateFilterInput;
emailVerified?: boolean;
lastLogin?: DateFilterInput;
};

/*
Expand Down

0 comments on commit 1acab50

Please sign in to comment.