mirror of
https://github.com/hanzoai/calendar.git
synced 2026-10-30 14:46:43 +00:00
* add eslint config * migrate autoLock to features * migrate teamService to features * migrate userCreationService * migrate insights services to features * migrate ProfileRepository * update imports * migrate filter segmen tests * migrate filter segment repository * migrate getBusyTimes * migrate getLocaleFromRequest * refactor csvUtils * make filename clearer * migrate getLuckyUser integration test * migrate autoLock test to features * wip * refactors * migrate useBookerUrl * migrate more * wip * Migrate eventTypeRepository * membership repository * update imports * update imports * migrate * move organization repository * update imports * update imports * wip * wip * wip * wip * wip * wip * wip * wip * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix tests * fix type checks * fix * fix * migrate * update imports * fix tests * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix
77 lines
2.0 KiB
TypeScript
77 lines
2.0 KiB
TypeScript
import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service";
|
|
import { CreditsRepository } from "@calcom/lib/server/repository/credits";
|
|
import { TeamService } from "@calcom/features/ee/teams/services/teamService";
|
|
import { MembershipRole } from "@calcom/prisma/enums";
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
import type { TDownloadExpenseLogSchema } from "./downloadExpenseLog.schema";
|
|
|
|
type DownloadExpenseLogOptions = {
|
|
ctx: {
|
|
user: { id: number };
|
|
};
|
|
input: TDownloadExpenseLogSchema;
|
|
};
|
|
|
|
const headers = [
|
|
"Date",
|
|
"Credits",
|
|
"Type",
|
|
"Booking UID",
|
|
"Number of Segments",
|
|
"Call Duration",
|
|
"External Ref",
|
|
"Phone Number",
|
|
"Email",
|
|
];
|
|
|
|
export const downloadExpenseLogHandler = async ({ ctx, input }: DownloadExpenseLogOptions) => {
|
|
const { teamId, startDate, endDate } = 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",
|
|
});
|
|
}
|
|
}
|
|
|
|
const creditBalance = await CreditsRepository.findCreditBalanceWithExpenseLogs({
|
|
teamId,
|
|
userId: ctx.user.id,
|
|
startDate: new Date(startDate),
|
|
endDate: new Date(endDate),
|
|
});
|
|
|
|
if (!creditBalance) {
|
|
return { csvData: headers.join(",") };
|
|
}
|
|
|
|
const rows = creditBalance.expenseLogs.map((log) => [
|
|
log.date.toISOString(),
|
|
log.credits?.toString() ?? "",
|
|
log.creditType,
|
|
log.bookingUid ?? "",
|
|
log.smsSegments?.toString() ?? "-",
|
|
log.phoneNumber ?? "",
|
|
log.email ?? "",
|
|
log.callDuration?.toString() ?? "-",
|
|
log.externalRef ?? "-",
|
|
]);
|
|
|
|
const csvData = [headers, ...rows].map((row) => row.join(",")).join("\n");
|
|
|
|
return { csvData };
|
|
};
|