From a80ca507f881b20fd7510893835eb733bf369ece Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sat, 2 Nov 2024 13:45:26 +0100 Subject: [PATCH] Feature/add lastRequestAt to analytics (#4010) * Add lastRequestAt to Analytics --- apps/api/src/app/admin/admin.service.ts | 4 ++-- apps/api/src/app/auth/jwt.strategy.ts | 2 +- apps/api/src/app/info/info.service.ts | 2 +- .../migration.sql | 5 +++++ prisma/schema.prisma | 2 ++ 5 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 prisma/migrations/20241102121004_added_last_request_at_to_analytics/migration.sql diff --git a/apps/api/src/app/admin/admin.service.ts b/apps/api/src/app/admin/admin.service.ts index 860f19851..49964c77d 100644 --- a/apps/api/src/app/admin/admin.service.ts +++ b/apps/api/src/app/admin/admin.service.ts @@ -641,7 +641,7 @@ export class AdminService { } private async getUsersWithAnalytics(): Promise { - let orderBy: any = { + let orderBy: Prisma.UserOrderByWithRelationInput = { createdAt: 'desc' }; let where: Prisma.UserWhereInput; @@ -649,7 +649,7 @@ export class AdminService { if (this.configurationService.get('ENABLE_FEATURE_SUBSCRIPTION')) { orderBy = { Analytics: { - updatedAt: 'desc' + lastRequestAt: 'desc' } }; where = { diff --git a/apps/api/src/app/auth/jwt.strategy.ts b/apps/api/src/app/auth/jwt.strategy.ts index a8ad8fd08..1200ed465 100644 --- a/apps/api/src/app/auth/jwt.strategy.ts +++ b/apps/api/src/app/auth/jwt.strategy.ts @@ -46,7 +46,7 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') { update: { country, activityCount: { increment: 1 }, - updatedAt: new Date() + lastRequestAt: new Date() }, where: { userId: user.id } }); diff --git a/apps/api/src/app/info/info.service.ts b/apps/api/src/app/info/info.service.ts index f81ddd710..62a78d1d8 100644 --- a/apps/api/src/app/info/info.service.ts +++ b/apps/api/src/app/info/info.service.ts @@ -142,7 +142,7 @@ export class InfoService { }, { Analytics: { - updatedAt: { + lastRequestAt: { gt: subDays(new Date(), aDays) } } diff --git a/prisma/migrations/20241102121004_added_last_request_at_to_analytics/migration.sql b/prisma/migrations/20241102121004_added_last_request_at_to_analytics/migration.sql new file mode 100644 index 000000000..b7af31037 --- /dev/null +++ b/prisma/migrations/20241102121004_added_last_request_at_to_analytics/migration.sql @@ -0,0 +1,5 @@ +-- AlterTable +ALTER TABLE "Analytics" ADD COLUMN "lastRequestAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP; + +-- CreateIndex +CREATE INDEX "Analytics_lastRequestAt_idx" ON "Analytics"("lastRequestAt"); diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 7f386a712..5a34e8e11 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -68,10 +68,12 @@ model Analytics { activityCount Int @default(0) country String? dataProviderGhostfolioDailyRequests Int @default(0) + lastRequestAt DateTime @default(now()) updatedAt DateTime @updatedAt userId String @id User User @relation(fields: [userId], onDelete: Cascade, references: [id]) + @@index([lastRequestAt]) @@index([updatedAt]) }