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!

Implemented a route to confirm a subscriber invite

Yevhenii Kulisidid690210641c7
diff --git a/src/action-tokens/action-tokens.service.ts b/src/action-tokens/action-tokens.service.ts
index 03715a67e..dd540c280 100644
--- a/src/action-tokens/action-tokens.service.ts
+++ b/src/action-tokens/action-tokens.service.ts
@@ -1,62 +1,70 @@
-import { Injectable } from '@nestjs/common'
-import { InjectRepository } from '@nestjs/typeorm'
-import { Repository } from 'typeorm'
-
-import { ActionToken, ActionTokenType } from './action-token.entity'
-import { User } from '../user/entities/user.entity'
-
-@Injectable()
-export class ActionTokensService {
- constructor(
- @InjectRepository(ActionToken)
- private actionTokensRepository: Repository<ActionToken>,
- ) {}
-
- async deleteMultiple(where: string): Promise<any> {
- return this.actionTokensRepository
- .createQueryBuilder()
- .delete()
- .where(where)
- .execute()
- }
-
- async createForUser(
- user: User,
- action: ActionTokenType,
- newValue: string = null,
- ): Promise<ActionToken> {
- return this.actionTokensRepository.save({ user, action, newValue })
- }
-
- async find(id: string): Promise<ActionToken> {
- return this.actionTokensRepository.findOneOrFail(id, {
- relations: ['user'],
- })
- }
-
- async delete(id: string): Promise<void> {
- await this.actionTokensRepository.delete(id)
- }
-
- public async createActionToken(
- userId: string,
- action: ActionTokenType,
- newValue?: string,
- ) {
- return await this.actionTokensRepository.save({
- user: { id: userId },
- action,
- newValue,
- })
- }
-
- public async findActionToken(token: string) {
- return await this.actionTokensRepository.findOne(token, {
- relations: ['user'],
- })
- }
-
- public async deleteActionToken(token: string) {
- await this.actionTokensRepository.delete(token)
- }
-}
+import { Injectable } from '@nestjs/common'
+import { InjectRepository } from '@nestjs/typeorm'
+import { Repository } from 'typeorm'
+
+import { ActionToken, ActionTokenType } from './action-token.entity'
+import { User } from '../user/entities/user.entity'
+
+@Injectable()
+export class ActionTokensService {
+ constructor(
+ @InjectRepository(ActionToken)
+ private actionTokensRepository: Repository<ActionToken>,
+ ) {}
+
+ async deleteMultiple(where: string): Promise<any> {
+ return this.actionTokensRepository
+ .createQueryBuilder()
+ .delete()
+ .where(where)
+ .execute()
+ }
+
+ async createForUser(
+ user: User,
+ action: ActionTokenType,
+ newValue: string = null,
+ ): Promise<ActionToken> {
+ return this.actionTokensRepository.save({ user, action, newValue })
+ }
+
+ async find(id: string): Promise<ActionToken> {
+ return this.actionTokensRepository.findOneOrFail(id, {
+ relations: ['user'],
+ })
+ }
+
+ async delete(id: string): Promise<void> {
+ await this.actionTokensRepository.delete(id)
+ }
+
+ public async createActionToken(
+ userId: string,
+ action: ActionTokenType,
+ newValue?: string,
+ ) {
+ return await this.actionTokensRepository.save({
+ user: { id: userId },
+ action,
+ newValue,
+ })
+ }
+
+ public async findActionToken(token: string) {
+ return await this.actionTokensRepository.findOne(token, {
+ relations: ['user'],
+ })
+ }
+
+ public async deleteActionToken(token: string) {
+ await this.actionTokensRepository.delete(token)
+ }
+
+ async getActionToken(token: string) {
+ return await this.actionTokensRepository.findOne({
+ where: {
+ id: token,
+ },
+ })
+ }
+}
diff --git a/src/project/project.controller.ts b/src/project/project.controller.ts
index d1218bbb4..e294e1752 100644
--- a/src/project/project.controller.ts
+++ b/src/project/project.controller.ts
@@ -841,13 +841,44 @@ export class ProjectController {
})
}
- @Post(':projectId/subscribers/invite')
+ @Get(':projectId/subscribers/invite')
@HttpCode(HttpStatus.OK)
async confirmSubscriberInvite(
@Param() params: ConfirmSubscriberInviteParamsDto,
@Query() queries: ConfirmSubscriberInviteQueriesDto,
): Promise<void> {
- // TODO: Implement
+ const project = await this.projectService.getProjectById(params.projectId)
+
+ if (!project) {
+ throw new NotFoundException('Project not found.')
+ }
+
+ const actionToken = await this.actionTokensService.getActionToken(
+ queries.token,
+ )
+
+ if (
+ !actionToken ||
+ actionToken.action !== ActionTokenType.ADDING_PROJECT_SUBSCRIBER
+ ) {
+ throw new BadRequestException('Invalid token.')
+ }
+
+ const [projectId, subscriberId] = actionToken.newValue.split(':')
+ const subscriber = await this.projectService.getSubscriber(
+ projectId,
+ subscriberId,
+ )
+
+ if (!subscriber) {
+ throw new NotFoundException('Subscriber not found.')
+ }
+
+ await this.projectService.confirmSubscriber(
+ projectId,
+ subscriberId,
+ actionToken.id,
+ )
}
@Get(':projectId/subscribers')
diff --git a/src/project/project.service.ts b/src/project/project.service.ts
index 84535c256..8142bad6f 100644
--- a/src/project/project.service.ts
+++ b/src/project/project.service.ts
@@ -456,6 +456,7 @@ export class ProjectService {
await this.sendSubscriberInvite(
data.userId,
data.projectId,
+ subscriber.id,
data.email,
data.origin,
)
@@ -466,13 +467,14 @@ export class ProjectService {
async sendSubscriberInvite(
userId: string,
projectId: string,
+ subscriberId: string,
email: string,
origin: string,
) {
const actionToken = await this.actionTokens.createActionToken(
userId,
ActionTokenType.ADDING_PROJECT_SUBSCRIBER,
- `${projectId}:${email}`,
+ `${projectId}:${subscriberId}`,
)
const inviteLink = `${origin}/projects/${projectId}/subscribers/invite?token=${actionToken.id}`
@@ -485,4 +487,28 @@ export class ProjectService {
},
)
}
+
+ async getProjectById(projectId: string) {
+ return await this.projectsRepository.findOne({
+ where: { id: projectId },
+ })
+ }
+
+ async getSubscriber(projectId: string, subscriberId: string) {
+ return await this.projectSubscriberRepository.findOne({
+ where: { id: subscriberId, projectId },
+ })
+ }
+
+ async confirmSubscriber(
+ projectId: string,
+ subscriberId: string,
+ token: string,
+ ) {
+ await this.projectSubscriberRepository.update(
+ { id: subscriberId, projectId },
+ { isConfirmed: true },
+ )
+ await this.actionTokens.deleteActionToken(token)
+ }
}