Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/api/src/modules/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { AuthService, GoogleAuthGuard, JwtAuthGuard, LocalAuthGuard } from '@gh/auth';
import { globalConfig } from '@gh/config';
import { PrismaService, User as PrismaUser } from '@gh/prisma';
import { LoginDto } from '@gh/shared/dtos';
import { AuthKeys } from '@gh/shared/models';
import { loggedMethod } from '@gh/shared/utils';
import { Body, Controller, Get, HttpCode, HttpStatus, Inject, Post, Req, Res, UnauthorizedException, UseGuards } from '@nestjs/common';
import { ConfigType } from '@nestjs/config';
import { ApiBody } from '@nestjs/swagger';
import { Request, Response } from 'express';
import { LoginDto } from './dtos';

@Controller('auth')
export class AuthController {
Expand Down
1 change: 0 additions & 1 deletion apps/api/src/modules/auth/dtos/index.ts

This file was deleted.

9 changes: 0 additions & 9 deletions apps/api/src/modules/auth/dtos/login.dto.ts

This file was deleted.

2 changes: 1 addition & 1 deletion apps/api/src/modules/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class UsersController {
@UseGuards(JwtAuthGuard)
@Patch(':id')
updateUser(@Param('id', ParseIntPipe) id: number, @Body() data: UpdateUserDto) {
return this.usersMicroservice.send<User>('update_user', data);
return this.usersMicroservice.send<User>('update_user', { id, data });
}

@UseGuards(JwtAuthGuard)
Expand Down
9 changes: 7 additions & 2 deletions libs/prisma/src/lib/prisma.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import { Injectable, OnModuleInit } from '@nestjs/common';
import { PrismaClient } from '../../generated/prisma/client/client';

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
export class PrismaService implements OnModuleInit {
readonly client: PrismaClient;
protected connected = false;

constructor() {
this.client = new PrismaClient();
}

get isConnected() {
return this.connected;
}
Expand All @@ -15,7 +20,7 @@ export class PrismaService extends PrismaClient implements OnModuleInit {

async connect() {
try {
await this.$connect();
await this.client.$connect();
this.connected = true;
console.log('PrismaService initialized and connected to the database');
} catch (error) {
Expand Down
10 changes: 8 additions & 2 deletions libs/shared/src/lib/dtos/create-user.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ import { IsEmail, IsNotEmpty, IsString } from 'class-validator';
export class CreateUserDto {
@IsEmail()
@IsNotEmpty()
@ApiProperty()
@ApiProperty({
type: String,
example: 'me@somecompany.com',
})
email = '';

@IsString()
@IsNotEmpty()
@ApiProperty()
@ApiProperty({
type: String,
example: 'password123',
})
password = '';
}
1 change: 1 addition & 0 deletions libs/shared/src/lib/dtos/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './create-user.dto';
export * from './update-user.dto';
export * from './login.dto'
18 changes: 18 additions & 0 deletions libs/shared/src/lib/dtos/login.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator';

export class LoginDto {
@IsString()
@ApiProperty({
type: String,
example: 'me@somecompany.com',
})
email = '';

@IsString()
@ApiProperty({
type: String,
example: 'password123',
})
password = '';
}
5 changes: 4 additions & 1 deletion libs/shared/src/lib/dtos/update-user.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { IsEmail, IsOptional } from 'class-validator';
export class UpdateUserDto {
@IsEmail()
@IsOptional()
@ApiProperty()
@ApiProperty({
type: String,
example: 'me@somecompany.com',
})
email?: string;
}
16 changes: 8 additions & 8 deletions libs/users/src/lib/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ export class UsersService {
) {}

async getUsers() {
return await this.prisma.user.findMany();
return await this.prisma.client.user.findMany();
}

async getUserById(id: number) {
try {
const user = await this.prisma.user.findUnique({ where: { id } });
const user = await this.prisma.client.user.findUnique({ where: { id } });

if (!user) {
throw new HttpException(`User with id ${id} not found`, HttpStatus.NOT_FOUND);
Expand All @@ -42,7 +42,7 @@ export class UsersService {

async getUserByEmail(email: string) {
try {
const user = await this.prisma.user.findUnique({ where: { email } });
const user = await this.prisma.client.user.findUnique({ where: { email } });

if (!user) {
throw new HttpException(`User with email ${email} not found`, HttpStatus.NOT_FOUND);
Expand All @@ -59,30 +59,30 @@ export class UsersService {

async createUser(data: Prisma.UserCreateInput) {
const { email, password } = data;
const user = await this.prisma.user.findUnique({ where: { email } });
const user = await this.prisma.client.user.findUnique({ where: { email } });

if (user) {
throw new HttpException(`User with email ${email} already exists`, HttpStatus.BAD_REQUEST);
}

data.password = await bcrypt.hash(password, this.config.crypt.saltRounds);

return this.prisma.user.create({ data });
return this.prisma.client.user.create({ data });
}

async updateUser(id: number, data: Prisma.UserUpdateInput) {
const user = await this.prisma.user.findUnique({ where: { id } });
const user = await this.prisma.client.user.findUnique({ where: { id } });

if (!user) {
throw new HttpException(`User with id ${id} not found`, HttpStatus.NOT_FOUND);
}

return this.prisma.user.update({ where: { id }, data });
return this.prisma.client.user.update({ where: { id }, data });
}

async deleteUserById(id: number) {
await this.getUserById(id);

return await this.prisma.user.delete({ where: { id } });
return await this.prisma.client.user.delete({ where: { id } });
}
}
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"@nestjs/platform-express": "^11.1.6",
"@nestjs/swagger": "^11.2.0",
"@ngrx/signals": "^20.0.0",
"@prisma/client": "^6.13.0",
"@prisma/client": "^6.14.0",
"axios": "^1.11.0",
"bcrypt": "^6.0.0",
"bootstrap": "^5.3.7",
Expand Down Expand Up @@ -116,10 +116,10 @@
"@types/passport-google-oauth20": "^2.0.16",
"@types/passport-jwt": "^4.0.1",
"@types/passport-local": "^1.0.38",
"@typescript-eslint/eslint-plugin": "^8.39.0",
"@typescript-eslint/parser": "^8.39.0",
"@typescript-eslint/eslint-plugin": "^8.39.1",
"@typescript-eslint/parser": "^8.39.1",
"copy-files-from-to": "^3.12.1",
"esbuild": "^0.25.8",
"esbuild": "^0.25.9",
"eslint": "^9.33.0",
"eslint-config-prettier": "^10.1.8",
"eslint-plugin-playwright": "^2.2.2",
Expand All @@ -132,7 +132,7 @@
"jsonc-eslint-parser": "^2.4.0",
"nx": "21.3.11",
"prettier": "^3.6.2",
"prisma": "^6.13.0",
"prisma": "^6.14.0",
"react-refresh": "^0.17.0",
"ts-jest": "^29.4.1",
"ts-node": "^10.9.2",
Expand Down
4 changes: 2 additions & 2 deletions services/users-service/src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export class UsersController {

@MessagePattern('update_user')
@loggedMethod('Users microservice: Update user')
updateUser(id: number, data: UpdateUserDto) {
return this.usersService.updateUser(id, data);
updateUser(param: { id: number, data: UpdateUserDto }) {
return this.usersService.updateUser(param.id, param.data);
}

@MessagePattern('delete_user_by_id')
Expand Down
24 changes: 14 additions & 10 deletions services/users-service/src/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { globalConfig } from '@gh/config';
import { PrismaService } from '@gh/prisma';
import { PrismaClient, PrismaService } from '@gh/prisma';
import { Prisma } from '@gh/prisma';
import { HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common';
import { ConfigType } from '@nestjs/config';
Expand All @@ -14,18 +14,22 @@ export type User = {

@Injectable()
export class UsersService {
#prismaClient: PrismaClient;

constructor(
@Inject(globalConfig.KEY) private readonly config: ConfigType<typeof globalConfig>,
private prisma: PrismaService,
) {}
) {
this.#prismaClient = this.prisma.client;
}

async getUsers() {
return await this.prisma.user.findMany();
return await this.#prismaClient.user.findMany();
}

async getUserById(id: number) {
try {
const user = await this.prisma.user.findUnique({ where: { id } });
const user = await this.#prismaClient.user.findUnique({ where: { id } });

if (!user) {
throw new HttpException(`User with id ${id} not found`, HttpStatus.NOT_FOUND);
Expand All @@ -42,7 +46,7 @@ export class UsersService {

async getUserByEmail(email: string) {
try {
const user = await this.prisma.user.findUnique({ where: { email } });
const user = await this.#prismaClient.user.findUnique({ where: { email } });

if (!user) {
throw new HttpException(`User with email ${email} not found`, HttpStatus.NOT_FOUND);
Expand All @@ -59,30 +63,30 @@ export class UsersService {

async createUser(data: Prisma.UserCreateInput) {
const { email, password } = data;
const user = await this.prisma.user.findUnique({ where: { email } });
const user = await this.#prismaClient.user.findUnique({ where: { email } });

if (user) {
throw new HttpException(`User with email ${email} already exists`, HttpStatus.BAD_REQUEST);
}

data.password = await bcrypt.hash(password, this.config.crypt.saltRounds);

return this.prisma.user.create({ data });
return this.#prismaClient.user.create({ data });
}

async updateUser(id: number, data: Prisma.UserUpdateInput) {
const user = await this.prisma.user.findUnique({ where: { id } });
const user = await this.#prismaClient.user.findUnique({ where: { id } });

if (!user) {
throw new HttpException(`User with id ${id} not found`, HttpStatus.NOT_FOUND);
}

return this.prisma.user.update({ where: { id }, data });
return this.#prismaClient.user.update({ where: { id }, data });
}

async deleteUserById(id: number) {
await this.getUserById(id);

return await this.prisma.user.delete({ where: { id } });
return await this.#prismaClient.user.delete({ where: { id } });
}
}
Loading
Loading