friend request db logic fix

This commit is contained in:
2025-12-15 21:21:44 +08:00
parent 9301df860e
commit 125d635dc2
2 changed files with 41 additions and 31 deletions

View File

@@ -71,6 +71,7 @@ describe('FriendsService', () => {
findMany: jest.fn(), findMany: jest.fn(),
update: jest.fn(), update: jest.fn(),
updateMany: jest.fn(), updateMany: jest.fn(),
delete: jest.fn(),
}, },
friendship: { friendship: {
create: jest.fn(), create: jest.fn(),
@@ -247,6 +248,7 @@ describe('FriendsService', () => {
const acceptedRequest = { const acceptedRequest = {
...mockFriendRequest, ...mockFriendRequest,
status: FriendRequestStatus.ACCEPTED, status: FriendRequestStatus.ACCEPTED,
updatedAt: expect.any(Date),
}; };
mockPrismaService.friendRequest.findUnique.mockResolvedValue( mockPrismaService.friendRequest.findUnique.mockResolvedValue(
mockFriendRequest, mockFriendRequest,
@@ -303,22 +305,20 @@ describe('FriendsService', () => {
const deniedRequest = { const deniedRequest = {
...mockFriendRequest, ...mockFriendRequest,
status: FriendRequestStatus.DENIED, status: FriendRequestStatus.DENIED,
updatedAt: expect.any(Date),
}; };
mockPrismaService.friendRequest.findUnique.mockResolvedValue( mockPrismaService.friendRequest.findUnique.mockResolvedValue(
mockFriendRequest, mockFriendRequest,
); );
mockPrismaService.friendRequest.update.mockResolvedValue(deniedRequest); mockPrismaService.friendRequest.delete.mockResolvedValue(
mockFriendRequest,
);
const result = await service.denyFriendRequest('request-1', 'user-2'); const result = await service.denyFriendRequest('request-1', 'user-2');
expect(result).toEqual(deniedRequest); expect(result).toEqual(deniedRequest);
expect(mockPrismaService.friendRequest.update).toHaveBeenCalledWith({ expect(mockPrismaService.friendRequest.delete).toHaveBeenCalledWith({
where: { id: 'request-1' }, where: { id: 'request-1' },
data: { status: FriendRequestStatus.DENIED },
include: {
sender: true,
receiver: true,
},
}); });
}); });

View File

@@ -43,17 +43,17 @@ export class FriendsService {
const existingRequest = await this.prisma.friendRequest.findFirst({ const existingRequest = await this.prisma.friendRequest.findFirst({
where: { where: {
OR: [ OR: [
{ senderId, receiverId, status: FriendRequestStatus.PENDING }, { senderId, receiverId },
{ {
senderId: receiverId, senderId: receiverId,
receiverId: senderId, receiverId: senderId,
status: FriendRequestStatus.PENDING,
}, },
], ],
}, },
}); });
if (existingRequest) { if (existingRequest) {
if (existingRequest.status === FriendRequestStatus.PENDING) {
if (existingRequest.senderId === senderId) { if (existingRequest.senderId === senderId) {
throw new ConflictException( throw new ConflictException(
'You already sent a friend request to this user', 'You already sent a friend request to this user',
@@ -63,6 +63,12 @@ export class FriendsService {
'This user already sent you a friend request', '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({ const friendRequest = await this.prisma.friendRequest.create({
@@ -148,14 +154,9 @@ export class FriendsService {
); );
} }
const [updatedRequest] = await this.prisma.$transaction([ await this.prisma.$transaction([
this.prisma.friendRequest.update({ this.prisma.friendRequest.delete({
where: { id: requestId }, where: { id: requestId },
data: { status: FriendRequestStatus.ACCEPTED },
include: {
sender: true,
receiver: true,
},
}), }),
this.prisma.friendship.create({ this.prisma.friendship.create({
data: { 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( this.logger.log(
`Friend request ${requestId} accepted. Users ${friendRequest.senderId} and ${friendRequest.receiverId} are now friends`, `Friend request ${requestId} accepted. Users ${friendRequest.senderId} and ${friendRequest.receiverId} are now friends`,
); );
return updatedRequest; return result;
} }
async denyFriendRequest( async denyFriendRequest(
@@ -206,18 +214,20 @@ export class FriendsService {
); );
} }
const updatedRequest = await this.prisma.friendRequest.update({ await this.prisma.friendRequest.delete({
where: { id: requestId }, 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}`); this.logger.log(`Friend request ${requestId} denied by user ${userId}`);
return updatedRequest; return result;
} }
async getFriends(userId: string) { async getFriends(userId: string) {