Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Return totalMonthlyEvents from user endpoints

Blue Mousefba1b6589bdb
diff --git a/backend/apps/cloud/src/auth/auth.controller.ts b/backend/apps/cloud/src/auth/auth.controller.ts
index 1eca6594d..1d9a191de 100644
--- a/backend/apps/cloud/src/auth/auth.controller.ts
+++ b/backend/apps/cloud/src/auth/auth.controller.ts
@@ -52,6 +52,7 @@ import {
SSOProviders,
} from './dtos'
import { JwtAccessTokenGuard, JwtRefreshTokenGuard, RolesGuard } from './guards'
+import { ProjectService } from '../project/project.service'
const OAUTH_RATE_LIMIT = 15
@@ -71,6 +72,7 @@ export class AuthController {
constructor(
private readonly userService: UserService,
private readonly authService: AuthService,
+ private readonly projectService: ProjectService,
) {}
@ApiOperation({ summary: 'Register a new user' })
@@ -85,7 +87,7 @@ export class AuthController {
@I18n() i18n: I18nContext,
@Headers() headers: unknown,
@Ip() requestIp: string,
- ): Promise<RegisterResponseDto> {
+ ) {
const ip = getIPFromHeaders(headers) || requestIp || ''
await checkRateLimit(ip, 'register', 5)
@@ -121,6 +123,7 @@ export class AuthController {
return {
...jwtTokens,
user: this.userService.omitSensitiveData(newUser),
+ totalMonthlyEvents: 0,
}
}
@@ -153,6 +156,10 @@ export class AuthController {
!user.isTwoFactorAuthenticationEnabled,
)
+ const meta = {
+ totalMonthlyEvents: undefined,
+ }
+
await this.authService.sendTelegramNotification(user.id, headers, ip)
if (user.isTwoFactorAuthenticationEnabled) {
@@ -166,10 +173,12 @@ export class AuthController {
user.sharedProjects = sharedProjects
user.organisationMemberships = organisationMemberships
+ meta.totalMonthlyEvents = await this.projectService.getRedisCount(user.id)
}
return {
...jwtTokens,
+ ...meta,
user: this.userService.omitSensitiveData(user as User),
}
}
diff --git a/backend/apps/cloud/src/auth/auth.service.ts b/backend/apps/cloud/src/auth/auth.service.ts
index 86e7440a9..e74d85db9 100644
--- a/backend/apps/cloud/src/auth/auth.service.ts
+++ b/backend/apps/cloud/src/auth/auth.service.ts
@@ -509,6 +509,10 @@ export class AuthService {
!user.isTwoFactorAuthenticationEnabled,
)
+ const meta = {
+ totalMonthlyEvents: undefined,
+ }
+
if (user.isTwoFactorAuthenticationEnabled) {
// @ts-expect-error
user = _pick(user, ['isTwoFactorAuthenticationEnabled', 'email'])
@@ -520,10 +524,13 @@ export class AuthService {
user.sharedProjects = sharedProjects
user.organisationMemberships = organisationMemberships
+
+ meta.totalMonthlyEvents = await this.projectService.getRedisCount(user.id)
}
return {
...jwtTokens,
+ ...meta,
user: this.userService.omitSensitiveData(user),
}
}
@@ -562,6 +569,7 @@ export class AuthService {
return {
...jwtTokens,
user: this.userService.omitSensitiveData(user),
+ totalMonthlyEvents: 0,
}
}
@@ -952,6 +960,7 @@ export class AuthService {
return {
...jwtTokens,
user: this.userService.omitSensitiveData(user),
+ totalMonthlyEvents: 0,
}
}
@@ -967,6 +976,10 @@ export class AuthService {
!user.isTwoFactorAuthenticationEnabled,
)
+ const meta = {
+ totalMonthlyEvents: undefined,
+ }
+
if (user.isTwoFactorAuthenticationEnabled) {
// @ts-expect-error
user = _pick(user, ['isTwoFactorAuthenticationEnabled', 'email'])
@@ -978,10 +991,12 @@ export class AuthService {
user.sharedProjects = sharedProjects
user.organisationMemberships = organisationMemberships
+ meta.totalMonthlyEvents = await this.projectService.getRedisCount(user.id)
}
return {
...jwtTokens,
+ ...meta,
user: this.userService.omitSensitiveData(user),
}
}
diff --git a/backend/apps/cloud/src/auth/dtos/register.dto.ts b/backend/apps/cloud/src/auth/dtos/register.dto.ts
index d0e977624..bf4e9b35b 100644
--- a/backend/apps/cloud/src/auth/dtos/register.dto.ts
+++ b/backend/apps/cloud/src/auth/dtos/register.dto.ts
@@ -59,4 +59,9 @@ export class RegisterResponseDto {
description: 'User entity',
})
user: object
+
+ @ApiProperty({
+ description: 'Total used monthly events',
+ })
+ totalMonthlyEvents: number
}
diff --git a/backend/apps/cloud/src/project/project.controller.ts b/backend/apps/cloud/src/project/project.controller.ts
index 8345f2212..5107bc838 100644
--- a/backend/apps/cloud/src/project/project.controller.ts
+++ b/backend/apps/cloud/src/project/project.controller.ts
@@ -32,7 +32,7 @@ import {
ApiOkResponse,
ApiNoContentResponse,
} from '@nestjs/swagger'
-import { Equal, In } from 'typeorm'
+import { Equal } from 'typeorm'
import _isEmpty from 'lodash/isEmpty'
import _map from 'lodash/map'
import _trim from 'lodash/trim'
@@ -153,10 +153,11 @@ export class ProjectController {
): Promise<Pagination<Project> | Project[] | object> {
this.logger.log({ userId, take, skip }, 'GET /project')
- const [paginated, totalMonthlyEvents] = await Promise.all([
- this.projectService.paginate({ take, skip }, userId, search),
- this.projectService.getRedisCount(userId),
- ])
+ const paginated = await this.projectService.paginate(
+ { take, skip },
+ userId,
+ search,
+ )
const pidsWithData =
await this.projectService.getPIDsWhereAnalyticsDataExists(
@@ -201,10 +202,7 @@ export class ProjectController {
}
})
- return {
- ...paginated,
- totalMonthlyEvents,
- }
+ return paginated
}
@ApiBearerAuth()
@@ -1920,51 +1918,6 @@ export class ProjectController {
await this.projectService.deleteShare(shareId)
}
- @ApiOperation({ summary: 'Get monitors for all user projects' })
- @ApiBearerAuth()
- @ApiOkResponse({ type: MonitorEntity })
- @Get('/monitors')
- @Auth([])
- public async getAllMonitors(
- @CurrentUserId() userId: string,
- @Query('take') take: number | undefined,
- @Query('skip') skip: number | undefined,
- ): Promise<Pagination<MonitorEntity>> {
- this.logger.log({ userId, take, skip }, 'GET /project/monitors')
-
- const projects = await this.projectService.find({
- where: {
- admin: { id: userId },
- },
- })
-
- if (_isEmpty(projects)) {
- return {
- results: [],
- total: 0,
- page_total: 0,
- }
- }
-
- const pids = _map(projects, project => project.id)
-
- const result = await this.projectService.paginateMonitors(
- {
- take,
- skip,
- },
- { project: In(pids) },
- )
-
- // @ts-expect-error
- result.results = _map(result.results, monitor => ({
- ..._omit(monitor, ['project']),
- projectId: monitor.project.id,
- }))
-
- return result
- }
-
@ApiBearerAuth()
@Get('/:id')
@Auth([], true, true)
@@ -2322,13 +2275,7 @@ export class ProjectController {
throw new NotFoundException('Project not found.')
}
- const user = await this.userService.findUserV2(userId, ['roles'])
-
- if (!user) {
- throw new NotFoundException('User not found.')
- }
-
- this.projectService.allowedToManage(project, userId, user.roles)
+ this.projectService.allowedToManage(project, userId)
const monitor = await this.projectService.getMonitor(monitorId)
diff --git a/backend/apps/cloud/src/user/user.controller.ts b/backend/apps/cloud/src/user/user.controller.ts
index fe11316ed..260735232 100644
--- a/backend/apps/cloud/src/user/user.controller.ts
+++ b/backend/apps/cloud/src/user/user.controller.ts
@@ -105,18 +105,23 @@ export class UserController {
async me(@CurrentUserId() uid: string) {
this.logger.log({ uid }, 'GET /user/me')
- const [sharedProjects, organisationMemberships, user] = await Promise.all([
- this.authService.getSharedProjectsForUser(uid),
- this.authService.getOrganisationsForUser(uid),
- this.userService.findOne({ where: { id: uid } }),
- ])
+ const [sharedProjects, organisationMemberships, user, totalMonthlyEvents] =
+ await Promise.all([
+ this.authService.getSharedProjectsForUser(uid),
+ this.authService.getOrganisationsForUser(uid),
+ this.userService.findOne({ where: { id: uid } }),
+ this.projectService.getRedisCount(uid),
+ ])
const sanitizedUser = this.userService.omitSensitiveData(user)
sanitizedUser.sharedProjects = sharedProjects
sanitizedUser.organisationMemberships = organisationMemberships
- return sanitizedUser
+ return {
+ user: sanitizedUser,
+ totalMonthlyEvents,
+ }
}
@ApiBearerAuth()
diff --git a/backend/apps/community/src/project/project.controller.ts b/backend/apps/community/src/project/project.controller.ts
index 870907245..89862cf83 100644
--- a/backend/apps/community/src/project/project.controller.ts
+++ b/backend/apps/community/src/project/project.controller.ts
@@ -161,7 +161,6 @@ export class ProjectController {
results,
page_total: _size(formatted),
total: _size(formatted),
- totalMonthlyEvents: 0, // not needed as it's selfhosed
}
}