improve User documentation

This commit is contained in:
tdurieux
2021-08-12 16:02:11 +02:00
parent 4d2defa630
commit 2bb05ff664

View File

@@ -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<GitHubRepository[]> {
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;
}