📈 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
fixing eslint and code formatting
Yevhenii Kulisidie1320c42adbd
diff --git a/src/action-tokens/action-token.entity.ts b/src/action-tokens/action-token.entity.tsindex 30daed747..1dfb4c2fb 100644--- a/src/action-tokens/action-token.entity.ts+++ b/src/action-tokens/action-token.entity.ts@@ -1,4 +1,11 @@-import { Entity, PrimaryGeneratedColumn, ManyToOne, Timestamp, CreateDateColumn, Column } from 'typeorm'+import {+ Entity,+ PrimaryGeneratedColumn,+ ManyToOne,+ Timestamp,+ CreateDateColumn,+ Column,+} from 'typeorm'import { User } from '../user/entities/user.entity'export enum ActionTokenType {diff --git a/src/action-tokens/action-tokens.module.ts b/src/action-tokens/action-tokens.module.tsindex a45093e22..44b8eff34 100644--- a/src/action-tokens/action-tokens.module.ts+++ b/src/action-tokens/action-tokens.module.ts@@ -5,10 +5,8 @@ import { ActionTokensService } from './action-tokens.service'import { ActionToken } from './action-token.entity'@Module({- imports: [- TypeOrmModule.forFeature([ActionToken]),- ],+ imports: [TypeOrmModule.forFeature([ActionToken])],providers: [ActionTokensService],- exports: [ActionTokensService]+ exports: [ActionTokensService],})export class ActionTokensModule {}diff --git a/src/action-tokens/action-tokens.service.ts b/src/action-tokens/action-tokens.service.tsindex cba33cbcb..e0ef0af07 100644--- a/src/action-tokens/action-tokens.service.ts+++ b/src/action-tokens/action-tokens.service.ts@@ -13,18 +13,25 @@ export class ActionTokensService {) {}async deleteMultiple(where: string): Promise<any> {- return this.actionTokensRepository.createQueryBuilder()+ return this.actionTokensRepository+ .createQueryBuilder().delete().where(where).execute()}- async createForUser(user: User, action: ActionTokenType, newValue: string = null): Promise<ActionToken> {+ 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'] })+ return this.actionTokensRepository.findOneOrFail(id, {+ relations: ['user'],+ })}async delete(id: string): Promise<void> {diff --git a/src/analytics/analytics.controller.ts b/src/analytics/analytics.controller.tsindex 56b528440..9bc908bbf 100644--- a/src/analytics/analytics.controller.ts+++ b/src/analytics/analytics.controller.ts@@ -3,7 +3,6 @@ import * as _isArray from 'lodash/isArray'import * as _toNumber from 'lodash/toNumber'import * as _size from 'lodash/size'import * as _last from 'lodash/last'-import * as _map from 'lodash/map'import * as dayjs from 'dayjs'import * as utc from 'dayjs/plugin/utc'import * as timezone from 'dayjs/plugin/timezone'@@ -11,15 +10,31 @@ import { v4 as uuidv4 } from 'uuid'import { hash } from 'blake3'import ct from 'countries-and-timezones'import {- Controller, Body, Query, UseGuards, Get, Post, Headers, BadRequestException, InternalServerErrorException,- UnprocessableEntityException, PreconditionFailedException, Ip, ForbiddenException, Response,+ Controller,+ Body,+ Query,+ UseGuards,+ Get,+ Post,+ Headers,+ BadRequestException,+ InternalServerErrorException,+ UnprocessableEntityException,+ PreconditionFailedException,+ Ip,+ ForbiddenException,+ Response,} from '@nestjs/common'import { ApiTags } from '@nestjs/swagger'import * as UAParser from 'ua-parser-js'import * as isbot from 'isbot'import {- AnalyticsService, getSessionKey, isValidTimezone, isValidDate, checkIfTBAllowed,+ AnalyticsService,+ getSessionKey,+ isValidTimezone,+ isValidDate,+ checkIfTBAllowed,} from './analytics.service'import { TaskManagerService } from '../task-manager/task-manager.service'import { CurrentUserId } from '../common/decorators/current-user-id.decorator'@@ -31,8 +46,14 @@ import { AnalyticsGET_DTO } from './dto/getData.dto'import { AppLoggerService } from '../logger/logger.service'import { SelfhostedGuard } from '../common/guards/selfhosted.guard'import {- REDIS_LOG_DATA_CACHE_KEY, redis, REDIS_LOG_CUSTOM_CACHE_KEY,- HEARTBEAT_SID_LIFE_TIME, REDIS_USERS_COUNT_KEY, REDIS_PROJECTS_COUNT_KEY, REDIS_PAGEVIEWS_COUNT_KEY, REDIS_SESSION_SALT_KEY,+ REDIS_LOG_DATA_CACHE_KEY,+ redis,+ REDIS_LOG_CUSTOM_CACHE_KEY,+ HEARTBEAT_SID_LIFE_TIME,+ REDIS_USERS_COUNT_KEY,+ REDIS_PROJECTS_COUNT_KEY,+ REDIS_PAGEVIEWS_COUNT_KEY,+ REDIS_SESSION_SALT_KEY,} from '../common/constants'import { BotDetection } from 'src/common/decorators/bot-detection.decorator'import { BotDetectionGuard } from 'src/common/guards/bot-detection.guard'@@ -40,10 +61,46 @@ import { BotDetectionGuard } from 'src/common/guards/bot-detection.guard'dayjs.extend(utc)dayjs.extend(timezone)-const getSessionKeyCustom = (ip: string, ua: string, pid: string, ev: string, salt: string = '') => `cses_${hash(`${ua}${ip}${pid}${ev}${salt}`).toString('hex')}`--const analyticsDTO = (pid: string, pg: string, dv: string, br: string, os: string, lc: string, ref: string, so: string, me: string, ca: string, lt: number | string, cc: string, unique: number): Array<string | number> => {- return [uuidv4(), pid, pg, dv, br, os, lc, ref, so, me, ca, lt, cc, unique, dayjs.utc().format('YYYY-MM-DD HH:mm:ss')]+const getSessionKeyCustom = (+ ip: string,+ ua: string,+ pid: string,+ ev: string,+ salt = '',+) => `cses_${hash(`${ua}${ip}${pid}${ev}${salt}`).toString('hex')}`++const analyticsDTO = (+ pid: string,+ pg: string,+ dv: string,+ br: string,+ os: string,+ lc: string,+ ref: string,+ so: string,+ me: string,+ ca: string,+ lt: number | string,+ cc: string,+ unique: number,+): Array<string | number> => {+ return [+ uuidv4(),+ pid,+ pg,+ dv,+ br,+ os,+ lc,+ ref,+ so,+ me,+ ca,+ lt,+ cc,+ unique,+ dayjs.utc().format('YYYY-MM-DD HH:mm:ss'),+ ]}const customLogDTO = (pid: string, ev: string): string => {@@ -57,7 +114,7 @@ const customLogDTO = (pid: string, ev: string): string => {return JSON.stringify(dto)}-const getElValue = (el) => {+const getElValue = el => {if (el === undefined || el === null || el === 'NULL') return 'NULL'return `'${el}'`}@@ -65,8 +122,14 @@ const getElValue = (el) => {const getPIDsArray = (pids, pid) => {const pidsEmpty = _isEmpty(pids)const pidEmpty = _isEmpty(pid)- if (pidsEmpty && pidEmpty) throw new BadRequestException('An array of Project ID\'s (pids) or a Project ID (pid) has to be provided')- else if (!pidsEmpty && !pidEmpty) throw new BadRequestException('Please provide either an array of Project ID\'s (pids) or a Project ID (pid), but not both')+ if (pidsEmpty && pidEmpty)+ throw new BadRequestException(+ "An array of Project ID's (pids) or a Project ID (pid) has to be provided",+ )+ else if (!pidsEmpty && !pidEmpty)+ throw new BadRequestException(+ "Please provide either an array of Project ID's (pids) or a Project ID (pid), but not both",+ )else if (!pidEmpty) {pids = JSON.stringify([pid])}@@ -74,18 +137,25 @@ const getPIDsArray = (pids, pid) => {try {pids = JSON.parse(pids)} catch (e) {- throw new UnprocessableEntityException('Cannot process the provided array of Project ID\'s')+ throw new UnprocessableEntityException(+ "Cannot process the provided array of Project ID's",+ )}if (!_isArray(pids)) {- throw new UnprocessableEntityException('An array of Project ID\'s has to be provided as a \'pids\' param')+ throw new UnprocessableEntityException(+ "An array of Project ID's has to be provided as a 'pids' param",+ )}return pids}// needed for serving 1x1 px GIF-const TRANSPARENT_GIF_BUFFER = Buffer.from('R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=', 'base64')+const TRANSPARENT_GIF_BUFFER = Buffer.from(+ 'R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=',+ 'base64',+)@ApiTags('Analytics')@UseGuards(RolesGuard)@@ -95,11 +165,22 @@ export class AnalyticsController {private readonly analyticsService: AnalyticsService,private readonly logger: AppLoggerService,private readonly taskManagerService: TaskManagerService,- ) { }+ ) {}@Get('/')- async getData(@Query() data: AnalyticsGET_DTO, @CurrentUserId() uid: string): Promise<any> {- const { pid, period, timeBucket, from, to, filters, timezone = DEFAULT_TIMEZONE } = data+ async getData(+ @Query() data: AnalyticsGET_DTO,+ @CurrentUserId() uid: string,+ ): Promise<any> {+ const {+ pid,+ period,+ timeBucket,+ from,+ to,+ filters,+ timezone = DEFAULT_TIMEZONE,+ } = datathis.analyticsService.validatePID(pid)if (!_isEmpty(period)) {@@ -107,12 +188,15 @@ export class AnalyticsController {}this.analyticsService.validateTimebucket(timeBucket)- const [filtersQuery, filtersParams] = this.analyticsService.getFiltersQuery(filters)+ const [filtersQuery, filtersParams] =+ this.analyticsService.getFiltersQuery(filters)await this.analyticsService.checkProjectAccess(pid, uid)- let groupFrom = from, groupTo = to+ let groupFrom = from,+ groupTo = to- const queryCustoms = 'SELECT ev, count() FROM customEV WHERE pid = {pid:FixedString(12)} AND created BETWEEN {groupFrom:String} AND {groupTo:String} GROUP BY ev'+ const queryCustoms =+ 'SELECT ev, count() FROM customEV WHERE pid = {pid:FixedString(12)} AND created BETWEEN {groupFrom:String} AND {groupTo:String} GROUP BY ev'const subQuery = `FROM analytics WHERE pid = {pid:FixedString(12)} ${filtersQuery} AND created BETWEEN {groupFrom:String} AND {groupTo:String}`const paramsData = {@@ -126,15 +210,21 @@ export class AnalyticsController {if (!_isEmpty(from) && !_isEmpty(to)) {if (!isValidDate(from)) {- throw new PreconditionFailedException('The timeframe \'from\' parameter is invalid')+ throw new PreconditionFailedException(+ "The timeframe 'from' parameter is invalid",+ )}if (!isValidDate(to)) {- throw new PreconditionFailedException('The timeframe \'to\' parameter is invalid')+ throw new PreconditionFailedException(+ "The timeframe 'to' parameter is invalid",+ )}if (dayjs.utc(from).isAfter(dayjs.utc(to), 'second')) {- throw new PreconditionFailedException('The timeframe \'from\' parameter cannot be greater than \'to\'')+ throw new PreconditionFailedException(+ "The timeframe 'from' parameter cannot be greater than 'to'",+ )}checkIfTBAllowed(timeBucket, from, to)@@ -142,14 +232,21 @@ export class AnalyticsController {groupFrom = dayjs.tz(from, timezone).utc().format('YYYY-MM-DD HH:mm:ss')if (from === to) {- groupTo = dayjs.tz(to, timezone).add(1, 'day').format('YYYY-MM-DD HH:mm:ss')+ groupTo = dayjs+ .tz(to, timezone)+ .add(1, 'day')+ .format('YYYY-MM-DD HH:mm:ss')} else {groupTo = dayjs.tz(to, timezone).format('YYYY-MM-DD HH:mm:ss')}} else if (!_isEmpty(period)) {if (period === 'today') {if (timezone !== DEFAULT_TIMEZONE && isValidTimezone(timezone)) {- groupFrom = dayjs().tz(timezone).startOf('d').utc().format('YYYY-MM-DD HH:mm:ss')+ groupFrom = dayjs()+ .tz(timezone)+ .startOf('d')+ .utc()+ .format('YYYY-MM-DD HH:mm:ss')groupTo = dayjs().tz(timezone).utc().format('YYYY-MM-DD HH:mm:ss')} else {groupFrom = dayjs.utc().startOf('d').format('YYYY-MM-DD')@@ -157,30 +254,60 @@ export class AnalyticsController {}} else if (period === 'yesterday') {if (timezone !== DEFAULT_TIMEZONE && isValidTimezone(timezone)) {- groupFrom = dayjs().tz(timezone).startOf('d').subtract(1, 'day').utc().format('YYYY-MM-DD HH:mm:ss')- groupTo = dayjs().tz(timezone).startOf('d').utc().format('YYYY-MM-DD HH:mm:ss')+ groupFrom = dayjs()+ .tz(timezone)+ .startOf('d')+ .subtract(1, 'day')+ .utc()+ .format('YYYY-MM-DD HH:mm:ss')+ groupTo = dayjs()+ .tz(timezone)+ .startOf('d')+ .utc()+ .format('YYYY-MM-DD HH:mm:ss')} else {- groupFrom = dayjs.utc().startOf('d').subtract(1, 'day').format('YYYY-MM-DD')+ groupFrom = dayjs+ .utc()+ .startOf('d')+ .subtract(1, 'day')+ .format('YYYY-MM-DD')groupTo = dayjs.utc().startOf('d').format('YYYY-MM-DD HH:mm:ss')}} else {- groupFrom = dayjs.utc().subtract(parseInt(period), _last(period)).format('YYYY-MM-DD')+ groupFrom = dayjs+ .utc()+ .subtract(parseInt(period), _last(period))+ .format('YYYY-MM-DD')groupTo = dayjs.utc().format('YYYY-MM-DD 23:59:59')checkIfTBAllowed(timeBucket, groupFrom, groupTo)}} else {- throw new BadRequestException('The timeframe (either from/to pair or period) has to be provided')+ throw new BadRequestException(+ 'The timeframe (either from/to pair or period) has to be provided',+ )}paramsData.params = {...paramsData.params,- groupFrom, groupTo,+ groupFrom,+ groupTo,}- const result = await this.analyticsService.groupByTimeBucket(timeBucket, groupFrom, groupTo, subQuery, filtersQuery, paramsData, timezone)-- const customs = await this.analyticsService.processCustomEV(queryCustoms, paramsData)+ const result = await this.analyticsService.groupByTimeBucket(+ timeBucket,+ groupFrom,+ groupTo,+ subQuery,+ filtersQuery,+ paramsData,+ timezone,+ )++ const customs = await this.analyticsService.processCustomEV(+ queryCustoms,+ paramsData,+ )return {...result,@@ -191,7 +318,10 @@ export class AnalyticsController {@Get('/birdseye')// returns overall short statistics per project- async getOverallStats(@Query() data, @CurrentUserId() uid: string): Promise<any> {+ async getOverallStats(+ @Query() data,+ @CurrentUserId() uid: string,+ ): Promise<any> {const { pids, pid } = dataconst pidsArray = getPIDsArray(pids, pid)@@ -206,7 +336,11 @@ export class AnalyticsController {@UseGuards(SelfhostedGuard)@Get('/generalStats')async getGeneralStats(): Promise<object> {- const exists = await redis.exists(REDIS_USERS_COUNT_KEY, REDIS_PROJECTS_COUNT_KEY, REDIS_PAGEVIEWS_COUNT_KEY)+ const exists = await redis.exists(+ REDIS_USERS_COUNT_KEY,+ REDIS_PROJECTS_COUNT_KEY,+ REDIS_PAGEVIEWS_COUNT_KEY,+ )if (exists) {const users = _toNumber(await redis.get(REDIS_USERS_COUNT_KEY))@@ -214,7 +348,9 @@ export class AnalyticsController {const pageviews = _toNumber(await redis.get(REDIS_PAGEVIEWS_COUNT_KEY))return {- users, projects, pageviews,+ users,+ projects,+ pageviews,}}@@ -222,7 +358,10 @@ export class AnalyticsController {}@Get('/hb')- async getHeartBeatStats(@Query() data, @CurrentUserId() uid: string): Promise<object> {+ async getHeartBeatStats(+ @Query() data,+ @CurrentUserId() uid: string,+ ): Promise<object> {const { pids, pid } = dataconst pidsArray = getPIDsArray(pids, pid)const pidsSize = _size(pidsArray)@@ -248,20 +387,33 @@ export class AnalyticsController {@Post('/custom')@UseGuards(BotDetectionGuard)@BotDetection()- async logCustom(@Body() eventsDTO: EventsDTO, @Headers() headers, @Ip() reqIP): Promise<any> {+ async logCustom(+ @Body() eventsDTO: EventsDTO,+ @Headers() headers,+ @Ip() reqIP,+ ): Promise<any> {const { 'user-agent': userAgent, origin } = headers- const ip = headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''+ const ip =+ headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''await this.analyticsService.validate(eventsDTO, origin, 'custom', ip)if (eventsDTO.unique) {const salt = await redis.get(REDIS_SESSION_SALT_KEY)- const sessionHash = getSessionKeyCustom(ip, userAgent, eventsDTO.pid, eventsDTO.ev, salt)+ const sessionHash = getSessionKeyCustom(+ ip,+ userAgent,+ eventsDTO.pid,+ eventsDTO.ev,+ salt,+ )const unique = await this.analyticsService.isUnique(sessionHash)if (!unique) {- throw new ForbiddenException('The unique option provided, while the custom event have already been created for this session')+ throw new ForbiddenException(+ 'The unique option provided, while the custom event have already been created for this session',+ )}}@@ -272,17 +424,28 @@ export class AnalyticsController {return} catch (e) {this.logger.error(e)- throw new InternalServerErrorException('Error occured while saving the custom event')+ throw new InternalServerErrorException(+ 'Error occured while saving the custom event',+ )}}@Post('/hb')- async heartbeat(@Body() logDTO: PageviewsDTO, @Headers() headers, @Ip() reqIP): Promise<any> {+ async heartbeat(+ @Body() logDTO: PageviewsDTO,+ @Headers() headers,+ @Ip() reqIP,+ ): Promise<any> {const { 'user-agent': userAgent } = headersconst { pid } = logDTO- const ip = headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''-- const sessionID = await this.analyticsService.validateHB(logDTO, userAgent, ip)+ const ip =+ headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''++ const sessionID = await this.analyticsService.validateHB(+ logDTO,+ userAgent,+ ip,+ )await redis.set(`hb:${pid}:${sessionID}`, 1, 'EX', HEARTBEAT_SID_LIFE_TIME)return}@@ -291,10 +454,15 @@ export class AnalyticsController {@Post('/')@UseGuards(BotDetectionGuard)@BotDetection()- async log(@Body() logDTO: PageviewsDTO, @Headers() headers, @Ip() reqIP): Promise<any> {+ async log(+ @Body() logDTO: PageviewsDTO,+ @Headers() headers,+ @Ip() reqIP,+ ): Promise<any> {const { 'user-agent': userAgent, origin } = headers- const ip = headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''+ const ip =+ headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''await this.analyticsService.validate(logDTO, origin, 'log', ip)@@ -309,12 +477,44 @@ export class AnalyticsController {const br = ua.browser.nameconst os = ua.os.name// trying to get country from timezome, otherwise using CloudFlare's IP based country code as a fallback- const cc = ct.getCountryForTimezone(logDTO.tz)?.id || (headers['cf-ipcountry'] === 'XX' ? 'NULL' : headers['cf-ipcountry'])- dto = analyticsDTO(logDTO.pid, logDTO.pg, dv, br, os, logDTO.lc, logDTO.ref, logDTO.so, logDTO.me, logDTO.ca, 'NULL' /* logDTO.lt */, cc, 1)+ const cc =+ ct.getCountryForTimezone(logDTO.tz)?.id ||+ (headers['cf-ipcountry'] === 'XX' ? 'NULL' : headers['cf-ipcountry'])+ dto = analyticsDTO(+ logDTO.pid,+ logDTO.pg,+ dv,+ br,+ os,+ logDTO.lc,+ logDTO.ref,+ logDTO.so,+ logDTO.me,+ logDTO.ca,+ 'NULL' /* logDTO.lt */,+ cc,+ 1,+ )} else if (!logDTO.unique) {- dto = analyticsDTO(logDTO.pid, logDTO.pg, 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 0)+ dto = analyticsDTO(+ logDTO.pid,+ logDTO.pg,+ 'NULL',+ 'NULL',+ 'NULL',+ 'NULL',+ 'NULL',+ 'NULL',+ 'NULL',+ 'NULL',+ 'NULL',+ 'NULL',+ 0,+ )} else {- throw new BadRequestException('Event was not saved because it was not unique while unique only param is provided')+ throw new BadRequestException(+ 'Event was not saved because it was not unique while unique only param is provided',+ )}// todo: fix: may be vulnerable to sql injection attack@@ -324,14 +524,21 @@ export class AnalyticsController {return} catch (e) {this.logger.error(e)- throw new InternalServerErrorException('Error occured while saving the log data')+ throw new InternalServerErrorException(+ 'Error occured while saving the log data',+ )}}// Fallback for logging pageviews for users with JavaScript disabled// Returns 1x1 transparent gif@Get('/noscript')- async noscript(@Query() data, @Headers() headers, @Ip() reqIP, @Response() res): Promise<any> {+ async noscript(+ @Query() data,+ @Headers() headers,+ @Ip() reqIP,+ @Response() res,+ ): Promise<any> {const { 'user-agent': userAgent, origin } = headersconst { pid } = data@@ -347,7 +554,8 @@ export class AnalyticsController {await this.analyticsService.validate(logDTO, origin)- const ip = headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''+ const ip =+ headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''const salt = await redis.get(REDIS_SESSION_SALT_KEY)const sessionHash = getSessionKey(ip, userAgent, logDTO.pid, salt)@@ -361,10 +569,39 @@ export class AnalyticsController {const br = ua.browser.nameconst os = ua.os.name// using CloudFlare's IP based country code- const cc = headers['cf-ipcountry'] === 'XX' ? 'NULL' : headers['cf-ipcountry']- dto = analyticsDTO(logDTO.pid, 'NULL', dv, br, os, 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', cc, 1)+ const cc =+ headers['cf-ipcountry'] === 'XX' ? 'NULL' : headers['cf-ipcountry']+ dto = analyticsDTO(+ logDTO.pid,+ 'NULL',+ dv,+ br,+ os,+ 'NULL',+ 'NULL',+ 'NULL',+ 'NULL',+ 'NULL',+ 'NULL',+ cc,+ 1,+ )} else {- dto = analyticsDTO(logDTO.pid, 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 0)+ dto = analyticsDTO(+ logDTO.pid,+ 'NULL',+ 'NULL',+ 'NULL',+ 'NULL',+ 'NULL',+ 'NULL',+ 'NULL',+ 'NULL',+ 'NULL',+ 'NULL',+ 'NULL',+ 0,+ )}// todo: fix: may be vulnerable to sql injection attackdiff --git a/src/analytics/analytics.service.ts b/src/analytics/analytics.service.tsindex a36893a0c..c141ce7b7 100644--- a/src/analytics/analytics.service.ts+++ b/src/analytics/analytics.service.ts@@ -1,15 +1,11 @@import * as _isEmpty from 'lodash/isEmpty'-import * as _isString from 'lodash/isString'import * as _split from 'lodash/split'-import * as _filter from 'lodash/filter'import * as _size from 'lodash/size'-import * as _isNull from 'lodash/isNull'import * as _includes from 'lodash/includes'import * as _map from 'lodash/map'import * as _toUpper from 'lodash/toUpper'import * as _join from 'lodash/join'import * as _some from 'lodash/some'-import * as _keys from 'lodash/keys'import * as _find from 'lodash/find'import * as _values from 'lodash/values'import * as dayjs from 'dayjs'@@ -19,14 +15,25 @@ import * as ipRangeCheck from 'ip-range-check'import { isLocale } from 'validator'import { hash } from 'blake3'import {- Injectable, BadRequestException, InternalServerErrorException, ForbiddenException, UnprocessableEntityException,+ Injectable,+ BadRequestException,+ InternalServerErrorException,+ ForbiddenException,+ UnprocessableEntityException,PreconditionFailedException,} from '@nestjs/common'import { ACCOUNT_PLANS, DEFAULT_TIMEZONE } from '../user/entities/user.entity'import {- redis, isValidPID, getRedisProjectKey, redisProjectCacheTimeout, UNIQUE_SESSION_LIFE_TIME, clickhouse,- getPercentageChange, isSelfhosted, REDIS_SESSION_SALT_KEY,+ redis,+ isValidPID,+ getRedisProjectKey,+ redisProjectCacheTimeout,+ UNIQUE_SESSION_LIFE_TIME,+ clickhouse,+ getPercentageChange,+ isSelfhosted,+ REDIS_SESSION_SALT_KEY,} from '../common/constants'import { getProjectsClickhouse } from '../common/utils'import { PageviewsDTO } from './dto/pageviews.dto'@@ -38,10 +45,20 @@ import { TimeBucketType } from './dto/getData.dto'dayjs.extend(utc)dayjs.extend(timezone)-export const getSessionKey = (ip: string, ua: string, pid: string, salt: string = '') => `ses_${hash(`${ua}${ip}${pid}${salt}`).toString('hex')}`+export const getSessionKey = (ip: string, ua: string, pid: string, salt = '') =>+ `ses_${hash(`${ua}${ip}${pid}${salt}`).toString('hex')}`export const cols = [- 'cc', 'pg', 'lc', 'br', 'os', 'dv', 'ref', 'so', 'me', 'ca',+ 'cc',+ 'pg',+ 'lc',+ 'br',+ 'os',+ 'dv',+ 'ref',+ 'so',+ 'me',+ 'ca',]interface chartCHResponse {@@ -55,8 +72,22 @@ interface customsCHResponse {'count()': number}-const validPeriods = ['today', 'yesterday', '1d', '7d', '4w', '3M', '12M', '24M']-const validTimebuckets = [TimeBucketType.HOUR, TimeBucketType.DAY, TimeBucketType.WEEK, TimeBucketType.MONTH]+const validPeriods = [+ 'today',+ 'yesterday',+ '1d',+ '7d',+ '4w',+ '3M',+ '12M',+ '24M',+]+const validTimebuckets = [+ TimeBucketType.HOUR,+ TimeBucketType.DAY,+ TimeBucketType.WEEK,+ TimeBucketType.MONTH,+]// mapping of allowed timebuckets per difference between days// (e.g. if difference is lower than (lt) (including) -> then the specified timebuckets are allowed to be applied)@@ -70,7 +101,10 @@ const timeBucketToDays = [// Smaller than 64 characters, must start with an English letter and contain only letters (a-z A-Z), numbers (0-9), underscores (_) and dots (.)const customEVvalidate = /^[a-zA-Z](?:[\w\.]){0,62}$/-interface GetFiltersQuery extends Array<string | object> { 0: string; 1: object }+interface GetFiltersQuery extends Array<string | object> {+ 0: string+ 1: object+}export const isValidTimezone = (timezone: string): boolean => {if (_isEmpty(timezone)) {@@ -85,7 +119,7 @@ export const isValidTimezone = (timezone: string): boolean => {}}-export const isValidDate = (date: string, format: string = 'YYYY-MM-DD'): boolean => {+export const isValidDate = (date: string, format = 'YYYY-MM-DD'): boolean => {if (_isEmpty(date)) {return false}@@ -93,27 +127,33 @@ export const isValidDate = (date: string, format: string = 'YYYY-MM-DD'): booleareturn dayjs(date, format).format(format) === date}-export const checkIfTBAllowed = (timeBucket: TimeBucketType, from: string, to: string): void => {+export const checkIfTBAllowed = (+ timeBucket: TimeBucketType,+ from: string,+ to: string,+): void => {const diff = dayjs(to).diff(dayjs(from), 'days')const tbMap = _find(timeBucketToDays, ({ lt }) => diff <= lt)if (_isEmpty(tbMap)) {- throw new PreconditionFailedException('The difference between \'from\' and \'to\' is greater than allowed')+ throw new PreconditionFailedException(+ "The difference between 'from' and 'to' is greater than allowed",+ )}const { tb } = tbMapif (!_includes(tb, timeBucket)) {- throw new PreconditionFailedException('The specified \'timeBucket\' parameter cannot be applied to the date range')+ throw new PreconditionFailedException(+ "The specified 'timeBucket' parameter cannot be applied to the date range",+ )}}@Injectable()export class AnalyticsService {- constructor(- private readonly projectService: ProjectService,- ) { }+ constructor(private readonly projectService: ProjectService) {}async getRedisProject(pid: string): Promise<Project | null> {const pidKey = getRedisProjectKey(pid)@@ -131,7 +171,10 @@ export class AnalyticsService {select: ['origins', 'active', 'admin', 'public', 'ipBlacklist'],})}- if (_isEmpty(project)) throw new BadRequestException('The provided Project ID (pid) is incorrect')+ if (_isEmpty(project))+ throw new BadRequestException(+ 'The provided Project ID (pid) is incorrect',+ )if (!isSelfhosted) {const share = await this.projectService.findShare({@@ -144,7 +187,12 @@ export class AnalyticsService {project = { ...project, share }}- await redis.set(pidKey, JSON.stringify(project), 'EX', redisProjectCacheTimeout)+ await redis.set(+ pidKey,+ JSON.stringify(project),+ 'EX',+ redisProjectCacheTimeout,+ )} else {try {project = JSON.parse(project)@@ -168,30 +216,49 @@ export class AnalyticsService {if (!_isEmpty(project.origins) && !_isEmpty(origin)) {if (origin === 'null') {if (!_includes(project.origins, 'null')) {- throw new BadRequestException('\'null\' origin is not added to your project\'s whitelist. To send requests from this origin either add it to your origins policy or leave it empty.')+ throw new BadRequestException(+ "'null' origin is not added to your project's whitelist. To send requests from this origin either add it to your origins policy or leave it empty.",+ )}} else {const hostname = new URL(origin).hostnameif (!_includes(project.origins, hostname)) {- throw new BadRequestException('This origin is prohibited by the project\'s origins policy')+ throw new BadRequestException(+ "This origin is prohibited by the project's origins policy",+ )}}}}checkIpBlacklist(project: Project, ip: string): void {- if (!_isEmpty(project.ipBlacklist) && ipRangeCheck(ip, project.ipBlacklist)) {- throw new BadRequestException('Incoming analytics is disabled for this IP address')+ if (+ !_isEmpty(project.ipBlacklist) &&+ ipRangeCheck(ip, project.ipBlacklist)+ ) {+ throw new BadRequestException(+ 'Incoming analytics is disabled for this IP address',+ )}}validatePID(pid: string): void {- if (_isEmpty(pid)) throw new BadRequestException('The Project ID (pid) has to be provided')- if (!isValidPID(pid)) throw new BadRequestException('The provided Project ID (pid) is incorrect')+ if (_isEmpty(pid))+ throw new BadRequestException('The Project ID (pid) has to be provided')+ if (!isValidPID(pid))+ throw new BadRequestException(+ 'The provided Project ID (pid) is incorrect',+ )}- async validate(logDTO: PageviewsDTO | EventsDTO, origin: string, type: 'custom' | 'log' = 'log', ip?: string): Promise<string | null> {- if (_isEmpty(logDTO)) throw new BadRequestException('The request cannot be empty')+ async validate(+ logDTO: PageviewsDTO | EventsDTO,+ origin: string,+ type: 'custom' | 'log' = 'log',+ ip?: string,+ ): Promise<string | null> {+ if (_isEmpty(logDTO))+ throw new BadRequestException('The request cannot be empty')const { pid } = logDTOthis.validatePID(pid)@@ -205,9 +272,12 @@ export class AnalyticsService {}if (!customEVvalidate.test(ev)) {- throw new BadRequestException('An incorrect event name (ev) is provided')+ throw new BadRequestException(+ 'An incorrect event name (ev) is provided',+ )}- } else { // the type is 'log'+ } else {+ // the type is 'log'// 'tz' does not need validation as it's based on getCountryForTimezone detection// @ts-ignoreconst { lc } = logDTO@@ -235,14 +305,20 @@ export class AnalyticsService {this.checkIpBlacklist(project, ip)- if (!project.active) throw new BadRequestException('Incoming analytics is disabled for this project')+ if (!project.active)+ throw new BadRequestException(+ 'Incoming analytics is disabled for this project',+ )if (!isSelfhosted) {const count = await this.projectService.getRedisCount(project.admin.id)- const maxCount = ACCOUNT_PLANS[project.admin.planCode].monthlyUsageLimit || 0+ const maxCount =+ ACCOUNT_PLANS[project.admin.planCode].monthlyUsageLimit || 0if (count >= maxCount) {- throw new ForbiddenException('You have exceeded the available monthly request limit for your account. Please upgrade your account plan if you need more requests.')+ throw new ForbiddenException(+ 'You have exceeded the available monthly request limit for your account. Please upgrade your account plan if you need more requests.',+ )}}@@ -251,8 +327,13 @@ export class AnalyticsService {return null}- async validateHB(logDTO: PageviewsDTO, userAgent: string, ip: string): Promise<string | null> {- if (_isEmpty(logDTO)) throw new BadRequestException('The request cannot be empty')+ async validateHB(+ logDTO: PageviewsDTO,+ userAgent: string,+ ip: string,+ ): Promise<string | null> {+ if (_isEmpty(logDTO))+ throw new BadRequestException('The request cannot be empty')const { pid } = logDTOthis.validatePID(pid)@@ -261,7 +342,8 @@ export class AnalyticsService {const sessionHash = getSessionKey(ip, userAgent, pid, salt)const sessionExists = await this.isSessionOpen(sessionHash)- if (!sessionExists) throw new ForbiddenException('The Heartbeat session does not exist')+ if (!sessionExists)+ throw new ForbiddenException('The Heartbeat session does not exist')return sessionHash}@@ -297,7 +379,9 @@ export class AnalyticsService {const { column, filter, isExclusive } = parsed[i]if (!_includes(cols, column)) {- throw new UnprocessableEntityException(`The provided filter (${column}) is not supported`)+ throw new UnprocessableEntityException(+ `The provided filter (${column}) is not supported`,+ )}// working only on 1 filter per 1 columnconst colFilter = `cf_${column}`@@ -306,7 +390,9 @@ export class AnalyticsService {...params,[colFilter]: filter,}- query += ` ${isExclusive ? 'AND NOT' : 'AND'} ${column}={${colFilter}:String}`+ query += ` ${+ isExclusive ? 'AND NOT' : 'AND'+ } ${column}={${colFilter}:String}`}return [query, params]@@ -314,7 +400,9 @@ export class AnalyticsService {validateTimebucket(tb: TimeBucketType): void {if (!_includes(validTimebuckets, tb)) {- throw new UnprocessableEntityException('The provided timebucket is incorrect')+ throw new UnprocessableEntityException(+ 'The provided timebucket is incorrect',+ )}}@@ -334,7 +422,10 @@ export class AnalyticsService {for (let i = 0; i < _size(pids); ++i) {const pid = pids[i]- if (!isValidPID(pid)) throw new BadRequestException(`The provided Project ID (${pid}) is incorrect`)+ if (!isValidPID(pid))+ throw new BadRequestException(+ `The provided Project ID (${pid}) is incorrect`,+ )const now = dayjs.utc().format('YYYY-MM-DD HH:mm:ss')const oneWRaw = dayjs.utc().subtract(1, period)@@ -357,10 +448,16 @@ export class AnalyticsService {const q1res = await clickhouse.query(query1, paramsData).toPromise()const q2res = await clickhouse.query(query2, paramsData).toPromise()- const thisWeekUnique = _find(q1res, ({ unique }) => unique)?.['count()'] || 0- const thisWeekPV = (_find(q1res, ({ unique }) => !unique)?.['count()'] || 0) + thisWeekUnique- const lastWeekUnique = _find(q2res, ({ unique }) => unique)?.['count()'] || 0- const lastWeekPV = (_find(q2res, ({ unique }) => !unique)?.['count()'] || 0) + lastWeekUnique+ const thisWeekUnique =+ _find(q1res, ({ unique }) => unique)?.['count()'] || 0+ const thisWeekPV =+ (_find(q1res, ({ unique }) => !unique)?.['count()'] || 0) ++ thisWeekUnique+ const lastWeekUnique =+ _find(q2res, ({ unique }) => unique)?.['count()'] || 0+ const lastWeekPV =+ (_find(q2res, ({ unique }) => !unique)?.['count()'] || 0) ++ lastWeekUniqueresult[pid] = {thisWeek: thisWeekPV,@@ -371,17 +468,27 @@ export class AnalyticsService {percChangeUnique: getPercentageChange(thisWeekUnique, lastWeekUnique),}} catch {- throw new InternalServerErrorException('Can\'t process the provided PID. Please, try again later.')+ throw new InternalServerErrorException(+ "Can't process the provided PID. Please, try again later.",+ )}}return result}- async groupByTimeBucket(timeBucket: TimeBucketType, from: string, to: string, subQuery: string, filtersQuery: string, paramsData: object, timezone: string): Promise<object | void> {+ async groupByTimeBucket(+ timeBucket: TimeBucketType,+ from: string,+ to: string,+ subQuery: string,+ filtersQuery: string,+ paramsData: object,+ timezone: string,+ ): Promise<object | void> {const params = {}- for (let i of cols) {+ for (const i of cols) {const query1 = `SELECT ${i}, count(*) ${subQuery} AND ${i} IS NOT NULL GROUP BY ${i}`const res = await clickhouse.query(query1, paramsData).toPromise()@@ -395,7 +502,7 @@ export class AnalyticsService {}}- if (!_some(_values(params), (val) => !_isEmpty(val))) {+ if (!_some(_values(params), val => !_isEmpty(val))) {return Promise.resolve()}@@ -435,11 +542,17 @@ export class AnalyticsService {query += ' UNION ALL '}- query += `select ${i} index, unique, count() from analytics where pid = {pid:FixedString(12)} and created between '${xM[i]}' and '${xM[1 + i]}' ${filtersQuery} group by unique`+ query += `select ${i} index, unique, count() from analytics where pid = {pid:FixedString(12)} and created between '${+ xM[i]+ }' and '${xM[1 + i]}' ${filtersQuery} group by unique`}// @ts-ignore- const result: Array<chartCHResponse> = (await clickhouse.query(query, paramsData).toPromise()).sort((a, b) => a.index - b.index)+ const result: Array<chartCHResponse> = (+ await clickhouse.query(query, paramsData).toPromise()+ )+ // @ts-ignore+ .sort((a, b) => a.index - b.index)const visits = []const uniques = []@@ -478,7 +591,9 @@ export class AnalyticsService {}if (timezone !== DEFAULT_TIMEZONE && isValidTimezone(timezone)) {- x = _map(x, el => dayjs.utc(el).tz(timezone).format('YYYY-MM-DD HH:mm:ss'))+ x = _map(x, el =>+ dayjs.utc(el).tz(timezone).format('YYYY-MM-DD HH:mm:ss'),+ )}return Promise.resolve({@@ -495,7 +610,9 @@ export class AnalyticsService {const result = {}// @ts-ignore- const rawCustoms: Array<customsCHResponse> = await clickhouse.query(query, params).toPromise()+ const rawCustoms: Array<customsCHResponse> = await clickhouse+ .query(query, params)+ .toPromise()const size = _size(rawCustoms)for (let i = 0; i < size; ++i) {diff --git a/src/analytics/dto/events.dto.ts b/src/analytics/dto/events.dto.tsindex 026443cb3..3c3fcdf50 100644--- a/src/analytics/dto/events.dto.ts+++ b/src/analytics/dto/events.dto.ts@@ -19,7 +19,8 @@ export class EventsDTO {ev: string@ApiProperty({- description: 'If true, only 1 event with the same ID will be saved per user session',+ description:+ 'If true, only 1 event with the same ID will be saved per user session',})unique: object}diff --git a/src/analytics/dto/getData.dto.ts b/src/analytics/dto/getData.dto.tsindex 76cb64b56..1c2f82c8f 100644--- a/src/analytics/dto/getData.dto.ts+++ b/src/analytics/dto/getData.dto.ts@@ -28,7 +28,8 @@ export class AnalyticsGET_DTO {to: string@ApiProperty({- description: 'A stringified array of properties to filter [{ column, filter, isExclusive }]',+ description:+ 'A stringified array of properties to filter [{ column, filter, isExclusive }]',})filters: stringdiff --git a/src/analytics/dto/pageviews.dto.ts b/src/analytics/dto/pageviews.dto.tsindex 5bcd69831..a177e337c 100644--- a/src/analytics/dto/pageviews.dto.ts+++ b/src/analytics/dto/pageviews.dto.ts@@ -5,7 +5,7 @@ export class PageviewsDTO {@ApiProperty({example: 'aUn1quEid-3',required: true,- description: 'The project ID'+ description: 'The project ID',})@IsNotEmpty()pid: string@@ -13,56 +13,56 @@ export class PageviewsDTO {// Tracking metrics@ApiProperty({example: 'Europe/Kiev',- description: 'User\'s timezone'+ description: "User's timezone",})tz?: string@ApiProperty({example: '/articles/my-awesome-article-1',- description: 'A page that user sent data from'+ description: 'A page that user sent data from',})pg?: string@ApiProperty({example: 'en-GB',- description: 'User\'s locale'+ description: "User's locale",})lc?: string@ApiProperty({example: 'https://example.com',- description: 'The referrer'+ description: 'The referrer',})ref?: string@ApiProperty({example: 'duckduckgo',- description: 'utm_source URL parameter'+ description: 'utm_source URL parameter',})so?: string@ApiProperty({example: 'cpc',- description: 'utm_medium URL parameter'+ description: 'utm_medium URL parameter',})me?: string@ApiProperty({example: 'spring_sale',- description: 'utm_campaign URL parameter'+ description: 'utm_campaign URL parameter',})ca?: string@ApiProperty({example: false,- description: 'If true, only unique events will be saved'+ description: 'If true, only unique events will be saved',})unique?: boolean// Performance metrics@ApiProperty({example: 261,- description: 'Page load time (in milliseconds)'+ description: 'Page load time (in milliseconds)',})lt?: number-}\ No newline at end of file+}diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.tsindex accf393f8..573126381 100644--- a/src/auth/auth.controller.ts+++ b/src/auth/auth.controller.ts@@ -1,5 +1,15 @@-import {- Controller, Post, Body, Req, Get, Param, BadRequestException, UseGuards, Ip, Headers, UnprocessableEntityException,+import {+ Controller,+ Post,+ Body,+ Req,+ Get,+ Param,+ BadRequestException,+ UseGuards,+ Ip,+ Headers,+ UnprocessableEntityException,} from '@nestjs/common'import { Request } from 'express'import { ApiTags } from '@nestjs/swagger'@@ -23,7 +33,10 @@ import { LetterTemplate } from '../mailer/letter'import { AppLoggerService } from '../logger/logger.service'import { SelfhostedGuard } from '../common/guards/selfhosted.guard'import {- isSelfhosted, SELFHOSTED_EMAIL, SELFHOSTED_PASSWORD, SELFHOSTED_UUID,+ isSelfhosted,+ SELFHOSTED_EMAIL,+ SELFHOSTED_PASSWORD,+ SELFHOSTED_UUID,} from 'src/common/constants'import * as _pick from 'lodash/pick'@@ -37,7 +50,7 @@ export class AuthController {private mailerService: MailerService,private actionTokensService: ActionTokensService,private readonly projectService: ProjectService,- private readonly logger: AppLoggerService+ private readonly logger: AppLoggerService,) {}@Get('/me')@@ -60,7 +73,7 @@ export class AuthController {relations: ['project'],})user = this.authService.processUser(- await this.userService.findOneWhere({ id: user_id })+ await this.userService.findOneWhere({ id: user_id }),)user.sharedProjects = sharedProjects@@ -70,21 +83,32 @@ export class AuthController {}@Post('/login')- async loginUser(@Body() userLoginDTO: UserLoginDTO, @Headers() headers, @Ip() reqIP): Promise<any> {+ async loginUser(+ @Body() userLoginDTO: UserLoginDTO,+ @Headers() headers,+ @Ip() reqIP,+ ): Promise<any> {this.logger.log({ userLoginDTO }, 'POST /auth/login')- const ip = headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''+ const ip =+ headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''await checkRateLimit(ip, 'login', 10, 1800)// await this.authService.checkCaptcha(userLoginDTO.recaptcha)if (isSelfhosted) {- if (userLoginDTO.email !== SELFHOSTED_EMAIL || userLoginDTO.password !== SELFHOSTED_PASSWORD) {+ if (+ userLoginDTO.email !== SELFHOSTED_EMAIL ||+ userLoginDTO.password !== SELFHOSTED_PASSWORD+ ) {throw new UnprocessableEntityException('Email or password is incorrect')}return this.authService.login({email: SELFHOSTED_EMAIL,})} else {- const user = await this.authService.validateUser(userLoginDTO.email, userLoginDTO.password)+ const user = await this.authService.validateUser(+ userLoginDTO.email,+ userLoginDTO.password,+ )if (user.isTwoFactorAuthenticationEnabled) {const processedUser = this.authService.postLoginProcess(user)@@ -106,9 +130,15 @@ export class AuthController {@UseGuards(SelfhostedGuard)@Post('/register')- async register(@Body() userDTO: SignupUserDTO, /*@Body('recaptcha') recaptcha: string,*/ @Req() request: Request, @Headers() headers, @Ip() reqIP): Promise<any> {+ async register(+ @Body() userDTO: SignupUserDTO,+ /*@Body('recaptcha') recaptcha: string,*/ @Req() request: Request,+ @Headers() headers,+ @Ip() reqIP,+ ): Promise<any> {this.logger.log({ userDTO }, 'POST /auth/register')- const ip = headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''+ const ip =+ headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''if (userDTO.checkIfLeaked) {await this.authService.checkIfPasswordLeaked(userDTO.password)@@ -132,14 +162,23 @@ export class AuthController {try {const userToUpdate = _pick(userDTO, ['email', 'password'])const user = await this.userService.create(userToUpdate)- const actionToken = await this.actionTokensService.createForUser(user, ActionTokenType.EMAIL_VERIFICATION)+ const actionToken = await this.actionTokensService.createForUser(+ user,+ ActionTokenType.EMAIL_VERIFICATION,+ )const url = `${request.headers.origin}/verify/${actionToken.id}`- await this.mailerService.sendEmail(userDTO.email, LetterTemplate.SignUp, { url })+ await this.mailerService.sendEmail(userDTO.email, LetterTemplate.SignUp, {+ url,+ })user.sharedProjects = []return this.authService.login(user)- } catch(e) {- this.logger.log(`[ERROR WHILE CREATING ACCOUNT]: ${e}`, 'POST /auth/register', true)+ } catch (e) {+ this.logger.log(+ `[ERROR WHILE CREATING ACCOUNT]: ${e}`,+ 'POST /auth/register',+ true,+ )}}@@ -156,7 +195,10 @@ export class AuthController {}if (actionToken.action === ActionTokenType.EMAIL_VERIFICATION) {- await this.userService.update(actionToken.user.id, { ...actionToken.user, isActive: true})+ await this.userService.update(actionToken.user.id, {+ ...actionToken.user,+ isActive: true,+ })await this.actionTokensService.delete(actionToken.id)return}@@ -175,10 +217,15 @@ export class AuthController {}if (actionToken.action === ActionTokenType.EMAIL_CHANGE) {- await this.userService.update(actionToken.user.id,- { ...actionToken.user, email: actionToken.newValue })- await this.mailerService.sendEmail(actionToken.user.email,- LetterTemplate.MailAddressHadChanged, actionToken.user.locale)+ await this.userService.update(actionToken.user.id, {+ ...actionToken.user,+ email: actionToken.newValue,+ })+ await this.mailerService.sendEmail(+ actionToken.user.email,+ LetterTemplate.MailAddressHadChanged,+ actionToken.user.locale,+ )await this.actionTokensService.delete(actionToken.id)return}@@ -186,10 +233,16 @@ export class AuthController {@UseGuards(SelfhostedGuard)@Post('/reset-password')- async requestReset(@Body() body: RequestPasswordChangeDTO, @Req() request: Request, @Headers() headers, @Ip() reqIP): Promise<string> {+ async requestReset(+ @Body() body: RequestPasswordChangeDTO,+ @Req() request: Request,+ @Headers() headers,+ @Ip() reqIP,+ ): Promise<string> {this.logger.log({ body }, 'POST /auth/password-reset')const { email } = body- const ip = headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''+ const ip =+ headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''await checkRateLimit(ip, 'reset-password')await checkRateLimit(email, 'reset-password')@@ -199,16 +252,26 @@ export class AuthController {return 'A password reset URL has been sent to your email'}- const actionToken = await this.actionTokensService.createForUser(user, ActionTokenType.PASSWORD_RESET)+ const actionToken = await this.actionTokensService.createForUser(+ user,+ ActionTokenType.PASSWORD_RESET,+ )const url = `${request.headers.origin}/password-reset/${actionToken.id}`- await this.mailerService.sendEmail(email, LetterTemplate.ConfirmPasswordChange, { url })+ await this.mailerService.sendEmail(+ email,+ LetterTemplate.ConfirmPasswordChange,+ { url },+ )return 'A password reset URL has been sent to your email'}@UseGuards(SelfhostedGuard)@Post('/password-reset/:id')- async reset(@Param('id') id: string, @Body() body: PasswordChangeDTO): Promise<User> {+ async reset(+ @Param('id') id: string,+ @Body() body: PasswordChangeDTO,+ ): Promise<User> {this.logger.log({ id }, 'POST /auth/password-reset/:id')this.userService.validatePassword(body.password)let actionToken@@ -222,9 +285,12 @@ export class AuthController {if (actionToken.action === ActionTokenType.PASSWORD_RESET) {const password = await this.authService.hashPassword(body.password)- await this.userService.update(actionToken.user.id, { ...actionToken.user, password })+ await this.userService.update(actionToken.user.id, {+ ...actionToken.user,+ password,+ })await this.actionTokensService.delete(actionToken.id)- return+ return}}}diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.tsindex b88b5fe46..71281829f 100644--- a/src/auth/auth.service.ts+++ b/src/auth/auth.service.ts@@ -1,4 +1,8 @@-import { Injectable, UnprocessableEntityException, BadRequestException } from '@nestjs/common'+import {+ Injectable,+ UnprocessableEntityException,+ BadRequestException,+} from '@nestjs/common'import { sign } from 'jsonwebtoken'import * as bcrypt from 'bcrypt'import * as crypto from 'crypto'@@ -12,7 +16,9 @@ import { ACCOUNT_PLANS } from '../user/entities/user.entity'import { AppLoggerService } from '../logger/logger.service'import { User } from '../user/entities/user.entity'import {- isSelfhosted, JWT_LIFE_TIME, SELFHOSTED_UUID,+ isSelfhosted,+ JWT_LIFE_TIME,+ SELFHOSTED_UUID,} from '../common/constants'import { splitAt } from '../common/utils'@@ -22,15 +28,15 @@ const BCRYPT_SALT_ROUNDS = 10const httpsOptions = {headers: {'User-Agent': 'Swetrix API ([email protected])',- }+ },}@Injectable()export class AuthService {constructor(private userService: UserService,- private readonly logger: AppLoggerService- ) { }+ private readonly logger: AppLoggerService,+ ) {}async hashPassword(pass: string): Promise<string> {const salt = await bcrypt.genSalt(BCRYPT_SALT_ROUNDS)@@ -39,31 +45,35 @@ export class AuthService {async get(hash) {return new Promise((resolve, reject) => {- https.get(`${HIBP_URL}/${hash}`, httpsOptions, (res) => {- let data = ''+ https+ .get(`${HIBP_URL}/${hash}`, httpsOptions, res => {+ let data = ''- if (res.statusCode !== 200) {- return reject(`Failed to load ${HIBP_URL} API: ${res.statusCode}`)- }+ if (res.statusCode !== 200) {+ return reject(`Failed to load ${HIBP_URL} API: ${res.statusCode}`)+ }- res.on('data', (chunk) => {- data += chunk- })+ res.on('data', chunk => {+ data += chunk+ })- res.on('end', () => {- resolve(data)- })+ res.on('end', () => {+ resolve(data)+ })- return true- }).on('error', (err) => {- reject(err)- })+ return true+ })+ .on('error', err => {+ reject(err)+ })})}async sha1hash(string): Promise<string> {- return new Promise((resolve) => {- const shasum = _toUpper(crypto.createHash('sha1').update(string).digest('hex'))+ return new Promise(resolve => {+ const shasum = _toUpper(+ crypto.createHash('sha1').update(string).digest('hex'),+ )resolve(shasum)})}@@ -84,14 +94,19 @@ export class AuthService {}}- async checkPassword(passToCheck: string, hashedPass: string): Promise<boolean> {+ async checkPassword(+ passToCheck: string,+ hashedPass: string,+ ): Promise<boolean> {return await bcrypt.compare(passToCheck, hashedPass)}async validateUser(email: string, pass: string): Promise<User> {- const user = await this.userService.findOneWhere({ email }, ['sharedProjects'])+ const user = await this.userService.findOneWhere({ email }, [+ 'sharedProjects',+ ])- if (user && await this.checkPassword(pass, user.password)) {+ if (user && (await this.checkPassword(pass, user.password))) {return user}@@ -136,9 +151,14 @@ export class AuthService {}// @ts-ignore- const token = sign({ user_id: userData.id, isSecondFactorAuthenticated }, process.env.JWT_SECRET, {- expiresIn: JWT_LIFE_TIME,- })+ const token = sign(+ // @ts-ignore+ { user_id: userData.id, isSecondFactorAuthenticated },+ process.env.JWT_SECRET,+ {+ expiresIn: JWT_LIFE_TIME,+ },+ )return { access_token: token, user: userData }}diff --git a/src/auth/dto/request-pass-change.dto.ts b/src/auth/dto/request-pass-change.dto.tsindex e11f2a3ec..dd328565b 100644--- a/src/auth/dto/request-pass-change.dto.ts+++ b/src/auth/dto/request-pass-change.dto.ts@@ -2,7 +2,7 @@ import { ApiProperty } from '@nestjs/swagger'import { IsEmail } from 'class-validator'export class RequestPasswordChangeDTO {- @ApiProperty({example: '[email protected]'})+ @ApiProperty({ example: '[email protected]' })@IsEmail()email: string-}\ No newline at end of file+}diff --git a/src/common/constants.ts b/src/common/constants.tsindex 8bfb9a742..61dbb5092 100644--- a/src/common/constants.ts+++ b/src/common/constants.ts@@ -6,10 +6,14 @@ import * as _round from 'lodash/round'require('dotenv').config()-const redis = new Redis(_toNumber(process.env.REDIS_PORT), process.env.REDIS_HOST, {- password: process.env.REDIS_PASSWORD,- username: process.env.REDIS_USER,-})+const redis = new Redis(+ _toNumber(process.env.REDIS_PORT),+ process.env.REDIS_HOST,+ {+ password: process.env.REDIS_PASSWORD,+ username: process.env.REDIS_USER,+ },+)redis.defineCommand('countKeysByPattern', {numberOfKeys: 0,@@ -71,7 +75,8 @@ const CLICKHOUSE_INIT_QUERIES = [ENGINE = MergeTree()PARTITION BY toYYYYMM(created)ORDER BY (id, created, pid);`,- isSelfhosted && `CREATE TABLE IF NOT EXISTS analytics.project+ isSelfhosted &&+ `CREATE TABLE IF NOT EXISTS analytics.project(id FixedString(12),name String,@@ -103,8 +108,11 @@ initialiseClickhouse()const SELFHOSTED_EMAIL = process.env.EMAILconst SELFHOSTED_PASSWORD = process.env.PASSWORDconst UUIDV5_NAMESPACE = '912c64c1-73fd-42b6-859f-785f839a9f68'-const SELFHOSTED_UUID = isSelfhosted ? uuidv5(SELFHOSTED_EMAIL, UUIDV5_NAMESPACE) : ''-const TWO_FACTOR_AUTHENTICATION_APP_NAME = process.env.TWO_FACTOR_AUTHENTICATION_APP_NAME+const SELFHOSTED_UUID = isSelfhosted+ ? uuidv5(SELFHOSTED_EMAIL, UUIDV5_NAMESPACE)+ : ''+const TWO_FACTOR_AUTHENTICATION_APP_NAME =+ process.env.TWO_FACTOR_AUTHENTICATION_APP_NAME/*** Calculates in percent, the change between 2 numbers.@@ -113,7 +121,7 @@ const TWO_FACTOR_AUTHENTICATION_APP_NAME = process.env.TWO_FACTOR_AUTHENTICATION* @param oldVal The initial value* @param newVal The value that changed*/-function getPercentageChange(oldVal: number, newVal: number, round: number = 2) {+function getPercentageChange(oldVal: number, newVal: number, round = 2) {if (oldVal === 0) {if (newVal === 0) {return 0@@ -129,8 +137,10 @@ function getPercentageChange(oldVal: number, newVal: number, round: number = 2)const JWT_LIFE_TIME = 7 * 24 * 60 * 60const HISTORY_LIFE_TIME_DAYS = 30-const ORIGINS_REGEX = /^(?=.{1,255}$)[0-9A-Za-z\:](?:(?:[0-9A-Za-z\:]|-){0,61}[0-9A-Za-z])?(?:\.[0-9A-Za-z\:](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?)*\.?$/-const IP_REGEX = /^(([12]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])(\.|\/)){4}([1-2]?[0-9]|3[0-2])$/+const ORIGINS_REGEX =+ /^(?=.{1,255}$)[0-9A-Za-z\:](?:(?:[0-9A-Za-z\:]|-){0,61}[0-9A-Za-z])?(?:\.[0-9A-Za-z\:](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?)*\.?$/+const IP_REGEX =+ /^(([12]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])(\.|\/)){4}([1-2]?[0-9]|3[0-2])$/const PID_REGEX = /^(?!.*--)[a-zA-Z0-9-]{12}$/const isValidPID = (pid: string) => PID_REGEX.test(pid)@@ -166,11 +176,35 @@ const SEND_WARNING_AT_PERC = 85const PROJECT_INVITE_EXPIRE = 48export {- clickhouse, JWT_LIFE_TIME, HISTORY_LIFE_TIME_DAYS, redis, isValidPID, getRedisProjectKey,- redisProjectCacheTimeout, getPercentageChange, UNIQUE_SESSION_LIFE_TIME, REDIS_LOG_DATA_CACHE_KEY,- GDPR_EXPORT_TIMEFRAME, getRedisUserCountKey, redisProjectCountCacheTimeout, REDIS_LOG_CUSTOM_CACHE_KEY,- REDIS_SESSION_SALT_KEY, HEARTBEAT_SID_LIFE_TIME, isSelfhosted, UUIDV5_NAMESPACE, SELFHOSTED_EMAIL,- SELFHOSTED_PASSWORD, SELFHOSTED_UUID, CLICKHOUSE_INIT_QUERIES, REDIS_USERS_COUNT_KEY, REDIS_PROJECTS_COUNT_KEY,- REDIS_PAGEVIEWS_COUNT_KEY, SEND_WARNING_AT_PERC, PROJECT_INVITE_EXPIRE, TWO_FACTOR_AUTHENTICATION_APP_NAME,- IP_REGEX, isNewRelicEnabled, ORIGINS_REGEX,+ clickhouse,+ JWT_LIFE_TIME,+ HISTORY_LIFE_TIME_DAYS,+ redis,+ isValidPID,+ getRedisProjectKey,+ redisProjectCacheTimeout,+ getPercentageChange,+ UNIQUE_SESSION_LIFE_TIME,+ REDIS_LOG_DATA_CACHE_KEY,+ GDPR_EXPORT_TIMEFRAME,+ getRedisUserCountKey,+ redisProjectCountCacheTimeout,+ REDIS_LOG_CUSTOM_CACHE_KEY,+ REDIS_SESSION_SALT_KEY,+ HEARTBEAT_SID_LIFE_TIME,+ isSelfhosted,+ UUIDV5_NAMESPACE,+ SELFHOSTED_EMAIL,+ SELFHOSTED_PASSWORD,+ SELFHOSTED_UUID,+ CLICKHOUSE_INIT_QUERIES,+ REDIS_USERS_COUNT_KEY,+ REDIS_PROJECTS_COUNT_KEY,+ REDIS_PAGEVIEWS_COUNT_KEY,+ SEND_WARNING_AT_PERC,+ PROJECT_INVITE_EXPIRE,+ TWO_FACTOR_AUTHENTICATION_APP_NAME,+ IP_REGEX,+ isNewRelicEnabled,+ ORIGINS_REGEX,}diff --git a/src/common/decorators/2fa-disabled.decorator.ts b/src/common/decorators/2fa-disabled.decorator.tsindex cb5e944f8..335e4925b 100644--- a/src/common/decorators/2fa-disabled.decorator.ts+++ b/src/common/decorators/2fa-disabled.decorator.ts@@ -3,4 +3,5 @@ import { SetMetadata, CustomDecorator } from '@nestjs/common'/*** Should be applied to controllers where user has to be authenticated but 2FA is not required.*/-export const TwoFaNotRequired = (): CustomDecorator<string> => SetMetadata('twoFaNotRequired', true)+export const TwoFaNotRequired = (): CustomDecorator<string> =>+ SetMetadata('twoFaNotRequired', true)diff --git a/src/common/decorators/current-user-id.decorator.ts b/src/common/decorators/current-user-id.decorator.tsindex 13915326b..388055119 100644--- a/src/common/decorators/current-user-id.decorator.ts+++ b/src/common/decorators/current-user-id.decorator.ts@@ -21,5 +21,5 @@ export const CurrentUserId = createParamDecorator(} catch {return null}- }+ },)diff --git a/src/common/decorators/roles.decorator.ts b/src/common/decorators/roles.decorator.tsindex 99216a329..b891e1484 100644--- a/src/common/decorators/roles.decorator.ts+++ b/src/common/decorators/roles.decorator.ts@@ -1,3 +1,4 @@import { SetMetadata, CustomDecorator } from '@nestjs/common'-export const Roles = (...roles: string[]): CustomDecorator<string> => SetMetadata('roles', roles)+export const Roles = (...roles: string[]): CustomDecorator<string> =>+ SetMetadata('roles', roles)diff --git a/src/common/guards/roles.guard.ts b/src/common/guards/roles.guard.tsindex 4b17ddff5..05768f911 100644--- a/src/common/guards/roles.guard.ts+++ b/src/common/guards/roles.guard.ts@@ -1,4 +1,9 @@-import { Injectable, CanActivate, ExecutionContext, UnauthorizedException } from '@nestjs/common'+import {+ Injectable,+ CanActivate,+ ExecutionContext,+ UnauthorizedException,+} from '@nestjs/common'import { Reflector } from '@nestjs/core'import { Request } from 'express'import { verify } from 'jsonwebtoken'@@ -10,15 +15,15 @@ import { isSelfhosted } from '../constants'@Injectable()export class RolesGuard implements CanActivate {- constructor(- private reflector: Reflector,- private userService: UserService,- ) { }+ constructor(private reflector: Reflector, private userService: UserService) {}async canActivate(context: ExecutionContext): Promise<boolean> {const secret = process.env.JWT_SECRETconst roles = this.reflector.get<string[]>('roles', context.getHandler())- const twoFaNotRequired = this.reflector.get<boolean>('twoFaNotRequired', context.getHandler())+ const twoFaNotRequired = this.reflector.get<boolean>(+ 'twoFaNotRequired',+ context.getHandler(),+ )if (!roles || isSelfhosted) {return true}@@ -35,14 +40,22 @@ export class RolesGuard implements CanActivate {try {const decoded: any = verify(token, secret)if (decoded) {- let user: User = await this.userService.findOneWhere({ id: decoded.user_id })- const hasRole = user.roles.some(role => !!roles.find(item => item === role))+ const user: User = await this.userService.findOneWhere({+ id: decoded.user_id,+ })+ const hasRole = user.roles.some(+ role => !!roles.find(item => item === role),+ )if (!hasRole) {throw new Error()}- if (!twoFaNotRequired && user.isTwoFactorAuthenticationEnabled && !decoded.isSecondFactorAuthenticated) {+ if (+ !twoFaNotRequired &&+ user.isTwoFactorAuthenticationEnabled &&+ !decoded.isSecondFactorAuthenticated+ ) {throw new Error()}diff --git a/src/common/guards/selfhosted.guard.ts b/src/common/guards/selfhosted.guard.tsindex 10c00acf8..305e71573 100644--- a/src/common/guards/selfhosted.guard.ts+++ b/src/common/guards/selfhosted.guard.ts@@ -5,7 +5,9 @@ import { isSelfhosted } from '../constants'export class SelfhostedGuard implements CanActivate {canActivate(): boolean {if (isSelfhosted) {- throw new ForbiddenException('This API route is disabled in selfhosted edition')+ throw new ForbiddenException(+ 'This API route is disabled in selfhosted edition',+ )}return truediff --git a/src/common/pagination/pagination.options.interface.ts b/src/common/pagination/pagination.options.interface.tsindex 49f1807b0..37378118e 100644--- a/src/common/pagination/pagination.options.interface.ts+++ b/src/common/pagination/pagination.options.interface.ts@@ -1,4 +1,4 @@export interface PaginationOptionsInterface {- take: number;- skip: number;+ take: number+ skip: number}diff --git a/src/common/pagination/pagination.results.interface.ts b/src/common/pagination/pagination.results.interface.tsindex fff9e2303..fc38ee98a 100644--- a/src/common/pagination/pagination.results.interface.ts+++ b/src/common/pagination/pagination.results.interface.ts@@ -1,6 +1,6 @@export interface PaginationResultInterface<PaginationEntity> {- results: PaginationEntity[];- total: number;- next?: string;- previous?: string;+ results: PaginationEntity[]+ total: number+ next?: string+ previous?: string}diff --git a/src/common/pagination/pagination.ts b/src/common/pagination/pagination.tsindex fd5444d1d..51e8ef62b 100644--- a/src/common/pagination/pagination.ts+++ b/src/common/pagination/pagination.ts@@ -1,9 +1,9 @@import { PaginationResultInterface } from './pagination.results.interface'export class Pagination<PaginationEntity> {- public results: PaginationEntity[];- public page_total: number;- public total: number;+ public results: PaginationEntity[]+ public page_total: number+ public total: numberconstructor(paginationResults: PaginationResultInterface<PaginationEntity>) {this.results = paginationResults.resultsdiff --git a/src/common/utils.ts b/src/common/utils.tsindex 0c2d65d5b..1dd7f9fac 100644--- a/src/common/utils.ts+++ b/src/common/utils.ts@@ -28,7 +28,7 @@ const marketingTips = {'Contribute Articles to Expert Sites\nFind blogs or websites that are thought leaders in your industry, then submit articles that can put in front of their audience. This is also a great way to build backlinks and drive organic traffic to your website.','Create an Affiliate Program\nAffiliate programs are an easy way to get people clicking to your website to learn more about your company. To date, 81% of brands have adopted an affiliate marketing program.\nAn affiliate program is a long term investment; you will have to build a program people will want to join, try to get and manage quality affiliates so you can get the fruits of your labour.','Add Your Website Link To All Your Platforms\nAdding your links to all your platforms allows you to optimise your website for more clicks.',- 'Have a Backlink Strategy\nGoogle\'s search engine uses links as one of the three most important search engine ranking factors for a website.\nBelieve it or not, you can improve your website\'s SEO by backlinking your website in the article you contribute as a guest writer.\nOnce you have a strategy in place, you will begin to see your website\'s click-through rate increase and traffic coming your way.',+ "Have a Backlink Strategy\nGoogle's search engine uses links as one of the three most important search engine ranking factors for a website.\nBelieve it or not, you can improve your website's SEO by backlinking your website in the article you contribute as a guest writer.\nOnce you have a strategy in place, you will begin to see your website's click-through rate increase and traffic coming your way.",'Host a Giveaway on Your Website\nHosting a giveaway on your website is a sure way to promote your website. Giveaways pack more of a punch for your business than you think.','Use Influencers To Market Your Website\nInfluencer Marketing has been in the top five marketing tools and strategies for the past four years and counting.\nYou can use influencers in a wide variety of ways. One way is to partner with an influencer for a do co-promotions like a giveaway, a special discount, or prize package.','Fix Broken External Links & Errors\nBroken external links, just as the states, are links that lead nowhere or give users an error.\nMaybe your busy sharing other links from your site or there are some old content or web pages you forgot you deleted. Over time you will be losing potential traffic and clients if you forget to audit your site to find broken external links.',@@ -46,18 +46,27 @@ const RATE_LIMIT_TIMEOUT = 86400 // 24 hoursconst allowedToUpdateKeys = ['name', 'origins', 'active', 'public']-const _getRateLimitHash = (ip: string, salt: string = '') => `rl:${hash(`${ip}${salt}`).toString('hex')}`+const _getRateLimitHash = (ip: string, salt = '') =>+ `rl:${hash(`${ip}${salt}`).toString('hex')}`-const splitAt = (x, index): Array<Array<any> | string> => [x.slice(0, index), x.slice(index)]+const splitAt = (x, index): Array<Array<any> | string> => [+ x.slice(0, index),+ x.slice(index),+]-const getRandomTip = (language: string = 'en'): string => {+const getRandomTip = (language = 'en'): string => {return _sample(marketingTips[language])}// 'action' is used as a salt to differ rate limiting routes-const checkRateLimit = async (ip: string, action: string, reqAmount: number = RATE_LIMIT_REQUESTS_AMOUNT, reqTimeout: number = RATE_LIMIT_TIMEOUT): Promise<void> => {+const checkRateLimit = async (+ ip: string,+ action: string,+ reqAmount: number = RATE_LIMIT_REQUESTS_AMOUNT,+ reqTimeout: number = RATE_LIMIT_TIMEOUT,+): Promise<void> => {const rlHash = _getRateLimitHash(ip, action)- let rlCount: number = _toNumber(await redis.get(rlHash)) || 0+ const rlCount: number = _toNumber(await redis.get(rlHash)) || 0if (rlCount >= reqAmount) {throw new ForbiddenException('Too many requests, please try again later')@@ -88,18 +97,26 @@ const getProjectsClickhouse = async (id = null) => {}const updateProjectClickhouse = async (project: object) => {- const filtered = _reduce(_filter(_keys(project), key => allowedToUpdateKeys.includes(key)), (obj, key) => {- obj[key] = project[key]- return obj- }, {})+ const filtered = _reduce(+ _filter(_keys(project), key => allowedToUpdateKeys.includes(key)),+ (obj, key) => {+ obj[key] = project[key]+ return obj+ },+ {},+ )const columns = _keys(filtered)const values = _values(filtered)// @ts-ignore- const query = `ALTER table project UPDATE ${_join(_map(columns, (col, id) => `${col}='${values[id]}'`), ', ')} WHERE id='${project.id}'`+ const query = `ALTER table project UPDATE ${_join(+ _map(columns, (col, id) => `${col}='${values[id]}'`),+ ', ',+ // @ts-ignore+ )} WHERE id='${project.id}'`return await clickhouse.query(query).toPromise()}-const deleteProjectClickhouse = async (id) => {+const deleteProjectClickhouse = async id => {const query = `ALTER table project DELETE WHERE WHERE id='${id}'`return await clickhouse.query(query).toPromise()}@@ -110,17 +127,26 @@ const createProjectClickhouse = async (project: Project) => {...project,},}- const query = `INSERT INTO project (*) VALUES ({id:FixedString(12)},{name:String},'',1,0,'${dayjs.utc().format('YYYY-MM-DD HH:mm:ss')}')`+ const query = `INSERT INTO project (*) VALUES ({id:FixedString(12)},{name:String},'',1,0,'${dayjs+ .utc()+ .format('YYYY-MM-DD HH:mm:ss')}')`return await clickhouse.query(query, paramsData).toPromise()}-const generateRecoveryCode = () => randomstring.generate({- length: 30,- charset: 'alphabetic',- capitalization: 'uppercase',-})+const generateRecoveryCode = () =>+ randomstring.generate({+ length: 30,+ charset: 'alphabetic',+ capitalization: 'uppercase',+ })export {- getRandomTip, checkRateLimit, createProjectClickhouse, getProjectsClickhouse, updateProjectClickhouse,- deleteProjectClickhouse, splitAt, generateRecoveryCode,+ getRandomTip,+ checkRateLimit,+ createProjectClickhouse,+ getProjectsClickhouse,+ updateProjectClickhouse,+ deleteProjectClickhouse,+ splitAt,+ generateRecoveryCode,}diff --git a/src/logger/logger.service.ts b/src/logger/logger.service.tsindex bd6891413..45beafc24 100644--- a/src/logger/logger.service.ts+++ b/src/logger/logger.service.ts@@ -1,7 +1,7 @@import { Logger } from '@nestjs/common'export class AppLoggerService extends Logger {- log(value, route, forceLog: boolean = false) {+ log(value, route, forceLog = false) {if (process.env.NODE_ENV === 'development' || forceLog) {Logger.log(value, route)}diff --git a/src/mailer/mailer.module.ts b/src/mailer/mailer.module.tsindex c564e269a..e6ac47b24 100644--- a/src/mailer/mailer.module.ts+++ b/src/mailer/mailer.module.ts@@ -5,6 +5,6 @@ import { MailerService } from './mailer.service'@Module({providers: [MailerService],exports: [MailerService],- imports: [AppLoggerModule]+ imports: [AppLoggerModule],})export class MailerModule {}diff --git a/src/mailer/mailer.service.ts b/src/mailer/mailer.service.tsindex a28edfb54..4abf6b769 100644--- a/src/mailer/mailer.service.ts+++ b/src/mailer/mailer.service.ts@@ -46,12 +46,14 @@ const metaInfoJson = {},[LetterTemplate.ProjectReport]: {subject: {- en: (p: Params) => `${p.type === 'w' ? 'Weekly' : 'Monthly'} Report: ${p.date}`,+ en: (p: Params) =>+ `${p.type === 'w' ? 'Weekly' : 'Monthly'} Report: ${p.date}`,},},[LetterTemplate.TierWarning]: {subject: {- en: () => `You have used more than ${SEND_WARNING_AT_PERC}% of the available events per your tier for this month.`,+ en: () =>+ `You have used more than ${SEND_WARNING_AT_PERC}% of the available events per your tier for this month.`,},},[LetterTemplate.ProjectInvitation]: {@@ -76,7 +78,7 @@ interface Params {}handlebars.registerHelper('ifEquals', function (arg1, arg2, options) {- return (arg1 == arg2) ? options.fn(this) : options.inverse(this)+ return arg1 == arg2 ? options.fn(this) : options.inverse(this)})handlebars.registerHelper('greater', function (v1, v2, options) {@@ -90,11 +92,14 @@ const mailClient = new postmark.ServerClient(process.env.SMTP_PASSWORD)@Injectable()export class MailerService {- constructor(- private readonly logger: AppLoggerService,- ) { }+ constructor(private readonly logger: AppLoggerService) {}- async sendEmail(email: string, templateName: LetterTemplate, params: Params = null, messageStream: 'broadcast' | 'outbound' = 'outbound'): Promise<void> {+ async sendEmail(+ email: string,+ templateName: LetterTemplate,+ params: Params = null,+ messageStream: 'broadcast' | 'outbound' = 'outbound',+ ): Promise<void> {try {const templatePath = `${TEMPLATES_PATH}/en/${templateName}.html`const letter = fs.readFileSync(templatePath, { encoding: 'utf-8' })@@ -111,10 +116,14 @@ export class MailerService {}if (process.env.SMTP_MOCK) {- this.logger.log({- ...message,- params,- }, 'sendEmail', true)+ this.logger.log(+ {+ ...message,+ params,+ },+ 'sendEmail',+ true,+ )} else {await mailClient.sendEmail(message)}diff --git a/src/main.ts b/src/main.tsindex 8a99bc6d2..837fa0624 100644--- a/src/main.ts+++ b/src/main.ts@@ -17,8 +17,11 @@ async function bootstrap() {}app.use(async (req, res, next) => {- res.header('Cross-Origin-Embedder-Policy', 'require-corp; report-to=\'default\'')- res.header('Cross-Origin-Opener-Policy', 'same-site; report-to=\'default\'')+ res.header(+ 'Cross-Origin-Embedder-Policy',+ "require-corp; report-to='default'",+ )+ res.header('Cross-Origin-Opener-Policy', "same-site; report-to='default'")res.header('Cross-Origin-Resource-Policy', 'same-site')res.header('Permissions-Policy', 'interest-cohort=()')res.header('Referrer-Policy', 'strict-origin-when-cross-origin')diff --git a/src/newrelic.ts b/src/newrelic.tsindex 6995b446a..5a92412b1 100644--- a/src/newrelic.ts+++ b/src/newrelic.ts@@ -21,7 +21,7 @@ exports.config = {* issues with the agent, 'info' and higher will impose the least overhead on* production applications.*/- level: 'info'+ level: 'info',},/*** When true, all request headers except for those listed in attributes.exclude@@ -34,8 +34,8 @@ exports.config = {/*** Toggles whether the agent gathers log records for sending to New Relic.*/- enabled: true- }+ enabled: true,+ },},attributes: {/**@@ -56,7 +56,7 @@ exports.config = {'response.headers.authorization','response.headers.proxyAuthorization','response.headers.setCookie*',- 'response.headers.x*'- ]- }+ 'response.headers.x*',+ ],+ },}diff --git a/src/project/dto/project.dto.ts b/src/project/dto/project.dto.tsindex 4686431a0..157395e3c 100644--- a/src/project/dto/project.dto.ts+++ b/src/project/dto/project.dto.ts@@ -28,19 +28,21 @@ export class ProjectDTO {@ApiProperty({example: '::1,127.0.0.1,192.168.0.1/32',required: false,- description: 'Array of blocked IP addresses'+ description: 'Array of blocked IP addresses',})ipBlacklist: string[] | null@ApiProperty({required: false,- description: 'The project\'s state. If enabled - all the incoming analytics data will be saved.',+ description:+ "The project's state. If enabled - all the incoming analytics data will be saved.",})active: boolean@ApiProperty({required: false,- description: 'When true, anyone on the internet (including Google) would be able to see the project\'s Dashboard.',+ description:+ "When true, anyone on the internet (including Google) would be able to see the project's Dashboard.",})public: boolean}diff --git a/src/project/entity/project-share.entity.ts b/src/project/entity/project-share.entity.tsindex e702044f6..bbab7dd7a 100644--- a/src/project/entity/project-share.entity.ts+++ b/src/project/entity/project-share.entity.ts@@ -1,4 +1,10 @@-import { Entity, Column, ManyToOne, BeforeUpdate, PrimaryGeneratedColumn } from 'typeorm'+import {+ Entity,+ Column,+ ManyToOne,+ BeforeUpdate,+ PrimaryGeneratedColumn,+} from 'typeorm'import { ApiProperty } from '@nestjs/swagger'import { User } from '../../user/entities/user.entity'@@ -43,6 +49,6 @@ export class ProjectShare {@BeforeUpdate()updateTimestamp() {- this.updated = new Date+ this.updated = new Date()}}diff --git a/src/project/project.controller.ts b/src/project/project.controller.tsindex 48c779aa3..8233a1c68 100644--- a/src/project/project.controller.ts+++ b/src/project/project.controller.ts@@ -20,7 +20,6 @@ import * as _isEmpty from 'lodash/isEmpty'import * as _map from 'lodash/map'import * as _trim from 'lodash/trim'import * as _size from 'lodash/size'-import * as _values from 'lodash/values'import * as _includes from 'lodash/includes'import * as _omit from 'lodash/omit'@@ -46,7 +45,6 @@ import {redis,isValidPID,getRedisProjectKey,- redisProjectCacheTimeout,clickhouse,isSelfhosted,PROJECT_INVITE_EXPIRE,@@ -58,20 +56,20 @@ import {deleteProjectClickhouse,} from '../common/utils'-const updateProjectRedis = async (id: string, project: Project) => {- const key = getRedisProjectKey(id)-- try {- await redis.set(- key,- JSON.stringify(project),- 'EX',- redisProjectCacheTimeout,- )- } catch {- await redis.del(key)- }-}+// const updateProjectRedis = async (id: string, project: Project) => {+// const key = getRedisProjectKey(id)++// try {+// await redis.set(+// key,+// JSON.stringify(project),+// 'EX',+// redisProjectCacheTimeout,+// )+// } catch {+// await redis.del(key)+// }+// }export const deleteProjectRedis = async (id: string) => {const key = getRedisProjectKey(id)@@ -86,10 +84,7 @@ export const deleteProjectRedis = async (id: string) => {const PROJECTS_MAXIMUM = ACCOUNT_PLANS[PlanCode.free].maxProjectsconst isValidShareDTO = (share: ShareDTO): boolean => {- return (- !_isEmpty(_trim(share.email)) &&- _includes(roles, share.role)- )+ return !_isEmpty(_trim(share.email)) && _includes(roles, share.role)}const isValidUpdateShareDTO = (share: ShareUpdateDTO): boolean => {@@ -105,7 +100,7 @@ export class ProjectController {private readonly logger: AppLoggerService,private readonly actionTokensService: ActionTokensService,private readonly mailerService: MailerService,- ) { }+ ) {}@Get('/')@ApiQuery({ name: 'take', required: false })@@ -138,9 +133,7 @@ export class ProjectController {{ take, skip },where,)- const totalMonthlyEvents = await this.projectService.getRedisCount(- userId,- )+ const totalMonthlyEvents = await this.projectService.getRedisCount(userId)return {...paginated,@@ -164,7 +157,9 @@ export class ProjectController {this.logger.log({ userId, take, skip }, 'GET /project/shared')if (isSelfhosted) {- throw new NotImplementedException('This feature is not available on selfhosted yet')+ throw new NotImplementedException(+ 'This feature is not available on selfhosted yet',+ )} else {const where = Object()where.user = userId@@ -275,7 +270,9 @@ export class ProjectController {const maxProjects = ACCOUNT_PLANS[user.planCode]?.maxProjectsif (!user.isActive) {- throw new ForbiddenException('User\'s email address has to be verified first')+ throw new ForbiddenException(+ "User's email address has to be verified first",+ )}if (_size(user.projects) >= (maxProjects || PROJECTS_MAXIMUM)) {@@ -623,9 +620,7 @@ export class ProjectController {}if (!isValidShareDTO(shareDTO)) {- throw new BadRequestException(- 'The provided ShareDTO is incorrect',- )+ throw new BadRequestException('The provided ShareDTO is incorrect')}const user = await this.userService.findOne(uid)@@ -643,27 +638,36 @@ export class ProjectController {this.projectService.allowedToManage(project, uid, user.roles)- const invitee = await this.userService.findOneWhere({- email: shareDTO.email,- }, ['sharedProjects'])+ const invitee = await this.userService.findOneWhere(+ {+ email: shareDTO.email,+ },+ ['sharedProjects'],+ )if (!invitee) {- throw new NotFoundException(`User with email ${shareDTO.email} is not registered on Swetrix`)+ throw new NotFoundException(+ `User with email ${shareDTO.email} is not registered on Swetrix`,+ )}if (invitee.id === user.id) {throw new BadRequestException('You cannot share with yourself')}- const isSharingWithUser = !_isEmpty(await this.projectService.findShare({- where: {- project: project.id,- user: invitee.id,- },- }))+ const isSharingWithUser = !_isEmpty(+ await this.projectService.findShare({+ where: {+ project: project.id,+ user: invitee.id,+ },+ }),+ )if (isSharingWithUser) {- throw new BadRequestException(`You're already sharing the project with ${invitee.email}`)+ throw new BadRequestException(+ `You're already sharing the project with ${invitee.email}`,+ )}try {@@ -683,11 +687,23 @@ export class ProjectController {await this.userService.create(invitee)// TODO: Implement link expiration- const actionToken = await this.actionTokensService.createForUser(user, ActionTokenType.PROJECT_SHARE, share.id)+ const actionToken = await this.actionTokensService.createForUser(+ user,+ ActionTokenType.PROJECT_SHARE,+ share.id,+ )const url = `${headers.origin}/share/${actionToken.id}`- await this.mailerService.sendEmail(invitee.email, LetterTemplate.ProjectInvitation, {- url, email: user.email, name: project.name, role: share.role, expiration: PROJECT_INVITE_EXPIRE,- })+ await this.mailerService.sendEmail(+ invitee.email,+ LetterTemplate.ProjectInvitation,+ {+ url,+ email: user.email,+ name: project.name,+ role: share.role,+ expiration: PROJECT_INVITE_EXPIRE,+ },+ )const updatedProject = await this.projectService.findOne(pid, {relations: ['share', 'share.user'],@@ -697,7 +713,9 @@ export class ProjectController {await deleteProjectRedis(pid)return processProjectUser(updatedProject)} catch (e) {- console.error(`[ERROR] Could not share project (pid: ${project.id}, invitee ID: ${invitee.id}): ${e}`)+ console.error(+ `[ERROR] Could not share project (pid: ${project.id}, invitee ID: ${invitee.id}): ${e}`,+ )throw new BadRequestException(e)}}@@ -716,9 +734,7 @@ export class ProjectController {this.logger.log({ uid, shareDTO, shareId }, 'PUT /project/share/:shareId')if (!isValidUpdateShareDTO(shareDTO)) {- throw new BadRequestException(- 'The provided ShareUpdateDTO is incorrect',- )+ throw new BadRequestException('The provided ShareUpdateDTO is incorrect')}const user = await this.userService.findOne(uid)@@ -762,7 +778,7 @@ export class ProjectController {}if (actionToken.action === ActionTokenType.PROJECT_SHARE) {- const { newValue: shareId, id, user } = actionToken+ const { newValue: shareId, id } = actionTokenconst share = await this.projectService.findOneShare(shareId)share.confirmed = truediff --git a/src/project/project.service.ts b/src/project/project.service.tsindex d0f91c4e6..f4265f995 100644--- a/src/project/project.service.ts+++ b/src/project/project.service.ts@@ -1,9 +1,15 @@-import { ForbiddenException, Injectable, BadRequestException, UnprocessableEntityException, InternalServerErrorException, ConflictException } from '@nestjs/common'+import {+ ForbiddenException,+ Injectable,+ BadRequestException,+ UnprocessableEntityException,+ InternalServerErrorException,+ ConflictException,+} from '@nestjs/common'import { InjectRepository } from '@nestjs/typeorm'import { Repository } from 'typeorm'import * as _isEmpty from 'lodash/isEmpty'import * as _isString from 'lodash/isString'-import * as _isArray from 'lodash/isArray'import * as _size from 'lodash/size'import * as _join from 'lodash/join'import * as _find from 'lodash/find'@@ -24,7 +30,14 @@ import { ProjectDTO } from './dto/project.dto'import { UserType } from '../user/entities/user.entity'import { Role } from '../project/entity/project-share.entity'import {- isValidPID, redisProjectCountCacheTimeout, getRedisUserCountKey, redis, clickhouse, isSelfhosted, IP_REGEX, ORIGINS_REGEX,+ isValidPID,+ redisProjectCountCacheTimeout,+ getRedisUserCountKey,+ redis,+ clickhouse,+ isSelfhosted,+ IP_REGEX,+ ORIGINS_REGEX,} from '../common/constants'dayjs.extend(utc)@@ -60,7 +73,10 @@ export class ProjectService {private projectShareRepository: Repository<ProjectShare>,) {}- async paginate(options: PaginationOptionsInterface, where: Record<string, unknown> | undefined): Promise<Pagination<Project>> {+ async paginate(+ options: PaginationOptionsInterface,+ where: Record<string, unknown> | undefined,+ ): Promise<Pagination<Project>> {const [results, total] = await this.projectsRepository.findAndCount({take: options.take || 100,skip: options.skip || 0,@@ -77,7 +93,10 @@ export class ProjectService {})}- async paginateShared(options: PaginationOptionsInterface, where: Record<string, unknown> | undefined): Promise<Pagination<ProjectShare>> {+ async paginateShared(+ options: PaginationOptionsInterface,+ where: Record<string, unknown> | undefined,+ ): Promise<Pagination<ProjectShare>> {const [results, total] = await this.projectShareRepository.findAndCount({take: options.take || 100,skip: options.skip || 0,@@ -112,15 +131,19 @@ export class ProjectService {}async deleteMultiple(pids: string[]): Promise<any> {- return this.projectsRepository.createQueryBuilder()- .delete()- // TODO: !!! Enforce Prepared Statements and Parameterization- .where(`id IN (${pids})`)- .execute()+ return (+ this.projectsRepository+ .createQueryBuilder()+ .delete()+ // TODO: !!! Enforce Prepared Statements and Parameterization+ .where(`id IN (${pids})`)+ .execute()+ )}async deleteMultipleShare(where: string): Promise<any> {- return this.projectShareRepository.createQueryBuilder()+ return this.projectShareRepository+ .createQueryBuilder().delete().where(where).execute()@@ -142,7 +165,10 @@ export class ProjectService {return this.projectShareRepository.find(params)}- async findOneShare(id: string, params: Object = {}): Promise<ProjectShare | null> {+ async findOneShare(+ id: string,+ params: Object = {},+ ): Promise<ProjectShare | null> {return this.projectShareRepository.findOne(id, params)}@@ -161,20 +187,38 @@ export class ProjectService {find(params: object): Promise<Project[]> {return this.projectsRepository.find(params)}- findOneWhere(where: Record<string, unknown>, params: object = {}): Promise<Project> {+ findOneWhere(+ where: Record<string, unknown>,+ params: object = {},+ ): Promise<Project> {return this.projectsRepository.findOne({ where, ...params })}allowedToView(project: Project, uid: string | null): void {- if (project.public || uid === project.admin?.id || _findIndex(project.share, ({ user }) => user?.id === uid) !== -1) {+ if (+ project.public ||+ uid === project.admin?.id ||+ _findIndex(project.share, ({ user }) => user?.id === uid) !== -1+ ) {return} else {throw new ForbiddenException('You are not allowed to view this project')}}- allowedToManage(project: Project, uid: string, roles: Array<UserType> = []): void {- if (uid === project.admin?.id || _includes(roles, UserType.ADMIN) || _findIndex(project.share, (share) => share.user?.id === uid && share.role === Role.admin) !== -1) {+ allowedToManage(+ project: Project,+ uid: string,+ roles: Array<UserType> = [],+ ): void {+ if (+ uid === project.admin?.id ||+ _includes(roles, UserType.ADMIN) ||+ _findIndex(+ project.share,+ share => share.user?.id === uid && share.role === Role.admin,+ ) !== -1+ ) {return} else {throw new ForbiddenException('You are not allowed to manage this project')@@ -202,7 +246,9 @@ export class ProjectService {// @ts-ignoreupdProject.public = Number(updProject.public)// @ts-ignore- updProject.origins = _isString(updProject.origins) ? updProject.origins : _join(updProject.origins, ',')+ updProject.origins = _isString(updProject.origins)+ ? updProject.origins+ : _join(updProject.origins, ',')return updProject}@@ -218,10 +264,20 @@ export class ProjectService {}validateProject(projectDTO: ProjectDTO) {- if (!isValidPID(projectDTO.id)) throw new UnprocessableEntityException('The provided Project ID (pid) is incorrect')- if (_size(projectDTO.name) > 50) throw new UnprocessableEntityException('The project name is too long')- if (_size(_join(projectDTO.origins, ',')) > 300) throw new UnprocessableEntityException('The list of allowed origins has to be smaller than 300 symbols')- if (_size(_join(projectDTO.ipBlacklist, ',')) > 300) throw new UnprocessableEntityException('The list of allowed blacklisted IP addresses must be less than 300 characters.')+ if (!isValidPID(projectDTO.id))+ throw new UnprocessableEntityException(+ 'The provided Project ID (pid) is incorrect',+ )+ if (_size(projectDTO.name) > 50)+ throw new UnprocessableEntityException('The project name is too long')+ if (_size(_join(projectDTO.origins, ',')) > 300)+ throw new UnprocessableEntityException(+ 'The list of allowed origins has to be smaller than 300 symbols',+ )+ if (_size(_join(projectDTO.ipBlacklist, ',')) > 300)+ throw new UnprocessableEntityException(+ 'The list of allowed blacklisted IP addresses must be less than 300 characters.',+ )_map(projectDTO.origins, host => {if (!ORIGINS_REGEX.test(_trim(host))) {@@ -242,7 +298,10 @@ export class ProjectService {let count: string | number = await redis.get(countKey)if (_isEmpty(count)) {- const monthStart = dayjs.utc().startOf('month').format('YYYY-MM-DD HH:mm:ss')+ const monthStart = dayjs+ .utc()+ .startOf('month')+ .format('YYYY-MM-DD HH:mm:ss')const monthEnd = dayjs.utc().endOf('month').format('YYYY-MM-DD HH:mm:ss')let pids@@ -263,15 +322,30 @@ export class ProjectService {return 0}- const count_ev_query = `SELECT COUNT() FROM analytics WHERE pid IN (${_join(_map(pids, el => `'${el.id}'`), ',')}) AND created BETWEEN '${monthStart}' AND '${monthEnd}'`- const count_custom_ev_query = `SELECT COUNT() FROM customEV WHERE pid IN (${_join(_map(pids, el => `'${el.id}'`), ',')}) AND created BETWEEN '${monthStart}' AND '${monthEnd}'`-- const pageviews = (await clickhouse.query(count_ev_query).toPromise())[0]['count()']- const customEvents = (await clickhouse.query(count_custom_ev_query).toPromise())[0]['count()']+ const count_ev_query = `SELECT COUNT() FROM analytics WHERE pid IN (${_join(+ _map(pids, el => `'${el.id}'`),+ ',',+ )}) AND created BETWEEN '${monthStart}' AND '${monthEnd}'`+ const count_custom_ev_query = `SELECT COUNT() FROM customEV WHERE pid IN (${_join(+ _map(pids, el => `'${el.id}'`),+ ',',+ )}) AND created BETWEEN '${monthStart}' AND '${monthEnd}'`++ const pageviews = (await clickhouse.query(count_ev_query).toPromise())[0][+ 'count()'+ ]+ const customEvents = (+ await clickhouse.query(count_custom_ev_query).toPromise()+ )[0]['count()']count = pageviews + customEvents- await redis.set(countKey, `${pageviews}`, 'EX', redisProjectCountCacheTimeout)+ await redis.set(+ countKey,+ `${pageviews}`,+ 'EX',+ redisProjectCountCacheTimeout,+ )} else {try {// @ts-ignorediff --git a/src/task-manager/task-manager.service.spec.ts b/src/task-manager/task-manager.service.spec.tsindex 82e663ab2..d77eadf20 100644--- a/src/task-manager/task-manager.service.spec.ts+++ b/src/task-manager/task-manager.service.spec.ts@@ -10,7 +10,7 @@ describe('TaskManagerService', () => {}).compile()service = module.get<TaskManagerService>(TaskManagerService)- });+ })it('should be defined', () => {expect(service).toBeDefined()diff --git a/src/task-manager/task-manager.service.ts b/src/task-manager/task-manager.service.tsindex 9487a03ee..96a6e9755 100644--- a/src/task-manager/task-manager.service.ts+++ b/src/task-manager/task-manager.service.ts@@ -19,8 +19,17 @@ import { LetterTemplate } from '../mailer/letter'import { AnalyticsService } from '../analytics/analytics.service'import { ReportFrequency, ACCOUNT_PLANS } from '../user/entities/user.entity'import {- clickhouse, redis, REDIS_LOG_DATA_CACHE_KEY, REDIS_LOG_CUSTOM_CACHE_KEY, isSelfhosted, REDIS_SESSION_SALT_KEY,- REDIS_USERS_COUNT_KEY, REDIS_PROJECTS_COUNT_KEY, REDIS_PAGEVIEWS_COUNT_KEY, SEND_WARNING_AT_PERC, PROJECT_INVITE_EXPIRE,+ clickhouse,+ redis,+ REDIS_LOG_DATA_CACHE_KEY,+ REDIS_LOG_CUSTOM_CACHE_KEY,+ isSelfhosted,+ REDIS_SESSION_SALT_KEY,+ REDIS_USERS_COUNT_KEY,+ REDIS_PROJECTS_COUNT_KEY,+ REDIS_PAGEVIEWS_COUNT_KEY,+ SEND_WARNING_AT_PERC,+ PROJECT_INVITE_EXPIRE,} from '../common/constants'import { getRandomTip } from '../common/utils'@@ -34,7 +43,7 @@ export class TaskManagerService {private readonly analyticsService: AnalyticsService,private readonly projectService: ProjectService,private readonly actionTokensService: ActionTokensService,- ) { }+ ) {}@Cron(CronExpression.EVERY_MINUTE)async saveLogData(): Promise<void> {@@ -68,13 +77,16 @@ export class TaskManagerService {async checkLeftEvents(): Promise<void> {const thisMonth = dayjs.utc().format('YYYY-MM-01')const users = await this.userService.find({- where: [{- evWarningSentOn: IsNull(),- isActive: true,- }, {- evWarningSentOn: LessThan(thisMonth),- isActive: true,- }],+ where: [+ {+ evWarningSentOn: IsNull(),+ isActive: true,+ },+ {+ evWarningSentOn: LessThan(thisMonth),+ isActive: true,+ },+ ],relations: ['projects'],select: ['id', 'email', 'planCode'],})@@ -93,10 +105,15 @@ export class TaskManagerService {const maxEventsCount = ACCOUNT_PLANS[planCode].monthlyUsageLimit || 0const totalMonthlyEvents = await this.projectService.getRedisCount(id)- const usedEV = totalMonthlyEvents * 100 / maxEventsCount+ const usedEV = (totalMonthlyEvents * 100) / maxEventsCountif (usedEV >= SEND_WARNING_AT_PERC) {- await this.mailerService.sendEmail(email, LetterTemplate.TierWarning, emailParams, 'broadcast')+ await this.mailerService.sendEmail(+ email,+ LetterTemplate.TierWarning,+ emailParams,+ 'broadcast',+ )await this.userService.update(id, {evWarningSentOn: dayjs.utc().format('YYYY-MM-DD HH:mm:ss'),})@@ -106,9 +123,16 @@ export class TaskManagerService {@Cron(CronExpression.EVERY_2_HOURS)async deleteOldShareInvitations(): Promise<void> {- const minDate = dayjs.utc().subtract(PROJECT_INVITE_EXPIRE, 'h').format('YYYY-MM-DD HH:mm:ss')- await this.actionTokensService.deleteMultiple(`action="${ActionTokenType.PROJECT_SHARE}" AND created<"${minDate}"`)- await this.projectService.deleteMultipleShare(`confirmed=0 AND created<"${minDate}"`)+ const minDate = dayjs+ .utc()+ .subtract(PROJECT_INVITE_EXPIRE, 'h')+ .format('YYYY-MM-DD HH:mm:ss')+ await this.actionTokensService.deleteMultiple(+ `action="${ActionTokenType.PROJECT_SHARE}" AND created<"${minDate}"`,+ )+ await this.projectService.deleteMultipleShare(+ `confirmed=0 AND created<"${minDate}"`,+ )}@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT)@@ -141,7 +165,7 @@ export class TaskManagerService {continue}- const ids = _map(users[i].projects, (p) => p.id)+ const ids = _map(users[i].projects, p => p.id)const data = await this.analyticsService.getSummary(ids, 'w')const result = {@@ -154,7 +178,12 @@ export class TaskManagerService {tip,}- await this.mailerService.sendEmail(users[i].email, LetterTemplate.ProjectReport, result, 'broadcast')+ await this.mailerService.sendEmail(+ users[i].email,+ LetterTemplate.ProjectReport,+ result,+ 'broadcast',+ )}}@@ -182,7 +211,7 @@ export class TaskManagerService {continue}- const ids = _map(users[i].projects, (p) => p.id)+ const ids = _map(users[i].projects, p => p.id)const data = await this.analyticsService.getSummary(ids, 'M')const result = {@@ -195,7 +224,12 @@ export class TaskManagerService {tip,}- await this.mailerService.sendEmail(users[i].email, LetterTemplate.ProjectReport, result, 'broadcast')+ await this.mailerService.sendEmail(+ users[i].email,+ LetterTemplate.ProjectReport,+ result,+ 'broadcast',+ )}}@@ -209,14 +243,18 @@ export class TaskManagerService {const CEquery = 'SELECT count(*) from customEV'const users = await this.userService.count()const projects = await this.projectService.count()- const pageviews = ((await clickhouse.query(PVquery).toPromise())[0]['count()']) + ((await clickhouse.query(CEquery).toPromise())[0]['count()'])+ const pageviews =+ (await clickhouse.query(PVquery).toPromise())[0]['count()'] ++ (await clickhouse.query(CEquery).toPromise())[0]['count()']await redis.set(REDIS_USERS_COUNT_KEY, users, 'EX', 630)await redis.set(REDIS_PROJECTS_COUNT_KEY, projects, 'EX', 630)await redis.set(REDIS_PAGEVIEWS_COUNT_KEY, pageviews, 'EX', 630)return {- users, projects, pageviews,+ users,+ projects,+ pageviews,}}}diff --git a/src/tg-integration/tg.update.ts b/src/tg-integration/tg.update.tsindex e31106036..b2736f0d9 100644--- a/src/tg-integration/tg.update.ts+++ b/src/tg-integration/tg.update.ts@@ -9,7 +9,7 @@ const HELLO_SCENE_ID = 'HELLO_SCENE_ID'export class SwetrixUpdate {@Start()onStart(): string {- return 'Say hello to me';+ return 'Say hello to me'}@Hears(['hi', 'hello', 'hey', 'qq'])@@ -17,11 +17,11 @@ export class SwetrixUpdate {@UpdateType() updateType: TelegrafUpdateType,@Sender('first_name') firstName: string,): string {- return `Hey ${firstName}`;+ return `Hey ${firstName}`}@Command('scene')async onSceneCommand(@Ctx() ctx: Context): Promise<void> {- await ctx.scene.enter(HELLO_SCENE_ID);+ await ctx.scene.enter(HELLO_SCENE_ID)}}diff --git a/src/twoFactorAuth/twoFactorAuth.controller.ts b/src/twoFactorAuth/twoFactorAuth.controller.tsindex e27634baa..383c4eb92 100644--- a/src/twoFactorAuth/twoFactorAuth.controller.ts+++ b/src/twoFactorAuth/twoFactorAuth.controller.ts@@ -1,5 +1,12 @@-import {- Controller, Post, UseGuards, HttpCode, Body, BadRequestException, Headers, Ip,+import {+ Controller,+ Post,+ UseGuards,+ HttpCode,+ Body,+ BadRequestException,+ Headers,+ Ip,} from '@nestjs/common'import { ApiTags } from '@nestjs/swagger'@@ -37,7 +44,9 @@ export class TwoFactorAuthController {async register(@CurrentUserId() id: string) {const user = await this.userService.findOneWhere({ id })- return await this.twoFactorAuthService.generateTwoFactorAuthenticationSecret(user)+ return await this.twoFactorAuthService.generateTwoFactorAuthenticationSecret(+ user,+ )}@Post('enable')@@ -45,18 +54,26 @@ export class TwoFactorAuthController {@UseGuards(SelfhostedGuard) // temporary this feature is not available for selfhosted@Roles(UserType.CUSTOMER, UserType.ADMIN)@TwoFaNotRequired()- async turnOnTwoFactorAuthentication(@Body() body: TwoFactorAuthDTO, @CurrentUserId() id: string, @Headers() headers, @Ip() reqIP) {+ async turnOnTwoFactorAuthentication(+ @Body() body: TwoFactorAuthDTO,+ @CurrentUserId() id: string,+ @Headers() headers,+ @Ip() reqIP,+ ) {this.logger.log({ body }, 'POST /2fa/enable')- const ip = headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''+ const ip =+ headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''await checkRateLimit(ip, '2fa-enable', 10, 1800)const user = await this.userService.findOneWhere({ id })const { twoFactorAuthenticationCode } = body- const isCodeValid = this.twoFactorAuthService.isTwoFactorAuthenticationCodeValid(- twoFactorAuthenticationCode, user,- )+ const isCodeValid =+ this.twoFactorAuthService.isTwoFactorAuthenticationCodeValid(+ twoFactorAuthenticationCode,+ user,+ )if (!isCodeValid) {throw new BadRequestException('Wrong authentication code')@@ -86,16 +103,27 @@ export class TwoFactorAuthController {@UseGuards(RolesGuard)@UseGuards(SelfhostedGuard) // temporary this feature is not available for selfhosted@Roles(UserType.CUSTOMER, UserType.ADMIN)- async turnOffTwoFactorAuthentication(@Body() body: TwoFactorAuthDTO, @CurrentUserId() id: string, @Headers() headers, @Ip() reqIP) {+ async turnOffTwoFactorAuthentication(+ @Body() body: TwoFactorAuthDTO,+ @CurrentUserId() id: string,+ @Headers() headers,+ @Ip() reqIP,+ ) {this.logger.log({ body }, 'POST /2fa/disable')- const ip = headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''+ const ip =+ headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''await checkRateLimit(ip, '2fa-disable', 10, 1800)const user = await this.userService.findOneWhere({ id })const { twoFactorAuthenticationCode } = body- const isCodeValid = user.twoFactorRecoveryCode === twoFactorAuthenticationCode || this.twoFactorAuthService.isTwoFactorAuthenticationCodeValid(twoFactorAuthenticationCode, user)+ const isCodeValid =+ user.twoFactorRecoveryCode === twoFactorAuthenticationCode ||+ this.twoFactorAuthService.isTwoFactorAuthenticationCodeValid(+ twoFactorAuthenticationCode,+ user,+ )if (!isCodeValid) {throw new BadRequestException('Wrong authentication code')@@ -116,16 +144,27 @@ export class TwoFactorAuthController {@UseGuards(SelfhostedGuard) // temporary this feature is not available for selfhosted@Roles(UserType.CUSTOMER, UserType.ADMIN)@TwoFaNotRequired()- async authenticate(@Body() body: TwoFactorAuthDTO, @CurrentUserId() id: string, @Headers() headers, @Ip() reqIP) {+ async authenticate(+ @Body() body: TwoFactorAuthDTO,+ @CurrentUserId() id: string,+ @Headers() headers,+ @Ip() reqIP,+ ) {this.logger.log({ body }, 'POST /2fa/authenticate')- const ip = headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''+ const ip =+ headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''await checkRateLimit(ip, '2fa-auth', 10, 1800)const user = await this.userService.findOneWhere({ id })const { twoFactorAuthenticationCode } = body- const isCodeValid = user.twoFactorRecoveryCode === twoFactorAuthenticationCode || this.twoFactorAuthService.isTwoFactorAuthenticationCodeValid(twoFactorAuthenticationCode, user)+ const isCodeValid =+ user.twoFactorRecoveryCode === twoFactorAuthenticationCode ||+ this.twoFactorAuthService.isTwoFactorAuthenticationCodeValid(+ twoFactorAuthenticationCode,+ user,+ )if (!isCodeValid) {throw new BadRequestException('Wrong authentication code')diff --git a/src/twoFactorAuth/twoFactorAuth.module.ts b/src/twoFactorAuth/twoFactorAuth.module.tsindex c6cddb168..27b7bd516 100644--- a/src/twoFactorAuth/twoFactorAuth.module.ts+++ b/src/twoFactorAuth/twoFactorAuth.module.ts@@ -7,12 +7,7 @@ import { AppLoggerModule } from '../logger/logger.module'import { MailerModule } from '../mailer/mailer.module'@Module({- imports: [- UserModule,- AppLoggerModule,- AuthModule,- MailerModule,- ],+ imports: [UserModule, AppLoggerModule, AuthModule, MailerModule],controllers: [TwoFactorAuthController],providers: [TwoFactorAuthService],exports: [TwoFactorAuthService],diff --git a/src/twoFactorAuth/twoFactorAuth.service.ts b/src/twoFactorAuth/twoFactorAuth.service.tsindex 2caaf3e85..1dd51765b 100644--- a/src/twoFactorAuth/twoFactorAuth.service.ts+++ b/src/twoFactorAuth/twoFactorAuth.service.ts@@ -3,19 +3,19 @@ import { authenticator } from 'otplib'import { UserService } from '../user/user.service'import { User } from '../user/entities/user.entity'-import {- TWO_FACTOR_AUTHENTICATION_APP_NAME,-} from '../common/constants'+import { TWO_FACTOR_AUTHENTICATION_APP_NAME } from '../common/constants'@Injectable()export class TwoFactorAuthService {- constructor(- private userService: UserService,- ) { }+ constructor(private userService: UserService) {}async generateTwoFactorAuthenticationSecret(user: User) {const secret = authenticator.generateSecret()- const otpauthUrl = authenticator.keyuri(user.email, TWO_FACTOR_AUTHENTICATION_APP_NAME, secret)+ const otpauthUrl = authenticator.keyuri(+ user.email,+ TWO_FACTOR_AUTHENTICATION_APP_NAME,+ secret,+ )await this.userService.update(user.id, {twoFactorAuthenticationSecret: secret,@@ -27,7 +27,10 @@ export class TwoFactorAuthService {}}- isTwoFactorAuthenticationCodeValid(twoFactorAuthenticationCode: string, user: User) {+ isTwoFactorAuthenticationCodeValid(+ twoFactorAuthenticationCode: string,+ user: User,+ ) {return authenticator.verify({token: twoFactorAuthenticationCode,secret: user.twoFactorAuthenticationSecret,diff --git a/src/user/entities/user.entity.ts b/src/user/entities/user.entity.tsindex b6bdafc0f..2b014aedd 100644--- a/src/user/entities/user.entity.ts+++ b/src/user/entities/user.entity.ts@@ -1,4 +1,10 @@-import { Entity, Column, PrimaryGeneratedColumn, OneToMany, BeforeUpdate } from 'typeorm'+import {+ Entity,+ Column,+ PrimaryGeneratedColumn,+ OneToMany,+ BeforeUpdate,+} from 'typeorm'import { ActionToken } from 'src/action-tokens/action-token.entity'import { Project } from 'src/project/entity/project.entity'import { ProjectShare } from 'src/project/entity/project-share.entity'@@ -137,7 +143,7 @@ export class User {@BeforeUpdate()updateTimestamp() {- this.updated = new Date+ this.updated = new Date()}@OneToMany(() => Project, project => project.admin)diff --git a/src/user/user.controller.spec.ts b/src/user/user.controller.spec.tsindex c4723dce1..36a66dc08 100644--- a/src/user/user.controller.spec.ts+++ b/src/user/user.controller.spec.ts@@ -1,5 +1,5 @@-import { Test, TestingModule } from '@nestjs/testing';-import { UserController } from './user.controller';+import { Test, TestingModule } from '@nestjs/testing'+import { UserController } from './user.controller'describe('UserController', () => {let controller: UserControllerdiff --git a/src/user/user.controller.ts b/src/user/user.controller.tsindex 3f1af6fd3..af9316151 100644--- a/src/user/user.controller.ts+++ b/src/user/user.controller.ts@@ -1,5 +1,20 @@import {- Controller, Query, Req, Body, Param, Get, Post, Put, Delete, HttpCode, BadRequestException, UseGuards, MethodNotAllowedException, ConflictException, Headers, Ip+ Controller,+ Query,+ Req,+ Body,+ Param,+ Get,+ Post,+ Put,+ Delete,+ HttpCode,+ BadRequestException,+ UseGuards,+ MethodNotAllowedException,+ ConflictException,+ Headers,+ Ip,} from '@nestjs/common'import { Request } from 'express'import { ApiTags, ApiQuery, ApiResponse } from '@nestjs/swagger'@@ -17,13 +32,14 @@ import { UserService } from './user.service'import { ProjectService } from '../project/project.service'import { deleteProjectRedis } from '../project/project.controller'import {- User, UserType, MAX_EMAIL_REQUESTS, PlanCode,+ User,+ UserType,+ MAX_EMAIL_REQUESTS,+ PlanCode,} from './entities/user.entity'import { Roles } from '../common/decorators/roles.decorator'import { Pagination } from '../common/pagination/pagination'-import {- GDPR_EXPORT_TIMEFRAME, clickhouse,-} from '../common/constants'+import { GDPR_EXPORT_TIMEFRAME, clickhouse } from '../common/constants'import { RolesGuard } from 'src/common/guards/roles.guard'import { SelfhostedGuard } from '../common/guards/selfhosted.guard'import { UpdateUserProfileDTO } from './dto/update-user.dto'@@ -59,7 +75,10 @@ export class UserController {@UseGuards(RolesGuard)@UseGuards(SelfhostedGuard)@Roles(UserType.ADMIN)- async get(@Query('take') take: number | undefined, @Query('skip') skip: number | undefined): Promise<Pagination<User> | User[]> {+ async get(+ @Query('take') take: number | undefined,+ @Query('skip') skip: number | undefined,+ ): Promise<Pagination<User> | User[]> {this.logger.log({ take, skip }, 'GET /user')return await this.userService.paginate({ take, skip })}@@ -69,7 +88,9 @@ export class UserController {@UseGuards(RolesGuard)@UseGuards(SelfhostedGuard)@Roles(UserType.ADMIN)- async searchUsers(@Query('query') query: string | undefined): Promise<User[]> {+ async searchUsers(+ @Query('query') query: string | undefined,+ ): Promise<User[]> {this.logger.log({ query }, 'GET /user/search')return await this.userService.search(query)}@@ -86,9 +107,9 @@ export class UserController {try {const user = await this.userService.create({ ...userDTO, isActive: true })return user- } catch(e) {- if (e.code === 'ER_DUP_ENTRY'){- if(e.sqlMessage.includes(userDTO.email)) {+ } catch (e) {+ if (e.code === 'ER_DUP_ENTRY') {+ if (e.sqlMessage.includes(userDTO.email)) {throw new BadRequestException('User with this email already exists')}}@@ -99,12 +120,17 @@ export class UserController {@UseGuards(RolesGuard)@UseGuards(SelfhostedGuard)@Roles(UserType.CUSTOMER, UserType.ADMIN)- async generateApiKey(@CurrentUserId() userId: string, @Headers() headers, @Ip() reqIP): Promise<{+ async generateApiKey(+ @CurrentUserId() userId: string,+ @Headers() headers,+ @Ip() reqIP,+ ): Promise<{apiKey: string}> {this.logger.log({ userId }, 'POST /user/api-key')- const ip = headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''+ const ip =+ headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''await checkRateLimit(ip, 'generate-api-key', 5, 3600)@@ -142,7 +168,10 @@ export class UserController {@UseGuards(RolesGuard)@UseGuards(SelfhostedGuard)@Roles(UserType.ADMIN)- async delete(@Param('id') id: string, @CurrentUserId() uid: string): Promise<any> {+ async delete(+ @Param('id') id: string,+ @CurrentUserId() uid: string,+ ): Promise<any> {this.logger.log({ id, uid }, 'DELETE /user/:id')const user = await this.userService.findOne(id, {relations: ['projects'],@@ -159,7 +188,10 @@ export class UserController {try {if (!_isEmpty(user.projects)) {- const pids = _join(_map(user.projects, el => `'${el.id}'`), ',')+ const pids = _join(+ _map(user.projects, el => `'${el.id}'`),+ ',',+ )const query1 = `ALTER table analytics DELETE WHERE pid IN (${pids})`const query2 = `ALTER table customEV DELETE WHERE pid IN (${pids})`await this.projectService.deleteMultiple(pids)@@ -169,7 +201,7 @@ export class UserController {await this.userService.delete(id)return 'accountDeleted'- } catch(e) {+ } catch (e) {this.logger.error(e)throw new BadRequestException('accountDeleteError')}@@ -194,7 +226,10 @@ export class UserController {try {if (!_isEmpty(user.projects)) {- const pids = _join(_map(user.projects, el => `'${el.id}'`), ',')+ const pids = _join(+ _map(user.projects, el => `'${el.id}'`),+ ',',+ )const query1 = `ALTER table analytics DELETE WHERE pid IN (${pids})`const query2 = `ALTER table customEV DELETE WHERE pid IN (${pids})`await this.projectService.deleteMultiple(pids)@@ -205,7 +240,7 @@ export class UserController {await this.userService.delete(id)return 'accountDeleted'- } catch(e) {+ } catch (e) {this.logger.error(e)throw new BadRequestException('accountDeleteError')}@@ -277,18 +312,33 @@ export class UserController {@UseGuards(RolesGuard)@UseGuards(SelfhostedGuard)@Roles(UserType.CUSTOMER, UserType.ADMIN)- async sendEmailConfirmation(@CurrentUserId() id: string, @Req() request: Request): Promise<boolean> {+ async sendEmailConfirmation(+ @CurrentUserId() id: string,+ @Req() request: Request,+ ): Promise<boolean> {this.logger.log({ id }, 'POST /user/confirm_email')const user = await this.userService.findOneWhere({ id })- if (!user || !user.email || user.isActive || user.emailRequests >= MAX_EMAIL_REQUESTS) return false-- const token = await this.actionTokensService.createForUser(user, ActionTokenType.EMAIL_VERIFICATION, user.email)+ if (+ !user ||+ !user.email ||+ user.isActive ||+ user.emailRequests >= MAX_EMAIL_REQUESTS+ )+ return false++ const token = await this.actionTokensService.createForUser(+ user,+ ActionTokenType.EMAIL_VERIFICATION,+ user.email,+ )const url = `${request.headers.origin}/verify/${token.id}`await this.userService.update(id, { emailRequests: 1 + user.emailRequests })- await this.mailerService.sendEmail(user.email, LetterTemplate.SignUp, { url })+ await this.mailerService.sendEmail(user.email, LetterTemplate.SignUp, {+ url,+ })return true}@@ -296,7 +346,10 @@ export class UserController {@UseGuards(RolesGuard)@UseGuards(SelfhostedGuard)@Roles(UserType.ADMIN)- async update(@Body() userDTO: AdminUpdateUserProfileDTO, @Param('id') id: string): Promise<User> {+ async update(+ @Body() userDTO: AdminUpdateUserProfileDTO,+ @Param('id') id: string,+ ): Promise<User> {this.logger.log({ userDTO, id }, 'PUT /user/:id')if (userDTO.password) {@@ -312,9 +365,11 @@ export class UserController {}await this.userService.update(id, { ...user, ...userDTO })// omit sensitive data before returning using this.userService.omitSensitiveData function- return this.userService.omitSensitiveData(await this.userService.findOne(id))+ return this.userService.omitSensitiveData(+ await this.userService.findOne(id),+ )} catch (e) {- if (e.code === 'ER_DUP_ENTRY'){+ if (e.code === 'ER_DUP_ENTRY') {if (e.sqlMessage.includes(userDTO.email)) {throw new BadRequestException('User with this email already exists')}@@ -327,31 +382,68 @@ export class UserController {@UseGuards(RolesGuard)@UseGuards(SelfhostedGuard)@Roles(UserType.CUSTOMER, UserType.ADMIN)- async updateCurrentUser(@Body() userDTO: UpdateUserProfileDTO, @CurrentUserId() id: string, @Req() request: Request): Promise<User> {+ async updateCurrentUser(+ @Body() userDTO: UpdateUserProfileDTO,+ @CurrentUserId() id: string,+ @Req() request: Request,+ ): Promise<User> {this.logger.log({ userDTO, id }, 'PUT /user')const user = await this.userService.findOneWhere({ id })if (!_isEmpty(userDTO.password) && _isString(userDTO.password)) {this.userService.validatePassword(userDTO.password)userDTO.password = await this.authService.hashPassword(userDTO.password)- await this.mailerService.sendEmail(userDTO.email, LetterTemplate.PasswordChanged)+ await this.mailerService.sendEmail(+ userDTO.email,+ LetterTemplate.PasswordChanged,+ )}try {if (userDTO.email && user.email !== userDTO.email) {- const userWithByEmail = await this.userService.findOneWhere({ email: userDTO.email })+ const userWithByEmail = await this.userService.findOneWhere({+ email: userDTO.email,+ })if (userWithByEmail) {throw new BadRequestException('User with this email already exists')}- const token = await this.actionTokensService.createForUser(user, ActionTokenType.EMAIL_CHANGE, userDTO.email)+ const token = await this.actionTokensService.createForUser(+ user,+ ActionTokenType.EMAIL_CHANGE,+ userDTO.email,+ )const url = `${request.headers.origin}/change-email/${token.id}`- await this.mailerService.sendEmail(user.email, LetterTemplate.MailAddressChangeConfirmation, { url })+ await this.mailerService.sendEmail(+ user.email,+ LetterTemplate.MailAddressChangeConfirmation,+ { url },+ )}// delete internal properties from userDTO before updating it// todo: use _pick instead of _omit- const userToUpdate = _omit(userDTO, ['id', 'sharedProjects', 'isActive', 'evWarningSentOn', 'exportedAt', 'subID', 'subUpdateURL', 'subCancelURL', 'projects', 'actionTokens', 'roles', 'created', 'updated', 'planCode', 'billingFrequency', 'nextBillDate', 'twoFactorRecoveryCode', 'twoFactorAuthenticationSecret', 'isTwoFactorAuthenticationEnabled'])+ const userToUpdate = _omit(userDTO, [+ 'id',+ 'sharedProjects',+ 'isActive',+ 'evWarningSentOn',+ 'exportedAt',+ 'subID',+ 'subUpdateURL',+ 'subCancelURL',+ 'projects',+ 'actionTokens',+ 'roles',+ 'created',+ 'updated',+ 'planCode',+ 'billingFrequency',+ 'nextBillDate',+ 'twoFactorRecoveryCode',+ 'twoFactorAuthenticationSecret',+ 'isTwoFactorAuthenticationEnabled',+ ])await this.userService.update(id, userToUpdate)const updatedUser = await this.userService.findOneWhere({ id })@@ -371,8 +463,16 @@ export class UserController {const where = Object({ admin: user_id })const projects = await this.projectService.findWhere(where)- if (!_isNull(user.exportedAt) && !dayjs().isAfter(dayjs.utc(user.exportedAt).add(GDPR_EXPORT_TIMEFRAME, 'day'), 'day')) {- throw new MethodNotAllowedException(`Please, try again later. You can request a GDPR Export only once per ${GDPR_EXPORT_TIMEFRAME} days.`)+ if (+ !_isNull(user.exportedAt) &&+ !dayjs().isAfter(+ dayjs.utc(user.exportedAt).add(GDPR_EXPORT_TIMEFRAME, 'day'),+ 'day',+ )+ ) {+ throw new MethodNotAllowedException(+ `Please, try again later. You can request a GDPR Export only once per ${GDPR_EXPORT_TIMEFRAME} days.`,+ )}const data = {@@ -380,8 +480,12 @@ export class UserController {...user,created: dayjs(user.created).format('YYYY/MM/DD HH:mm:ss'),updated: dayjs(user.updated).format('YYYY/MM/DD HH:mm:ss'),- exportedAt: _isNull(user.exportedAt) ? '-' : dayjs(user.exportedAt).format('YYYY/MM/DD HH:mm:ss'),- evWarningSentOn: _isNull(user.evWarningSentOn) ? '-' : dayjs(user.evWarningSentOn).format('YYYY/MM/DD HH:mm:ss'),+ exportedAt: _isNull(user.exportedAt)+ ? '-'+ : dayjs(user.exportedAt).format('YYYY/MM/DD HH:mm:ss'),+ evWarningSentOn: _isNull(user.evWarningSentOn)+ ? '-'+ : dayjs(user.evWarningSentOn).format('YYYY/MM/DD HH:mm:ss'),subID: user.subID || '-',subUpdateURL: user.subUpdateURL || '-',subCancelURL: user.subCancelURL || '-',@@ -391,15 +495,18 @@ export class UserController {created: dayjs(project.created).format('YYYY/MM/DD HH:mm:ss'),origins: _join(project.origins, ', '),ipBlacklist: _join(project.ipBlacklist, ', '),- }))+ })),}- await this.mailerService.sendEmail(user.email, LetterTemplate.GDPRDataExport, data)+ await this.mailerService.sendEmail(+ user.email,+ LetterTemplate.GDPRDataExport,+ data,+ )await this.userService.update(user.id, {exportedAt: dayjs.utc().format('YYYY-MM-DD HH:mm:ss'),})return user}-}diff --git a/src/user/user.service.ts b/src/user/user.service.tsindex df05eae1d..9cbcf9b4d 100644--- a/src/user/user.service.ts+++ b/src/user/user.service.ts@@ -2,8 +2,8 @@ import { Injectable, BadRequestException } from '@nestjs/common'import { InjectRepository } from '@nestjs/typeorm'import { Repository } from 'typeorm'import * as _isEmpty from 'lodash/isEmpty'-import * as _size from 'lodash/size'-import * as _omit from 'lodash/omit'+import * as _size from 'lodash/size'+import * as _omit from 'lodash/omit'import { Pagination, PaginationOptionsInterface } from '../common/pagination'import { User } from './entities/user.entity'@@ -20,27 +20,35 @@ export class UserService {return await this.usersRepository.save(userDTO)}- async paginate(options: PaginationOptionsInterface): Promise<Pagination<User>> {+ async paginate(+ options: PaginationOptionsInterface,+ ): Promise<Pagination<User>> {const [results, total] = await this.usersRepository.findAndCount({take: options.take || 10,skip: options.skip || 0,})-+return new Pagination<User>({results,total,})- }+ }async update(id: string, update: Record<string, unknown>): Promise<any> {return this.usersRepository.update({ id }, update)}- async updateByEmail(email: string, update: Record<string, unknown>): Promise<any> {+ async updateByEmail(+ email: string,+ update: Record<string, unknown>,+ ): Promise<any> {return this.usersRepository.update({ email }, update)}- async updateBySubID(subID: string, update: Record<string, unknown>): Promise<any> {+ async updateBySubID(+ subID: string,+ update: Record<string, unknown>,+ ): Promise<any> {return this.usersRepository.update({ subID }, update)}@@ -53,16 +61,24 @@ export class UserService {}omitSensitiveData(user: User): User {- return _omit(user, ['password', 'twoFactorRecoveryCode', 'twoFactorAuthenticationSecret'])+ return _omit(user, [+ 'password',+ 'twoFactorRecoveryCode',+ 'twoFactorAuthenticationSecret',+ ])}- findOneWhere(where: Record<string, unknown>, relations: string[] = []): Promise<User> {+ findOneWhere(+ where: Record<string, unknown>,+ relations: string[] = [],+ ): Promise<User> {return this.usersRepository.findOne({ where, relations })}findOne(id: string, params: object = {}): Promise<User> {return this.usersRepository.findOne({- where: { id }, ...params,+ where: { id },+ ...params,})}@@ -70,9 +86,13 @@ export class UserService {return this.usersRepository.findOne(id, { relations })}- findWhereWithRelations(where: Record<string, unknown>, relations: string[]): Promise<User[]> {+ findWhereWithRelations(+ where: Record<string, unknown>,+ relations: string[],+ ): Promise<User[]> {return this.usersRepository.find({- where, relations,+ where,+ relations,})}@@ -90,11 +110,11 @@ export class UserService {err.push('Password cannot be empty')}- if(_size(pass) > 50) {+ if (_size(pass) > 50) {err.push('Maximum password length is 50 letters')}- if(_size(pass) < 8) {+ if (_size(pass) < 8) {err.push('at least 8 characters')}diff --git a/src/webhook/webhook.controller.ts b/src/webhook/webhook.controller.tsindex bad15cf6e..602f2603b 100644--- a/src/webhook/webhook.controller.ts+++ b/src/webhook/webhook.controller.ts@@ -1,12 +1,21 @@import {- Controller, Body, Post, Headers, BadRequestException, NotFoundException, UseGuards, Ip,+ Controller,+ Body,+ Post,+ Headers,+ BadRequestException,+ NotFoundException,+ UseGuards,+ Ip,} from '@nestjs/common'import { ApiTags } from '@nestjs/swagger'-import * as _isEmpty from 'lodash/isEmpty'-import * as _keys from 'lodash/keys'import * as _find from 'lodash/find'-import { PlanCode, ACCOUNT_PLANS, BillingFrequency } from '../user/entities/user.entity'+import {+ PlanCode,+ ACCOUNT_PLANS,+ BillingFrequency,+} from '../user/entities/user.entity'import { UserService } from '../user/user.service'import { AppLoggerService } from '../logger/logger.service'import { WebhookService } from './webhook.service'@@ -19,12 +28,17 @@ export class WebhookController {private readonly logger: AppLoggerService,private readonly userService: UserService,private readonly webhookService: WebhookService,- ) { }+ ) {}@UseGuards(SelfhostedGuard)@Post('/paddle')- async paddleWebhook(@Body() body, @Headers() headers, @Ip() reqIP): Promise<any> {- const ip = headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''+ async paddleWebhook(+ @Body() body,+ @Headers() headers,+ @Ip() reqIP,+ ): Promise<any> {+ const ip =+ headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''this.webhookService.verifyIP(ip)this.webhookService.validateWebhook(body)@@ -33,26 +47,44 @@ export class WebhookController {case 'subscription_created':case 'subscription_updated': {const {- passthrough, email, subscription_id, subscription_plan_id, cancel_url, update_url, next_bill_date,+ passthrough,+ email,+ subscription_id,+ subscription_plan_id,+ cancel_url,+ update_url,+ next_bill_date,} = bodylet uidtry {uid = JSON.parse(passthrough)?.uid} catch {- this.logger.error(`[${body.alert_name}] Cannot parse the uid: ${JSON.stringify(body)}`)+ this.logger.error(+ `[${body.alert_name}] Cannot parse the uid: ${JSON.stringify(+ body,+ )}`,+ )}let monthlyBilling = true- let plan = _find(ACCOUNT_PLANS, ({ pid }) => pid === subscription_plan_id)+ let plan = _find(+ ACCOUNT_PLANS,+ ({ pid }) => pid === subscription_plan_id,+ )if (!plan) {monthlyBilling = false- plan = _find(ACCOUNT_PLANS, ({ ypid }) => ypid === subscription_plan_id)+ plan = _find(+ ACCOUNT_PLANS,+ ({ ypid }) => ypid === subscription_plan_id,+ )}if (!plan) {- throw new NotFoundException(`The selected account plan (${subscription_plan_id}) is not available`)+ throw new NotFoundException(+ `The selected account plan (${subscription_plan_id}) is not available`,+ )}const updateParams = {@@ -61,7 +93,9 @@ export class WebhookController {subUpdateURL: update_url,subCancelURL: cancel_url,nextBillDate: next_bill_date,- billingFrequency: monthlyBilling ? BillingFrequency.Monthly : BillingFrequency.Yearly,+ billingFrequency: monthlyBilling+ ? BillingFrequency.Monthly+ : BillingFrequency.Yearly,}if (uid) {diff --git a/src/webhook/webhook.module.ts b/src/webhook/webhook.module.tsindex 0a9d94965..554b611fb 100644--- a/src/webhook/webhook.module.ts+++ b/src/webhook/webhook.module.ts@@ -6,10 +6,7 @@ import { AppLoggerModule } from '../logger/logger.module'import { WebhookService } from './webhook.service'@Module({- imports: [- forwardRef(() => UserModule),- AppLoggerModule,- ],+ imports: [forwardRef(() => UserModule), AppLoggerModule],providers: [WebhookService],exports: [WebhookService],controllers: [WebhookController],diff --git a/src/webhook/webhook.service.ts b/src/webhook/webhook.service.tsindex d0395f430..08211c9d1 100644--- a/src/webhook/webhook.service.ts+++ b/src/webhook/webhook.service.ts@@ -1,4 +1,8 @@-import { Injectable, BadRequestException, ForbiddenException } from '@nestjs/common'+import {+ Injectable,+ BadRequestException,+ ForbiddenException,+} from '@nestjs/common'import * as crypto from 'crypto'import * as Serialize from 'php-serialize'import * as _includes from 'lodash/includes'@@ -32,14 +36,12 @@ const paddleWhitelistIPs = [@Injectable()export class WebhookService {- constructor(- private readonly logger: AppLoggerService,- ) { }+ constructor(private readonly logger: AppLoggerService) {}ksort(obj) {const keys = Object.keys(obj).sort()- let sortedObj = {}- for (let i in keys) {+ const sortedObj = {}+ for (const i in keys) {sortedObj[keys[i]] = obj[keys[i]]}return sortedObj@@ -52,11 +54,13 @@ export class WebhookService {delete data.p_signature// Need to sort array by key in ascending orderdata = this.ksort(data)- for (let property in data) {- if (data.hasOwnProperty(property) && (typeof data[property]) !== "string") {- if (Array.isArray(data[property])) { // is it an array+ for (const property in data) {+ if (data.hasOwnProperty(property) && typeof data[property] !== 'string') {+ if (Array.isArray(data[property])) {+ // is it an arraydata[property] = data[property].toString()- } else { // if its not an array and not a string, then it is a JSON obj+ } else {+ // if its not an array and not a string, then it is a JSON objdata[property] = JSON.stringify(data[property])}}diff --git a/test/app.e2e-spec.ts b/test/app.e2e-spec.tsindex 50cda6233..bbdc02890 100644--- a/test/app.e2e-spec.ts+++ b/test/app.e2e-spec.ts@@ -1,24 +1,24 @@-import { Test, TestingModule } from '@nestjs/testing';-import { INestApplication } from '@nestjs/common';-import * as request from 'supertest';-import { AppModule } from './../src/app.module';+import { Test, TestingModule } from '@nestjs/testing'+import { INestApplication } from '@nestjs/common'+import * as request from 'supertest'+import { AppModule } from './../src/app.module'describe('AppController (e2e)', () => {- let app: INestApplication;+ let app: INestApplicationbeforeEach(async () => {const moduleFixture: TestingModule = await Test.createTestingModule({imports: [AppModule],- }).compile();+ }).compile()- app = moduleFixture.createNestApplication();- await app.init();- });+ app = moduleFixture.createNestApplication()+ await app.init()+ })it('/ (GET)', () => {return request(app.getHttpServer()).get('/').expect(200)- .expect('Hello World!');- });-});+ .expect('Hello World!')+ })+})