Skip to content

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

Added GDPR Report Email template & API Logic

Blue Mouse801bd1432d04
diff --git a/src/common/constants.ts b/src/common/constants.ts
index 043564e8d..62ce32602 100644
--- a/src/common/constants.ts
+++ b/src/common/constants.ts
@@ -63,7 +63,11 @@ const redisProjectCacheTimeout = 3600
// 30 minues -> the amount of time analytics requests within one session are counted as non-unique
const UNIQUE_SESSION_LIFE_TIME = 1800
+// how often can user request a fresh GDPR export of their data; in days.
+const GDPR_EXPORT_TIMEFRAME = 14
+
export {
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,
}
diff --git a/src/common/templates/en/gdpr_data_export.html b/src/common/templates/en/gdpr_data_export.html
new file mode 100644
index 000000000..23b339aff
--- /dev/null
+++ b/src/common/templates/en/gdpr_data_export.html
@@ -0,0 +1,195 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ <title>Swetrix Account Data Export</title>
+ <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700" rel="stylesheet" />
+ </head>
+ <body style="margin: 0; padding: 0; background-color: #f0f4f7;">
+ <table cellpadding="0" cellspacing="0" border="0" width="100%" style="background: #f0f4f7; min-width: 340px; font-size: 1px; line-height: normal;">
+ <tr>
+ <td align="center" valign="top" bgcolor="#f0f4f7">
+ <!--[if (gte mso 9)|(IE)]>
+ <table border="0" cellspacing="0" cellpadding="0">
+ <tr><td align="center" valign="top" width="750"><![endif]-->
+ <table cellpadding="0" cellspacing="0" border="0" width="750" style="width: 100%; max-width: 750px; min-width: 340px; background: #f0f4f7;">
+ <tr>
+ <td width="25" style="width: 25px; max-width: 25px; min-width: 25px;">&nbsp;</td>
+ <td align="center" valign="top" style="background: #ffffff;">
+ <table cellpadding="0" cellspacing="0" border="0" width="100%" style="width: 100% !important; min-width: 100%; max-width: 100%; background: #f0f4f7;">
+ <tr>
+ <td align="right" valign="top">
+ <div style="height: 25px; line-height: 25px; font-size: 23px;">&nbsp;</div>
+ </td>
+ </tr>
+ </table>
+ <table cellpadding="0" cellspacing="0" border="0" width="88%" style="width: 88% !important; min-width: 88%; max-width: 88%;">
+ <tr>
+ <td align="center" valign="top">
+ <div style="height: 40px; line-height: 40px; font-size: 38px;">&nbsp;</div>
+ <a href="https://swetrix.com" target="_blank" style="display: block; max-width: 173px;">
+ <img src="file:///home/rand/Downloads/blue_logo.png" alt="Swetrix Analytics" width="175" border="0" style="display: block; width: 175px;" />
+ </a>
+ <div style="height: 40px; line-height: 40px; font-size: 40px;">&nbsp;</div>
+ </td>
+ </tr>
+ </table>
+ <table cellpadding="0" cellspacing="0" border="0" width="88%" style="width: 88% !important; min-width: 88%; max-width: 88%;">
+ <tr>
+ <td align="center" valign="top">
+ <font align="center" style="font-size: 52px; line-height: 55px; font-weight: 300; letter-spacing: -1.5px;">
+ <span
+ align="center"
+ style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif; font-size: 44px; color: #1a1a1a;"
+ >
+ What we know about you
+ </span>
+ </font>
+ <div style="height: 10px; line-height: 10px; font-size: 9px;">&nbsp;</div>
+ <font style="font-size: 36px; line-height: 45px; font-weight: 300; letter-spacing: -1px;">
+ <span
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #5e5e5e;
+ font-size: 30px;
+ line-height: 45px;
+ font-weight: 300;
+ letter-spacing: -1px;
+ "
+ >
+ GDPR Account Data Export
+ </span>
+ </font>
+ <div style="height: 38px; line-height: 38px; font-size: 36px;">&nbsp;</div>
+ <table cellpadding="0" cellspacing="0" border="0" width="88%" style="width: 88% !important; min-width: 88%; max-width: 88%;">
+ <tr>
+ <td align="left" valign="top">
+ <font face="'Source Sans Pro', sans-serif" color="#7f7f7f" style="font-size: 17px; line-height: 23px;">
+ <span
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #7f7f7f;
+ font-size: 17px;
+ line-height: 23px;
+ "
+ >
+ <i>Note: All times are Greenwich Mean Time.</i><br />
+ Below is the list of your Swetrix account information and settings (including your created projects) that you can request at any time.<br />
+ Our <a href="https://swetrix.com" target="_blank">Privacy Policy</a> has more information about what data we collect and how we use it.
+
+ <div style="height: 5px; line-height: 5px; font-size: 3px;">&nbsp;</div>
+ <h3 style="margin-bottom: 5px;">Account Information:</h3>
+ <b>Email address:</b>&nbsp;{{user.email}}<br />
+ <b>Email confirmed:</b>&nbsp;{{#if user.isActive}} Yes {{else}} No {{/if}}<br />
+ <b>Verification emails sent:</b>&nbsp;{{user.emailRequests}}<br />
+ <b>Account created at:</b>&nbsp;{{user.created}}<br />
+ <b>Account updated at:</b>&nbsp;{{user.updated}}<br />
+ <b>Last report generation time:</b>&nbsp;{{user.exportedAt}}
+
+ <div style="height: 10px; line-height: 10px; font-size: 8px;">&nbsp;</div>
+ <h3 style="margin-bottom: 5px;">Projects Information:</h3>
+ {{#each projects}}
+ <b>Project Identifier:</b>&nbsp;{{id}}<br />
+ <b>Name:</b>&nbsp;{{name}}<br />
+ <b>Active:</b>&nbsp;{{#if active}} Yes {{else}} No {{/if}}<br />
+ <b>Allowed origins:</b>&nbsp;{{#if origins}}{{origins}}{{else}}*{{/if}}<br />
+ <b>Created at:</b>&nbsp;{{created}}
+ {{else}}
+ There are no projects associated with this account.
+ {{/each}}
+ </span>
+ </font>
+ <div style="height: 30px; line-height: 30px; font-size: 28px;">&nbsp;</div>
+ </td>
+ </tr>
+ </table>
+ </td>
+ </tr>
+ </table>
+
+ <table cellpadding="0" cellspacing="0" border="0" width="100%" style="width: 100% !important; min-width: 100%; max-width: 100%; background: #f0f4f7;">
+ <tr>
+ <td align="center" valign="top">
+ <div style="height: 25px; line-height: 25px; font-size: 32px;">&nbsp;</div>
+ <table cellpadding="0" cellspacing="0" border="0" width="88%" style="width: 88% !important; min-width: 88%; max-width: 88%;">
+ <tr>
+ <td align="center" valign="top">
+ <font style="font-size: 17px; line-height: 20px;">
+ <span
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #3c4858;
+ font-size: 17px;
+ line-height: 20px;
+ "
+ >
+ <a
+ href="https://swetrix.com"
+ target="_blank"
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #3c4858;
+ font-size: 17px;
+ line-height: 20px;
+ text-decoration: none;
+ "
+ >
+ Terms of Service
+ </a>
+ </span>
+ </font>
+ <font face="'Source Sans Pro', sans-serif" color="#3c4858" style="font-size: 17px; line-height: 20px;">
+ <span
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #1a1a1a;
+ font-size: 17px;
+ line-height: 20px;
+ "
+ >
+ &nbsp;&nbsp;|&nbsp;&nbsp;
+ </span>
+ </font>
+ <font style="font-size: 17px; line-height: 20px;">
+ <span
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #1a1a1a;
+ font-size: 17px;
+ line-height: 20px;
+ "
+ >
+ <a
+ href="https://swetrix.com"
+ target="_blank"
+ style="
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
+ color: #3c4858;
+ font-size: 17px;
+ line-height: 20px;
+ text-decoration: none;
+ "
+ >
+ Privacy Policy
+ </a>
+ </span>
+ </font>
+ </td>
+ </tr>
+ </table>
+ <div style="height: 25px; line-height: 25px; font-size: 28px;">&nbsp;</div>
+ </td>
+ </tr>
+ </table>
+ </td>
+ <td width="25" style="width: 25px; max-width: 25px; min-width: 25px;">&nbsp;</td>
+ </tr>
+ </table>
+ <!--[if (gte mso 9)|(IE)]>
+ </td></tr>
+ </table><![endif]-->
+ </td>
+ </tr>
+ </table>
+ </body>
+</html>
diff --git a/src/mailer/mailer.service.ts b/src/mailer/mailer.service.ts
index 4483fca1c..95bd99630 100644
--- a/src/mailer/mailer.service.ts
+++ b/src/mailer/mailer.service.ts
@@ -40,7 +40,7 @@ const metaInfoJson = {
},
[LetterTemplate.GDPRDataExport]: {
subject: {
- en: 'Swetrix account data export',
+ en: 'Swetrix Account data export',
},
},
}
diff --git a/src/user/entities/user.entity.ts b/src/user/entities/user.entity.ts
index fd17f3795..3c408bdea 100644
--- a/src/user/entities/user.entity.ts
+++ b/src/user/entities/user.entity.ts
@@ -60,6 +60,9 @@ export class User {
@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
updated: Date;
+ @Column({ type: 'timestamp', nullable: true })
+ exportedAt: Date;
+
@BeforeUpdate()
updateTimestamp() {
this.updated = new Date;
diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts
index 0f788a138..0af438f69 100644
--- a/src/user/user.controller.ts
+++ b/src/user/user.controller.ts
@@ -1,14 +1,20 @@
import {
- Controller, Query, Req, Body, Param, Get, Post, Put, Delete, HttpCode, BadRequestException, UseGuards,
+ Controller, Query, Req, Body, Param, Get, Post, Put, Delete, HttpCode, BadRequestException, UseGuards, MethodNotAllowedException,
} from '@nestjs/common'
import { Request } from 'express'
import { ApiTags, ApiQuery } from '@nestjs/swagger'
+import * as dayjs from 'dayjs'
+import * as utc from 'dayjs/plugin/utc'
+import * as _map from 'lodash/map'
+import * as _join from 'lodash/join'
+import * as _isNull from 'lodash/isNull'
import { UserService } from './user.service'
import { ProjectService } from '../project/project.service'
import { User, UserType, MAX_EMAIL_REQUESTS } from './entities/user.entity'
import { Roles } from '../common/decorators/roles.decorator'
import { Pagination } from '../common/pagination/pagination'
+import { GDPR_EXPORT_TIMEFRAME } from '../common/constants'
import { RolesGuard } from 'src/common/guards/roles.guard'
import { UpdateUserProfileDTO } from './dto/update-user.dto'
import { CurrentUserId } from 'src/common/decorators/current-user-id.decorator'
@@ -20,6 +26,8 @@ import { LetterTemplate } from 'src/mailer/letter'
import { AppLoggerService } from 'src/logger/logger.service'
import { UserProfileDTO } from './dto/user.dto'
+dayjs.extend(utc)
+
@ApiTags('User')
@Controller('user')
@UseGuards(RolesGuard)
@@ -188,12 +196,28 @@ export class UserController {
const user = await this.userService.findOneWhere({ id: user_id })
const where = Object({ admin: user_id })
const projects = await this.projectService.findWhere(where)
- console.log(user, projects)
- // TODO: Add related projects and other user controlled objects into report
- // TODO: Allow users to request exports only once per day
- await this.mailerService.sendEmail(user.email, LetterTemplate.GDPRDataExport, {
- user, projects,
+ 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 = {
+ user: {
+ ...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'),
+ },
+ projects: _map(projects, project => ({
+ ...project,
+ created: dayjs(project.created).format('YYYY/MM/DD HH:mm:ss'),
+ origins: _join(project.origins, ', '),
+ }))
+ }
+
+ 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