generated from xgeekshq/oss-template
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: endpoint uses team user repository
- Loading branch information
Showing
40 changed files
with
492 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default class BaseModel { | ||
_id?: string; | ||
} |
32 changes: 27 additions & 5 deletions
32
backend/src/libs/repositories/interfaces/base.repository.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,40 @@ | ||
import { UpdateQuery } from 'mongoose'; | ||
import { FilterQuery, PopulateOptions, UpdateQuery } from 'mongoose'; | ||
import { ModelProps, SelectedValues } from '../types'; | ||
|
||
export type PopulateType = PopulateOptions | (PopulateOptions | string)[]; | ||
|
||
export interface BaseInterfaceRepository<T> { | ||
getAll(selectedValues?: SelectedValues<T>): Promise<T[]>; | ||
findAll(selectedValues?: SelectedValues<T>): Promise<T[]>; | ||
|
||
findOneById(id: any, selectedValues?: SelectedValues<T>, populate?: PopulateType): Promise<T>; | ||
|
||
findAllWithQuery( | ||
query: any, | ||
selectedValues?: SelectedValues<T>, | ||
populate?: PopulateType | ||
): Promise<T[]>; | ||
|
||
get(id: string, selectedValues?: SelectedValues<T>): Promise<T>; | ||
findOneByField(fields: ModelProps<T>): Promise<T>; | ||
|
||
create(item: T): Promise<T>; | ||
|
||
update(id: string, item: T); | ||
insertMany(listOfItems: T[]): Promise<T[]>; | ||
|
||
getByProp(value: ModelProps<T>): Promise<T>; | ||
update(id: string, item: T): Promise<T>; | ||
|
||
deleteMany(field: FilterQuery<T>, withSession: boolean): Promise<number>; | ||
|
||
countDocuments(): Promise<number>; | ||
|
||
findOneByFieldAndUpdate(value: ModelProps<T>, query: UpdateQuery<T>): Promise<T>; | ||
|
||
findOneAndRemoveByField(fields: ModelProps<T>, withSession: boolean): Promise<T>; | ||
|
||
startTransaction(): Promise<void>; | ||
|
||
commitTransaction(): Promise<void>; | ||
|
||
abortTransaction(): Promise<void>; | ||
|
||
endSession(): Promise<void>; | ||
} |
105 changes: 89 additions & 16 deletions
105
backend/src/libs/repositories/mongo/mongo-generic.repository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,118 @@ | ||
import { Model, UpdateQuery } from 'mongoose'; | ||
import { BaseInterfaceRepository } from '../interfaces/base.repository.interface'; | ||
import { ClientSession, FilterQuery, Model, UpdateQuery } from 'mongoose'; | ||
import { BaseInterfaceRepository, PopulateType } from '../interfaces/base.repository.interface'; | ||
import { ModelProps, SelectedValues } from '../types'; | ||
|
||
export class MongoGenericRepository<T> implements BaseInterfaceRepository<T> { | ||
private _repository: Model<T>; | ||
private _populateOnFind: string[]; | ||
protected _repository: Model<T>; | ||
protected _session: ClientSession; | ||
|
||
constructor(repository: Model<T>, populateOnFind: string[] = []) { | ||
constructor(repository: Model<T>) { | ||
this._repository = repository; | ||
this._populateOnFind = populateOnFind; | ||
} | ||
|
||
getAll(selectedValues?: SelectedValues<T>): Promise<T[]> { | ||
return this._repository.find().select(selectedValues).populate(this._populateOnFind).exec(); | ||
countDocuments(): Promise<number> { | ||
return this._repository.countDocuments().lean().exec(); | ||
} | ||
|
||
findAll(selectedValues?: SelectedValues<T>, populate?: PopulateType): Promise<T[]> { | ||
return this._repository.find().select(selectedValues).populate(populate).exec(); | ||
} | ||
|
||
get(id: any, selectedValues?: SelectedValues<T>): Promise<T> { | ||
findOneById(id: any, selectedValues?: SelectedValues<T>, populate?: PopulateType): Promise<T> { | ||
return this._repository | ||
.findById(id) | ||
.select(selectedValues) | ||
.populate(this._populateOnFind) | ||
.populate(populate) | ||
.exec() as Promise<T>; | ||
} | ||
|
||
getByProp(value: ModelProps<T>): Promise<T> { | ||
findOneByField(value: ModelProps<T>): Promise<T> { | ||
return this._repository.findOne(value).exec(); | ||
} | ||
|
||
findAllWithQuery( | ||
query: any, | ||
selectedValues?: SelectedValues<T>, | ||
populate?: PopulateType | ||
): Promise<T[]> { | ||
return this._repository | ||
.find(query) | ||
.select(selectedValues) | ||
.populate(populate) | ||
.lean({ virtuals: true }) | ||
.exec() as unknown as Promise<T[]>; | ||
} | ||
|
||
create(item: T): Promise<T> { | ||
return this._repository.create(item); | ||
} | ||
|
||
update(id: string, item: T) { | ||
return this._repository.findByIdAndUpdate(id, item); | ||
insertMany(listOfItems: T[]): Promise<T[]> { | ||
return this._repository.insertMany(listOfItems); | ||
} | ||
|
||
countDocuments(): Promise<number> { | ||
return this._repository.countDocuments().exec(); | ||
update(id: string, item: T): Promise<T> { | ||
return this._repository.findByIdAndUpdate(id, item).exec(); | ||
} | ||
|
||
findOneByFieldAndUpdate(value: ModelProps<T>, query: UpdateQuery<T>): Promise<T> { | ||
return this._repository.findOneAndUpdate(value, query).exec(); | ||
return this._repository.findOneAndUpdate(value, query, { new: true }).exec(); | ||
} | ||
|
||
findOneAndRemove(id: string, withSession = false): Promise<T> { | ||
return this._repository | ||
.findOneAndRemove( | ||
{ | ||
_id: id | ||
}, | ||
{ session: withSession ? this._session : undefined } | ||
) | ||
.exec(); | ||
} | ||
|
||
findOneAndRemoveByField(fields: ModelProps<T>, withSession: boolean): Promise<T> { | ||
return this._repository | ||
.findOneAndRemove(fields, { | ||
session: withSession ? this._session : undefined | ||
}) | ||
.exec(); | ||
} | ||
|
||
async deleteMany(field: FilterQuery<T>, withSession = false): Promise<number> { | ||
const { deletedCount } = await this._repository | ||
.deleteMany(field, { session: withSession ? this._session : undefined }) | ||
.exec(); | ||
|
||
return deletedCount; | ||
} | ||
|
||
async deleteManyByListOfIds(teamUserIds: string[], withSession: boolean): Promise<number> { | ||
const { deletedCount } = await this._repository | ||
.deleteMany( | ||
{ | ||
_id: teamUserIds | ||
}, | ||
{ session: withSession ? this._session : undefined } | ||
) | ||
.exec(); | ||
|
||
return deletedCount; | ||
} | ||
|
||
async startTransaction() { | ||
this._session = await this._repository.db.startSession(); | ||
this._session.startTransaction(); | ||
} | ||
|
||
async commitTransaction() { | ||
await this._session.commitTransaction(); | ||
} | ||
|
||
async abortTransaction() { | ||
await this._session.abortTransaction(); | ||
} | ||
|
||
async endSession() { | ||
await this._session.endSession(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
backend/src/modules/boards/applications/delete.board.application.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
backend/src/modules/boards/interfaces/services/delete.board.service.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export interface DeleteBoardService { | ||
export interface DeleteBoardServiceInterface { | ||
delete(boardId: string, userId: string): Promise<boolean>; | ||
deleteBoardsByTeamId(teamId: string): Promise<boolean>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.