📈 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!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
Added function to generate and delete API key
Yevhenii K002eebe4c997
diff --git a/src/user/entities/user.entity.ts b/src/user/entities/user.entity.tsindex 40e192b67..b6bdafc0f 100644--- a/src/user/entities/user.entity.ts+++ b/src/user/entities/user.entity.ts@@ -158,4 +158,13 @@ export class User {@Column({ type: 'date', nullable: true })nextBillDate: Date++ @Column({+ type: 'varchar',+ length: 36,+ unique: true,+ nullable: true,+ default: null,+ })+ apiKey: string | null}diff --git a/src/user/user.controller.ts b/src/user/user.controller.tsindex a32e2e2e3..571477d07 100644--- a/src/user/user.controller.ts+++ b/src/user/user.controller.ts@@ -1,5 +1,5 @@-import {- Controller, Query, Req, Body, Param, Get, Post, Put, Delete, HttpCode, BadRequestException, UseGuards, MethodNotAllowedException,+import {+ Controller, Query, Req, Body, Param, Get, Post, Put, Delete, HttpCode, BadRequestException, UseGuards, MethodNotAllowedException, ConflictException,} from '@nestjs/common'import { Request } from 'express'import { ApiTags, ApiQuery, ApiResponse } from '@nestjs/swagger'@@ -11,6 +11,7 @@ import * as _isNull from 'lodash/isNull'import * as _isEmpty from 'lodash/isEmpty'import * as _isString from 'lodash/isString'import * as _omit from 'lodash/omit'+import { v4 as uuidv4 } from 'uuid'import { UserService } from './user.service'import { ProjectService } from '../project/project.service'@@ -254,14 +255,14 @@ export class UserController {@Roles(UserType.ADMIN)async update(@Body() userDTO: AdminUpdateUserProfileDTO, @Param('id') id: string): Promise<User> {this.logger.log({ userDTO, id }, 'PUT /user/:id')-+if (userDTO.password) {this.userService.validatePassword(userDTO.password)userDTO.password = await this.authService.hashPassword(userDTO.password)}-+const user = await this.userService.findOneWhere({ id })-+try {if (!user) {await this.userService.create({ ...userDTO })@@ -356,4 +357,40 @@ export class UserController {return user}++ @Post('api-key')+ @UseGuards(RolesGuard)+ @Roles(UserType.CUSTOMER, UserType.ADMIN)+ async generateApiKey(@CurrentUserId() userId: string): Promise<{+ generatedApiKey: string+ }> {+ const apiKey = await this.userService.findOne(userId, {+ select: ['apiKey'],+ })++ if (!_isNull(apiKey)) {+ throw new ConflictException('You already have an API key')+ }++ const generatedApiKey: string = uuidv4()++ await this.userService.update(userId, { apiKey: generatedApiKey })++ return { generatedApiKey }+ }++ @Delete('api-key')+ @UseGuards(RolesGuard)+ @Roles(UserType.CUSTOMER, UserType.ADMIN)+ async deleteApiKey(@CurrentUserId() userId: string): Promise<void> {+ const apiKey = await this.userService.findOne(userId, {+ select: ['apiKey'],+ })++ if (_isNull(apiKey)) {+ throw new ConflictException("You don't have an API key")+ }++ await this.userService.update(userId, { apiKey: null })+ }}