From 2bb05ff6643c19086ae43bc16c1b6818ccfc4d7f Mon Sep 17 00:00:00 2001 From: tdurieux Date: Thu, 12 Aug 2021 16:02:11 +0200 Subject: [PATCH] improve User documentation --- src/User.ts | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/User.ts b/src/User.ts index b8be17f..357b012 100644 --- a/src/User.ts +++ b/src/User.ts @@ -5,6 +5,9 @@ import { IUserDocument } from "./database/users/users.types"; import Repository from "./Repository"; import { GitHubRepository } from "./source/GitHubRepository"; +/** + * Model for a user + */ export default class User { private _model: IUserDocument; constructor(model: IUserDocument) { @@ -31,10 +34,22 @@ export default class User { this._model.default = d; } + /** + * Get the GitHub repositories of the user + * @param opt options + * @returns the list of github repositories + */ async getGitHubRepositories(opt?: { + /** + * Get the repository from GitHub + */ force: boolean; }): Promise { - if (!this._model.repositories || opt?.force === true) { + if ( + !this._model.repositories || + this._model.repositories.length == 0 || + opt?.force === true + ) { // get the list of repo from github const octokit = new Octokit({ auth: this.accessToken }); const repositories = ( @@ -53,6 +68,7 @@ export default class User { }); }); + // find the repositories that are already in the database const finds = ( await RepositoryModel.find({ externalId: { @@ -61,12 +77,14 @@ export default class User { }).select("externalId") ).map((m) => m.externalId); + // save all the new repositories await Promise.all( repositories .filter((r) => finds.indexOf(r.externalId) == -1) .map((r) => r.save()) ); + // save only the if of the repositories in the user model this._model.repositories = ( await RepositoryModel.find({ externalId: { @@ -74,6 +92,8 @@ export default class User { }, }).select("id") ).map((m) => m.id); + + // have the model await this._model.save(); return repositories.map((r) => new GitHubRepository(r)); } else { @@ -83,24 +103,28 @@ export default class User { } } + /** + * Get the lost of anonymized repositories + * @returns the list of anonymized repositories + */ async getRepositories() { const repositories = ( await AnonymizedRepositoryModel.find({ owner: this.username, }).exec() ).map((d) => new Repository(d)); + const promises = []; for (let repo of repositories) { - if (repo.options.expirationDate) { - repo.options.expirationDate = new Date(repo.options.expirationDate); - } if ( repo.options.expirationMode != "never" && repo.options.expirationDate != null && repo.options.expirationDate < new Date() ) { - await repo.expire(); + // expire the repository + promises.push(repo.expire()); } } + await Promise.all(promises); return repositories; }