Skip to content

Commit

Permalink
[GH-38] feat(User): add MongoAuthRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
pablojvritx committed Dec 14, 2023
1 parent 93060ad commit f4357b6
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { MongoClient } from 'mongodb';
import { Nullable } from '../../../../shared/domain/Nullable';
import { MongoRepository } from '../../../../shared/infrastructure/persistence/mongo/MongoRepository';
import { User, UserRepository } from '../../domain';
import { UserPatch } from '../../domain/UserPatch';

export interface AuthDocument {
_id: string;
email: string;
username: string;
password: string;
emailValidated: boolean;
roles: string[];
}

export class MongoAuthRepository
extends MongoRepository<User | UserPatch>
implements UserRepository
{
constructor(client: Promise<MongoClient>) {
super(client);
this.createUniqueIndex();
}
protected collectionName(): string {
return 'users';
}

async save(user: User): Promise<void> {
return this.persist(user.id.value, user);
}

async update(user: UserPatch): Promise<void> {
return this.patch(user);
}

async search(email: string): Promise<Nullable<User>> {
const collection = await this.collection();
const document = await collection.findOne<AuthDocument>({ email });

return document
? User.fromPrimitives({
id: document._id,
email: document.email,
username: document.username,
password: document.password,
emailValidated: document.emailValidated,
roles: document.roles
})
: null;
}

private async createUniqueIndex(): Promise<void> {
const collection = await this.collection();
await collection.createIndex({ email: 1 }, { unique: true });
await collection.createIndex({ username: 1 }, { unique: true });
}
}
31 changes: 31 additions & 0 deletions src/apps/apiApp/dependency-injection/Auth/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
services:
apiApp.MongoConfig:
factory:
class: ../../../../Contexts/shared/infrastructure/persistence/mongo/MongoConfigFactory
method: 'createConfig'

apiApp.MongoClient:
factory:
class: ../../../../Contexts/shared/infrastructure/persistence/mongo/MongoClientFactory
method: 'createClient'
arguments: ['apiApp', '@apiApp.MongoConfig']

# plugin.Encrypter:
# class: ../../../../Contexts/shared/plugins/CryptAdapter
# arguments: []

apiApp.Auth.domain.AuthRepository:
class: ../../../../Contexts/apiApp/Auth/infrastructure/persistence/MongoAuthRepository
arguments: ['@apiApp.MongoClient']

# apiApp.Auth.application.LoginUser:
# class: ../../../../Contexts/apiApp/Auth/application/LoginUser
# arguments: ['@apiApp.Auth.domain.AuthRepository', '@plugin.Encrypter']

# apiApp.Auth.application.RegisterUser:
# class: ../../../../Contexts/apiApp/Auth/application/RegisterUser
# arguments: ['@apiApp.Auth.domain.AuthRepository', '@plugin.Encrypter']

# apiApp.Auth.application.ValidateMail:
# class: ../../../../Contexts/apiApp/Auth/application/ValidateMail
# arguments: ['@apiApp.Auth.domain.AuthRepository', '@plugin.Encrypter']
1 change: 1 addition & 0 deletions src/apps/apiApp/dependency-injection/application.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
imports:
- { resource: ./apps/application.yaml }
- { resource: ./Auth/application.yaml }
- { resource: ./Books/application.yaml }
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import container from '../../../../../../src/apps/apiApp/dependency-injection';
import { UserRepository } from '../../../../../../src/Contexts/apiApp/Auth/domain/UserRepository';

import { EnvironmentArranger } from '../../../../shared/infrastructure/arranger/EnvironmentArranger';
import { UserMother } from '../../domain/mothers';

const repository: UserRepository = container.get(
'apiApp.Auth.domain.AuthRepository'
);

const environmentArranger: Promise<EnvironmentArranger> = container.get(
'apiApp.EnvironmentArranger'
);

describe('MongoAuthRepository', () => {
beforeEach(async () => {
await (await environmentArranger).arrange();
});

afterAll(async () => {
await (await environmentArranger).arrange();
await (await environmentArranger).close();
});

describe('save', () => {
it('should save a user', async () => {
const user = UserMother.random();

await repository.save(user);
});
});

describe('update', () => {
it('should update an existing user', async () => {
const user = UserMother.random();
await repository.save(user);
const userPatch = UserMother.randomPatch(user.id.value);

await repository.update(userPatch);

expect(await repository.search(user.email.value)).toMatchObject(
userPatch
);
});
});

describe('search', () => {
it('should return an existing user', async () => {
const user = UserMother.random();

await repository.save(user);

expect(await repository.search(user.email.value)).toMatchObject(user);
});

it('should not return a non existing user', async () => {
expect(await repository.search(UserMother.random().email.value)).toBe(
null
);
});
});
});

0 comments on commit f4357b6

Please sign in to comment.