Skip to content
Open
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
296 changes: 277 additions & 19 deletions backend/api-inventory/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions backend/api-inventory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@nestjs/platform-express": "^10.0.0",
"@nestjs/swagger": "^7.4.0",
"@prisma/client": "^5.18.0",
"bcrypt": "^5.1.1",
"prisma": "^5.18.0",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Warnings:

- The primary key for the `User` table will be changed. If it partially fails, the table could be left without primary key constraint.
- You are about to drop the column `name` on the `User` table. All the data in the column will be lost.
- Added the required column `password` to the `User` table without a default value. This is not possible if the table is not empty.
- Added the required column `updatedAt` to the `User` table without a default value. This is not possible if the table is not empty.
- Added the required column `username` to the `User` table without a default value. This is not possible if the table is not empty.

*/
-- AlterTable
ALTER TABLE "User" DROP CONSTRAINT "User_pkey",
DROP COLUMN "name",
ADD COLUMN "password" TEXT NOT NULL,
ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL,
ADD COLUMN "username" TEXT NOT NULL,
ALTER COLUMN "id" DROP DEFAULT,
ALTER COLUMN "id" SET DATA TYPE TEXT,
ADD CONSTRAINT "User_pkey" PRIMARY KEY ("id");
DROP SEQUENCE "User_id_seq";
8 changes: 5 additions & 3 deletions backend/api-inventory/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ datasource db {
url = env("DATABASE_URL")
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
email String @unique
username String
password String
}
model Product {
id String @id
Expand Down
5 changes: 1 addition & 4 deletions backend/api-inventory/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,5 @@ export class AppController {
console.log("hi to the cloud from health route")
return 'hello cloud'
}
@Post('/auth/register')
register() : any{
return this.appService.getlello()
}

}
4 changes: 2 additions & 2 deletions backend/api-inventory/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { AppController } from './app.controller';
import { AppService } from './app.service';

import { PmModule } from './product-management/pm.module';
import { UmModule } from './user-management/um.module';

import { PrismaService } from './prisma/prisma.service';
import { UserModule } from './user-management/um.module';

import { CartModule } from './cart-management/cm.module';
import { OrderModule } from './order-management/om.module';
Expand All @@ -13,7 +14,6 @@ import { PayModule } from './payment-processing/pay.module';

@Module({
imports: [PmModule, UmModule, CartModule, OrderModule, SmModule, PayModule],

controllers: [AppController],
providers: [AppService, PrismaService],
})
Expand Down
29 changes: 25 additions & 4 deletions backend/api-inventory/src/dto/um.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
export class Umdto{
username : string
password : string
}
export class RegisterUserDto {
username: string;
email: string;
password: string;
}

export class LoginUserDto {
email: string;
password: string;
}

export class UpdateUserDto {
username?: string;
email?: string;
password?: string;
}

export class UserResponseDto {
id: string;
username: string;
email: string;
createdAt: Date;
updatedAt: Date;
}

51 changes: 42 additions & 9 deletions backend/api-inventory/src/user-management/um.controller.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,46 @@
import { Controller, Get, Post } from '@nestjs/common';
import { UmService } from './um.service';
import { Controller, Post, Body, Get, Param, Put, Delete } from '@nestjs/common';
import { UserService } from './um.service';
import { LoginUserDto, RegisterUserDto, UpdateUserDto, UserResponseDto } from 'src/dto/um.dto';

@Controller('user')
export class UmController {
constructor(private readonly umservice : UmService) {}

@Get('/getuser')
async getRegistered(){
console.log("##################")
return this.umservice.GetUsers();
@Controller('auth')
export class UserController {
constructor(private readonly userService: UserService) {}

@Post('register')
async register(@Body('data') registerUserDto: RegisterUserDto) {
console.log("Reguest gyi ", registerUserDto)
return this.userService.registerUser(registerUserDto);
}

@Post('login')
async login(@Body('data') loginUserDto: LoginUserDto): Promise<{ token: string }> {
const token = await this.userService.loginUser(loginUserDto);
return { token };
}

@Post('logout')
async logout(@Body('userId') userId: string): Promise<void> {
return this.userService.logoutUser(userId);
}
@Get('users/:userId')
async getUser(@Param('userId') userId: string) {
return this.userService.getUserById(userId);
}

@Put('users/:userId')
async updateUser(
@Param('userId') userId: string,
@Body('data') updateUserDto: UpdateUserDto
): Promise<UserResponseDto> {
console.log(userId, ' is the user id in controller')
console.log(updateUserDto, " is the dto in service")

return this.userService.updateUser(userId, updateUserDto);
}

@Delete('users/:userId')
async deleteUser(@Param('userId') userId: string): Promise<any> {
return this.userService.deleteUser(userId);
}
}
15 changes: 8 additions & 7 deletions backend/api-inventory/src/user-management/um.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Module } from '@nestjs/common';
import { UmService } from './um.service';
import { UmController } from './um.controller';
import { PrismaService } from 'src/prisma/prisma.service';

import { PrismaService } from '../prisma/prisma.service';
import { UserController } from './um.controller';
import { UserService } from './um.service';

@Module({
imports: [],
controllers: [UmController],
providers: [UmService, PrismaService],
controllers: [UserController],
providers: [UserService, PrismaService],
})
export class UmModule {}
export class UserModule {}
98 changes: 83 additions & 15 deletions backend/api-inventory/src/user-management/um.service.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,92 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { PrismaService } from 'src/prisma/prisma.service';
import { Injectable, NotFoundException, BadRequestException } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';

import * as bcrypt from 'bcrypt';
import { LoginUserDto, RegisterUserDto, UpdateUserDto, UserResponseDto } from 'src/dto/um.dto';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class UmService {
constructor(private readonly prismaService : PrismaService){}

async GetUsers() {
console.log("get users called")
const products = await this.prismaService.user.findMany();
if (!products) {
export class UserService {
constructor(private readonly prismaService: PrismaService) {}

async registerUser(registerUserDto: RegisterUserDto) {
console.log("Reguest aa gyi ", registerUserDto)
if(!registerUserDto){
throw new BadRequestException({
success: false,
message: 'products not found'
success : false,
message : 'pls send registeration data along with that'
})
}
console.log(products, "--------------------")
const hashedPassword = await bcrypt.hash(registerUserDto.password, 10);
const user = await this.prismaService.user.create({
data: {
...registerUserDto,
password: hashedPassword,
},
});
return this.toUserResponseDto(user);
}

async loginUser(loginUserDto: LoginUserDto): Promise<string> {
const user = await this.prismaService.user.findUnique({
where: { email: loginUserDto.email },
});

if (!user || !(await bcrypt.compare(loginUserDto.password, user.password))) {
throw new BadRequestException('Invalid credentials');
}

// Simulate JWT token return (use a real JWT service in production)
return 'fake-jwt-token';
}

async logoutUser(userId: string): Promise<void> {
// Invalidate user session logic here (e.g., remove token from DB)
// This is a placeholder
}

async getUserById(userId: string) {

const user = await this.prismaService.user.findUnique({
where: { id: userId },
});

if (!user) {
throw new NotFoundException('User not found');
}

return this.toUserResponseDto(user);
}

async updateUser(userId: string, updateUserDto: UpdateUserDto): Promise<UserResponseDto> {
console.log(userId, ' is the user id in service')
console.log(updateUserDto, " is the dto in service")
const user = await this.prismaService.user.update({
where: { id: userId },
data: updateUserDto,
});

return this.toUserResponseDto(user);
}

async deleteUser(userId: string): Promise<any> {
await this.prismaService.user.delete({
where: { id: userId },
});
return {
success : true,
message : 'user deleted successfully'

}
}

private toUserResponseDto(user: any): UserResponseDto {
return {
success: true,
message: 'all products returned successfully',
data: products
id: user.id,
username: user.username,
email: user.email,
createdAt: user.createdAt,
updatedAt: user.updatedAt,
};
}
}