Skip to content

Commit

Permalink
check new types
Browse files Browse the repository at this point in the history
  • Loading branch information
Berat Genç committed Jan 20, 2024
1 parent 49ff619 commit e9a6ee7
Show file tree
Hide file tree
Showing 15 changed files with 178 additions and 120 deletions.
35 changes: 35 additions & 0 deletions prisma/migrations/20240119212433_cascade_added/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
-- DropForeignKey
ALTER TABLE `EmployeeOnTeams` DROP FOREIGN KEY `EmployeeOnTeams_employeeId_fkey`;

-- DropForeignKey
ALTER TABLE `EmployeeOnTeams` DROP FOREIGN KEY `EmployeeOnTeams_teamId_fkey`;

-- DropForeignKey
ALTER TABLE `Project` DROP FOREIGN KEY `Project_employeeId_fkey`;

-- DropForeignKey
ALTER TABLE `Project` DROP FOREIGN KEY `Project_teamId_fkey`;

-- DropForeignKey
ALTER TABLE `Task` DROP FOREIGN KEY `Task_projectId_fkey`;

-- DropForeignKey
ALTER TABLE `Task` DROP FOREIGN KEY `Task_userId_fkey`;

-- AddForeignKey
ALTER TABLE `EmployeeOnTeams` ADD CONSTRAINT `EmployeeOnTeams_teamId_fkey` FOREIGN KEY (`teamId`) REFERENCES `Team`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `EmployeeOnTeams` ADD CONSTRAINT `EmployeeOnTeams_employeeId_fkey` FOREIGN KEY (`employeeId`) REFERENCES `Employee`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Project` ADD CONSTRAINT `Project_teamId_fkey` FOREIGN KEY (`teamId`) REFERENCES `Team`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Project` ADD CONSTRAINT `Project_employeeId_fkey` FOREIGN KEY (`employeeId`) REFERENCES `Employee`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Task` ADD CONSTRAINT `Task_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `Employee`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Task` ADD CONSTRAINT `Task_projectId_fkey` FOREIGN KEY (`projectId`) REFERENCES `Project`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
12 changes: 6 additions & 6 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ model Team {
model EmployeeOnTeams {
teamId Int
employeeId Int
team Team @relation(fields: [teamId], references: [id])
employee Employee @relation(fields: [employeeId], references: [id])
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
employee Employee @relation(fields: [employeeId], references: [id], onDelete: Cascade)
@@id([teamId, employeeId])
}
Expand All @@ -41,8 +41,8 @@ model Project {
tasks Task[]
teamId Int
employeeId Int?
team Team @relation(fields: [teamId], references: [id])
employee Employee? @relation(fields: [employeeId], references: [id])
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
employee Employee? @relation(fields: [employeeId], references: [id], onDelete: Cascade)
}

model Task {
Expand All @@ -54,7 +54,7 @@ model Task {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
userId Int
employee Employee @relation(fields: [userId], references: [id])
employee Employee @relation(fields: [userId], references: [id], onDelete: Cascade)
projectId Int?
project Project? @relation(fields: [projectId], references: [id])
project Project? @relation(fields: [projectId], references: [id], onDelete: Cascade)
}
4 changes: 3 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import { EmployeeModule } from './employee/employee.module'
import { TasksModule } from './tasks/tasks.module'
import { TeamModule } from './team/team.module'
import { ProjectModule } from './project/project.module'
import { AuthModule } from './auth/auth.module';

@Module({
imports: [
DatabaseModule,
EmployeeModule,
TasksModule,
TeamModule,
ProjectModule
ProjectModule,
AuthModule
],
controllers: [AppController],
providers: [AppService]
Expand Down
57 changes: 57 additions & 0 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {
Body,
Controller,
Get,
Post,
Req,
Res,
UnauthorizedException
} from '@nestjs/common'
import { AuthService } from './auth.service'
import { Prisma } from '@prisma/client'
import { Request, Response } from 'express'

@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}

@Post('/signup')
async signup(
@Body() createUserDto: Prisma.EmployeeCreateInput,
@Req() request: Request
) {
const userData = await this.authService.signup(createUserDto)

// @ts-ignore-
request.session.userId = userData.userId
// @ts-ignore-
request.session.teamId = userData.teamId
}

@Post('/signin')
async login(
@Body() loginData: Partial<Prisma.EmployeeCreateInput>,
@Req() request: Request
) {
const userData = await this.authService.signin(loginData)

if (userData) {
// @ts-ignore-
request.session.userId = userData.userId
// @ts-ignore-
request.session.teamId = userData.teamId
return { teamId: userData.teamId, userId: userData.userId }
}

throw new UnauthorizedException('Invalid credentials')
}

@Get()
async logout(@Req() req: Request, @Res() res: Response) {
req.session.destroy(err => {
res
.clearCookie('sessionCookie', { domain: 'auth-test.site' })
.sendStatus(200)
})
}
}
9 changes: 9 additions & 0 deletions src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthController } from './auth.controller';

@Module({
controllers: [AuthController],
providers: [AuthService],
})
export class AuthModule {}
30 changes: 30 additions & 0 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Injectable } from '@nestjs/common'
import { Prisma } from '@prisma/client'
import { DatabaseService } from 'src/database/database.service'

@Injectable()
export class AuthService {
constructor(private readonly databaseService: DatabaseService) {}

async signup(createUserDto: Prisma.EmployeeCreateInput) {
const user = await this.databaseService.employee.create({
data: {
...createUserDto,
teams: { create: { team: { create: { name: 'My Team' } } } }
},

include: { teams: { include: { team: true } } }
})

return { userId: user.id, teamId: user.teams[0].teamId }
}

async signin({ email, password }: Partial<Prisma.EmployeeCreateInput>) {
const user = await this.databaseService.employee.findUnique({
where: { email, password },
include: { teams: { include: { team: true } } }
})

return user ? { userId: user.id, teamId: user.teams[0].teamId } : false
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
// src/session/session.module.ts

import { Module } from '@nestjs/common'
const RedisStore = require('connect-redis').default
import { createClient } from 'redis'
import * as session from 'express-session'

const redisClient = createClient({
password: '3JluMijpNIdRLNlgqVdyLE6HnhNClCTC',
Expand Down Expand Up @@ -38,7 +34,7 @@ export const sessionOptions = {
// cookie: {
// path: '/',
// // domain: "auth-test.site",
// maxAge: 1000 * 60 * 30
// maxAge: 1000 * 60 * 60
// // sameSite: "none",
// // secure: true,
// // httpOnly: true,
Expand Down
46 changes: 9 additions & 37 deletions src/employee/employee.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,64 +18,36 @@ import { Request } from 'express'
export class EmployeeController {
constructor(private readonly employeeService: EmployeeService) {}

@Post()
create(
@Body() createEmployeeDto: Prisma.EmployeeCreateInput,
@Req() request: Request
) {
request.session.userMail = createEmployeeDto.email
return this.employeeService.create(createEmployeeDto)
}

@Get('/getUserId')
findUserId(@Req() request: Request) {
if (request.session.userMail) {
const mailAdress = request.session.userMail
return this.employeeService.findUserId(mailAdress)
} else {
return 'No access'
}
}

// login(
// @Body() createEmployeeDto: Prisma.EmployeeCreateInput,
// @Req() request: Request
// ) {
// return this.employeeService.login()
// }

@Get()
findAll() {
return this.employeeService.findAll()
}

@Get('/only-mails')
@Get('/get-all-only-mails')
findAllWithMails() {
return this.employeeService.findAllWithMails()
}

@Get(':id')
@Get('/getData')
findOne(@Param('id') id: string, @Req() request: Request) {
return this.employeeService.findOne(+id)
const userId = request.session.userId
return this.employeeService.findOne(userId)
}

@Patch(':id')
@Patch('/update')
update(
@Param('id') id: number,
@Body() updateEmployeeDto: Prisma.EmployeeUpdateInput
@Body() updateEmployeeDto: Prisma.EmployeeUpdateInput,
@Req() request: Request
) {
return this.employeeService.update(Number(id), updateEmployeeDto)
const userId = request.session.userId
return this.employeeService.update(userId, updateEmployeeDto)
}

@Patch(':id/assignTask/:taskId')
assignTask(@Param('id') id: number, @Param('taskId') taskId: number) {
return this.employeeService.assignTask(+id, +taskId)
}

@Delete(':id')
remove(@Param('id') id: string) {
return this.employeeService.remove()
}
@Delete('/remove-all')
remove() {
return this.employeeService.removeAll()
Expand Down
7 changes: 4 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ import { NestFactory } from '@nestjs/core'
import { AppModule } from './app.module'

import * as session from 'express-session'
import { sessionOptions } from './session/session.module'
import { sessionOptions } from './config/sessionConfig'

async function bootstrap() {
const app = await NestFactory.create(AppModule)
app.enableCors({
origin: 'https://app.taskermanager.online',
// origin: 'http://localhost:3000',
credentials: true
})

app.set('trust proxy', 1)
app.enable('trust proxy')

app.use(session(sessionOptions))
console.log(`--- app is listening oooon ${process.env.PORT}`)
await app.listen(process.env.PORT ?? 3005)
console.log(`--- app is listening on ${process.env.PORT}`)
await app.listen(process.env.PORT ?? 3001)
}
bootstrap()
12 changes: 9 additions & 3 deletions src/project/project.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ export class ProjectController {

@Post()
create(
@Body() createProjectDto: Prisma.ProjectCreateInput,
@Body() body: { name: string; description: string },
@Req() request: Request
) {
console.log(request.session)
return this.projectService.create(createProjectDto)
//@ts-ignore
const { userId, teamId } = request.session
const data = {
...body,
teamId: teamId,
employeeId: userId
}
return this.projectService.create(data)
}

@Get()
Expand Down
7 changes: 5 additions & 2 deletions src/project/project.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DatabaseService } from 'src/database/database.service'
@Injectable()
export class ProjectService {
constructor(private readonly databaseService: DatabaseService) {}
create(createProjectDto: Prisma.ProjectCreateInput) {
create(createProjectDto) {
return this.databaseService.project.create({ data: createProjectDto })
}

Expand All @@ -14,7 +14,10 @@ export class ProjectService {
}

findOne(id: number) {
return this.databaseService.project.findUnique({ where: { id } })
return this.databaseService.project.findUnique({
where: { id },
include: { employee: true, tasks: { include: { employee: true } } }
})
}

update(id: number, updateProjectDto: Prisma.ProjectUpdateInput) {
Expand Down
20 changes: 0 additions & 20 deletions src/session/session.controller.spec.ts

This file was deleted.

18 changes: 0 additions & 18 deletions src/session/session.service.spec.ts

This file was deleted.

Loading

0 comments on commit e9a6ee7

Please sign in to comment.