Skip to content

Commit

Permalink
remove domain actions & refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal committed Feb 28, 2024
1 parent 8ce4fb5 commit 8bb8306
Show file tree
Hide file tree
Showing 60 changed files with 507 additions and 1,143 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,6 @@ describe('LogoutUserCommandHandlerImpl', () => {

const user = await userTestUtils.createAndPersist();

await userTestUtils.createAndPersistRefreshToken({
input: {
userId: user.id,
token: refreshToken,
},
});

await commandHandler.execute({
userId: user.id,
refreshToken,
Expand Down Expand Up @@ -96,63 +89,4 @@ describe('LogoutUserCommandHandlerImpl', () => {
},
});
});

it('throws an error - when UserTokens not found', async () => {
const refreshToken = tokenService.createToken({
data: { valid: 'true' },
expiresIn: Generator.number(10000, 100000),
});

const user = await userTestUtils.createAndPersist();

await expect(
async () =>
await commandHandler.execute({
userId: user.id,
refreshToken,
}),
).toThrowErrorInstance({
instance: OperationNotValidError,
context: {
reason: 'User tokens not found.',
userId: user.id,
},
});
});

it('throws an error - when UserTokens were found but refreshToken is different', async () => {
const refreshToken = tokenService.createToken({
data: { valid: 'true' },
expiresIn: Generator.number(10000, 100000),
});

const user = await userTestUtils.createAndPersist();

await userTestUtils.createAndPersistRefreshToken({
input: {
userId: user.id,
token: refreshToken,
},
});

const invalidRefreshToken = tokenService.createToken({
data: { invalid: 'true' },
expiresIn: Generator.number(),
});

await expect(
async () =>
await commandHandler.execute({
userId: user.id,
refreshToken: invalidRefreshToken,
}),
).toThrowErrorInstance({
instance: OperationNotValidError,
context: {
reason: 'Refresh token is not valid.',
userId: user.id,
refreshToken: invalidRefreshToken,
},
});
});
});
Loading

0 comments on commit 8bb8306

Please sign in to comment.