Compare commits

...

7 Commits

22 changed files with 262 additions and 6 deletions

View File

@@ -3,6 +3,28 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
# [1.41.0](https://github.com/standardnotes/server/compare/@standardnotes/analytics@1.40.0...@standardnotes/analytics@1.41.0) (2022-11-04)
### Bug Fixes
* **analytics:** linter setup with migrations ([262d295](https://github.com/standardnotes/server/commit/262d2951218753f6f14613c5d8ae20ade0e4ef06))
### Features
* **analytics:** add retrieving user analytics id ([4e5ac0a](https://github.com/standardnotes/server/commit/4e5ac0a47b469e5fa681f0131d04c9823cebedf3))
# [1.40.0](https://github.com/standardnotes/server/compare/@standardnotes/analytics@1.39.1...@standardnotes/analytics@1.40.0) (2022-11-04)
### Features
* **analytics:** add analytics entities ([f315b1a](https://github.com/standardnotes/server/commit/f315b1ac5c9369d36fa616c6b4bb5492148564f8))
## [1.39.1](https://github.com/standardnotes/server/compare/@standardnotes/analytics@1.39.0...@standardnotes/analytics@1.39.1) (2022-11-04)
### Bug Fixes
* **analytics:** linter setup ([5329f2a](https://github.com/standardnotes/server/commit/5329f2a2fb815f8691e37279fcadcf01beb716ad))
# [1.39.0](https://github.com/standardnotes/server/compare/@standardnotes/analytics@1.38.0...@standardnotes/analytics@1.39.0) (2022-11-04)
### Features

View File

@@ -0,0 +1,16 @@
import { MigrationInterface, QueryRunner } from 'typeorm'
export class initDatabase1667555285111 implements MigrationInterface {
name = 'initDatabase1667555285111'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
'CREATE TABLE `analytics_entities` (`id` int NOT NULL AUTO_INCREMENT, `user_uuid` varchar(36) NOT NULL, INDEX `user_uuid` (`user_uuid`), PRIMARY KEY (`id`)) ENGINE=InnoDB',
)
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query('DROP INDEX `user_uuid` ON `analytics_entities`')
await queryRunner.query('DROP TABLE `analytics_entities`')
}
}

View File

@@ -1,6 +1,6 @@
{
"name": "@standardnotes/analytics",
"version": "1.39.0",
"version": "1.41.0",
"engines": {
"node": ">=14.0.0 <17.0.0"
},

View File

@@ -28,6 +28,11 @@ import { AnalyticsStoreInterface } from '../Domain/Analytics/AnalyticsStoreInter
import { RedisAnalyticsStore } from '../Infra/Redis/RedisAnalyticsStore'
import { StatisticsStoreInterface } from '../Domain/Statistics/StatisticsStoreInterface'
import { RedisStatisticsStore } from '../Infra/Redis/RedisStatisticsStore'
import { AnalyticsEntityRepositoryInterface } from '../Domain/Entity/AnalyticsEntityRepositoryInterface'
import { MySQLAnalyticsEntityRepository } from '../Infra/MySQL/MySQLAnalyticsEntityRepository'
import { Repository } from 'typeorm'
import { AnalyticsEntity } from '../Domain/Entity/AnalyticsEntity'
import { GetUserAnalyticsId } from '../Domain/UseCase/GetUserAnalyticsId/GetUserAnalyticsId'
// eslint-disable-next-line @typescript-eslint/no-var-requires
const newrelicFormatter = require('@newrelic/winston-enricher')
@@ -97,10 +102,17 @@ export class ContainerConfigLoader {
container.bind(TYPES.NEW_RELIC_ENABLED).toConstantValue(env.get('NEW_RELIC_ENABLED', true))
// Repositories
container
.bind<AnalyticsEntityRepositoryInterface>(TYPES.AnalyticsEntityRepository)
.to(MySQLAnalyticsEntityRepository)
// ORM
container
.bind<Repository<AnalyticsEntity>>(TYPES.ORMAnalyticsEntityRepository)
.toConstantValue(AppDataSource.getRepository(AnalyticsEntity))
// Use Case
container.bind<GetUserAnalyticsId>(TYPES.GetUserAnalyticsId).to(GetUserAnalyticsId)
// Hanlders

View File

@@ -1,4 +1,7 @@
import { DataSource, LoggerOptions } from 'typeorm'
import { AnalyticsEntity } from '../Domain/Entity/AnalyticsEntity'
import { Env } from './Env'
const env: Env = new Env()
@@ -33,7 +36,7 @@ export const AppDataSource = new DataSource({
],
removeNodeErrorCount: 10,
},
entities: [],
entities: [AnalyticsEntity],
migrations: [env.get('DB_MIGRATIONS_PATH', true) ?? 'dist/migrations/*.js'],
migrationsRun: true,
logging: <LoggerOptions>env.get('DB_DEBUG_LEVEL'),

View File

@@ -12,8 +12,11 @@ const TYPES = {
REDIS_EVENTS_CHANNEL: Symbol.for('REDIS_EVENTS_CHANNEL'),
NEW_RELIC_ENABLED: Symbol.for('NEW_RELIC_ENABLED'),
// Repositories
AnalyticsEntityRepository: Symbol.for('AnalyticsEntityRepository'),
// ORM
ORMAnalyticsEntityRepository: Symbol.for('ORMAnalyticsEntityRepository'),
// Use Case
GetUserAnalyticsId: Symbol.for('GetUserAnalyticsId'),
// Handlers
// Services
DomainEventPublisher: Symbol.for('DomainEventPublisher'),

View File

@@ -0,0 +1,14 @@
import { Column, Entity, Index, PrimaryGeneratedColumn } from 'typeorm'
@Entity({ name: 'analytics_entities' })
export class AnalyticsEntity {
@PrimaryGeneratedColumn()
declare id: number
@Column({
name: 'user_uuid',
length: 36,
})
@Index('user_uuid')
declare userUuid: string
}

View File

@@ -0,0 +1,7 @@
import { Uuid } from '@standardnotes/common'
import { AnalyticsEntity } from './AnalyticsEntity'
export interface AnalyticsEntityRepositoryInterface {
save(analyticsEntity: AnalyticsEntity): Promise<AnalyticsEntity>
findOneByUserUuid(userUuid: Uuid): Promise<AnalyticsEntity | null>
}

View File

@@ -0,0 +1,37 @@
import 'reflect-metadata'
import { AnalyticsEntity } from '../../Entity/AnalyticsEntity'
import { AnalyticsEntityRepositoryInterface } from '../../Entity/AnalyticsEntityRepositoryInterface'
import { GetUserAnalyticsId } from './GetUserAnalyticsId'
describe('GetUserAnalyticsId', () => {
let analyticsEntityRepository: AnalyticsEntityRepositoryInterface
let analyticsEntity: AnalyticsEntity
const createUseCase = () => new GetUserAnalyticsId(analyticsEntityRepository)
beforeEach(() => {
analyticsEntity = { id: 123 } as jest.Mocked<AnalyticsEntity>
analyticsEntityRepository = {} as jest.Mocked<AnalyticsEntityRepositoryInterface>
analyticsEntityRepository.findOneByUserUuid = jest.fn().mockReturnValue(analyticsEntity)
})
it('should return analytics id for a user', async () => {
expect(await createUseCase().execute({ userUuid: '1-2-3' })).toEqual({ analyticsId: 123 })
})
it('should throw error if user is missing analytics entity', async () => {
analyticsEntityRepository.findOneByUserUuid = jest.fn().mockReturnValue(null)
let error = null
try {
await createUseCase().execute({ userUuid: '1-2-3' })
} catch (caughtError) {
error = caughtError
}
expect(error).not.toBeNull()
})
})

View File

@@ -0,0 +1,25 @@
import { inject, injectable } from 'inversify'
import TYPES from '../../../Bootstrap/Types'
import { AnalyticsEntityRepositoryInterface } from '../../Entity/AnalyticsEntityRepositoryInterface'
import { UseCaseInterface } from '../UseCaseInterface'
import { GetUserAnalyticsIdDTO } from './GetUserAnalyticsIdDTO'
import { GetUserAnalyticsIdResponse } from './GetUserAnalyticsIdResponse'
@injectable()
export class GetUserAnalyticsId implements UseCaseInterface {
constructor(
@inject(TYPES.AnalyticsEntityRepository) private analyticsEntityRepository: AnalyticsEntityRepositoryInterface,
) {}
async execute(dto: GetUserAnalyticsIdDTO): Promise<GetUserAnalyticsIdResponse> {
const analyticsEntity = await this.analyticsEntityRepository.findOneByUserUuid(dto.userUuid)
if (analyticsEntity === null) {
throw new Error(`Could not find analytics entity for user ${dto.userUuid}`)
}
return {
analyticsId: analyticsEntity.id,
}
}
}

View File

@@ -0,0 +1,5 @@
import { Uuid } from '@standardnotes/common'
export type GetUserAnalyticsIdDTO = {
userUuid: Uuid
}

View File

@@ -0,0 +1,3 @@
export type GetUserAnalyticsIdResponse = {
analyticsId: number
}

View File

@@ -0,0 +1,3 @@
export interface UseCaseInterface {
execute(...args: any[]): Promise<Record<string, unknown>>
}

View File

@@ -0,0 +1,42 @@
import 'reflect-metadata'
import { Repository, SelectQueryBuilder } from 'typeorm'
import { AnalyticsEntity } from '../../Domain/Entity/AnalyticsEntity'
import { MySQLAnalyticsEntityRepository } from './MySQLAnalyticsEntityRepository'
describe('MySQLAnalyticsEntityRepository', () => {
let ormRepository: Repository<AnalyticsEntity>
let analyticsEntity: AnalyticsEntity
let queryBuilder: SelectQueryBuilder<AnalyticsEntity>
const createRepository = () => new MySQLAnalyticsEntityRepository(ormRepository)
beforeEach(() => {
analyticsEntity = {} as jest.Mocked<AnalyticsEntity>
queryBuilder = {} as jest.Mocked<SelectQueryBuilder<AnalyticsEntity>>
ormRepository = {} as jest.Mocked<Repository<AnalyticsEntity>>
ormRepository.save = jest.fn()
ormRepository.createQueryBuilder = jest.fn().mockImplementation(() => queryBuilder)
})
it('should save', async () => {
await createRepository().save(analyticsEntity)
expect(ormRepository.save).toHaveBeenCalledWith(analyticsEntity)
})
it('should find one by user uuid', async () => {
queryBuilder.where = jest.fn().mockReturnThis()
queryBuilder.getOne = jest.fn().mockReturnValue(analyticsEntity)
const result = await createRepository().findOneByUserUuid('123')
expect(queryBuilder.where).toHaveBeenCalledWith('analytics_entity.user_uuid = :userUuid', { userUuid: '123' })
expect(result).toEqual(analyticsEntity)
})
})

View File

@@ -0,0 +1,26 @@
import { Uuid } from '@standardnotes/common'
import { inject, injectable } from 'inversify'
import { Repository } from 'typeorm'
import TYPES from '../../Bootstrap/Types'
import { AnalyticsEntity } from '../../Domain/Entity/AnalyticsEntity'
import { AnalyticsEntityRepositoryInterface } from '../../Domain/Entity/AnalyticsEntityRepositoryInterface'
@injectable()
export class MySQLAnalyticsEntityRepository implements AnalyticsEntityRepositoryInterface {
constructor(
@inject(TYPES.ORMAnalyticsEntityRepository)
private ormRepository: Repository<AnalyticsEntity>,
) {}
async findOneByUserUuid(userUuid: Uuid): Promise<AnalyticsEntity | null> {
return this.ormRepository
.createQueryBuilder('analytics_entity')
.where('analytics_entity.user_uuid = :userUuid', { userUuid })
.getOne()
}
async save(analyticsEntity: AnalyticsEntity): Promise<AnalyticsEntity> {
return this.ormRepository.save(analyticsEntity)
}
}

View File

@@ -5,7 +5,9 @@
"outDir": "./dist",
},
"include": [
"src/**/*"
"src/**/*",
"bin/**/*",
"migrations/**/*"
],
"references": []
}

View File

@@ -3,6 +3,18 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [1.36.3](https://github.com/standardnotes/api-gateway/compare/@standardnotes/api-gateway@1.36.2...@standardnotes/api-gateway@1.36.3) (2022-11-04)
**Note:** Version bump only for package @standardnotes/api-gateway
## [1.36.2](https://github.com/standardnotes/api-gateway/compare/@standardnotes/api-gateway@1.36.1...@standardnotes/api-gateway@1.36.2) (2022-11-04)
**Note:** Version bump only for package @standardnotes/api-gateway
## [1.36.1](https://github.com/standardnotes/api-gateway/compare/@standardnotes/api-gateway@1.36.0...@standardnotes/api-gateway@1.36.1) (2022-11-04)
**Note:** Version bump only for package @standardnotes/api-gateway
# [1.36.0](https://github.com/standardnotes/api-gateway/compare/@standardnotes/api-gateway@1.35.1...@standardnotes/api-gateway@1.36.0) (2022-11-04)
### Features

View File

@@ -1,6 +1,6 @@
{
"name": "@standardnotes/api-gateway",
"version": "1.36.0",
"version": "1.36.3",
"engines": {
"node": ">=16.0.0 <17.0.0"
},

View File

@@ -3,6 +3,18 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [1.49.6](https://github.com/standardnotes/server/compare/@standardnotes/auth-server@1.49.5...@standardnotes/auth-server@1.49.6) (2022-11-04)
**Note:** Version bump only for package @standardnotes/auth-server
## [1.49.5](https://github.com/standardnotes/server/compare/@standardnotes/auth-server@1.49.4...@standardnotes/auth-server@1.49.5) (2022-11-04)
**Note:** Version bump only for package @standardnotes/auth-server
## [1.49.4](https://github.com/standardnotes/server/compare/@standardnotes/auth-server@1.49.3...@standardnotes/auth-server@1.49.4) (2022-11-04)
**Note:** Version bump only for package @standardnotes/auth-server
## [1.49.3](https://github.com/standardnotes/server/compare/@standardnotes/auth-server@1.49.2...@standardnotes/auth-server@1.49.3) (2022-11-04)
**Note:** Version bump only for package @standardnotes/auth-server

View File

@@ -1,6 +1,6 @@
{
"name": "@standardnotes/auth-server",
"version": "1.49.3",
"version": "1.49.6",
"engines": {
"node": ">=16.0.0 <17.0.0"
},

View File

@@ -3,6 +3,18 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [1.10.14](https://github.com/standardnotes/syncing-server-js/compare/@standardnotes/syncing-server@1.10.13...@standardnotes/syncing-server@1.10.14) (2022-11-04)
**Note:** Version bump only for package @standardnotes/syncing-server
## [1.10.13](https://github.com/standardnotes/syncing-server-js/compare/@standardnotes/syncing-server@1.10.12...@standardnotes/syncing-server@1.10.13) (2022-11-04)
**Note:** Version bump only for package @standardnotes/syncing-server
## [1.10.12](https://github.com/standardnotes/syncing-server-js/compare/@standardnotes/syncing-server@1.10.11...@standardnotes/syncing-server@1.10.12) (2022-11-04)
**Note:** Version bump only for package @standardnotes/syncing-server
## [1.10.11](https://github.com/standardnotes/syncing-server-js/compare/@standardnotes/syncing-server@1.10.10...@standardnotes/syncing-server@1.10.11) (2022-11-04)
**Note:** Version bump only for package @standardnotes/syncing-server

View File

@@ -1,6 +1,6 @@
{
"name": "@standardnotes/syncing-server",
"version": "1.10.11",
"version": "1.10.14",
"engines": {
"node": ">=16.0.0 <17.0.0"
},