📈 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
Implemented route to add project subscribers
Yevhenii Kulisidi8b3d20b58c27
diff --git a/src/common/templates/en/project-subscriber-invitation.html b/src/common/templates/en/project-subscriber-invitation.htmlnew file mode 100644index 000000000..3913f2a55--- /dev/null+++ b/src/common/templates/en/project-subscriber-invitation.html@@ -0,0 +1 @@+<p>{{inviteLink}}</p>diff --git a/src/mailer/letter.ts b/src/mailer/letter.tsindex b88105109..11aa01c9a 100644--- a/src/mailer/letter.ts+++ b/src/mailer/letter.ts@@ -1,16 +1,17 @@-export enum LetterTemplate {- SignUp = 'customer_sign-up',- ConfirmPasswordChange = 'password-change_confirmation',- PasswordChanged = 'password-change_information',- MailAddressChangeConfirmation = 'mailaddress-change_confirmation',- MailAddressHadChanged = 'mailaddress-change_information',- PasswordChangeRequest = 'password-change_request',- GDPRDataExport = 'gdpr_data_export',- ProjectReport = 'project-report',- TierWarning = 'tier-warning',- ProjectInvitation = 'customer_project-invitation',- TwoFAOn = '2fa-turned-on',- TwoFAOff = '2fa-turned-off',- TrialExpired = 'trial-expired',- TrialEndsTomorrow = 'trial-ends-tomorrow',-}+export enum LetterTemplate {+ SignUp = 'customer_sign-up',+ ConfirmPasswordChange = 'password-change_confirmation',+ PasswordChanged = 'password-change_information',+ MailAddressChangeConfirmation = 'mailaddress-change_confirmation',+ MailAddressHadChanged = 'mailaddress-change_information',+ PasswordChangeRequest = 'password-change_request',+ GDPRDataExport = 'gdpr_data_export',+ ProjectReport = 'project-report',+ TierWarning = 'tier-warning',+ ProjectInvitation = 'customer_project-invitation',+ TwoFAOn = '2fa-turned-on',+ TwoFAOff = '2fa-turned-off',+ TrialExpired = 'trial-expired',+ TrialEndsTomorrow = 'trial-ends-tomorrow',+ ProjectSubscriberInvitation = 'project-subscriber-invitation',+}diff --git a/src/mailer/mailer.service.ts b/src/mailer/mailer.service.tsindex f71e3dc02..f2dd273fb 100644--- a/src/mailer/mailer.service.ts+++ b/src/mailer/mailer.service.ts@@ -1,144 +1,149 @@-import { Injectable } from '@nestjs/common'-import * as postmark from 'postmark'-import handlebars from 'handlebars'-import { LetterTemplate } from './letter'-import fs = require('fs')-import path = require('path')-import { AppLoggerService } from '../logger/logger.service'-import { SEND_WARNING_AT_PERC } from '../common/constants'--const TEMPLATES_PATH = path.join(__dirname, '..', 'common', 'templates')-const metaInfoJson = {- [LetterTemplate.SignUp]: {- subject: {- en: () => 'Please verify your account registration',- },- },- [LetterTemplate.ConfirmPasswordChange]: {- subject: {- en: () => 'Please confirm the password change',- },- },- [LetterTemplate.MailAddressChangeConfirmation]: {- subject: {- en: () => 'Please confirm the new e-mail address',- },- },- [LetterTemplate.MailAddressHadChanged]: {- subject: {- en: () => 'A new e-mail-address has been saved',- },- },- [LetterTemplate.PasswordChangeRequest]: {- subject: {- en: () => 'Changing your password',- },- },- [LetterTemplate.PasswordChanged]: {- subject: {- en: () => 'Your password was successfully changed',- },- },- [LetterTemplate.GDPRDataExport]: {- subject: {- en: () => 'Swetrix Account data export',- },- },- [LetterTemplate.ProjectReport]: {- subject: {- 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.`,- },- },- [LetterTemplate.ProjectInvitation]: {- subject: {- en: () => 'You have been invited to join the project.',- },- },- [LetterTemplate.TwoFAOn]: {- subject: {- en: () => '2FA has been enabled on your Swetrix account',- },- },- [LetterTemplate.TwoFAOff]: {- subject: {- en: () => '2FA has been disabled on your Swetrix account',- },- },- [LetterTemplate.TrialExpired]: {- subject: {- en: () => 'Your free Swetrix trial has ended',- },- },- [LetterTemplate.TrialEndsTomorrow]: {- subject: {- en: () => 'Your free Swetrix trial ends tomorrow',- },- },-}--interface Params {- [name: string]: any-}--handlebars.registerHelper('ifEquals', function (arg1, arg2, options) {- return arg1 == arg2 ? options.fn(this) : options.inverse(this)-})--handlebars.registerHelper('greater', function (v1, v2, options) {- if (v1 > v2) {- return options.fn(this)- }- return options.inverse(this)-})--const mailClient = new postmark.ServerClient(process.env.SMTP_PASSWORD)--@Injectable()-export class MailerService {- constructor(private readonly logger: AppLoggerService) {}-- 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' })- const subject = metaInfoJson[templateName].subject.en(params)- const template = handlebars.compile(letter)- const htmlToSend = template(params)-- const message = {- From: process.env.FROM_EMAIL,- To: email,- Subject: subject,- HtmlBody: htmlToSend,- MessageStream: messageStream,- }-- if (process.env.SMTP_MOCK) {- this.logger.log(- {- ...message,- params,- },- 'sendEmail',- true,- )- } else {- await mailClient.sendEmail(message)- }- } catch (error) {- console.error(`[ERROR][MAILER] ${error}`)- }- }-}+import { Injectable } from '@nestjs/common'+import * as postmark from 'postmark'+import handlebars from 'handlebars'+import { LetterTemplate } from './letter'+import fs = require('fs')+import path = require('path')+import { AppLoggerService } from '../logger/logger.service'+import { SEND_WARNING_AT_PERC } from '../common/constants'++const TEMPLATES_PATH = path.join(__dirname, '..', 'common', 'templates')+const metaInfoJson = {+ [LetterTemplate.SignUp]: {+ subject: {+ en: () => 'Please verify your account registration',+ },+ },+ [LetterTemplate.ConfirmPasswordChange]: {+ subject: {+ en: () => 'Please confirm the password change',+ },+ },+ [LetterTemplate.MailAddressChangeConfirmation]: {+ subject: {+ en: () => 'Please confirm the new e-mail address',+ },+ },+ [LetterTemplate.MailAddressHadChanged]: {+ subject: {+ en: () => 'A new e-mail-address has been saved',+ },+ },+ [LetterTemplate.PasswordChangeRequest]: {+ subject: {+ en: () => 'Changing your password',+ },+ },+ [LetterTemplate.PasswordChanged]: {+ subject: {+ en: () => 'Your password was successfully changed',+ },+ },+ [LetterTemplate.GDPRDataExport]: {+ subject: {+ en: () => 'Swetrix Account data export',+ },+ },+ [LetterTemplate.ProjectReport]: {+ subject: {+ 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.`,+ },+ },+ [LetterTemplate.ProjectInvitation]: {+ subject: {+ en: () => 'You have been invited to join the project.',+ },+ },+ [LetterTemplate.TwoFAOn]: {+ subject: {+ en: () => '2FA has been enabled on your Swetrix account',+ },+ },+ [LetterTemplate.TwoFAOff]: {+ subject: {+ en: () => '2FA has been disabled on your Swetrix account',+ },+ },+ [LetterTemplate.TrialExpired]: {+ subject: {+ en: () => 'Your free Swetrix trial has ended',+ },+ },+ [LetterTemplate.TrialEndsTomorrow]: {+ subject: {+ en: () => 'Your free Swetrix trial ends tomorrow',+ },+ },+ [LetterTemplate.ProjectSubscriberInvitation]: {+ subject: {+ en: () => 'You have been invited to join the project.',+ },+ },+}++interface Params {+ [name: string]: any+}++handlebars.registerHelper('ifEquals', function (arg1, arg2, options) {+ return arg1 == arg2 ? options.fn(this) : options.inverse(this)+})++handlebars.registerHelper('greater', function (v1, v2, options) {+ if (v1 > v2) {+ return options.fn(this)+ }+ return options.inverse(this)+})++const mailClient = new postmark.ServerClient(process.env.SMTP_PASSWORD)++@Injectable()+export class MailerService {+ constructor(private readonly logger: AppLoggerService) {}++ 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' })+ const subject = metaInfoJson[templateName].subject.en(params)+ const template = handlebars.compile(letter)+ const htmlToSend = template(params)++ const message = {+ From: process.env.FROM_EMAIL,+ To: email,+ Subject: subject,+ HtmlBody: htmlToSend,+ MessageStream: messageStream,+ }++ if (process.env.SMTP_MOCK) {+ this.logger.log(+ {+ ...message,+ params,+ },+ 'sendEmail',+ true,+ )+ } else {+ await mailClient.sendEmail(message)+ }+ } catch (error) {+ console.error(`[ERROR][MAILER] ${error}`)+ }+ }+}diff --git a/src/project/entity/project-subscriber.entity.ts b/src/project/entity/project-subscriber.entity.tsindex 99b06a794..7e3238db2 100644--- a/src/project/entity/project-subscriber.entity.ts+++ b/src/project/entity/project-subscriber.entity.ts@@ -2,6 +2,7 @@ import {Column,CreateDateColumn,Entity,+ JoinColumn,ManyToOne,PrimaryGeneratedColumn,UpdateDateColumn,@@ -14,6 +15,9 @@ export class ProjectSubscriber {@PrimaryGeneratedColumn('uuid')id: string+ @Column('varchar', { length: 12 })+ projectId: string+@Column('varchar', { length: 254 })email: string@@ -30,5 +34,6 @@ export class ProjectSubscriber {updatedAt: Date@ManyToOne(() => Project, project => project.subscribers)+ @JoinColumn()project: Project}diff --git a/src/project/project.controller.ts b/src/project/project.controller.tsindex dfdfebd18..8c5ed88d6 100644--- a/src/project/project.controller.ts+++ b/src/project/project.controller.ts@@ -798,11 +798,44 @@ export class ProjectController {}@Post(':projectId/subscribers')+ @Auth([UserType.ADMIN, UserType.CUSTOMER])async addSubscriber(@Param() params: AddSubscriberParamsDto,@Body() body: AddSubscriberBodyDto,- ): Promise<void> {- // TODO: Implement+ @Headers() headers: { origin: string },+ @CurrentUserId() userId: string,+ ) {+ const project = await this.projectService.getProject(+ params.projectId,+ userId,+ )++ if (!project) {+ throw new NotFoundException('Project not found.')+ }++ const user = await this.userService.getUser(userId)++ if (user.email === body.email) {+ throw new BadRequestException('You cannot subscribe to your own project.')+ }++ const subscriber = await this.projectService.getSubscriberByEmail(+ params.projectId,+ body.email,+ )++ if (subscriber) {+ throw new BadRequestException('Subscriber already exists.')+ }++ return await this.projectService.addSubscriber({+ userId,+ projectId: params.projectId,+ email: body.email,+ reportFrequency: body.reportFrequency,+ origin: headers.origin,+ })}@Get(':projectId/subscribers')diff --git a/src/project/project.service.ts b/src/project/project.service.tsindex 4fb2c157c..84535c256 100644--- a/src/project/project.service.ts+++ b/src/project/project.service.ts@@ -1,427 +1,488 @@-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 _size from 'lodash/size'-import * as _join from 'lodash/join'-import * as _find from 'lodash/find'-import * as _map from 'lodash/map'-import * as _pick from 'lodash/pick'-import * as _trim from 'lodash/trim'-import * as _findIndex from 'lodash/findIndex'-import * as _includes from 'lodash/includes'-import * as dayjs from 'dayjs'-import * as utc from 'dayjs/plugin/utc'-// @ts-ignore-import * as validateIP from 'validate-ip-node'--import { Pagination, PaginationOptionsInterface } from '../common/pagination'-import { Project } from './entity/project.entity'-import { ProjectShare } from './entity/project-share.entity'-import { ProjectDTO } from './dto/project.dto'-import { UserType } from '../user/entities/user.entity'-import { UserService } from 'src/user/user.service'-import { Role } from '../project/entity/project-share.entity'-import {- isValidPID,- redisProjectCountCacheTimeout,- getRedisUserCountKey,- redis,- clickhouse,- isSelfhosted,- IP_REGEX,- ORIGINS_REGEX,- getRedisProjectKey,-} from '../common/constants'--dayjs.extend(utc)--// 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)-- try {- await redis.del(key)- } catch (e) {- console.error(`Error deleting project ${id} from redis: ${e}`)- }-}--export const deleteProjectsRedis = async (ids: string[]) => {- await Promise.all(_map(ids, deleteProjectRedis))-}--export const processProjectUser = (project: Project): Project => {- const { share } = project-- for (let j = 0; j < _size(share); ++j) {- const { user } = share[j]-- if (user) {- share[j].user = _pick(user, ['email'])- }- }-- return project-}--export const processProjectsUser = (projects: Project[]): Project[] => {- for (let i = 0; i < _size(projects); ++i) {- projects[i] = processProjectUser(projects[i])- }-- return projects-}--@Injectable()-export class ProjectService {- constructor(- @InjectRepository(Project)- private projectsRepository: Repository<Project>,- @InjectRepository(ProjectShare)- private projectShareRepository: Repository<ProjectShare>,- private userService: UserService,- ) { }-- 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,- where,- order: {- name: 'ASC',- },- relations: ['share', 'share.user'],- })-- return new Pagination<Project>({- results: processProjectsUser(results),- total,- })- }-- 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,- where,- order: {- project: 'ASC',- },- relations: ['project'],- })-- return new Pagination<ProjectShare>({- // results: processProjectsUser(results),- results,- total,- })- }-- async count(): Promise<number> {- return await this.projectsRepository.count()- }-- async create(project: ProjectDTO | Project): Promise<Project> {- return this.projectsRepository.save(project)- }-- async update(- id: string,- projectDTO: ProjectDTO,- ): Promise<any> {- return this.projectsRepository.update(id, projectDTO)- }-- async delete(id: string): Promise<any> {- return this.projectsRepository.delete(id)- }-- async deleteMultiple(pids: string[]): Promise<any> {- 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()- .delete()- .where(where)- .execute()- }-- async createShare(share: ProjectShare): Promise<ProjectShare> {- return this.projectShareRepository.save(share)- }-- async deleteShare(id: string): Promise<any> {- return this.projectShareRepository.delete(id)- }-- async updateShare(id: string, share: ProjectShare | Object): Promise<any> {- return this.projectShareRepository.update(id, share)- }-- async findShare(params: object): Promise<ProjectShare[]> {- return this.projectShareRepository.find(params)- }-- async findOneShare(- id: string,- params: Object = {},- ): Promise<ProjectShare | null> {- return this.projectShareRepository.findOne(id, params)- }-- findOneWithRelations(id: string): Promise<Project | null> {- return this.projectsRepository.findOne(id, { relations: ['admin'] })- }-- findOne(id: string, params: Object = {}): Promise<Project | null> {- return this.projectsRepository.findOne(id, params)- }-- findWhere(- where: Record<string, unknown>,- relations?: string[],- ): Promise<Project[]> {- return this.projectsRepository.find({ where, relations })- }-- find(params: object): Promise<Project[]> {- return this.projectsRepository.find(params)- }-- 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- ) {- return- } else {- throw new ForbiddenException('You are not allowed to view this project')- }- }-- allowedToManage(- project: Project,- uid: string,- roles: Array<UserType> = [],- message: string = 'You are not allowed to manage this project'- ): 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(message)- }- }-- async checkIfIDUnique(projectID: string): Promise<void> {- const project = await this.findOne(projectID)-- if (project) {- throw new BadRequestException('Selected project ID is already in use')- }- }-- checkIfIDUniqueClickhouse(projects: Array<object>, projectID: string): void {- if (_find(projects, ({ id }) => id === projectID)) {- throw new BadRequestException('Selected project ID is already in use')- }- }-- formatToClickhouse(project: Project): object {- const updProject = { ...project }- // @ts-ignore- updProject.active = Number(updProject.active)- // @ts-ignore- updProject.public = Number(updProject.public)- // @ts-ignore- updProject.origins = _isString(updProject.origins)- ? updProject.origins- : _join(updProject.origins, ',')-- return updProject- }-- formatFromClickhouse(project: object): object {- const updProject = { ...project }- // @ts-ignore- updProject.active = Boolean(updProject.active)- // @ts-ignore- updProject.public = Boolean(updProject.public)-- return updProject- }-- 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.',- )-- _map(projectDTO.origins, host => {- if (!ORIGINS_REGEX.test(_trim(host))) {- throw new ConflictException(`Host ${host} is not correct`)- }- })-- _map(projectDTO.ipBlacklist, ip => {- if (!validateIP(_trim(ip)) && !IP_REGEX.test(_trim(ip))) {- throw new ConflictException(`IP address ${ip} is not correct`)- }- })- }-- // Returns amount of exirsting events starting from month- async getRedisCount(uid: string): Promise<number | null> {- const countKey = getRedisUserCountKey(uid)- 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 monthEnd = dayjs.utc().endOf('month').format('YYYY-MM-DD HH:mm:ss')-- let pids-- if (isSelfhosted) {- // selfhosted has no limits- return 0- } else {- pids = await this.find({- where: {- admin: uid,- },- select: ['id'],- })- }-- if (_isEmpty(pids)) {- 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()']-- count = pageviews + customEvents-- await redis.set(- countKey,- `${pageviews}`,- 'EX',- redisProjectCountCacheTimeout,- )- } else {- try {- // @ts-ignore- count = Number(count)- } catch (e) {- count = 0- console.error(e)- throw new InternalServerErrorException('Error while processing project')- }- }-- // @ts-ignore- return count- }-- async clearProjectsRedisCache(uid: string): Promise<void> {- const projects = await this.findWhere({- admin: uid,- })-- if (_isEmpty(projects)) {- return- }-- const pids = _map(projects, 'id')-- await deleteProjectsRedis(pids)- }-- async clearProjectsRedisCacheByEmail(email: string): Promise<void> {- const user = await this.userService.findOneWhere({ email })-- if (!user) {- return- }-- await this.clearProjectsRedisCache(user.id)- }-}+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 _size from 'lodash/size'+import * as _join from 'lodash/join'+import * as _find from 'lodash/find'+import * as _map from 'lodash/map'+import * as _pick from 'lodash/pick'+import * as _trim from 'lodash/trim'+import * as _findIndex from 'lodash/findIndex'+import * as _includes from 'lodash/includes'+import * as dayjs from 'dayjs'+import * as utc from 'dayjs/plugin/utc'+// @ts-ignore+import * as validateIP from 'validate-ip-node'++import { Pagination, PaginationOptionsInterface } from '../common/pagination'+import { Project } from './entity/project.entity'+import { ProjectShare } from './entity/project-share.entity'+import { ProjectDTO } from './dto/project.dto'+import { UserType } from '../user/entities/user.entity'+import { UserService } from 'src/user/user.service'+import { Role } from '../project/entity/project-share.entity'+import {+ isValidPID,+ redisProjectCountCacheTimeout,+ getRedisUserCountKey,+ redis,+ clickhouse,+ isSelfhosted,+ IP_REGEX,+ ORIGINS_REGEX,+ getRedisProjectKey,+} from '../common/constants'+import { ProjectSubscriber } from './entity'+import { ActionTokensService } from 'src/action-tokens/action-tokens.service'+import { ActionTokenType } from 'src/action-tokens/action-token.entity'+import { MailerService } from 'src/mailer/mailer.service'+import { LetterTemplate } from 'src/mailer/letter'+import { AddSubscriberType } from './types'++dayjs.extend(utc)++// 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)++ try {+ await redis.del(key)+ } catch (e) {+ console.error(`Error deleting project ${id} from redis: ${e}`)+ }+}++export const deleteProjectsRedis = async (ids: string[]) => {+ await Promise.all(_map(ids, deleteProjectRedis))+}++export const processProjectUser = (project: Project): Project => {+ const { share } = project++ for (let j = 0; j < _size(share); ++j) {+ const { user } = share[j]++ if (user) {+ share[j].user = _pick(user, ['email'])+ }+ }++ return project+}++export const processProjectsUser = (projects: Project[]): Project[] => {+ for (let i = 0; i < _size(projects); ++i) {+ projects[i] = processProjectUser(projects[i])+ }++ return projects+}++@Injectable()+export class ProjectService {+ constructor(+ @InjectRepository(Project)+ private projectsRepository: Repository<Project>,+ @InjectRepository(ProjectShare)+ private projectShareRepository: Repository<ProjectShare>,+ private userService: UserService,+ @InjectRepository(ProjectSubscriber)+ private readonly projectSubscriberRepository: Repository<ProjectSubscriber>,+ private readonly actionTokens: ActionTokensService,+ private readonly mailerService: MailerService,+ ) {}++ 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,+ where,+ order: {+ name: 'ASC',+ },+ relations: ['share', 'share.user'],+ })++ return new Pagination<Project>({+ results: processProjectsUser(results),+ total,+ })+ }++ 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,+ where,+ order: {+ project: 'ASC',+ },+ relations: ['project'],+ })++ return new Pagination<ProjectShare>({+ // results: processProjectsUser(results),+ results,+ total,+ })+ }++ async count(): Promise<number> {+ return await this.projectsRepository.count()+ }++ async create(project: ProjectDTO | Project): Promise<Project> {+ return this.projectsRepository.save(project)+ }++ async update(+ id: string,+ projectDTO: ProjectDTO,+ ): Promise<any> {+ return this.projectsRepository.update(id, projectDTO)+ }++ async delete(id: string): Promise<any> {+ return this.projectsRepository.delete(id)+ }++ async deleteMultiple(pids: string[]): Promise<any> {+ 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()+ .delete()+ .where(where)+ .execute()+ }++ async createShare(share: ProjectShare): Promise<ProjectShare> {+ return this.projectShareRepository.save(share)+ }++ async deleteShare(id: string): Promise<any> {+ return this.projectShareRepository.delete(id)+ }++ async updateShare(id: string, share: ProjectShare | Object): Promise<any> {+ return this.projectShareRepository.update(id, share)+ }++ async findShare(params: object): Promise<ProjectShare[]> {+ return this.projectShareRepository.find(params)+ }++ async findOneShare(+ id: string,+ params: Object = {},+ ): Promise<ProjectShare | null> {+ return this.projectShareRepository.findOne(id, params)+ }++ findOneWithRelations(id: string): Promise<Project | null> {+ return this.projectsRepository.findOne(id, { relations: ['admin'] })+ }++ findOne(id: string, params: Object = {}): Promise<Project | null> {+ return this.projectsRepository.findOne(id, params)+ }++ findWhere(+ where: Record<string, unknown>,+ relations?: string[],+ ): Promise<Project[]> {+ return this.projectsRepository.find({ where, relations })+ }++ find(params: object): Promise<Project[]> {+ return this.projectsRepository.find(params)+ }++ 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+ ) {+ return+ } else {+ throw new ForbiddenException('You are not allowed to view this project')+ }+ }++ allowedToManage(+ project: Project,+ uid: string,+ roles: Array<UserType> = [],+ message: string = 'You are not allowed to manage this project'+ ): 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(message)+ }+ }++ async checkIfIDUnique(projectID: string): Promise<void> {+ const project = await this.findOne(projectID)++ if (project) {+ throw new BadRequestException('Selected project ID is already in use')+ }+ }++ checkIfIDUniqueClickhouse(projects: Array<object>, projectID: string): void {+ if (_find(projects, ({ id }) => id === projectID)) {+ throw new BadRequestException('Selected project ID is already in use')+ }+ }++ formatToClickhouse(project: Project): object {+ const updProject = { ...project }+ // @ts-ignore+ updProject.active = Number(updProject.active)+ // @ts-ignore+ updProject.public = Number(updProject.public)+ // @ts-ignore+ updProject.origins = _isString(updProject.origins)+ ? updProject.origins+ : _join(updProject.origins, ',')++ return updProject+ }++ formatFromClickhouse(project: object): object {+ const updProject = { ...project }+ // @ts-ignore+ updProject.active = Boolean(updProject.active)+ // @ts-ignore+ updProject.public = Boolean(updProject.public)++ return updProject+ }++ 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.',+ )++ _map(projectDTO.origins, host => {+ if (!ORIGINS_REGEX.test(_trim(host))) {+ throw new ConflictException(`Host ${host} is not correct`)+ }+ })++ _map(projectDTO.ipBlacklist, ip => {+ if (!validateIP(_trim(ip)) && !IP_REGEX.test(_trim(ip))) {+ throw new ConflictException(`IP address ${ip} is not correct`)+ }+ })+ }++ // Returns amount of exirsting events starting from month+ async getRedisCount(uid: string): Promise<number | null> {+ const countKey = getRedisUserCountKey(uid)+ 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 monthEnd = dayjs.utc().endOf('month').format('YYYY-MM-DD HH:mm:ss')++ let pids++ if (isSelfhosted) {+ // selfhosted has no limits+ return 0+ } else {+ pids = await this.find({+ where: {+ admin: uid,+ },+ select: ['id'],+ })+ }++ if (_isEmpty(pids)) {+ 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()']++ count = pageviews + customEvents++ await redis.set(+ countKey,+ `${pageviews}`,+ 'EX',+ redisProjectCountCacheTimeout,+ )+ } else {+ try {+ // @ts-ignore+ count = Number(count)+ } catch (e) {+ count = 0+ console.error(e)+ throw new InternalServerErrorException('Error while processing project')+ }+ }++ // @ts-ignore+ return count+ }++ async clearProjectsRedisCache(uid: string): Promise<void> {+ const projects = await this.findWhere({+ admin: uid,+ })++ if (_isEmpty(projects)) {+ return+ }++ const pids = _map(projects, 'id')++ await deleteProjectsRedis(pids)+ }++ async clearProjectsRedisCacheByEmail(email: string): Promise<void> {+ const user = await this.userService.findOneWhere({ email })++ if (!user) {+ return+ }++ await this.clearProjectsRedisCache(user.id)+ }++ async getProject(projectId: string, userId: string) {+ return await this.projectsRepository.findOne({+ where: {+ id: projectId,+ admin: { id: userId },+ },+ })+ }++ async getSubscriberByEmail(projectId: string, email: string) {+ return await this.projectSubscriberRepository.findOne({+ where: { projectId, email },+ })+ }++ async addSubscriber(data: AddSubscriberType) {+ const subscriber = await this.projectSubscriberRepository.save({ ...data })++ await this.sendSubscriberInvite(+ data.userId,+ data.projectId,+ data.email,+ data.origin,+ )++ return subscriber+ }++ async sendSubscriberInvite(+ userId: string,+ projectId: string,+ email: string,+ origin: string,+ ) {+ const actionToken = await this.actionTokens.createActionToken(+ userId,+ ActionTokenType.ADDING_PROJECT_SUBSCRIBER,+ `${projectId}:${email}`,+ )++ const inviteLink = `${origin}/projects/${projectId}/subscribers/invite?token=${actionToken.id}`++ await this.mailerService.sendEmail(+ email,+ LetterTemplate.ProjectSubscriberInvitation,+ {+ inviteLink,+ },+ )+ }+}diff --git a/src/project/types/add-subscriber.type.ts b/src/project/types/add-subscriber.type.tsnew file mode 100644index 000000000..f35dedaa6--- /dev/null+++ b/src/project/types/add-subscriber.type.ts@@ -0,0 +1,6 @@+import { AddSubscriberBodyDto, AddSubscriberParamsDto } from '../dto'++export type AddSubscriberType = { userId: string } & AddSubscriberParamsDto &+ AddSubscriberBodyDto & {+ origin: string+ }diff --git a/src/project/types/index.ts b/src/project/types/index.tsnew file mode 100644index 000000000..17d652ee9--- /dev/null+++ b/src/project/types/index.ts@@ -0,0 +1 @@+export * from './add-subscriber.type'diff --git a/src/user/user.service.ts b/src/user/user.service.tsindex e4be6ebf5..1c1772902 100644--- a/src/user/user.service.ts+++ b/src/user/user.service.ts@@ -1,206 +1,210 @@-import { Injectable, BadRequestException } from '@nestjs/common'-import { InjectRepository } from '@nestjs/typeorm'-import { Repository } from 'typeorm'-import * as dayjs from 'dayjs'-import * as _isEmpty from 'lodash/isEmpty'-import * as _size from 'lodash/size'-import * as _omit from 'lodash/omit'--import { Pagination, PaginationOptionsInterface } from '../common/pagination'-import { User, ACCOUNT_PLANS, TRIAL_DURATION } from './entities/user.entity'-import { UserProfileDTO } from './dto/user.dto'-import { RefreshToken } from './entities/refresh-token.entity'--@Injectable()-export class UserService {- constructor(- @InjectRepository(User)- private usersRepository: Repository<User>,- @InjectRepository(RefreshToken)- private readonly refreshTokenRepository: Repository<RefreshToken>,- ) {}-- async create(userDTO: UserProfileDTO | User): Promise<User> {- return await this.usersRepository.save(userDTO)- }-- 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> {- return this.usersRepository.update({ email }, update)- }-- async updateBySubID(- subID: string,- update: Record<string, unknown>,- ): Promise<any> {- return this.usersRepository.update({ subID }, update)- }-- async delete(id: string): Promise<any> {- return this.usersRepository.delete(id)- }-- async count(): Promise<number> {- return await this.usersRepository.count()- }-- omitSensitiveData(user: User): User {- return _omit(user, [- 'password',- 'twoFactorRecoveryCode',- 'twoFactorAuthenticationSecret',- ])- }-- 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,- })- }-- findOneWithRelations(id: string, relations: string[]): Promise<User> {- return this.usersRepository.findOne(id, { relations })- }-- findWhereWithRelations(- where: Record<string, unknown>,- relations: string[],- ): Promise<User[]> {- return this.usersRepository.find({- where,- relations,- })- }-- find(params: object): Promise<User[]> {- return this.usersRepository.find(params)- }-- findWhere(where: Record<string, unknown>): Promise<User[]> {- return this.usersRepository.find({ where })- }-- validatePassword(pass: string): void {- const err = []- if (_isEmpty(pass)) {- err.push('Password cannot be empty')- }-- if (_size(pass) > 50) {- err.push('Maximum password length is 50 letters')- }-- if (_size(pass) < 8) {- err.push('at least 8 characters')- }-- // if(!/[a-z]/.test(pass))- // err.push('should contain at least one lower case')- // if(!/[A-Z]/.test(pass))- // err.push('should contain at least one upper case')- // if(!(/[!@#$%^&*(),.?":{}|<>]/g.test(pass)))- // err.push('should contain at least one symbol')-- if (!_isEmpty(err)) {- throw new BadRequestException(err)- }- }-- processUser(user: User): object {- // @ts-ignore- const maxEventsCount = ACCOUNT_PLANS[user?.planCode]?.monthlyUsageLimit || 0- const userData = {- // @ts-ignore- ...user,- password: undefined,- twoFactorRecoveryCode: undefined,- twoFactorAuthenticationSecret: undefined,- maxEventsCount,- }-- return userData- }-- search(query: string): Promise<User[]> {- return this.usersRepository- .createQueryBuilder('user')- .select()- .where('email like :query', { query: `%${query}%` })- .limit(5)- .getMany()- }-- public async findUser(email: string) {- return await this.usersRepository.findOne({ email })- }-- public async createUser(user: Pick<User, 'email' | 'password'>) {- return await this.usersRepository.save({- ...user,- trialEndDate: dayjs.utc().add(TRIAL_DURATION, 'day').format('YYYY-MM-DD HH:mm:ss'),- })- }-- public async findUserById(id: string) {- return await this.usersRepository.findOne({ id })- }-- public async updateUser(id: string, user: Partial<Omit<User, 'id'>>) {- return await this.usersRepository.update({ id }, user)- }-- public async saveRefreshToken(userId: string, refreshToken: string) {- return await this.refreshTokenRepository.save({- userId,- refreshToken,- })- }-- public async findRefreshToken(userId: string, refreshToken: string) {- return await this.refreshTokenRepository.findOne({- where: {- userId,- refreshToken,- },- })- }-- public async deleteRefreshToken(userId: string, refreshToken: string) {- await this.refreshTokenRepository.delete({- userId,- refreshToken,- })- }-- public async findUserByApiKey(apiKey: string) {- return await this.usersRepository.findOne({ where: { apiKey } })- }-}+import { Injectable, BadRequestException } from '@nestjs/common'+import { InjectRepository } from '@nestjs/typeorm'+import { Repository } from 'typeorm'+import * as dayjs from 'dayjs'+import * as _isEmpty from 'lodash/isEmpty'+import * as _size from 'lodash/size'+import * as _omit from 'lodash/omit'++import { Pagination, PaginationOptionsInterface } from '../common/pagination'+import { User, ACCOUNT_PLANS, TRIAL_DURATION } from './entities/user.entity'+import { UserProfileDTO } from './dto/user.dto'+import { RefreshToken } from './entities/refresh-token.entity'++@Injectable()+export class UserService {+ constructor(+ @InjectRepository(User)+ private usersRepository: Repository<User>,+ @InjectRepository(RefreshToken)+ private readonly refreshTokenRepository: Repository<RefreshToken>,+ ) {}++ async create(userDTO: UserProfileDTO | User): Promise<User> {+ return await this.usersRepository.save(userDTO)+ }++ 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> {+ return this.usersRepository.update({ email }, update)+ }++ async updateBySubID(+ subID: string,+ update: Record<string, unknown>,+ ): Promise<any> {+ return this.usersRepository.update({ subID }, update)+ }++ async delete(id: string): Promise<any> {+ return this.usersRepository.delete(id)+ }++ async count(): Promise<number> {+ return await this.usersRepository.count()+ }++ omitSensitiveData(user: User): User {+ return _omit(user, [+ 'password',+ 'twoFactorRecoveryCode',+ 'twoFactorAuthenticationSecret',+ ])+ }++ 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,+ })+ }++ findOneWithRelations(id: string, relations: string[]): Promise<User> {+ return this.usersRepository.findOne(id, { relations })+ }++ findWhereWithRelations(+ where: Record<string, unknown>,+ relations: string[],+ ): Promise<User[]> {+ return this.usersRepository.find({+ where,+ relations,+ })+ }++ find(params: object): Promise<User[]> {+ return this.usersRepository.find(params)+ }++ findWhere(where: Record<string, unknown>): Promise<User[]> {+ return this.usersRepository.find({ where })+ }++ validatePassword(pass: string): void {+ const err = []+ if (_isEmpty(pass)) {+ err.push('Password cannot be empty')+ }++ if (_size(pass) > 50) {+ err.push('Maximum password length is 50 letters')+ }++ if (_size(pass) < 8) {+ err.push('at least 8 characters')+ }++ // if(!/[a-z]/.test(pass))+ // err.push('should contain at least one lower case')+ // if(!/[A-Z]/.test(pass))+ // err.push('should contain at least one upper case')+ // if(!(/[!@#$%^&*(),.?":{}|<>]/g.test(pass)))+ // err.push('should contain at least one symbol')++ if (!_isEmpty(err)) {+ throw new BadRequestException(err)+ }+ }++ processUser(user: User): object {+ // @ts-ignore+ const maxEventsCount = ACCOUNT_PLANS[user?.planCode]?.monthlyUsageLimit || 0+ const userData = {+ // @ts-ignore+ ...user,+ password: undefined,+ twoFactorRecoveryCode: undefined,+ twoFactorAuthenticationSecret: undefined,+ maxEventsCount,+ }++ return userData+ }++ search(query: string): Promise<User[]> {+ return this.usersRepository+ .createQueryBuilder('user')+ .select()+ .where('email like :query', { query: `%${query}%` })+ .limit(5)+ .getMany()+ }++ public async findUser(email: string) {+ return await this.usersRepository.findOne({ email })+ }++ public async createUser(user: Pick<User, 'email' | 'password'>) {+ return await this.usersRepository.save({+ ...user,+ trialEndDate: dayjs.utc().add(TRIAL_DURATION, 'day').format('YYYY-MM-DD HH:mm:ss'),+ })+ }++ public async findUserById(id: string) {+ return await this.usersRepository.findOne({ id })+ }++ public async updateUser(id: string, user: Partial<Omit<User, 'id'>>) {+ return await this.usersRepository.update({ id }, user)+ }++ public async saveRefreshToken(userId: string, refreshToken: string) {+ return await this.refreshTokenRepository.save({+ userId,+ refreshToken,+ })+ }++ public async findRefreshToken(userId: string, refreshToken: string) {+ return await this.refreshTokenRepository.findOne({+ where: {+ userId,+ refreshToken,+ },+ })+ }++ public async deleteRefreshToken(userId: string, refreshToken: string) {+ await this.refreshTokenRepository.delete({+ userId,+ refreshToken,+ })+ }++ public async findUserByApiKey(apiKey: string) {+ return await this.usersRepository.findOne({ where: { apiKey } })+ }++ async getUser(id: string) {+ return await this.usersRepository.findOne({ id })+ }+}