Skip to content

Commit

Permalink
adding change Owner -only for the owner-
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayoub Essabiry authored and Ayoub Essabiry committed Oct 13, 2023
1 parent eba62bb commit 14e2c57
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
16 changes: 16 additions & 0 deletions backend/code/src/rooms/dto/change-owner.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// import { ApiProperty } from '@nestjs/swagger';
import {
IsNotEmpty,
IsString,
} from 'class-validator';

export class ChangeOwnerDto{
// @ApiProperty()
@IsNotEmpty()
@IsString()
roomId: string;
@IsNotEmpty()
@IsString()
NewOwnerId: string;

}
10 changes: 10 additions & 0 deletions backend/code/src/rooms/rooms.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { JoinRoomDto } from './dto/join-room.dto';
import { LeaveRoomDto } from './dto/leave-room.dto';
import { DeleteRoomDto } from './dto/delete-room.dto';
import { UpdateRoomDto } from './dto/update-room.dto';
import { ChangeOwnerDto } from './dto/change-owner.dto';
import { ApiCookieAuth, ApiTags } from '@nestjs/swagger';

@ApiTags('rooms')
Expand Down Expand Up @@ -70,4 +71,13 @@ export class RoomsController {
) {
return await this.roomsService.updateRoom(roomdata, userId);
}
@Post('changeOwner')
@HttpCode(HttpStatus.OK)
@UseGuards(AtGuard)
async changeOwner(
@Body() roomdata: ChangeOwnerDto,
@GetCurrentUser('userId') userId: string,
){
return await this.roomsService.changeOwner(roomdata, userId);
}
}
15 changes: 15 additions & 0 deletions backend/code/src/rooms/rooms.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { PrismaService } from 'src/prisma/prisma.service';
import { JoinRoomDto } from './dto/join-room.dto';
import { LeaveRoomDto } from './dto/leave-room.dto';
import { DeleteRoomDto } from './dto/delete-room.dto';
import { ChangeOwnerDto } from './dto/change-owner.dto';
import * as bcrypt from 'bcrypt';
import { UpdateRoomDto } from './dto/update-room.dto';

Expand Down Expand Up @@ -123,4 +124,18 @@ export class RoomsService {
data: roomData,
});
}
async changeOwner(roomData: ChangeOwnerDto, userId: string) {
const { ownerId } = await this.prisma.room.findUnique({
where: { id: roomData.roomId },
select: { ownerId: true },
});
if (ownerId !== userId)
throw new UnauthorizedException('You are not the owner of this room');
const updateRoomOwner = await this.prisma.room.update({

Check failure on line 134 in backend/code/src/rooms/rooms.service.ts

View workflow job for this annotation

GitHub Actions / Build_backend

'updateRoomOwner' is assigned a value but never used
where: { id: roomData.roomId },
data: { owner: { connect: { userId: roomData.NewOwnerId } } }
});
return { message: 'change roomOwner successfully' };
}

}

0 comments on commit 14e2c57

Please sign in to comment.