Просмотр исходного кода

Merge branch 'feat/85969-85962-create-access-token-model' into feat/85969-85966-access-token-parser-use-access-token-model

reiji-h 1 год назад
Родитель
Сommit
d2917fcc5c

+ 13 - 6
apps/app/src/server/models/access-token.ts

@@ -33,7 +33,8 @@ export interface IAccessTokenDocument extends IAccessToken, Document {
 
 export interface IAccessTokenModel extends Model<IAccessTokenDocument> {
   generateToken: (userId: Types.ObjectId, expiredAt: Date, scope: string[], description?: string,) => Promise<string>
-  deleteToken: (model: IAccessTokenModel) => Promise<void>
+  deleteToken: (token: string) => Promise<void>
+  deleteTokenById: (tokenId: Types.ObjectId) => Promise<void>
   deleteAllTokensByUserId: (userId: Types.ObjectId) => Promise<void>
   deleteExpiredToken: () => Promise<void>
   findUserIdByToken: (token: string) => Promise<IAccessTokenDocument>
@@ -60,12 +61,14 @@ IAccessTokenSchema.statics.generateToken = async function(userId: Types.ObjectId
   const tokenHash = generateTokenHash(token);
 
   try {
-    await this.create({
+    const { _id } = await this.create({
       userId, tokenHash, expiredAt, scope, description,
     });
 
     logger.debug('Token generated');
-    return token;
+    return {
+      token, _id, expiredAt, scope, description,
+    };
   }
   catch (err) {
     logger.debug('Failed to generate token');
@@ -73,9 +76,13 @@ IAccessTokenSchema.statics.generateToken = async function(userId: Types.ObjectId
   }
 };
 
-IAccessTokenSchema.statics.deleteToken = async function(userId: Types.ObjectId, token: string) {
+IAccessTokenSchema.statics.deleteToken = async function(token: string) {
   const tokenHash = generateTokenHash(token);
-  return this.deleteOne({ userId, tokenHash });
+  return this.deleteOne({ tokenHash });
+};
+
+IAccessTokenSchema.statics.deleteTokenById = async function(tokenId: Types.ObjectId) {
+  return this.deleteOne({ _id: tokenId });
 };
 
 IAccessTokenSchema.statics.deleteAllTokensByUserId = async function(userId: Types.ObjectId) {
@@ -95,7 +102,7 @@ IAccessTokenSchema.statics.findUserIdByToken = async function(token: string) {
 
 IAccessTokenSchema.statics.findTokenByUserId = async function(userId: Types.ObjectId) {
   const now = new Date();
-  return this.find({ userId, expiredAt: { $gt: now } }).select('expiredAt scope description');
+  return this.find({ userId, expiredAt: { $gt: now } }).select('_id expiredAt scope description');
 };
 
 IAccessTokenSchema.statics.validateTokenScopes = async function(token: string, requiredScopes: string[]) {

+ 1 - 1
apps/app/src/server/service/import/construct-convert-map.integ.ts

@@ -30,6 +30,6 @@ describe('constructConvertMap', () => {
 
     // assert
     expect(result).not.toBeNull();
-    expect(Object.keys(result).length).toEqual(36);
+    expect(Object.keys(result).length).toEqual(37);
   });
 });