Files
cal.diy/packages/trpc/server/routers/viewer/webhook/getByViewer.handler.ts
T
aabfd3b3f6 chore: Chore team metadata table + isOrganization migration from metadata (#12828)
* Migration+schema changes

* Remove org utils from schema

* Users fixture

* Auth signup handlers

* checkIsOrg fn

* use team.isOrganization > metadata

* Add isOrganization to getTeamWithMembers query

* list handler + optimise queries

* Update trpc handlers to use team.isOrganization && orgSettings table

* Update fixture

* Fix typo in prisma.schema

* next auth options use organizationSettings instread of tema metadata

* Admin tooling

* Updating components to use orgsettings over metadata

* Add unique index + fix org lib files

* validateUsername

* Add a seperate unique constraint between isOrg and slug

* Invite Member Logic

* fix resend file

* force isOrg to exist

* Team settings

* update createOrUpdateMemberships

* fix: admin get handler

* fix: team list handler - improve select

* fix: update admin update handler

* fix: settings layout - team get select

* organziationSettings Schema + adminupdate

* organziationSettings Schema + adminupdate

* typefix onSubtmi

* Fix:team repo throwing error if no metadata is found

* fix: add onDelete to be cascade instead of restri

* fix: org - team invite e2e

* fix: team invites not working in subteam context

* rename to organizationID

* fix user fixture team names

* Fix the organization created in tests

* fix: update organization metadata

* variable rename

* Reuse isOrganization

* variable rename

* Remove unnecessary optional chaining

* Update packages/trpc/server/routers/viewer/organizations/adminUpdate.handler.ts

Co-authored-by: Hariom Balhara <hariombalhara@gmail.com>

* Update packages/trpc/server/routers/viewer/organizations/adminUpdate.handler.ts

Co-authored-by: Hariom Balhara <hariombalhara@gmail.com>

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

---------

Co-authored-by: Hariom <hariombalhara@gmail.com>
Co-authored-by: Keith Williams <keithwillcode@gmail.com>
2024-01-16 10:18:17 +00:00

159 lines
3.9 KiB
TypeScript

import { getBookerBaseUrl } from "@calcom/lib/getBookerUrl/server";
import { prisma } from "@calcom/prisma";
import type { Webhook } from "@calcom/prisma/client";
import { MembershipRole } from "@calcom/prisma/enums";
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
import { TRPCError } from "@trpc/server";
import { compareMembership } from "../eventTypes/getByViewer.handler";
type GetByViewerOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
};
};
type WebhookGroup = {
teamId?: number | null;
profile: {
slug: string | null;
name: string | null;
image?: string;
};
metadata?: {
readOnly: boolean;
};
webhooks: Webhook[];
};
export type WebhooksByViewer = {
webhookGroups: WebhookGroup[];
profiles: {
readOnly?: boolean | undefined;
slug: string | null;
name: string | null;
image?: string | undefined;
teamId: number | null | undefined;
}[];
};
const filterWebhooks = (webhook: Webhook) => {
const appIds = [
"zapier",
// Add more if needed
];
return !appIds.some((appId: string) => webhook.appId == appId);
};
export const getByViewerHandler = async ({ ctx }: GetByViewerOptions) => {
const user = await prisma.user.findUnique({
where: {
id: ctx.user.id,
},
select: {
username: true,
avatar: true,
name: true,
webhooks: true,
organizationId: true,
teams: {
where: {
accepted: true,
},
select: {
role: true,
team: {
select: {
id: true,
isOrganization: true,
name: true,
slug: true,
parentId: true,
metadata: true,
members: {
select: {
userId: true,
},
},
webhooks: true,
},
},
},
},
},
});
if (!user) {
throw new TRPCError({ code: "INTERNAL_SERVER_ERROR" });
}
let userWebhooks = user.webhooks;
userWebhooks = userWebhooks.filter(filterWebhooks);
let webhookGroups: WebhookGroup[] = [];
const bookerUrl = await getBookerBaseUrl(user);
const image = user?.username ? `${bookerUrl}/${user.username}/avatar.png` : undefined;
webhookGroups.push({
teamId: null,
profile: {
slug: user.username,
name: user.name,
image,
},
webhooks: userWebhooks,
metadata: {
readOnly: false,
},
});
const teamMemberships = user.teams.map((membership) => ({
teamId: membership.team.id,
membershipRole: membership.role,
}));
const teamWebhookGroups: WebhookGroup[] = user.teams
.filter((mmship) => {
return !mmship.team.isOrganization;
})
.map((membership) => {
const orgMembership = teamMemberships.find(
(teamM) => teamM.teamId === membership.team.parentId
)?.membershipRole;
return {
teamId: membership.team.id,
profile: {
name: membership.team.name,
slug: membership.team.slug
? !membership.team.parentId
? `/team`
: `${membership.team.slug}`
: null,
image: `${bookerUrl}/team/${membership.team.slug}/avatar.png`,
},
metadata: {
readOnly:
membership.role ===
(membership.team.parentId
? orgMembership && compareMembership(orgMembership, membership.role)
? orgMembership
: MembershipRole.MEMBER
: MembershipRole.MEMBER),
},
webhooks: membership.team.webhooks.filter(filterWebhooks),
};
});
webhookGroups = webhookGroups.concat(teamWebhookGroups);
return {
webhookGroups: webhookGroups.filter((groupBy) => !!groupBy.webhooks?.length),
profiles: webhookGroups.map((group) => ({
teamId: group.teamId,
...group.profile,
...group.metadata,
})),
};
};