Skip to content

Commit

Permalink
Error handling and improved findMembers
Browse files Browse the repository at this point in the history
  • Loading branch information
peterlipt committed May 27, 2024
1 parent 8cc8015 commit 4357620
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 39 deletions.
111 changes: 74 additions & 37 deletions apps/backend/src/groups/groups.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,41 @@ export class GroupsService {
constructor(private readonly prisma: PrismaService) {}

async create(data: Prisma.GroupCreateInput): Promise<Group> {
return await this.prisma.group.create({ data });
try {
return await this.prisma.group.create({ data });
} catch {
throw new NotFoundException('Group could not be created');
}
}

async findAll(): Promise<Group[]> {
if (!this.prisma.group) {
throw new NotFoundException('Groups not found');
try {
return await this.prisma.group.findMany();
} catch {
throw new NotFoundException('Groups could not be found');
}
return await this.prisma.group.findMany();
}

async findOne(id: number): Promise<Group> {
const group = await this.prisma.group.findUnique({ where: { id: id } });
if (!group) {
try {
return await this.prisma.group.findUnique({ where: { id } });
} catch {
throw new NotFoundException(`Group with ID ${id} not found`);
}
return group;
}

async findMembers(id: number): Promise<User[]> {
const group = await this.prisma.group.findUnique({ where: { id }, include: { members: true } });
if (!group) {
async findMembers(id: number): Promise<{ user: User; role: Role }[]> {
const groupMembers = await this.prisma.groupMembers.findMany({
where: { groupId: id },
include: { User: true },
});
if (!groupMembers) {
throw new NotFoundException(`Group with ID ${id} not found`);
}
return this.prisma.user.findMany({ where: { id: { in: group.members.map((member) => member.userId) } } });
return groupMembers.map((groupMember) => ({
user: groupMember.User,
role: groupMember.role,
}));
}

async update(id: number, data: Prisma.GroupUpdateInput): Promise<Group> {
Expand All @@ -41,7 +52,7 @@ export class GroupsService {
}
}

async remove(id: number) {
async remove(id: number): Promise<Group> {
try {
return await this.prisma.group.delete({ where: { id } });
} catch {
Expand All @@ -50,37 +61,63 @@ export class GroupsService {
}

async addMember(groupId: number, userId: number): Promise<GroupMembers> {
const groupMember = await this.prisma.groupMembers.create({
data: {
groupId: groupId,
userId: userId,
},
});
return groupMember;
if (await this.prisma.groupMembers.findUnique({ where: { groupId_userId: { groupId, userId } } })) {
throw new NotFoundException('User is already a member of the group');
}
if (!(await this.prisma.user.findUnique({ where: { id: userId } }))) {
throw new NotFoundException('User does not exist');
}
try {
return await this.prisma.groupMembers.create({
data: {
groupId,
userId,
},
});
} catch {
throw new NotFoundException('User could not be added to the group');
}
}

async updateMember(groupId: number, userId: number, newRole: Role): Promise<GroupMembers> {
return this.prisma.groupMembers.update({
where: {
groupId_userId: {
groupId,
userId,
if (Role[newRole] === Role.ADMIN) {
throw new NotFoundException('newRole is ADMIN.');
}
if (!(await this.prisma.groupMembers.findUnique({ where: { groupId_userId: { groupId, userId } } }))) {
throw new NotFoundException(`User is not a member of the group ${newRole}`);
}
try {
return await this.prisma.groupMembers.update({
where: {
groupId_userId: {
groupId,
userId,
},
},
},
data: {
role: newRole,
},
});
data: {
role: newRole,
},
});
} catch {
throw new NotFoundException('User role could not be updated');
}
}

removeMember(groupId: number, userId: number) {
return this.prisma.groupMembers.delete({
where: {
groupId_userId: {
groupId,
userId,
async removeMember(groupId: number, userId: number): Promise<GroupMembers> {
if (!(await this.prisma.groupMembers.findUnique({ where: { groupId_userId: { groupId, userId } } }))) {
throw new NotFoundException('User is not a member of the group');
}
try {
return await this.prisma.groupMembers.delete({
where: {
groupId_userId: {
groupId,
userId,
},
},
},
});
});
} catch {
throw new NotFoundException('User could not be removed from the group');
}
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"start:frontend": "cd apps/frontend && yarn dev",
"build:backend": "cd apps/backend && yarn build",
"build:frontend": "cd apps/frontend && yarn build",
"lint": "eslint apps/* --ext .ts,.tsx --max-warnings 0",
"lint:fix": "eslint apps/* --ext .ts,.tsx --fix",
"lint": "eslint apps --ext .ts,.tsx --max-warnings 0",
"lint:fix": "eslint apps --ext .ts,.tsx --fix",
"format": "prettier --write apps",
"format:check": "prettier --check apps"
},
Expand Down

0 comments on commit 4357620

Please sign in to comment.