Skip to content

Commit 9e121fd

Browse files
committed
cleanup
1 parent 0ebbf59 commit 9e121fd

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

server/models/user.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {
44
UserDocument,
55
UserModel,
66
CookieConsentOptions,
7-
EmailConfirmationStates
7+
EmailConfirmationStates,
8+
ApiKeyDocument
89
} from '../types';
910
import { apiKeySchema } from './apiKey';
1011

@@ -147,7 +148,7 @@ userSchema.set('toJSON', {
147148
*/
148149
userSchema.methods.comparePassword = async function comparePassword(
149150
candidatePassword: string
150-
) {
151+
): Promise<boolean> {
151152
if (!this.password) {
152153
return false;
153154
}
@@ -165,7 +166,7 @@ userSchema.methods.comparePassword = async function comparePassword(
165166
*/
166167
userSchema.methods.findMatchingKey = async function findMatchingKey(
167168
candidateKey: string
168-
) {
169+
): Promise<{ isMatch: boolean; keyDocument: ApiKeyDocument | null }> {
169170
let keyObj = { isMatch: false, keyDocument: null };
170171
/* eslint-disable no-restricted-syntax */
171172
for (const k of this.apiKeys) {
@@ -194,7 +195,9 @@ userSchema.methods.findMatchingKey = async function findMatchingKey(
194195
* @callback [cb] - Optional error-first callback that passes User document
195196
* @return {Object} - Returns User Object fulfilled by User document
196197
*/
197-
userSchema.statics.findByEmail = async function findByEmail(email) {
198+
userSchema.statics.findByEmail = async function findByEmail(
199+
email: string | string[]
200+
): Promise<UserDocument | null> {
198201
const user = this;
199202
const query = Array.isArray(email) ? { email: { $in: email } } : { email };
200203

@@ -212,9 +215,11 @@ userSchema.statics.findByEmail = async function findByEmail(email) {
212215
* Queries User collection by emails and returns all Users that match.
213216
*
214217
* @param {string[]} emails - Array of email strings
215-
* @return {Promise<Object>} - Returns Promise fulfilled by User document
218+
* @return {Promise<UserDocument[]>} - Returns Promise fulfilled by User document
216219
*/
217-
userSchema.statics.findAllByEmails = async function findAllByEmails(emails) {
220+
userSchema.statics.findAllByEmails = async function findAllByEmails(
221+
emails: string[]
222+
): Promise<UserDocument[] | null> {
218223
const user = this;
219224
const query = {
220225
email: { $in: emails }
@@ -235,22 +240,18 @@ userSchema.statics.findAllByEmails = async function findAllByEmails(emails) {
235240
* @param {string} username - Username string
236241
* @param {Object} [options] - Optional options
237242
* @param {boolean} options.caseInsensitive - Does a caseInsensitive query, defaults to false
238-
* @return {Object} - Returns User Object fulfilled by User document
243+
* @return {UserDocument} - Returns User Object fulfilled by User document
239244
*/
240245
userSchema.statics.findByUsername = async function findByUsername(
241-
username,
242-
options
243-
) {
246+
username: string,
247+
options?: { caseInsensitive?: boolean }
248+
): Promise<UserDocument | null> {
244249
const user = this;
245250
const query = {
246251
username
247252
};
248253

249-
if (
250-
arguments.length === 2 &&
251-
typeof options === 'object' &&
252-
options.caseInsensitive
253-
) {
254+
if (options?.caseInsensitive) {
254255
const foundUser = await user
255256
.findOne(query)
256257
.collation({ locale: 'en', strength: 2 })
@@ -274,17 +275,16 @@ userSchema.statics.findByUsername = async function findByUsername(
274275
* default query for username or email, defaults
275276
* to false
276277
* @param {("email"|"username")} options.valueType - Prevents automatic type inferrence
277-
* @return {Object} - Returns User Object fulfilled by User document
278+
* @return {UserDocument} - Returns User Object fulfilled by User document
278279
*/
279280
userSchema.statics.findByEmailOrUsername = async function findByEmailOrUsername(
280-
value,
281-
options
282-
) {
281+
value: string,
282+
options?: { caseInsensitive?: boolean; valueType?: 'email' | 'username' }
283+
): Promise<UserDocument | null> {
283284
const user = this;
284-
const isEmail =
285-
options && options.valueType
286-
? options.valueType === 'email'
287-
: value.indexOf('@') > -1;
285+
const isEmail = options?.valueType
286+
? options.valueType === 'email'
287+
: value.indexOf('@') > -1;
288288

289289
// do the case insensitive stuff
290290
if (
@@ -316,12 +316,12 @@ userSchema.statics.findByEmailOrUsername = async function findByEmailOrUsername(
316316
*
317317
* @param {string} email
318318
* @param {string} username
319-
* @return {Object} - Returns User Object fulfilled by User document
319+
* @return {UserDocument} - Returns User Object fulfilled by User document
320320
*/
321321
userSchema.statics.findByEmailAndUsername = async function findByEmailAndUsername(
322-
email,
323-
username
324-
) {
322+
email: string,
323+
username: string
324+
): Promise<UserDocument | null> {
325325
const user = this;
326326
const query = {
327327
$or: [{ email }, { username }]

server/types/user.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Document, Model, Types } from 'mongoose';
2+
import { VirtualId, MongooseTimestamps } from './mongoose';
23
import { UserPreferences, CookieConsentOptions } from './userPreferences';
34
import { EmailConfirmationStates } from './email';
45
import { ApiKeyDocument } from './apiKey';
56

67
/** Full User interface */
7-
export interface IUser {
8-
id: string;
8+
export interface IUser extends VirtualId, MongooseTimestamps {
99
name: string;
1010
username: string;
1111
password: string;
@@ -52,7 +52,7 @@ export interface UserDocument
5252
comparePassword(candidatePassword: string): Promise<boolean>;
5353
findMatchingKey(
5454
candidateKey: string
55-
): Promise<{ isMatch: boolean; keyDocument: UserDocument | null }>;
55+
): Promise<{ isMatch: boolean; keyDocument: ApiKeyDocument | null }>;
5656
}
5757

5858
/** Mongoose model for User */

0 commit comments

Comments
 (0)