Files
calendar/packages/trpc/server/routers/viewer/attributes/toggleActive.handler.ts
T
3c1297aa72 fix: calcom trpc sessio circle dep (#19893)
* fix trpc session circle dep

* fixes trpc session cirlce dep

* fix relative imports

* fix more imports to use types and not trpc

* fix exports

* Fixed types

---------

Co-authored-by: Keith Williams <keithwillcode@gmail.com>
2025-03-19 05:50:22 -03:00

62 lines
1.3 KiB
TypeScript

import prisma from "@calcom/prisma";
import { TRPCError } from "@trpc/server";
import type { TrpcSessionUser } from "../../../types";
import type { ZToggleActiveSchema } from "./toggleActive.schema";
type GetOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
};
input: ZToggleActiveSchema;
};
const toggleActiveHandler = async ({ input, ctx }: GetOptions) => {
const org = ctx.user.organization;
if (!org.id) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "You need to be apart of an organization to use this feature",
});
}
// Ensure that this users org owns the attribute
const attribute = await prisma.attribute.findUnique({
where: {
id: input.attributeId,
teamId: org.id,
},
select: {
id: true,
enabled: true,
},
});
if (!attribute) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Attribute not found",
});
}
// Toggle the attribute
await prisma.attribute.update({
where: {
id: input.attributeId,
},
data: {
enabled: !attribute.enabled,
},
});
// Save us refetching the attribute to get the correct value
return {
...attribute,
enabled: !attribute.enabled,
};
};
export default toggleActiveHandler;