Skip to content

Commit

Permalink
feat: user repository (#655)
Browse files Browse the repository at this point in the history
Co-authored-by: Rui Silva <r.silva@kigroup.de>
  • Loading branch information
nunocaseiro and Rui Silva authored Dec 5, 2022
1 parent 546c563 commit 3530417
Show file tree
Hide file tree
Showing 51 changed files with 302 additions and 190 deletions.
11 changes: 5 additions & 6 deletions backend/jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
"coverageDirectory": "report",
"collectCoverageFrom": ["src/**/*.{ts,tsx}", "!node_modules/", "!src/database/migrations/**/*.{ts,tsx}"],
"transform": {
"\\.ts$": ["ts-jest"]
"\\.ts$": [
"ts-jest", {
"tsconfig": "<rootDir>/tsconfig.json"
}
]
},
"testEnvironment": "node",
"preset": "ts-jest",
"globals": {
"ts-jest": {
"tsconfig": "<rootDir>/tsconfig.json"
}
},
"testTimeout": 30000
}
81 changes: 50 additions & 31 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@nestjs/core": "^9.0.0",
"@nestjs/jwt": "^9.0.0",
"@nestjs/mapped-types": "^1.0.1",
"@nestjs/mongoose": "^9.0.3",
"@nestjs/mongoose": "^9.2.1",
"@nestjs/passport": "^9.0.0",
"@nestjs/platform-express": "^9.0.0",
"@nestjs/platform-socket.io": "^9.0.0",
Expand All @@ -55,7 +55,7 @@
"moment": "^2.29.4",
"moment-business-days": "^1.2.0",
"mongodb": "4.5.0",
"mongoose": "^6.7.0",
"mongoose": "^6.7.4",
"mongoose-lean-virtuals": "0.9.0",
"nodemailer": "^6.7.5",
"passport": "^0.6.0",
Expand Down Expand Up @@ -98,7 +98,7 @@
"ts-loader": "^9.2.8",
"ts-node": "^10.7.0",
"tsconfig-paths": "^4.0.0",
"typescript": "^4.6.3"
"typescript": "^4.9.3"
},
"lint-staged": {
"*.{js,ts,tsx}": [
Expand Down
2 changes: 1 addition & 1 deletion backend/src/infrastructure/database/mongoose.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import BoardUser, { BoardUserSchema } from 'src/modules/boards/schemas/board.use
import Schedules, { SchedulesSchema } from 'src/modules/schedules/schemas/schedules.schema';
import TeamUser, { TeamUserSchema } from 'src/modules/teams/schemas/team.user.schema';
import Team, { TeamSchema } from 'src/modules/teams/schemas/teams.schema';
import User, { UserSchema } from 'src/modules/users/schemas/user.schema';
import User, { UserSchema } from 'src/modules/users/entities/user.schema';

export const mongooseBoardModule = MongooseModule.forFeature([
{ name: Board.name, schema: BoardSchema }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { UpdateQuery } from 'mongoose';
import { ModelProps, SelectedValues } from '../types';

export interface BaseInterfaceRepository<T> {
getAll(selectedValues?: SelectedValues<T>): Promise<T[]>;

get(id: string, selectedValues?: SelectedValues<T>): Promise<T>;

create(item: T): Promise<T>;

update(id: string, item: T);

getByProp(value: ModelProps<T>): Promise<T>;

countDocuments(): Promise<number>;

findOneByFieldAndUpdate(value: ModelProps<T>, query: UpdateQuery<T>): Promise<T>;
}
45 changes: 45 additions & 0 deletions backend/src/libs/repositories/mongo/mongo-generic.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Model, UpdateQuery } from 'mongoose';
import { BaseInterfaceRepository } from '../interfaces/base.repository.interface';
import { ModelProps, SelectedValues } from '../types';

export class MongoGenericRepository<T> implements BaseInterfaceRepository<T> {
private _repository: Model<T>;
private _populateOnFind: string[];

constructor(repository: Model<T>, populateOnFind: string[] = []) {
this._repository = repository;
this._populateOnFind = populateOnFind;
}

getAll(selectedValues?: SelectedValues<T>): Promise<T[]> {
return this._repository.find().select(selectedValues).populate(this._populateOnFind).exec();
}

get(id: any, selectedValues?: SelectedValues<T>): Promise<T> {
return this._repository
.findById(id)
.select(selectedValues)
.populate(this._populateOnFind)
.exec() as Promise<T>;
}

getByProp(value: ModelProps<T>): Promise<T> {
return this._repository.findOne(value).exec();
}

create(item: T): Promise<T> {
return this._repository.create(item);
}

update(id: string, item: T) {
return this._repository.findByIdAndUpdate(id, item);
}

countDocuments(): Promise<number> {
return this._repository.countDocuments().exec();
}

findOneByFieldAndUpdate(value: ModelProps<T>, query: UpdateQuery<T>): Promise<T> {
return this._repository.findOneAndUpdate(value, query).exec();
}
}
9 changes: 9 additions & 0 deletions backend/src/libs/repositories/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export type SelectedValues<T> =
| string // maintain compatibility with existing code
| {
[K in keyof T]?: 1 | 0;
};

export type ModelProps<T> = {
[K in keyof T]?: T[K];
};
4 changes: 3 additions & 1 deletion backend/src/modules/auth/controller/auth.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import {
getUserApplication,
getUserService,
updateUserApplication,
updateUserService
updateUserService,
userRepository
} from 'src/modules/users/users.providers';

describe('AuthController', () => {
Expand Down Expand Up @@ -56,6 +57,7 @@ describe('AuthController', () => {
createUserService,
getUserApplication,
getUserService,
userRepository,
ConfigService,
{
provide: ConfigService,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { LeanDocument } from 'mongoose';
import CreateUserDto from 'src/modules/users/dto/create.user.dto';
import { UserDocument } from 'src/modules/users/schemas/user.schema';
import User from 'src/modules/users/entities/user.schema';

export interface RegisterAuthApplication {
register(registrationData: CreateUserDto): Promise<LeanDocument<UserDocument>>;
register(registrationData: CreateUserDto): Promise<User>;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { LeanDocument } from 'mongoose';
import CreateUserDto from 'src/modules/users/dto/create.user.dto';
import { UserDocument } from 'src/modules/users/schemas/user.schema';
import User from 'src/modules/users/entities/user.schema';

export interface RegisterAuthService {
register(registrationData: CreateUserDto): Promise<LeanDocument<UserDocument>>;
register(registrationData: CreateUserDto): Promise<User>;
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import { LeanDocument } from 'mongoose';
import { UserDocument } from 'src/modules/users/schemas/user.schema';
import User from 'src/modules/users/entities/user.schema';

export interface ValidateUserAuthService {
validateUserWithCredentials(
email: string,
plainTextPassword: string
): Promise<LeanDocument<UserDocument> | null>;
validateUserWithCredentials(email: string, plainTextPassword: string): Promise<User | null>;

validateUserById(userId: string): Promise<LeanDocument<UserDocument> | null>;
validateUserById(userId: string): Promise<User | null>;

validateUserByRefreshToken(
authorization: string,
userId: string
): Promise<false | LeanDocument<UserDocument>>;
validateUserByRefreshToken(authorization: string, userId: string): Promise<false | User>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Test, TestingModule } from '@nestjs/testing';
import configService from 'src/libs/test-utils/mocks/configService.mock';
import jwtService from 'src/libs/test-utils/mocks/jwtService.mock';
import GetTokenAuthServiceImpl from 'src/modules/auth/services/get-token.auth.service';
import { updateUserService } from 'src/modules/users/users.providers';
import { updateUserService, userRepository } from 'src/modules/users/users.providers';

describe('AuthService', () => {
let service: GetTokenAuthServiceImpl;
Expand All @@ -15,6 +15,7 @@ describe('AuthService', () => {
providers: [
GetTokenAuthServiceImpl,
updateUserService,
userRepository,
{
provide: ConfigService,
useValue: configService
Expand Down
Loading

0 comments on commit 3530417

Please sign in to comment.