friend request db logic fix
This commit is contained in:
@@ -71,6 +71,7 @@ describe('FriendsService', () => {
|
||||
findMany: jest.fn(),
|
||||
update: jest.fn(),
|
||||
updateMany: jest.fn(),
|
||||
delete: jest.fn(),
|
||||
},
|
||||
friendship: {
|
||||
create: jest.fn(),
|
||||
@@ -247,6 +248,7 @@ describe('FriendsService', () => {
|
||||
const acceptedRequest = {
|
||||
...mockFriendRequest,
|
||||
status: FriendRequestStatus.ACCEPTED,
|
||||
updatedAt: expect.any(Date),
|
||||
};
|
||||
mockPrismaService.friendRequest.findUnique.mockResolvedValue(
|
||||
mockFriendRequest,
|
||||
@@ -303,22 +305,20 @@ describe('FriendsService', () => {
|
||||
const deniedRequest = {
|
||||
...mockFriendRequest,
|
||||
status: FriendRequestStatus.DENIED,
|
||||
updatedAt: expect.any(Date),
|
||||
};
|
||||
mockPrismaService.friendRequest.findUnique.mockResolvedValue(
|
||||
mockFriendRequest,
|
||||
);
|
||||
mockPrismaService.friendRequest.update.mockResolvedValue(deniedRequest);
|
||||
mockPrismaService.friendRequest.delete.mockResolvedValue(
|
||||
mockFriendRequest,
|
||||
);
|
||||
|
||||
const result = await service.denyFriendRequest('request-1', 'user-2');
|
||||
|
||||
expect(result).toEqual(deniedRequest);
|
||||
expect(mockPrismaService.friendRequest.update).toHaveBeenCalledWith({
|
||||
expect(mockPrismaService.friendRequest.delete).toHaveBeenCalledWith({
|
||||
where: { id: 'request-1' },
|
||||
data: { status: FriendRequestStatus.DENIED },
|
||||
include: {
|
||||
sender: true,
|
||||
receiver: true,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -43,17 +43,17 @@ export class FriendsService {
|
||||
const existingRequest = await this.prisma.friendRequest.findFirst({
|
||||
where: {
|
||||
OR: [
|
||||
{ senderId, receiverId, status: FriendRequestStatus.PENDING },
|
||||
{ senderId, receiverId },
|
||||
{
|
||||
senderId: receiverId,
|
||||
receiverId: senderId,
|
||||
status: FriendRequestStatus.PENDING,
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
if (existingRequest) {
|
||||
if (existingRequest.status === FriendRequestStatus.PENDING) {
|
||||
if (existingRequest.senderId === senderId) {
|
||||
throw new ConflictException(
|
||||
'You already sent a friend request to this user',
|
||||
@@ -63,6 +63,12 @@ export class FriendsService {
|
||||
'This user already sent you a friend request',
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// If there's an existing request that is not pending (accepted or denied), delete it so a new one can be created
|
||||
await this.prisma.friendRequest.delete({
|
||||
where: { id: existingRequest.id },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const friendRequest = await this.prisma.friendRequest.create({
|
||||
@@ -148,14 +154,9 @@ export class FriendsService {
|
||||
);
|
||||
}
|
||||
|
||||
const [updatedRequest] = await this.prisma.$transaction([
|
||||
this.prisma.friendRequest.update({
|
||||
await this.prisma.$transaction([
|
||||
this.prisma.friendRequest.delete({
|
||||
where: { id: requestId },
|
||||
data: { status: FriendRequestStatus.ACCEPTED },
|
||||
include: {
|
||||
sender: true,
|
||||
receiver: true,
|
||||
},
|
||||
}),
|
||||
this.prisma.friendship.create({
|
||||
data: {
|
||||
@@ -171,11 +172,18 @@ export class FriendsService {
|
||||
}),
|
||||
]);
|
||||
|
||||
// Since we deleted the request, we return the original request object but with status accepted
|
||||
const result = {
|
||||
...friendRequest,
|
||||
status: FriendRequestStatus.ACCEPTED,
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
this.logger.log(
|
||||
`Friend request ${requestId} accepted. Users ${friendRequest.senderId} and ${friendRequest.receiverId} are now friends`,
|
||||
);
|
||||
|
||||
return updatedRequest;
|
||||
return result;
|
||||
}
|
||||
|
||||
async denyFriendRequest(
|
||||
@@ -206,18 +214,20 @@ export class FriendsService {
|
||||
);
|
||||
}
|
||||
|
||||
const updatedRequest = await this.prisma.friendRequest.update({
|
||||
await this.prisma.friendRequest.delete({
|
||||
where: { id: requestId },
|
||||
data: { status: FriendRequestStatus.DENIED },
|
||||
include: {
|
||||
sender: true,
|
||||
receiver: true,
|
||||
},
|
||||
});
|
||||
|
||||
// Since we deleted the request, we return the original request object but with status denied
|
||||
const result = {
|
||||
...friendRequest,
|
||||
status: FriendRequestStatus.DENIED,
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
this.logger.log(`Friend request ${requestId} denied by user ${userId}`);
|
||||
|
||||
return updatedRequest;
|
||||
return result;
|
||||
}
|
||||
|
||||
async getFriends(userId: string) {
|
||||
|
||||
Reference in New Issue
Block a user