-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
661 additions
and
71 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
save-prefix='' | ||
save-prefix='' | ||
public-hoist-pattern[]=*nestjs-base-service* | ||
public-hoist-pattern[]=*typeorm* |
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,9 @@ | ||
import { createParamDecorator, ExecutionContext } from '@nestjs/common'; | ||
import { User } from '@shared/entities/users/user.entity'; | ||
|
||
export const GetUser = createParamDecorator( | ||
(data: unknown, ctx: ExecutionContext): User => { | ||
const request = ctx.switchToHttp().getRequest(); | ||
return request.user; | ||
}, | ||
); |
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 |
---|---|---|
@@ -1,4 +1,67 @@ | ||
import { Controller } from '@nestjs/common'; | ||
import { | ||
Controller, | ||
ClassSerializerInterceptor, | ||
Body, | ||
Param, | ||
ParseUUIDPipe, | ||
UseInterceptors, | ||
HttpStatus, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
|
||
@Controller('users') | ||
export class UsersController {} | ||
import { UsersService } from './users.service'; | ||
import { tsRestHandler, TsRestHandler } from '@ts-rest/nest'; | ||
import { GetUser } from '@api/decorators/get-user.decorator'; | ||
import { User } from '@shared/entities/users/user.entity'; | ||
import { usersContract as c } from '@shared/contracts/users.contract'; | ||
|
||
import { UpdateUserPasswordDto } from '@shared/dtos/users/update-user-password.dto'; | ||
import { UpdateUserDto } from '@shared/dtos/users/update-user.dto'; | ||
|
||
@Controller() | ||
@UseInterceptors(ClassSerializerInterceptor) | ||
export class UsersController { | ||
constructor(private usersService: UsersService) {} | ||
|
||
@TsRestHandler(c.findMe) | ||
async findMe(@GetUser() user: User): Promise<any> { | ||
return tsRestHandler(c.findMe, async ({ query }) => { | ||
const foundUser = await this.usersService.getById(user.id, query); | ||
if (!foundUser) { | ||
throw new UnauthorizedException(); | ||
} | ||
return { body: { data: foundUser }, status: HttpStatus.OK }; | ||
}); | ||
} | ||
|
||
@TsRestHandler(c.updatePassword) | ||
async updatePassword( | ||
@Body() dto: UpdateUserPasswordDto['newPassword'], | ||
@GetUser() user: User, | ||
): Promise<any> { | ||
return tsRestHandler(c.updatePassword, async () => { | ||
const updatedUser = await this.usersService.updatePassword(user, dto); | ||
return { body: { data: updatedUser }, status: HttpStatus.OK }; | ||
}); | ||
} | ||
|
||
@TsRestHandler(c.updateUser) | ||
async update( | ||
@Param('id', ParseUUIDPipe) id: string, | ||
@Body() dto: UpdateUserDto, | ||
): Promise<any> { | ||
return tsRestHandler(c.updateUser, async () => { | ||
const user = await this.usersService.update(id, dto); | ||
//return { body: { data: user }, status: HttpStatus.CREATED }; | ||
return { body: { data: user }, status: HttpStatus.CREATED }; | ||
}); | ||
} | ||
|
||
@TsRestHandler(c.deleteMe) | ||
async deleteMe(@GetUser() user: User): Promise<any> { | ||
return tsRestHandler(c.deleteMe, async () => { | ||
await this.usersService.remove(user.id); | ||
return { body: null, status: HttpStatus.OK }; | ||
}); | ||
} | ||
} |
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,67 @@ | ||
import { | ||
BaseService, | ||
DEFAULT_PAGINATION, | ||
FetchSpecification, | ||
} from 'nestjs-base-service'; | ||
|
||
import { Repository } from 'typeorm'; | ||
import { PaginationMeta } from '@shared/dtos/global/api-response.dto'; | ||
|
||
export abstract class AppBaseService< | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
Entity extends object, | ||
CreateModel, | ||
UpdateModel, | ||
Info, | ||
> extends BaseService<Entity, CreateModel, UpdateModel, Info> { | ||
constructor( | ||
protected readonly repository: Repository<Entity>, | ||
protected alias: string = 'base_entity', | ||
protected pluralAlias: string = 'base_entities', | ||
protected idProperty: string = 'id', | ||
) { | ||
super(repository, alias, { idProperty }); | ||
} | ||
|
||
async findAllPaginated( | ||
fetchSpecification?: FetchSpecification, | ||
extraOps?: Record<string, any>, | ||
info?: Info, | ||
): Promise<{ | ||
data: (Partial<Entity> | undefined)[]; | ||
metadata: PaginationMeta | undefined; | ||
}> { | ||
const entitiesAndCount: [Partial<Entity>[], number] = await this.findAll( | ||
{ ...fetchSpecification, ...extraOps }, | ||
info, | ||
); | ||
return this._paginate(entitiesAndCount, fetchSpecification); | ||
} | ||
|
||
private _paginate( | ||
entitiesAndCount: [Partial<Entity>[], number], | ||
fetchSpecification?: FetchSpecification, | ||
): { | ||
data: (Partial<Entity> | undefined)[]; | ||
metadata: PaginationMeta | undefined; | ||
} { | ||
const totalItems: number = entitiesAndCount[1]; | ||
const entities: Partial<Entity>[] = entitiesAndCount[0]; | ||
const pageSize: number = | ||
fetchSpecification?.pageSize ?? DEFAULT_PAGINATION.pageSize ?? 25; | ||
const page: number = | ||
fetchSpecification?.pageNumber ?? DEFAULT_PAGINATION.pageNumber ?? 1; | ||
const disablePagination: boolean | undefined = | ||
fetchSpecification?.disablePagination; | ||
const meta: PaginationMeta | undefined = disablePagination | ||
? undefined | ||
: new PaginationMeta({ | ||
totalPages: Math.ceil(totalItems / pageSize), | ||
totalItems, | ||
size: pageSize, | ||
page, | ||
}); | ||
|
||
return { data: entities, metadata: meta }; | ||
} | ||
} |
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,4 @@ | ||
import { InfoDTO } from 'nestjs-base-service'; | ||
import { User } from '@shared/entities/users/user.entity'; | ||
|
||
export type AppInfoDTO = InfoDTO<User>; |
Oops, something went wrong.