feat: lock event types for org users (#14000)

* chroe: org settings schema

* wip: UI work + modal on toggle

* chore: update i18n

* fix: list handler

* fix: update handler on untoggle

* chore: extract

* feat: update handler for hide and delete logic

* chore: radio translations

* chore: fix modal state from flickering

* feat: update copy & add danger colour on delete focus

* chore: update copy

* design: remove modal icon

* chore: include more seeds

* fix: update handler

* feat: hide create for profile user

* feat: use readonly instead

* feat: hide personal event type header when read only

* chore: move away form readOnly as it was used in other ways

* chore: fix read-only bug

* fix: hide readonly create screen

* fix: show managed event types when events are locked

* fix: hide if not admin

* Update packages/trpc/server/routers/viewer/eventTypes/create.handler.ts

Co-authored-by: Carina Wollendorfer <[email protected]>

* fix: feedback

* fix: lock eventtype single switch

* fix: empty state create button

* fix: more profileGroup check outside CTA to pass value to filter too

---------

Co-authored-by: Udit Takkar <[email protected]>
Co-authored-by: Carina Wollendorfer <[email protected]>
Co-authored-by: CarinaWolli <[email protected]>
This commit is contained in:
authored and GitHub committed 2024-03-14 09:07:37 +00:00
1 parent 3aff79e7d1
commit 09ca75eb1f
15 files changed
+397 -58

No files matched your search

@@ -28,6 +28,7 @@ export const createHandler = async ({ ctx, input }: CreateOptions) => {
const userId = ctx.user.id;
const isManagedEventType = schedulingType === SchedulingType.MANAGED;
const isOrgAdmin = !!ctx.user?.organization?.isOrgAdmin;
// Get Users default conferencing app
const defaultConferencingData = userMetadataSchema.parse(ctx.user.metadata)?.defaultConferencingApp;
@@ -70,8 +71,6 @@ export const createHandler = async ({ ctx, input }: CreateOptions) => {
},
});
const isOrgAdmin = !!ctx.user?.organization?.isOrgAdmin;
if (!hasMembership?.role || !(["ADMIN", "OWNER"].includes(hasMembership.role) || isOrgAdmin)) {
console.warn(`User ${userId} does not have permission to create this new event type`);
throw new TRPCError({ code: "UNAUTHORIZED" });
@@ -85,6 +84,27 @@ export const createHandler = async ({ ctx, input }: CreateOptions) => {
data.schedulingType = schedulingType;
}
// If we are in an organization & they are not admin & they are not creating an event on a teamID
// Check if evenTypes are locked.
if (ctx.user.organizationId && !ctx.user?.organization?.isOrgAdmin && !teamId) {
const orgSettings = await ctx.prisma.organizationSettings.findUnique({
where: {
organizationId: ctx.user.organizationId,
},
select: {
lockEventTypeCreationForUsers: true,
},
});
const orgHasLockedEventTypes = !!orgSettings?.lockEventTypeCreationForUsers;
if (orgHasLockedEventTypes) {
console.warn(
`User ${userId} does not have permission to create this new event type - Locked status: ${orgHasLockedEventTypes}`
);
throw new TRPCError({ code: "UNAUTHORIZED" });
}
}
const profile = ctx.user.profile;
try {
const eventType = await EventTypeRepository.create({
@@ -44,8 +44,9 @@ export const getByViewerHandler = async ({ ctx, input }: GetByViewerOptions) =>
rateLimitingType: "common",
});
const lightProfile = ctx.user.profile;
const profile = await ProfileRepository.findByUpId(lightProfile.upId);
const parentOrgHasLockedEventTypes =
profile?.organization?.organizationSettings?.lockEventTypeCreationForUsers;
const isFilterSet = input?.filters && hasFilter(input.filters);
const isUpIdInFilter = input?.filters?.upIds?.includes(lightProfile.upId);
const shouldListUserEvents = !isFilterSet || isUpIdInFilter;
@@ -150,6 +151,7 @@ export const getByViewerHandler = async ({ ctx, input }: GetByViewerOptions) =>
slug: (typeof profile)["username"] | null;
name: (typeof profile)["name"];
image: string;
eventTypesLockedByOrg?: boolean;
};
metadata: {
membershipCount: number;
@@ -188,6 +190,7 @@ export const getByViewerHandler = async ({ ctx, input }: GetByViewerOptions) =>
avatarUrl: profile.avatarUrl,
profile: profile,
}),
eventTypesLockedByOrg: parentOrgHasLockedEventTypes,
},
eventTypes: orderBy(unmanagedEventTypes, ["position", "id"], ["desc", "asc"]),
metadata: {
@@ -29,6 +29,15 @@ export const listHandler = async ({ ctx }: ListHandlerInput) => {
},
});
const organizationSettings = await ctx.prisma.organizationSettings.findUnique({
where: {
organizationId: ctx.user.organization.id,
},
select: {
lockEventTypeCreationForUsers: true,
},
});
if (!membership) {
throw new TRPCError({
code: "BAD_REQUEST",
@@ -39,6 +48,9 @@ export const listHandler = async ({ ctx }: ListHandlerInput) => {
const metadata = teamMetadataSchema.parse(membership?.team.metadata);
return {
organizationSettings: {
lockEventTypeCreationForUsers: organizationSettings?.lockEventTypeCreationForUsers,
},
user: {
role: membership?.role,
accepted: membership?.accepted,
@@ -116,9 +116,63 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
}
}
const updatedOrganisation = await prisma.team.update({
where: { id: currentOrgId },
data,
const updatedOrganisation = await prisma.$transaction(async (tx) => {
const updatedOrganisation = await tx.team.update({
where: { id: currentOrgId },
data,
});
await tx.organizationSettings.update({
where: {
organizationId: currentOrgId,
},
data: {
lockEventTypeCreationForUsers: !!input.lockEventTypeCreation,
},
});
if (input.lockEventTypeCreation) {
switch (input.lockEventTypeCreationOptions) {
case "HIDE":
await tx.eventType.updateMany({
where: {
teamId: null, // Not assigned to a team
parentId: null, // Not a managed event type
owner: {
profiles: {
some: {
organizationId: currentOrgId,
},
},
},
},
data: {
hidden: true,
},
});
break;
case "DELETE":
await tx.eventType.deleteMany({
where: {
teamId: null, // Not assigned to a team
parentId: null, // Not a managed event type
owner: {
profiles: {
some: {
organizationId: currentOrgId,
},
},
},
},
});
break;
default:
break;
}
}
return updatedOrganisation;
});
// Sync Services: Close.com
@@ -33,6 +33,8 @@ export const ZUpdateInputSchema = z.object({
weekStart: z.string().optional(),
timeFormat: z.number().optional(),
metadata: teamMetadataSchema.unwrap().optional(),
lockEventTypeCreation: z.boolean().optional(),
lockEventTypeCreationOptions: z.enum(["DELETE", "HIDE"]).optional(),
});
export type TUpdateInputSchema = z.infer<typeof ZUpdateInputSchema>;