Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate emails can be registered when registering users #523

Open
horizon-dev-group opened this issue Aug 20, 2024 · 2 comments
Open

Duplicate emails can be registered when registering users #523

horizon-dev-group opened this issue Aug 20, 2024 · 2 comments

Comments

@horizon-dev-group
Copy link

image

As you can see when checking for duplicate emails we gave it an option withDeleted: true, the intention is clear, we don't want duplicate emails even if they're deleted but the thing is the final db query becomes like this:

{"email":{"$regex": /\bexample@mail.com\b/,"$options":"i"},"deleted":true}

there fore the above query will always be false because the emails registerd in our database don't have the property set to true by default. so this will give result of false even if we have an email: example@mail.com

the reason is when we first register with the email: example@mail.com the deleted property is false. but the above query is looking for emails with the deleted property true there for allowing for duplicate emails.

@horizon-dev-group
Copy link
Author

horizon-dev-group commented Aug 20, 2024

The solution is to modify the exists code like this or something better:

async exists(
        find: Record<string, any>,
        options?: IDatabaseExistOptions
    ): Promise<boolean> {
        if (options?.excludeId) {
            find = {
                ...find,
                _id: {
                    $nin: options?.excludeId ?? [],
                },
            };
        }

        const repository = this._repository.exists(find);

        if (options?.withDeleted) {
            repository.or([
                {
                    deletedAt: { $exists: false },
                },
                {
                    deletedAt: { $exists: true },
                },
            ]);
        } else {
            repository.where('deletedAt').exists(false);
        }

        if (options?.join) {
            repository.populate(
                (typeof options.join === 'boolean' && options.join
                    ? this._join
                    : options.join) as
                    | PopulateOptions
                    | (string | PopulateOptions)[]
            );
        }

        if (options?.session) {
            repository.session(options.session);
        }

        const result = await repository;
        console.log('Result: ' + JSON.stringify(result, null, 2));
        return result ? true : false;
    }

@andrechristikan
Copy link
Owner

u rock man
yes, its need some adjustment when check withDeleted in base repository.

i will take a note of it
thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants