mirror of
https://github.com/hanzoai/calendar.git
synced 2026-10-30 14:46:43 +00:00
* move SystemField to features * migrate workflow service * merge two tests for team repository * update imports and migrate team repository * migrate delegation credential repository * migrate credential repository * migrate entityPermissionUtils * migrate hashedLink service and repository * migrate membership service * update imports * remove file * migrate buildEventUrlFromBooking * migrate getAllUserBookings to features * update imports * update organizationMock * migrate slots * migrate date-ranges to schedules dir * migrate getAggregatedAvailability * fix * refactor * migrate useCreateEventType hook to features * migrate assignValueToUser * migrate validateUsername to auth features * migrate system field back to lib * migrate getLabelValueMapFromResponses back to lib * update imports * use relative path * fix type checks * fix * fix * fix tests * update gh codeowners * fix * fix
88 lines
2.8 KiB
TypeScript
88 lines
2.8 KiB
TypeScript
import { StripeBillingService } from "@calcom/features/ee/billing/stripe-billing-service";
|
|
import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service";
|
|
import { WEBAPP_URL } from "@calcom/lib/constants";
|
|
import { MembershipRepository } from "@calcom/features/membership/repositories/MembershipRepository";
|
|
import { TeamRepository } from "@calcom/features/ee/teams/repositories/TeamRepository";
|
|
import { TeamService } from "@calcom/features/ee/teams/services/teamService";
|
|
import prisma from "@calcom/prisma";
|
|
import { MembershipRole } from "@calcom/prisma/enums";
|
|
import type { TrpcSessionUser } from "@calcom/trpc/server/types";
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
import type { TBuyCreditsSchema } from "./buyCredits.schema";
|
|
|
|
type BuyCreditsOptions = {
|
|
ctx: {
|
|
user: NonNullable<TrpcSessionUser>;
|
|
};
|
|
input: TBuyCreditsSchema;
|
|
};
|
|
|
|
export const buyCreditsHandler = async ({ ctx, input }: BuyCreditsOptions) => {
|
|
if (!process.env.NEXT_PUBLIC_STRIPE_CREDITS_PRICE_ID) {
|
|
throw new TRPCError({
|
|
code: "INTERNAL_SERVER_ERROR",
|
|
message: "Credits are not enabled",
|
|
});
|
|
}
|
|
|
|
const { quantity, teamId } = input;
|
|
|
|
if (teamId) {
|
|
const team = await TeamService.fetchTeamOrThrow(teamId);
|
|
|
|
const permissionService = new PermissionCheckService();
|
|
const hasManageBillingPermission = await permissionService.checkPermission({
|
|
userId: ctx.user.id,
|
|
teamId,
|
|
permission: team.isOrganization ? "organization.manageBilling" : "team.manageBilling",
|
|
fallbackRoles: [MembershipRole.ADMIN, MembershipRole.OWNER],
|
|
});
|
|
|
|
if (!hasManageBillingPermission) {
|
|
throw new TRPCError({
|
|
code: "UNAUTHORIZED",
|
|
});
|
|
}
|
|
} else {
|
|
// if user id is part of a team, user can't buy credits for themselves
|
|
const memberships = await MembershipRepository.findAllAcceptedPublishedTeamMemberships(ctx.user.id);
|
|
|
|
if (memberships && memberships.length > 0) {
|
|
throw new TRPCError({
|
|
code: "UNAUTHORIZED",
|
|
});
|
|
}
|
|
}
|
|
|
|
let redirectUrl = `${WEBAPP_URL}/settings/billing`;
|
|
|
|
if (teamId) {
|
|
// Check if the team is an organization
|
|
const teamRepository = new TeamRepository(prisma);
|
|
const team = await teamRepository.findById({ id: teamId });
|
|
|
|
if (team?.isOrganization) {
|
|
redirectUrl = `${WEBAPP_URL}/settings/organizations/billing`;
|
|
} else {
|
|
redirectUrl = `${WEBAPP_URL}/settings/teams/${teamId}/billing`;
|
|
}
|
|
}
|
|
|
|
const billingService = new StripeBillingService();
|
|
|
|
const { checkoutUrl } = await billingService.createOneTimeCheckout({
|
|
priceId: process.env.NEXT_PUBLIC_STRIPE_CREDITS_PRICE_ID,
|
|
quantity,
|
|
successUrl: redirectUrl,
|
|
cancelUrl: redirectUrl,
|
|
metadata: {
|
|
...(teamId && { teamId: teamId.toString() }),
|
|
userId: ctx.user.id.toString(),
|
|
},
|
|
});
|
|
|
|
return { sessionUrl: checkoutUrl };
|
|
};
|