Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove domain actions & refactor #18

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ jobs:
- run: npm ci
- run: npm run build:dev
- run: npm run test:unit
- run: docker-compose -f docker-compose.dev.yml up -d
- run: docker-compose up -d
- run: npm run test:integration
# - run: NODE_ENV=ci npm run test:e2e
3 changes: 1 addition & 2 deletions apps/backend/src/common/types/databaseTable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export interface DatabaseTable<Entity> {
export interface DatabaseTable {
readonly name: string;
readonly columns: Record<keyof Entity, string>;
}
5 changes: 0 additions & 5 deletions apps/backend/src/libs/database/types/queryBuilder.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ResourceNotFoundError } from '../../../../../common/errors/common/resou
import { type SqliteDatabaseClient } from '../../../../../core/database/sqliteDatabaseClient/sqliteDatabaseClient.js';
import { coreSymbols } from '../../../../../core/symbols.js';
import { type DependencyInjectionContainer } from '../../../../../libs/dependencyInjection/dependencyInjectionContainer.js';
import { type UserBucketTestUtils } from '../../../../userModule/tests/utils/userBucketTestUtils/userBucketTestUtils.js';
import { type UserTestUtils } from '../../../../userModule/tests/utils/userTestUtils/userTestUtils.js';
import { symbols } from '../../../symbols.js';
import { type S3TestUtils } from '../../../tests/utils/s3TestUtils.js';
Expand All @@ -26,6 +27,8 @@ describe('DeleteResourceCommandHandlerImpl', () => {

let userTestUtils: UserTestUtils;

let userBucketTestUtils: UserBucketTestUtils;

const resourcesDirectory = path.resolve(__dirname, '../../../../../../../../resources');

const sampleFileName = 'sample_video1.mp4';
Expand All @@ -41,16 +44,22 @@ describe('DeleteResourceCommandHandlerImpl', () => {

userTestUtils = container.get<UserTestUtils>(testSymbols.userTestUtils);

userBucketTestUtils = container.get<UserBucketTestUtils>(testSymbols.userBucketTestUtils);

s3TestUtils = container.get<S3TestUtils>(testSymbols.s3TestUtils);

await userTestUtils.truncate();

await userBucketTestUtils.truncate();

await s3TestUtils.createBucket(bucketName);
});

afterEach(async () => {
await userTestUtils.truncate();

await userBucketTestUtils.truncate();

await sqliteDatabaseClient.destroy();

await s3TestUtils.deleteBucket(bucketName);
Expand Down Expand Up @@ -97,7 +106,7 @@ describe('DeleteResourceCommandHandlerImpl', () => {

const user = await userTestUtils.createAndPersist();

await userTestUtils.createAndPersistUserBucket({
await userBucketTestUtils.createAndPersist({
input: {
userId: user.id,
bucketName,
Expand All @@ -122,7 +131,7 @@ describe('DeleteResourceCommandHandlerImpl', () => {
it('deletes a resource', async () => {
const user = await userTestUtils.createAndPersist();

await userTestUtils.createAndPersistUserBucket({
await userBucketTestUtils.createAndPersist({
input: {
userId: user.id,
bucketName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ResourceNotFoundError } from '../../../../../common/errors/common/resou
import { type SqliteDatabaseClient } from '../../../../../core/database/sqliteDatabaseClient/sqliteDatabaseClient.js';
import { coreSymbols } from '../../../../../core/symbols.js';
import { type DependencyInjectionContainer } from '../../../../../libs/dependencyInjection/dependencyInjectionContainer.js';
import { type UserBucketTestUtils } from '../../../../userModule/tests/utils/userBucketTestUtils/userBucketTestUtils.js';
import { type UserTestUtils } from '../../../../userModule/tests/utils/userTestUtils/userTestUtils.js';
import { symbols } from '../../../symbols.js';
import { type S3TestUtils } from '../../../tests/utils/s3TestUtils.js';
Expand All @@ -25,6 +26,8 @@ describe('FindResourcesMetadataQueryHandlerImpl', () => {

let userTestUtils: UserTestUtils;

let userBucketTestUtils: UserBucketTestUtils;

const resourcesDirectory = path.resolve(__dirname, '../../../../../../../../resources');

const sampleFileName1 = 'sample_video1.mp4';
Expand All @@ -40,16 +43,22 @@ describe('FindResourcesMetadataQueryHandlerImpl', () => {

userTestUtils = container.get<UserTestUtils>(testSymbols.userTestUtils);

userBucketTestUtils = container.get<UserBucketTestUtils>(testSymbols.userBucketTestUtils);

s3TestUtils = container.get<S3TestUtils>(testSymbols.s3TestUtils);

await userTestUtils.truncate();

await userBucketTestUtils.truncate();

await s3TestUtils.createBucket(bucketName);
});

afterEach(async () => {
await userTestUtils.truncate();

await userBucketTestUtils.truncate();

await sqliteDatabaseClient.destroy();

await s3TestUtils.deleteBucket(bucketName);
Expand Down Expand Up @@ -94,7 +103,7 @@ describe('FindResourcesMetadataQueryHandlerImpl', () => {
it('throws an error - when Resource does not exist', async () => {
const user = await userTestUtils.createAndPersist();

await userTestUtils.createAndPersistUserBucket({
await userBucketTestUtils.createAndPersist({
input: {
userId: user.id,
bucketName,
Expand All @@ -119,7 +128,7 @@ describe('FindResourcesMetadataQueryHandlerImpl', () => {
it('downloads a Resource', async () => {
const user = await userTestUtils.createAndPersist();

await userTestUtils.createAndPersistUserBucket({
await userBucketTestUtils.createAndPersist({
input: {
userId: user.id,
bucketName,
Expand All @@ -134,8 +143,6 @@ describe('FindResourcesMetadataQueryHandlerImpl', () => {
bucketName,
});

// TODO: validate data

expect(resource).toEqual({
name: sampleFileName1,
updatedAt: expect.any(Date),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ResourceNotFoundError } from '../../../../../common/errors/common/resou
import { type SqliteDatabaseClient } from '../../../../../core/database/sqliteDatabaseClient/sqliteDatabaseClient.js';
import { coreSymbols } from '../../../../../core/symbols.js';
import { type DependencyInjectionContainer } from '../../../../../libs/dependencyInjection/dependencyInjectionContainer.js';
import { type UserBucketTestUtils } from '../../../../userModule/tests/utils/userBucketTestUtils/userBucketTestUtils.js';
import { type UserTestUtils } from '../../../../userModule/tests/utils/userTestUtils/userTestUtils.js';
import { symbols } from '../../../symbols.js';
import { type S3TestUtils } from '../../../tests/utils/s3TestUtils.js';
Expand All @@ -25,6 +26,8 @@ describe('FindResourcesMetadataQueryHandlerImpl', () => {

let userTestUtils: UserTestUtils;

let userBucketTestUtils: UserBucketTestUtils;

const resourcesDirectory = path.resolve(__dirname, '../../../../../../../../resources');

const sampleFileName1 = 'sample_video1.mp4';
Expand All @@ -42,16 +45,22 @@ describe('FindResourcesMetadataQueryHandlerImpl', () => {

userTestUtils = container.get<UserTestUtils>(testSymbols.userTestUtils);

userBucketTestUtils = container.get<UserBucketTestUtils>(testSymbols.userBucketTestUtils);

s3TestUtils = container.get<S3TestUtils>(testSymbols.s3TestUtils);

await userTestUtils.truncate();

await userBucketTestUtils.truncate();

await s3TestUtils.createBucket(bucketName);
});

afterEach(async () => {
await userTestUtils.truncate();

await userBucketTestUtils.truncate();

await sqliteDatabaseClient.destroy();

await s3TestUtils.deleteBucket(bucketName);
Expand Down Expand Up @@ -98,7 +107,7 @@ describe('FindResourcesMetadataQueryHandlerImpl', () => {
it('finds resources metadata', async () => {
const user = await userTestUtils.createAndPersist();

await userTestUtils.createAndPersistUserBucket({
await userBucketTestUtils.createAndPersist({
input: {
userId: user.id,
bucketName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ export class CrateUserCommandHandlerImpl implements CreateUserCommandHandler {

const hashedPassword = await this.hashService.hash({ plainData: password });

const user = await this.userRepository.createUser({
email,
password: hashedPassword,
role: UserRole.user,
const user = await this.userRepository.saveUser({
user: {
email,
password: hashedPassword,
role: UserRole.user,
},
});

this.loggerService.debug({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { OperationNotValidError } from '../../../../../common/errors/common/oper
import { type SqliteDatabaseClient } from '../../../../../core/database/sqliteDatabaseClient/sqliteDatabaseClient.js';
import { coreSymbols } from '../../../../../core/symbols.js';
import { symbols } from '../../../symbols.js';
import { type UserBucketTestUtils } from '../../../tests/utils/userBucketTestUtils/userBucketTestUtils.js';
import { type UserTestUtils } from '../../../tests/utils/userTestUtils/userTestUtils.js';

describe('GrantBucketAccessCommandHandlerImpl', () => {
Expand All @@ -18,6 +19,8 @@ describe('GrantBucketAccessCommandHandlerImpl', () => {

let userTestUtils: UserTestUtils;

let userBucketTestUtils: UserBucketTestUtils;

beforeEach(async () => {
const container = TestContainer.create();

Expand All @@ -27,12 +30,18 @@ describe('GrantBucketAccessCommandHandlerImpl', () => {

userTestUtils = container.get<UserTestUtils>(testSymbols.userTestUtils);

userBucketTestUtils = container.get<UserBucketTestUtils>(testSymbols.userBucketTestUtils);

await userTestUtils.truncate();

await userBucketTestUtils.truncate();
});

afterEach(async () => {
await userTestUtils.truncate();

await userBucketTestUtils.truncate();

await sqliteDatabaseClient.destroy();
});

Expand All @@ -46,7 +55,7 @@ describe('GrantBucketAccessCommandHandlerImpl', () => {
bucketName,
});

const userBuckets = await userTestUtils.findBucketsByUserId({ userId: user.id });
const userBuckets = await userBucketTestUtils.findUserBuckets({ userId: user.id });

expect(userBuckets.find((userBucket) => userBucket.bucketName === bucketName)).toBeDefined();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import {
} from './grantBucketAccessCommandHandler.js';
import { OperationNotValidError } from '../../../../../common/errors/common/operationNotValidError.js';
import { type LoggerService } from '../../../../../libs/logger/services/loggerService/loggerService.js';
import { type UserBucketRepository } from '../../../domain/repositories/userBucketRepository/userBucketRepository.js';
import { type UserRepository } from '../../../domain/repositories/userRepository/userRepository.js';

export class GrantBucketAccessCommandHandlerImpl implements GrantBucketAccessCommandHandler {
public constructor(
private readonly userRepository: UserRepository,
private readonly userBucketRepository: UserBucketRepository,
private readonly loggerService: LoggerService,
) {}

Expand All @@ -30,7 +32,7 @@ export class GrantBucketAccessCommandHandlerImpl implements GrantBucketAccessCom
});
}

const existingBuckets = await this.userRepository.findUserBuckets({ userId });
const existingBuckets = await this.userBucketRepository.findUserBuckets({ userId });

if (existingBuckets.find((userBucket) => userBucket.getBucketName() === bucketName)) {
this.loggerService.debug({
Expand All @@ -42,13 +44,9 @@ export class GrantBucketAccessCommandHandlerImpl implements GrantBucketAccessCom
return;
}

existingUser.addGrantBucketAccessAction({
await this.userBucketRepository.createUserBucket({
bucketName,
});

await this.userRepository.updateUser({
id: userId,
domainActions: existingUser.getDomainActions(),
userId,
});

this.loggerService.debug({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,6 @@ describe('LoginUserCommandHandler', () => {

expect(refreshTokenPayload['userId']).toBe(createdUser.getId());

const userTokens = await userTestUtils.findTokensByUserId({
userId: createdUser.getId(),
});

expect(userTokens.refreshTokens.includes(refreshToken)).toBe(true);

expect(accessTokenExpiresIn).toBe(configProvider.getAccessTokenExpiresIn());
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,6 @@ export class LoginUserCommandHandlerImpl implements LoginUserCommandHandler {
expiresIn: refreshTokenExpiresIn,
});

const expiresAt = new Date(Date.now() + refreshTokenExpiresIn * 1000);

user.addCreateRefreshTokenAction({
token: refreshToken,
expiresAt,
});

await this.userRepository.updateUser({
id: user.getId(),
domainActions: user.getDomainActions(),
});

this.loggerService.debug({
message: 'User logged in.',
email,
Expand Down
Loading
Loading