🪙 refactor: Auth Token Retrieval with Sorting and Query Options (#9884)

This commit is contained in:
Danny Avila 2025-09-29 09:06:40 -04:00 committed by GitHub
parent a1471c2f37
commit c0eb19730a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 201 additions and 8 deletions

View file

@ -1,3 +1,4 @@
import type { QueryOptions } from 'mongoose';
import { IToken, TokenCreateData, TokenQuery, TokenUpdateData, TokenDeleteResult } from '~/types';
import logger from '~/config/winston';
@ -81,7 +82,7 @@ export function createTokenMethods(mongoose: typeof import('mongoose')) {
/**
* Finds a Token document that matches the provided query.
*/
async function findToken(query: TokenQuery): Promise<IToken | null> {
async function findToken(query: TokenQuery, options?: QueryOptions): Promise<IToken | null> {
try {
const Token = mongoose.models.Token;
const conditions = [];
@ -99,9 +100,7 @@ export function createTokenMethods(mongoose: typeof import('mongoose')) {
conditions.push({ identifier: query.identifier });
}
const token = await Token.findOne({
$and: conditions,
}).lean();
const token = await Token.findOne({ $and: conditions }, null, options).lean();
return token as IToken | null;
} catch (error) {