chore: Track lastActiveAt for teams (#6491)

This commit is contained in:
Tom Moor
2024-02-04 11:14:18 -08:00
committed by GitHub
parent 234613580d
commit 8ee266f7b1
3 changed files with 45 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ import fs from "fs";
import path from "path";
import { URL } from "url";
import util from "util";
import { subMinutes } from "date-fns";
import {
InferAttributes,
InferCreationAttributes,
@@ -163,6 +164,10 @@ class Team extends ParanoidModel<
@Column
suspendedAt: Date | null;
@IsDate
@Column
lastActiveAt: Date | null;
// getters
/**
@@ -248,6 +253,27 @@ class Team extends ParanoidModel<
TeamPreferenceDefaults[preference] ??
false;
/**
* Updates the lastActiveAt timestamp to the current time.
*
* @param force Whether to force the update even if the last update was recent
* @returns A promise that resolves with the updated team
*/
public updateActiveAt = async (force = false) => {
const fiveMinutesAgo = subMinutes(new Date(), 5);
// ensure this is updated only every few minutes otherwise
// we'll be constantly writing to the DB as API requests happen
if (!this.lastActiveAt || this.lastActiveAt < fiveMinutesAgo || force) {
this.lastActiveAt = new Date();
}
// Save only writes to the database if there are changes
return this.save({
hooks: false,
});
};
provisionFirstCollection = async (userId: string) => {
await this.sequelize!.transaction(async (transaction) => {
const collection = await Collection.create(
@@ -356,6 +382,10 @@ class Team extends ParanoidModel<
// Set here rather than in TeamPreferenceDefaults as we only want to enable by default for new
// workspaces.
model.setPreference(TeamPreference.MembersCanInvite, true);
// Set last active at on creation.
model.lastActiveAt = new Date();
return model;
}