From 5c06f7571f794de338a204dc45d7ab59f7ba67e2 Mon Sep 17 00:00:00 2001 From: CamilleLegeron Date: Tue, 16 Apr 2024 11:27:11 +0200 Subject: [PATCH] use date and not datetime for lastConnectionAt --- app/gen-server/lib/HomeDBManager.ts | 24 +++++++++---------- .../1713186031023-UserLastConnection.ts | 4 +--- landing.sqbpro | 2 ++ 3 files changed, 15 insertions(+), 15 deletions(-) create mode 100644 landing.sqbpro diff --git a/app/gen-server/lib/HomeDBManager.ts b/app/gen-server/lib/HomeDBManager.ts index e5fc2eeb578..cbc899e8ce7 100644 --- a/app/gen-server/lib/HomeDBManager.ts +++ b/app/gen-server/lib/HomeDBManager.ts @@ -616,12 +616,15 @@ export class HomeDBManager extends EventEmitter { if (!props.isFirstTimeUser) { isWelcomed = true; } } if (props.newConnection === true) { - // set last connection time to now (remove milliseconds for compatibility with other - // timestamps in db set by typeorm, and since second level precision is fine) - const nowish = new Date(); - nowish.setMilliseconds(0); - user.lastConnectionAt = nowish; - needsSave = true; + // set last connection to today (keep date, remove time) + const today = new Date(); + today.setHours(0, 0, 0, 0); + if (today.getFullYear() !== user.lastConnectionAt?.getFullYear() || + today.getMonth() !== user.lastConnectionAt?.getMonth() || + today.getDate() !== user.lastConnectionAt?.getDate()){ + user.lastConnectionAt = today; + needsSave = true; + } } if (needsSave) { await user.save(); @@ -710,15 +713,12 @@ export class HomeDBManager extends EventEmitter { user.name = (profile && (profile.name || email.split('@')[0])) || ''; needUpdate = true; } - if (profile) { - // set first login time and last connection time to now (remove milliseconds for compatibility with other + if (profile && !user.firstLoginAt) { + // set first login time to now (remove milliseconds for compatibility with other // timestamps in db set by typeorm, and since second level precision is fine) const nowish = new Date(); nowish.setMilliseconds(0); - user.lastConnectionAt = nowish; - if (!user.firstLoginAt) { - user.firstLoginAt = nowish; - } + user.firstLoginAt = nowish; needUpdate = true; } if (!user.picture && profile && profile.picture) { diff --git a/app/gen-server/migration/1713186031023-UserLastConnection.ts b/app/gen-server/migration/1713186031023-UserLastConnection.ts index e1688502f05..793c0638eb0 100644 --- a/app/gen-server/migration/1713186031023-UserLastConnection.ts +++ b/app/gen-server/migration/1713186031023-UserLastConnection.ts @@ -3,11 +3,9 @@ import { MigrationInterface, QueryRunner, TableColumn} from 'typeorm'; export class UserLastConnection1713186031023 implements MigrationInterface { public async up(queryRunner: QueryRunner): Promise { - const sqlite = queryRunner.connection.driver.options.type === 'sqlite'; - const datetime = sqlite ? "datetime" : "timestamp with time zone"; await queryRunner.addColumn('users', new TableColumn({ name: 'last_connection_at', - type: datetime, + type: "date", isNullable: true })); } diff --git a/landing.sqbpro b/landing.sqbpro new file mode 100644 index 00000000000..fa1aeffc74d --- /dev/null +++ b/landing.sqbpro @@ -0,0 +1,2 @@ +select * from migrations; +delete from migrations where id = 34;