Skip to content

Commit

Permalink
#115 [FIX]: Visibility of disabled protocols restricted to managers
Browse files Browse the repository at this point in the history
  • Loading branch information
IosBonaldi committed Nov 4, 2024
1 parent 849eafb commit 6fc0edd
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/controllers/protocolController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,12 @@ const validateItemValidations = async (itemType: ItemType, validations: any[]) =
};

const validateManagers = async (managers: (number | undefined)[], institutionId: number | null) => {
for (const owner of managers) {
for (const manager of managers) {
const user = await prismaClient.user.findUnique({
where: {
id: owner,
id: manager,
institutionId: institutionId,
role: {
in: [UserRole.PUBLISHER, UserRole.COORDINATOR, UserRole.ADMIN],
},
role: { in: [UserRole.PUBLISHER, UserRole.COORDINATOR, UserRole.ADMIN] },
},
});
if (!user || !institutionId) throw new Error('Managers must be publishers, coordinators or admins of the same institution.');
Expand Down Expand Up @@ -467,7 +465,7 @@ export const createProtocol = async (req: Request, res: Response) => {
description: protocol.description,
enabled: protocol.enabled,
creatorId: protocol.creatorId,
managers: { connect: protocol.managers.map((owner) => ({ id: owner })) },
managers: { connect: protocol.managers.map((manager) => ({ id: manager })) },
visibility: protocol.visibility as VisibilityMode,
applicability: protocol.applicability as VisibilityMode,
answersVisibility: protocol.answersVisibility as VisibilityMode,
Expand Down Expand Up @@ -743,7 +741,7 @@ export const updateProtocol = async (req: Request, res: Response): Promise<void>
title: protocol.title,
description: protocol.description,
enabled: protocol.enabled,
managers: { set: [], connect: protocol.managers.map((owner) => ({ id: owner })) },
managers: { set: [], connect: protocol.managers.map((manager) => ({ id: manager })) },
visibility: protocol.visibility as VisibilityMode,
applicability: protocol.applicability as VisibilityMode,
answersVisibility: protocol.answersVisibility as VisibilityMode,
Expand Down Expand Up @@ -1085,6 +1083,7 @@ export const getVisibleProtocols = async (req: Request, res: Response): Promise<
{ creatorId: user.id },
{ visibility: VisibilityMode.PUBLIC },
],
enabled: true,
},
select: fields,
});
Expand Down Expand Up @@ -1125,11 +1124,11 @@ export const getProtocol = async (req: Request, res: Response): Promise<void> =>
id: protocolId,
OR: [
{ managers: { some: { id: user.id } } },
{ appliers: { some: { id: user.id } } },
{ viewersUser: { some: { id: user.id } } },
{ viewersClassroom: { some: { users: { some: { id: user.id } } } } },
{ appliers: { some: { id: user.id } }, enabled: true },
{ viewersUser: { some: { id: user.id } }, enabled: true },
{ viewersClassroom: { some: { users: { some: { id: user.id } } } }, enabled: true },
{ creatorId: user.id },
{ visibility: VisibilityMode.PUBLIC },
{ visibility: VisibilityMode.PUBLIC, enabled: true },
],
},
select: fieldsWViewers,
Expand All @@ -1139,7 +1138,7 @@ export const getProtocol = async (req: Request, res: Response): Promise<void> =>
user.role !== UserRole.USER &&
(user.role === UserRole.ADMIN ||
protocol.creator.id === user.id ||
protocol.managers.some((owner) => owner.id === user.id) ||
protocol.managers.some((manager) => manager.id === user.id) ||
protocol.appliers.some((applier) => applier.id === user.id) ||
protocol.applicability === VisibilityMode.PUBLIC)
? protocol
Expand Down

0 comments on commit 6fc0edd

Please sign in to comment.