Skip to content

Commit

Permalink
add more functions to block service
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaid-maker committed Dec 18, 2023
1 parent 88e477b commit 2e5600d
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions lib/block-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,99 @@ export const isBlockedByUser = async (id: string) => {
return false;
}
};

export const blockUser = async (id: string) => {
const self = await getSelf();

if (self.id === id) {
throw new Error("Cannot block yourself");
}

const otherUser = await db.user.findUnique({
where: { id },
});

if (!otherUser) {
throw new Error("User not found");
}

const existingBlock = await db.block.findUnique({
where: {
blockerId_blockedId: {
blockerId: self.id,
blockedId: otherUser.id,
},
},
});

if (existingBlock) {
throw new Error("Already blocked");
}

const block = await db.block.create({
data: {
blockerId: self.id,
blockedId: otherUser.id,
},
include: {
blocked: true,
},
});

return block;
};

export const unblockUser = async (id: string) => {
const self = await getSelf();

if (self.id === id) {
throw new Error("Cannot unblock yourself");
}

const otherUser = await db.user.findUnique({
where: { id },
});

if (!otherUser) {
throw new Error("User not found");
}

const existingBlock = await db.block.findUnique({
where: {
blockerId_blockedId: {
blockerId: self.id,
blockedId: otherUser.id,
},
},
});

if (!existingBlock) {
throw new Error("Not blocked");
}

const unblock = await db.block.delete({
where: {
id: existingBlock.id,
},
include: {
blocked: true,
},
});

return unblock;
};

export const getBlockedUsers = async () => {
const self = await getSelf();

const blockedUsers = await db.block.findMany({
where: {
blockerId: self.id,
},
include: {
blocked: true,
},
});

return blockedUsers;
};

0 comments on commit 2e5600d

Please sign in to comment.