2023-03-23 22:10:01 +00:00
|
|
|
import type { Prisma } from "@prisma/client";
|
2023-04-25 22:39:47 +00:00
|
|
|
import md5 from "md5";
|
2023-03-23 22:10:01 +00:00
|
|
|
import { z } from "zod";
|
|
|
|
|
|
|
|
import dayjs from "@calcom/dayjs";
|
2023-05-09 19:27:05 +00:00
|
|
|
import authedProcedure from "@calcom/trpc/server/procedures/authedProcedure";
|
|
|
|
import { router } from "@calcom/trpc/server/trpc";
|
2023-03-23 22:10:01 +00:00
|
|
|
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
|
|
|
|
import { EventsInsights } from "./events";
|
|
|
|
|
|
|
|
const UserBelongsToTeamInput = z.object({
|
2023-04-04 11:58:19 +00:00
|
|
|
teamId: z.coerce.number().optional().nullable(),
|
2023-06-29 17:03:44 +00:00
|
|
|
isAll: z.boolean().optional(),
|
2023-03-23 22:10:01 +00:00
|
|
|
});
|
|
|
|
|
2023-05-09 19:27:05 +00:00
|
|
|
const userBelongsToTeamProcedure = authedProcedure.use(async ({ ctx, next, rawInput }) => {
|
2023-03-23 22:10:01 +00:00
|
|
|
const parse = UserBelongsToTeamInput.safeParse(rawInput);
|
|
|
|
if (!parse.success) {
|
|
|
|
throw new TRPCError({ code: "BAD_REQUEST" });
|
|
|
|
}
|
|
|
|
|
2023-04-04 11:58:19 +00:00
|
|
|
// If teamId is provided, check if user belongs to team
|
|
|
|
// If teamId is not provided, check if user belongs to any team
|
|
|
|
|
|
|
|
const membershipWhereConditional: Prisma.MembershipWhereInput = {
|
|
|
|
userId: ctx.user.id,
|
2023-06-29 17:03:44 +00:00
|
|
|
accepted: true,
|
2023-04-04 11:58:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (parse.data.teamId) {
|
|
|
|
membershipWhereConditional["teamId"] = parse.data.teamId;
|
|
|
|
}
|
|
|
|
|
|
|
|
const membership = await ctx.prisma.membership.findFirst({
|
|
|
|
where: membershipWhereConditional,
|
2023-03-23 22:10:01 +00:00
|
|
|
});
|
|
|
|
|
2023-06-29 17:03:44 +00:00
|
|
|
// Probably we couldn't find a membership because the user is not a direct member of the team
|
|
|
|
// So that would mean ctx.user.organization is present
|
|
|
|
if ((parse.data.isAll && ctx.user.organizationId) || (!membership && ctx.user.organizationId)) {
|
|
|
|
//Look for membership type in organizationId
|
|
|
|
const membershipOrg = await ctx.prisma.membership.findFirst({
|
|
|
|
where: {
|
|
|
|
userId: ctx.user.id,
|
|
|
|
teamId: ctx.user.organizationId,
|
|
|
|
accepted: true,
|
|
|
|
role: {
|
|
|
|
in: ["OWNER", "ADMIN"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (!membershipOrg) {
|
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
}
|
|
|
|
|
|
|
|
return next({
|
|
|
|
ctx: {
|
|
|
|
...ctx,
|
|
|
|
user: {
|
|
|
|
...ctx.user,
|
|
|
|
isOwnerAdminOfParentTeam: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2023-03-23 22:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
|
|
|
const UserSelect = {
|
|
|
|
id: true,
|
|
|
|
name: true,
|
|
|
|
email: true,
|
2023-06-29 17:03:44 +00:00
|
|
|
username: true,
|
2023-03-23 22:10:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const emptyResponseEventsByStatus = {
|
|
|
|
empty: true,
|
|
|
|
created: {
|
|
|
|
count: 0,
|
|
|
|
deltaPrevious: 0,
|
|
|
|
},
|
|
|
|
completed: {
|
|
|
|
count: 0,
|
|
|
|
deltaPrevious: 0,
|
|
|
|
},
|
|
|
|
rescheduled: {
|
|
|
|
count: 0,
|
|
|
|
deltaPrevious: 0,
|
|
|
|
},
|
|
|
|
cancelled: {
|
|
|
|
count: 0,
|
|
|
|
deltaPrevious: 0,
|
|
|
|
},
|
|
|
|
previousRange: {
|
|
|
|
startDate: dayjs().toISOString(),
|
|
|
|
endDate: dayjs().toISOString(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-06-29 17:03:44 +00:00
|
|
|
interface IResultTeamList {
|
|
|
|
id: number;
|
|
|
|
slug: string | null;
|
|
|
|
name: string | null;
|
|
|
|
logo: string | null;
|
|
|
|
userId?: number;
|
|
|
|
isOrg?: boolean;
|
|
|
|
}
|
|
|
|
|
2023-03-23 22:10:01 +00:00
|
|
|
export const insightsRouter = router({
|
|
|
|
eventsByStatus: userBelongsToTeamProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
teamId: z.coerce.number().optional().nullable(),
|
|
|
|
startDate: z.string(),
|
|
|
|
endDate: z.string(),
|
|
|
|
eventTypeId: z.coerce.number().optional(),
|
2023-04-04 11:58:19 +00:00
|
|
|
memberUserId: z.coerce.number().optional(),
|
2023-03-23 22:10:01 +00:00
|
|
|
userId: z.coerce.number().optional(),
|
2023-06-29 17:03:44 +00:00
|
|
|
isAll: z.boolean().optional(),
|
2023-03-23 22:10:01 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.query(async ({ ctx, input }) => {
|
2023-06-29 17:03:44 +00:00
|
|
|
const { teamId, startDate, endDate, eventTypeId, memberUserId, userId, isAll } = input;
|
2023-04-04 11:58:19 +00:00
|
|
|
if (userId && userId !== ctx.user.id) {
|
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
2023-03-23 22:10:01 +00:00
|
|
|
}
|
|
|
|
|
2023-04-19 20:14:09 +00:00
|
|
|
let whereConditional: Prisma.BookingTimeStatusWhereInput = {};
|
2023-06-29 17:03:44 +00:00
|
|
|
let teamConditional: Prisma.TeamWhereInput = {};
|
2023-03-23 22:10:01 +00:00
|
|
|
|
|
|
|
if (eventTypeId) {
|
2023-07-20 19:19:13 +00:00
|
|
|
whereConditional["OR"] = [
|
|
|
|
{
|
|
|
|
eventTypeId,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
eventParentId: eventTypeId,
|
|
|
|
},
|
|
|
|
];
|
2023-04-04 11:58:19 +00:00
|
|
|
}
|
|
|
|
if (memberUserId) {
|
|
|
|
whereConditional["userId"] = memberUserId;
|
|
|
|
}
|
|
|
|
if (userId) {
|
2023-04-19 20:14:09 +00:00
|
|
|
whereConditional["teamId"] = null;
|
2023-03-23 22:10:01 +00:00
|
|
|
whereConditional["userId"] = userId;
|
|
|
|
}
|
|
|
|
|
2023-06-29 17:03:44 +00:00
|
|
|
if (isAll && ctx.user.isOwnerAdminOfParentTeam && ctx.user.organizationId) {
|
|
|
|
const teamsFromOrg = await ctx.prisma.team.findMany({
|
|
|
|
where: {
|
|
|
|
parentId: ctx.user.organizationId,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (teamsFromOrg.length === 0) {
|
|
|
|
return emptyResponseEventsByStatus;
|
|
|
|
}
|
|
|
|
teamConditional = {
|
|
|
|
id: {
|
|
|
|
in: [ctx.user.organizationId, ...teamsFromOrg.map((t) => t.id)],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const usersFromOrg = await ctx.prisma.membership.findMany({
|
|
|
|
where: {
|
|
|
|
team: teamConditional,
|
|
|
|
accepted: true,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
userId: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const userIdsFromOrg = usersFromOrg.map((u) => u.userId);
|
|
|
|
whereConditional = {
|
|
|
|
...whereConditional,
|
|
|
|
OR: [
|
|
|
|
{
|
|
|
|
userId: {
|
|
|
|
in: userIdsFromOrg,
|
|
|
|
},
|
|
|
|
teamId: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
teamId: {
|
|
|
|
in: [ctx.user.organizationId, ...teamsFromOrg.map((t) => t.id)],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (teamId && !isAll) {
|
2023-04-04 11:58:19 +00:00
|
|
|
const usersFromTeam = await ctx.prisma.membership.findMany({
|
|
|
|
where: {
|
|
|
|
teamId: teamId,
|
2023-06-29 17:03:44 +00:00
|
|
|
accepted: true,
|
2023-04-04 11:58:19 +00:00
|
|
|
},
|
|
|
|
select: {
|
|
|
|
userId: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const userIdsFromTeam = usersFromTeam.map((u) => u.userId);
|
|
|
|
whereConditional = {
|
|
|
|
...whereConditional,
|
|
|
|
OR: [
|
|
|
|
{
|
2023-04-19 20:14:09 +00:00
|
|
|
teamId,
|
2023-04-04 11:58:19 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: {
|
|
|
|
in: userIdsFromTeam,
|
|
|
|
},
|
2023-04-19 20:14:09 +00:00
|
|
|
teamId: null,
|
2023-04-04 11:58:19 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-08-10 17:36:29 +00:00
|
|
|
const baseWhereCondition = {
|
2023-03-23 22:10:01 +00:00
|
|
|
...whereConditional,
|
|
|
|
createdAt: {
|
|
|
|
gte: new Date(startDate),
|
|
|
|
lte: new Date(endDate),
|
|
|
|
},
|
2023-08-10 17:36:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const baseBookingsCount = await EventsInsights.getBaseBookingCountForEventStatus(baseWhereCondition);
|
2023-04-04 11:58:19 +00:00
|
|
|
|
2023-03-23 22:10:01 +00:00
|
|
|
const startTimeEndTimeDiff = dayjs(endDate).diff(dayjs(startDate), "day");
|
|
|
|
|
2023-08-10 17:36:29 +00:00
|
|
|
const totalCompleted = await EventsInsights.getTotalCompletedEvents(baseWhereCondition);
|
2023-03-23 22:10:01 +00:00
|
|
|
|
2023-08-10 17:36:29 +00:00
|
|
|
const totalRescheduled = await EventsInsights.getTotalRescheduledEvents(baseWhereCondition);
|
2023-03-23 22:10:01 +00:00
|
|
|
|
2023-08-10 17:36:29 +00:00
|
|
|
const totalCancelled = await EventsInsights.getTotalCancelledEvents(baseWhereCondition);
|
2023-03-23 22:10:01 +00:00
|
|
|
|
|
|
|
const lastPeriodStartDate = dayjs(startDate).subtract(startTimeEndTimeDiff, "day");
|
|
|
|
const lastPeriodEndDate = dayjs(endDate).subtract(startTimeEndTimeDiff, "day");
|
|
|
|
|
2023-08-10 17:36:29 +00:00
|
|
|
const lastPeriodBaseCondition = {
|
2023-03-23 22:10:01 +00:00
|
|
|
...whereConditional,
|
|
|
|
createdAt: {
|
|
|
|
gte: lastPeriodStartDate.toDate(),
|
|
|
|
lte: lastPeriodEndDate.toDate(),
|
|
|
|
},
|
2023-04-19 20:14:09 +00:00
|
|
|
teamId: teamId,
|
2023-08-10 17:36:29 +00:00
|
|
|
};
|
2023-03-23 22:10:01 +00:00
|
|
|
|
2023-08-10 17:36:29 +00:00
|
|
|
const lastPeriodBaseBookingsCount = await EventsInsights.getBaseBookingCountForEventStatus(
|
|
|
|
lastPeriodBaseCondition
|
|
|
|
);
|
2023-03-23 22:10:01 +00:00
|
|
|
|
|
|
|
const lastPeriodTotalRescheduled = await EventsInsights.getTotalRescheduledEvents(
|
2023-08-10 17:36:29 +00:00
|
|
|
lastPeriodBaseCondition
|
2023-03-23 22:10:01 +00:00
|
|
|
);
|
|
|
|
|
2023-08-10 17:36:29 +00:00
|
|
|
const lastPeriodTotalCancelled = await EventsInsights.getTotalCancelledEvents(lastPeriodBaseCondition);
|
2023-04-04 11:58:19 +00:00
|
|
|
const result = {
|
2023-03-23 22:10:01 +00:00
|
|
|
empty: false,
|
|
|
|
created: {
|
2023-08-10 17:36:29 +00:00
|
|
|
count: baseBookingsCount,
|
|
|
|
deltaPrevious: EventsInsights.getPercentage(baseBookingsCount, lastPeriodBaseBookingsCount),
|
2023-03-23 22:10:01 +00:00
|
|
|
},
|
|
|
|
completed: {
|
2023-07-20 19:19:13 +00:00
|
|
|
count: totalCompleted,
|
2023-03-23 22:10:01 +00:00
|
|
|
deltaPrevious: EventsInsights.getPercentage(
|
2023-08-10 17:36:29 +00:00
|
|
|
baseBookingsCount - totalCancelled - totalRescheduled,
|
|
|
|
lastPeriodBaseBookingsCount - lastPeriodTotalCancelled - lastPeriodTotalRescheduled
|
2023-03-23 22:10:01 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
rescheduled: {
|
|
|
|
count: totalRescheduled,
|
|
|
|
deltaPrevious: EventsInsights.getPercentage(totalRescheduled, lastPeriodTotalRescheduled),
|
|
|
|
},
|
|
|
|
cancelled: {
|
|
|
|
count: totalCancelled,
|
|
|
|
deltaPrevious: EventsInsights.getPercentage(totalCancelled, lastPeriodTotalCancelled),
|
|
|
|
},
|
|
|
|
previousRange: {
|
|
|
|
startDate: lastPeriodStartDate.format("YYYY-MM-DD"),
|
|
|
|
endDate: lastPeriodEndDate.format("YYYY-MM-DD"),
|
|
|
|
},
|
|
|
|
};
|
2023-04-04 11:58:19 +00:00
|
|
|
if (
|
|
|
|
result.created.count === 0 &&
|
|
|
|
result.completed.count === 0 &&
|
|
|
|
result.rescheduled.count === 0 &&
|
|
|
|
result.cancelled.count === 0
|
|
|
|
) {
|
|
|
|
return emptyResponseEventsByStatus;
|
|
|
|
}
|
2023-07-20 19:19:13 +00:00
|
|
|
|
2023-04-04 11:58:19 +00:00
|
|
|
return result;
|
2023-03-23 22:10:01 +00:00
|
|
|
}),
|
|
|
|
eventsTimeline: userBelongsToTeamProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
2023-04-04 11:58:19 +00:00
|
|
|
teamId: z.coerce.number().optional().nullable(),
|
2023-03-23 22:10:01 +00:00
|
|
|
startDate: z.string(),
|
|
|
|
endDate: z.string(),
|
|
|
|
eventTypeId: z.coerce.number().optional(),
|
2023-04-04 11:58:19 +00:00
|
|
|
memberUserId: z.coerce.number().optional(),
|
2023-06-29 17:03:44 +00:00
|
|
|
timeView: z.enum(["week", "month", "year", "day"]),
|
2023-04-04 11:58:19 +00:00
|
|
|
userId: z.coerce.number().optional(),
|
2023-06-29 17:03:44 +00:00
|
|
|
isAll: z.boolean().optional(),
|
2023-03-23 22:10:01 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.query(async ({ ctx, input }) => {
|
|
|
|
const {
|
|
|
|
teamId,
|
|
|
|
eventTypeId,
|
2023-04-04 11:58:19 +00:00
|
|
|
memberUserId,
|
2023-06-29 17:03:44 +00:00
|
|
|
isAll,
|
|
|
|
startDate: startDateString,
|
|
|
|
endDate: endDateString,
|
2023-03-23 22:10:01 +00:00
|
|
|
timeView: inputTimeView,
|
2023-04-04 11:58:19 +00:00
|
|
|
userId: selfUserId,
|
2023-03-23 22:10:01 +00:00
|
|
|
} = input;
|
2023-04-04 11:58:19 +00:00
|
|
|
|
2023-03-23 22:10:01 +00:00
|
|
|
const startDate = dayjs(startDateString);
|
|
|
|
const endDate = dayjs(endDateString);
|
|
|
|
const user = ctx.user;
|
2023-04-04 11:58:19 +00:00
|
|
|
|
|
|
|
if (selfUserId && user?.id !== selfUserId) {
|
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!teamId && !selfUserId) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2023-06-29 17:03:44 +00:00
|
|
|
let timeView = inputTimeView;
|
|
|
|
|
|
|
|
if (timeView === "week") {
|
|
|
|
// Difference between start and end date is less than 14 days use day view
|
|
|
|
if (endDate.diff(startDate, "day") < 14) {
|
|
|
|
timeView = "day";
|
|
|
|
}
|
|
|
|
}
|
2023-03-23 22:10:01 +00:00
|
|
|
|
2023-04-19 20:14:09 +00:00
|
|
|
let whereConditional: Prisma.BookingTimeStatusWhereInput = {};
|
2023-03-23 22:10:01 +00:00
|
|
|
|
2023-06-29 17:03:44 +00:00
|
|
|
if (isAll && ctx.user.isOwnerAdminOfParentTeam && ctx.user.organizationId) {
|
|
|
|
const teamsFromOrg = await ctx.prisma.team.findMany({
|
|
|
|
where: {
|
|
|
|
parentId: user.organizationId,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const usersFromOrg = await ctx.prisma.membership.findMany({
|
|
|
|
where: {
|
|
|
|
teamId: {
|
|
|
|
in: [ctx.user.organizationId, ...teamsFromOrg.map((t) => t.id)],
|
|
|
|
},
|
|
|
|
accepted: true,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
userId: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const userIdsFromOrg = usersFromOrg.map((u) => u.userId);
|
|
|
|
|
|
|
|
whereConditional = {
|
|
|
|
OR: [
|
|
|
|
{
|
|
|
|
userId: {
|
|
|
|
in: userIdsFromOrg,
|
|
|
|
},
|
|
|
|
teamId: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
teamId: {
|
|
|
|
in: [ctx.user.organizationId, ...teamsFromOrg.map((t) => t.id)],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (teamId && !isAll) {
|
2023-04-04 11:58:19 +00:00
|
|
|
const usersFromTeam = await ctx.prisma.membership.findMany({
|
|
|
|
where: {
|
|
|
|
teamId,
|
2023-06-29 17:03:44 +00:00
|
|
|
accepted: true,
|
2023-04-04 11:58:19 +00:00
|
|
|
},
|
|
|
|
select: {
|
|
|
|
userId: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const userIdsFromTeams = usersFromTeam.map((u) => u.userId);
|
|
|
|
|
|
|
|
whereConditional = {
|
|
|
|
OR: [
|
|
|
|
{
|
2023-04-19 20:14:09 +00:00
|
|
|
teamId,
|
2023-04-04 11:58:19 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: {
|
|
|
|
in: userIdsFromTeams,
|
|
|
|
},
|
2023-04-19 20:14:09 +00:00
|
|
|
teamId: null,
|
2023-04-04 11:58:19 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memberUserId) {
|
2023-03-23 22:10:01 +00:00
|
|
|
whereConditional = {
|
|
|
|
...whereConditional,
|
2023-04-04 11:58:19 +00:00
|
|
|
userId: memberUserId,
|
2023-03-23 22:10:01 +00:00
|
|
|
};
|
|
|
|
}
|
2023-04-04 11:58:19 +00:00
|
|
|
|
2023-03-23 22:10:01 +00:00
|
|
|
if (eventTypeId && !!whereConditional) {
|
|
|
|
whereConditional = {
|
2023-07-20 19:19:13 +00:00
|
|
|
OR: [
|
|
|
|
{
|
|
|
|
eventTypeId,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
eventParentId: eventTypeId,
|
|
|
|
},
|
|
|
|
],
|
2023-03-23 22:10:01 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-04 11:58:19 +00:00
|
|
|
if (selfUserId && !!whereConditional) {
|
|
|
|
// In this delete we are deleting the teamId filter
|
|
|
|
whereConditional["userId"] = selfUserId;
|
2023-04-19 20:14:09 +00:00
|
|
|
whereConditional["teamId"] = null;
|
2023-04-04 11:58:19 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 22:10:01 +00:00
|
|
|
// Get timeline data
|
|
|
|
const timeline = await EventsInsights.getTimeLine(timeView, dayjs(startDate), dayjs(endDate));
|
|
|
|
|
|
|
|
// iterate timeline and fetch data
|
|
|
|
if (!timeline) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const dateFormat: string = timeView === "year" ? "YYYY" : timeView === "month" ? "MMM YYYY" : "ll";
|
|
|
|
const result = [];
|
|
|
|
|
|
|
|
for (const date of timeline) {
|
|
|
|
const EventData = {
|
|
|
|
Month: dayjs(date).format(dateFormat),
|
|
|
|
Created: 0,
|
|
|
|
Completed: 0,
|
|
|
|
Rescheduled: 0,
|
|
|
|
Cancelled: 0,
|
|
|
|
};
|
2023-06-29 17:03:44 +00:00
|
|
|
const startOfEndOf = timeView;
|
|
|
|
let startDate = dayjs(date).startOf(startOfEndOf);
|
|
|
|
let endDate = dayjs(date).endOf(startOfEndOf);
|
|
|
|
if (timeView === "week") {
|
|
|
|
startDate = dayjs(date).startOf("day");
|
|
|
|
endDate = dayjs(date).add(6, "day").endOf("day");
|
|
|
|
}
|
2023-03-23 22:10:01 +00:00
|
|
|
const promisesResult = await Promise.all([
|
|
|
|
EventsInsights.getCreatedEventsInTimeRange(
|
|
|
|
{
|
|
|
|
start: startDate,
|
|
|
|
end: endDate,
|
|
|
|
},
|
|
|
|
whereConditional
|
|
|
|
),
|
|
|
|
EventsInsights.getCompletedEventsInTimeRange(
|
|
|
|
{
|
|
|
|
start: startDate,
|
|
|
|
end: endDate,
|
|
|
|
},
|
|
|
|
whereConditional
|
|
|
|
),
|
|
|
|
EventsInsights.getRescheduledEventsInTimeRange(
|
|
|
|
{
|
|
|
|
start: startDate,
|
|
|
|
end: endDate,
|
|
|
|
},
|
|
|
|
whereConditional
|
|
|
|
),
|
|
|
|
EventsInsights.getCancelledEventsInTimeRange(
|
|
|
|
{
|
|
|
|
start: startDate,
|
|
|
|
end: endDate,
|
|
|
|
},
|
|
|
|
whereConditional
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
EventData["Created"] = promisesResult[0];
|
|
|
|
EventData["Completed"] = promisesResult[1];
|
|
|
|
EventData["Rescheduled"] = promisesResult[2];
|
|
|
|
EventData["Cancelled"] = promisesResult[3];
|
|
|
|
result.push(EventData);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}),
|
|
|
|
popularEventTypes: userBelongsToTeamProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
2023-04-04 11:58:19 +00:00
|
|
|
memberUserId: z.coerce.number().optional(),
|
2023-03-23 22:10:01 +00:00
|
|
|
teamId: z.coerce.number().optional().nullable(),
|
|
|
|
startDate: z.string(),
|
|
|
|
endDate: z.string(),
|
2023-04-04 11:58:19 +00:00
|
|
|
userId: z.coerce.number().optional(),
|
2023-06-29 17:03:44 +00:00
|
|
|
isAll: z.boolean().optional(),
|
2023-03-23 22:10:01 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.query(async ({ ctx, input }) => {
|
2023-06-29 17:03:44 +00:00
|
|
|
const { teamId, startDate, endDate, memberUserId, userId, isAll } = input;
|
2023-04-04 11:58:19 +00:00
|
|
|
|
2023-03-23 22:10:01 +00:00
|
|
|
const user = ctx.user;
|
|
|
|
|
2023-04-04 11:58:19 +00:00
|
|
|
if (userId && user?.id !== userId) {
|
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
2023-03-23 22:10:01 +00:00
|
|
|
}
|
|
|
|
|
2023-04-04 11:58:19 +00:00
|
|
|
if (!teamId && !userId) {
|
|
|
|
return [];
|
|
|
|
}
|
2023-03-23 22:10:01 +00:00
|
|
|
|
2023-04-19 20:14:09 +00:00
|
|
|
let bookingWhere: Prisma.BookingTimeStatusWhereInput = {
|
2023-03-23 22:10:01 +00:00
|
|
|
createdAt: {
|
|
|
|
gte: dayjs(startDate).startOf("day").toDate(),
|
|
|
|
lte: dayjs(endDate).endOf("day").toDate(),
|
|
|
|
},
|
|
|
|
};
|
2023-06-29 17:03:44 +00:00
|
|
|
|
|
|
|
if (isAll && ctx.user.isOwnerAdminOfParentTeam && ctx.user.organizationId) {
|
|
|
|
const teamsFromOrg = await ctx.prisma.team.findMany({
|
|
|
|
where: {
|
|
|
|
parentId: user.organizationId,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const usersFromOrg = await ctx.prisma.membership.findMany({
|
|
|
|
where: {
|
|
|
|
teamId: {
|
|
|
|
in: [ctx.user.organizationId, ...teamsFromOrg.map((t) => t.id)],
|
|
|
|
},
|
|
|
|
accepted: true,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
userId: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const userIdsFromOrg = usersFromOrg.map((u) => u.userId);
|
|
|
|
|
|
|
|
bookingWhere = {
|
|
|
|
...bookingWhere,
|
|
|
|
OR: [
|
|
|
|
{
|
|
|
|
userId: {
|
|
|
|
in: userIdsFromOrg,
|
|
|
|
},
|
|
|
|
teamId: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
teamId: {
|
|
|
|
in: [ctx.user.organizationId, ...teamsFromOrg.map((t) => t.id)],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (teamId && !isAll) {
|
2023-04-04 11:58:19 +00:00
|
|
|
const usersFromTeam = await ctx.prisma.membership.findMany({
|
|
|
|
where: {
|
|
|
|
teamId,
|
2023-06-29 17:03:44 +00:00
|
|
|
accepted: true,
|
2023-04-04 11:58:19 +00:00
|
|
|
},
|
|
|
|
select: {
|
|
|
|
userId: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const userIdsFromTeams = usersFromTeam.map((u) => u.userId);
|
|
|
|
|
|
|
|
bookingWhere = {
|
|
|
|
...bookingWhere,
|
|
|
|
OR: [
|
|
|
|
{
|
2023-04-19 20:14:09 +00:00
|
|
|
teamId,
|
2023-04-04 11:58:19 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: {
|
|
|
|
in: userIdsFromTeams,
|
|
|
|
},
|
2023-04-19 20:14:09 +00:00
|
|
|
teamId: null,
|
2023-04-04 11:58:19 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
2023-03-23 22:10:01 +00:00
|
|
|
|
|
|
|
if (userId) {
|
|
|
|
bookingWhere.userId = userId;
|
2023-04-04 11:58:19 +00:00
|
|
|
// Don't take bookings from any team
|
2023-04-19 20:14:09 +00:00
|
|
|
bookingWhere.teamId = null;
|
2023-03-23 22:10:01 +00:00
|
|
|
}
|
|
|
|
|
2023-04-04 11:58:19 +00:00
|
|
|
if (memberUserId) {
|
|
|
|
bookingWhere.userId = memberUserId;
|
|
|
|
}
|
|
|
|
|
2023-04-19 20:14:09 +00:00
|
|
|
const bookingsFromSelected = await ctx.prisma.bookingTimeStatus.groupBy({
|
2023-03-23 22:10:01 +00:00
|
|
|
by: ["eventTypeId"],
|
|
|
|
where: bookingWhere,
|
|
|
|
_count: {
|
|
|
|
id: true,
|
|
|
|
},
|
|
|
|
orderBy: {
|
|
|
|
_count: {
|
|
|
|
id: "desc",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
take: 10,
|
|
|
|
});
|
2023-04-04 11:58:19 +00:00
|
|
|
|
|
|
|
const eventTypeIds = bookingsFromSelected
|
2023-03-23 22:10:01 +00:00
|
|
|
.filter((booking) => typeof booking.eventTypeId === "number")
|
|
|
|
.map((booking) => booking.eventTypeId);
|
2023-04-04 11:58:19 +00:00
|
|
|
|
|
|
|
const eventTypeWhereConditional: Prisma.EventTypeWhereInput = {
|
|
|
|
id: {
|
|
|
|
in: eventTypeIds as number[],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const eventTypesFrom = await ctx.prisma.eventType.findMany({
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
title: true,
|
|
|
|
teamId: true,
|
|
|
|
userId: true,
|
|
|
|
slug: true,
|
|
|
|
users: {
|
|
|
|
select: {
|
|
|
|
username: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
team: {
|
|
|
|
select: {
|
|
|
|
slug: true,
|
|
|
|
},
|
2023-03-23 22:10:01 +00:00
|
|
|
},
|
|
|
|
},
|
2023-04-04 11:58:19 +00:00
|
|
|
where: eventTypeWhereConditional,
|
2023-03-23 22:10:01 +00:00
|
|
|
});
|
|
|
|
|
2023-04-04 11:58:19 +00:00
|
|
|
const eventTypeHashMap: Map<
|
|
|
|
number,
|
|
|
|
Prisma.EventTypeGetPayload<{
|
|
|
|
select: {
|
|
|
|
id: true;
|
|
|
|
title: true;
|
|
|
|
teamId: true;
|
|
|
|
userId: true;
|
|
|
|
slug: true;
|
|
|
|
users: {
|
|
|
|
select: {
|
|
|
|
username: true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
team: {
|
|
|
|
select: {
|
|
|
|
slug: true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}>
|
|
|
|
> = new Map();
|
|
|
|
eventTypesFrom.forEach((eventType) => {
|
|
|
|
eventTypeHashMap.set(eventType.id, eventType);
|
2023-03-23 22:10:01 +00:00
|
|
|
});
|
|
|
|
|
2023-04-04 11:58:19 +00:00
|
|
|
const result = bookingsFromSelected.map((booking) => {
|
|
|
|
const eventTypeSelected = eventTypeHashMap.get(booking.eventTypeId ?? 0);
|
|
|
|
if (!eventTypeSelected) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
let eventSlug = "";
|
|
|
|
if (eventTypeSelected.userId) {
|
2023-04-07 07:13:22 +00:00
|
|
|
eventSlug = `${eventTypeSelected?.users[0]?.username}/${eventTypeSelected?.slug}`;
|
2023-04-04 11:58:19 +00:00
|
|
|
}
|
|
|
|
if (eventTypeSelected?.team && eventTypeSelected?.team?.slug) {
|
|
|
|
eventSlug = `${eventTypeSelected.team.slug}/${eventTypeSelected.slug}`;
|
|
|
|
}
|
2023-03-23 22:10:01 +00:00
|
|
|
return {
|
|
|
|
eventTypeId: booking.eventTypeId,
|
2023-04-04 11:58:19 +00:00
|
|
|
eventTypeName: eventSlug,
|
2023-03-23 22:10:01 +00:00
|
|
|
count: booking._count.id,
|
|
|
|
};
|
|
|
|
});
|
2023-04-04 11:58:19 +00:00
|
|
|
|
2023-03-23 22:10:01 +00:00
|
|
|
return result;
|
|
|
|
}),
|
|
|
|
averageEventDuration: userBelongsToTeamProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
2023-04-04 11:58:19 +00:00
|
|
|
memberUserId: z.coerce.number().optional(),
|
2023-03-23 22:10:01 +00:00
|
|
|
teamId: z.coerce.number().optional().nullable(),
|
|
|
|
startDate: z.string(),
|
|
|
|
endDate: z.string(),
|
2023-04-04 11:58:19 +00:00
|
|
|
userId: z.coerce.number().optional(),
|
2023-06-29 17:03:44 +00:00
|
|
|
isAll: z.boolean().optional(),
|
2023-03-23 22:10:01 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.query(async ({ ctx, input }) => {
|
2023-06-29 17:03:44 +00:00
|
|
|
const {
|
|
|
|
teamId,
|
|
|
|
startDate: startDateString,
|
|
|
|
endDate: endDateString,
|
|
|
|
memberUserId,
|
|
|
|
userId,
|
|
|
|
isAll,
|
|
|
|
} = input;
|
2023-03-23 22:10:01 +00:00
|
|
|
|
2023-04-04 11:58:19 +00:00
|
|
|
if (userId && ctx.user?.id !== userId) {
|
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!teamId && !userId) {
|
2023-03-23 22:10:01 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const startDate = dayjs(startDateString);
|
|
|
|
const endDate = dayjs(endDateString);
|
|
|
|
|
2023-04-19 20:14:09 +00:00
|
|
|
let whereConditional: Prisma.BookingTimeStatusWhereInput = {
|
2023-03-23 22:10:01 +00:00
|
|
|
createdAt: {
|
|
|
|
gte: dayjs(startDate).startOf("day").toDate(),
|
|
|
|
lte: dayjs(endDate).endOf("day").toDate(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
if (userId) {
|
2023-04-19 20:14:09 +00:00
|
|
|
delete whereConditional.teamId;
|
2023-03-23 22:10:01 +00:00
|
|
|
whereConditional["userId"] = userId;
|
|
|
|
}
|
|
|
|
|
2023-06-29 17:03:44 +00:00
|
|
|
if (isAll && ctx.user.isOwnerAdminOfParentTeam && ctx.user.organizationId) {
|
|
|
|
const teamsFromOrg = await ctx.prisma.team.findMany({
|
|
|
|
where: {
|
|
|
|
parentId: ctx.user?.organizationId,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
whereConditional = {
|
|
|
|
...whereConditional,
|
|
|
|
OR: [
|
|
|
|
{
|
|
|
|
teamId: {
|
|
|
|
in: [ctx.user?.organizationId, ...teamsFromOrg.map((t) => t.id)],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: ctx.user?.id,
|
|
|
|
teamId: null,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (teamId && !isAll) {
|
2023-04-04 11:58:19 +00:00
|
|
|
const usersFromTeam = await ctx.prisma.membership.findMany({
|
|
|
|
where: {
|
|
|
|
teamId,
|
2023-06-29 17:03:44 +00:00
|
|
|
accepted: true,
|
2023-04-04 11:58:19 +00:00
|
|
|
},
|
|
|
|
select: {
|
|
|
|
userId: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const userIdsFromTeams = usersFromTeam.map((u) => u.userId);
|
|
|
|
|
|
|
|
whereConditional = {
|
|
|
|
...whereConditional,
|
|
|
|
OR: [
|
|
|
|
{
|
2023-04-19 20:14:09 +00:00
|
|
|
teamId,
|
2023-04-04 11:58:19 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: {
|
|
|
|
in: userIdsFromTeams,
|
|
|
|
},
|
2023-04-19 20:14:09 +00:00
|
|
|
teamId: null,
|
2023-04-04 11:58:19 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memberUserId) {
|
|
|
|
whereConditional = {
|
|
|
|
userId: memberUserId,
|
2023-04-19 20:14:09 +00:00
|
|
|
teamId,
|
2023-04-04 11:58:19 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-03-23 22:10:01 +00:00
|
|
|
const timeView = EventsInsights.getTimeView("week", startDate, endDate);
|
|
|
|
const timeLine = await EventsInsights.getTimeLine("week", startDate, endDate);
|
|
|
|
|
|
|
|
if (!timeLine) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const dateFormat = "ll";
|
|
|
|
|
|
|
|
const result = [];
|
|
|
|
|
|
|
|
for (const date of timeLine) {
|
|
|
|
const EventData = {
|
|
|
|
Date: dayjs(date).format(dateFormat),
|
|
|
|
Average: 0,
|
|
|
|
};
|
|
|
|
const startOfEndOf = timeView === "year" ? "year" : timeView === "month" ? "month" : "week";
|
|
|
|
|
|
|
|
const startDate = dayjs(date).startOf(startOfEndOf);
|
|
|
|
const endDate = dayjs(date).endOf(startOfEndOf);
|
|
|
|
|
2023-04-19 20:14:09 +00:00
|
|
|
const bookingsInTimeRange = await ctx.prisma.bookingTimeStatus.findMany({
|
|
|
|
select: {
|
|
|
|
eventLength: true,
|
|
|
|
},
|
2023-03-23 22:10:01 +00:00
|
|
|
where: {
|
|
|
|
...whereConditional,
|
|
|
|
createdAt: {
|
|
|
|
gte: startDate.toDate(),
|
|
|
|
lte: endDate.toDate(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const avgDuration =
|
|
|
|
bookingsInTimeRange.reduce((acc, booking) => {
|
2023-04-19 20:14:09 +00:00
|
|
|
const duration = booking.eventLength || 0;
|
2023-03-23 22:10:01 +00:00
|
|
|
return acc + duration;
|
|
|
|
}, 0) / bookingsInTimeRange.length;
|
|
|
|
|
|
|
|
EventData["Average"] = Number(avgDuration) || 0;
|
|
|
|
result.push(EventData);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}),
|
|
|
|
membersWithMostBookings: userBelongsToTeamProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
2023-06-29 17:03:44 +00:00
|
|
|
teamId: z.coerce.number().nullable().optional(),
|
2023-03-23 22:10:01 +00:00
|
|
|
startDate: z.string(),
|
|
|
|
endDate: z.string(),
|
|
|
|
eventTypeId: z.coerce.number().optional(),
|
2023-06-29 17:03:44 +00:00
|
|
|
isAll: z.boolean().optional(),
|
2023-03-23 22:10:01 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.query(async ({ ctx, input }) => {
|
2023-06-29 17:03:44 +00:00
|
|
|
const { teamId, startDate, endDate, eventTypeId, isAll } = input;
|
|
|
|
|
2023-03-23 22:10:01 +00:00
|
|
|
if (!teamId) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const user = ctx.user;
|
|
|
|
|
2023-04-19 20:14:09 +00:00
|
|
|
const bookingWhere: Prisma.BookingTimeStatusWhereInput = {
|
|
|
|
teamId,
|
2023-04-04 11:58:19 +00:00
|
|
|
createdAt: {
|
|
|
|
gte: dayjs(startDate).startOf("day").toDate(),
|
|
|
|
lte: dayjs(endDate).endOf("day").toDate(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-04-19 20:14:09 +00:00
|
|
|
if (eventTypeId) {
|
2023-07-20 19:19:13 +00:00
|
|
|
bookingWhere["OR"] = [
|
|
|
|
{
|
|
|
|
eventTypeId,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
eventParentId: eventTypeId,
|
|
|
|
},
|
|
|
|
];
|
2023-04-19 20:14:09 +00:00
|
|
|
}
|
|
|
|
|
2023-06-29 17:03:44 +00:00
|
|
|
if (isAll && user.isOwnerAdminOfParentTeam && user.organizationId) {
|
|
|
|
delete bookingWhere.teamId;
|
|
|
|
const teamsFromOrg = await ctx.prisma.team.findMany({
|
|
|
|
where: {
|
|
|
|
parentId: user?.organizationId,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const usersFromTeam = await ctx.prisma.membership.findMany({
|
|
|
|
where: {
|
|
|
|
teamId: {
|
|
|
|
in: [user?.organizationId, ...teamsFromOrg.map((t) => t.id)],
|
|
|
|
},
|
|
|
|
accepted: true,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
userId: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
bookingWhere["OR"] = [
|
|
|
|
{
|
|
|
|
teamId: {
|
|
|
|
in: [user?.organizationId, ...teamsFromOrg.map((t) => t.id)],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: {
|
|
|
|
in: usersFromTeam.map((u) => u.userId),
|
|
|
|
},
|
|
|
|
teamId: null,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (teamId && !isAll) {
|
2023-04-04 11:58:19 +00:00
|
|
|
const usersFromTeam = await ctx.prisma.membership.findMany({
|
|
|
|
where: {
|
|
|
|
teamId,
|
2023-06-29 17:03:44 +00:00
|
|
|
accepted: true,
|
2023-04-04 11:58:19 +00:00
|
|
|
},
|
|
|
|
select: {
|
|
|
|
userId: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const userIdsFromTeams = usersFromTeam.map((u) => u.userId);
|
2023-04-19 20:14:09 +00:00
|
|
|
delete bookingWhere.eventTypeId;
|
|
|
|
delete bookingWhere.teamId;
|
2023-04-04 11:58:19 +00:00
|
|
|
bookingWhere["OR"] = [
|
|
|
|
{
|
2023-04-19 20:14:09 +00:00
|
|
|
teamId,
|
2023-04-04 11:58:19 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: {
|
|
|
|
in: userIdsFromTeams,
|
|
|
|
},
|
2023-04-19 20:14:09 +00:00
|
|
|
teamId: null,
|
2023-04-04 11:58:19 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-04-19 20:14:09 +00:00
|
|
|
const bookingsFromTeam = await ctx.prisma.bookingTimeStatus.groupBy({
|
2023-03-23 22:10:01 +00:00
|
|
|
by: ["userId"],
|
2023-04-04 11:58:19 +00:00
|
|
|
where: bookingWhere,
|
2023-03-23 22:10:01 +00:00
|
|
|
_count: {
|
|
|
|
id: true,
|
|
|
|
},
|
|
|
|
orderBy: {
|
|
|
|
_count: {
|
|
|
|
id: "desc",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
take: 10,
|
|
|
|
});
|
2023-04-04 11:58:19 +00:00
|
|
|
|
2023-03-23 22:10:01 +00:00
|
|
|
const userIds = bookingsFromTeam
|
|
|
|
.filter((booking) => typeof booking.userId === "number")
|
|
|
|
.map((booking) => booking.userId);
|
2023-04-04 11:58:19 +00:00
|
|
|
if (userIds.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
2023-06-29 17:03:44 +00:00
|
|
|
|
2023-03-23 22:10:01 +00:00
|
|
|
const usersFromTeam = await ctx.prisma.user.findMany({
|
|
|
|
where: {
|
|
|
|
id: {
|
|
|
|
in: userIds as number[],
|
|
|
|
},
|
|
|
|
},
|
2023-06-29 17:03:44 +00:00
|
|
|
select: UserSelect,
|
2023-03-23 22:10:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const userHashMap = new Map();
|
|
|
|
usersFromTeam.forEach((user) => {
|
|
|
|
userHashMap.set(user.id, user);
|
|
|
|
});
|
|
|
|
|
|
|
|
const result = bookingsFromTeam.map((booking) => {
|
|
|
|
return {
|
|
|
|
userId: booking.userId,
|
|
|
|
user: userHashMap.get(booking.userId),
|
2023-04-25 22:39:47 +00:00
|
|
|
emailMd5: md5(user?.email),
|
2023-03-23 22:10:01 +00:00
|
|
|
count: booking._count.id,
|
|
|
|
};
|
|
|
|
});
|
2023-04-04 11:58:19 +00:00
|
|
|
|
2023-03-23 22:10:01 +00:00
|
|
|
return result;
|
|
|
|
}),
|
|
|
|
membersWithLeastBookings: userBelongsToTeamProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
2023-06-29 17:03:44 +00:00
|
|
|
teamId: z.coerce.number().nullable().optional(),
|
2023-03-23 22:10:01 +00:00
|
|
|
startDate: z.string(),
|
|
|
|
endDate: z.string(),
|
|
|
|
eventTypeId: z.coerce.number().optional(),
|
2023-06-29 17:03:44 +00:00
|
|
|
isAll: z.boolean().optional(),
|
2023-03-23 22:10:01 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.query(async ({ ctx, input }) => {
|
2023-06-29 17:03:44 +00:00
|
|
|
const { teamId, startDate, endDate, eventTypeId, isAll } = input;
|
2023-03-23 22:10:01 +00:00
|
|
|
if (!teamId) {
|
|
|
|
return [];
|
|
|
|
}
|
2023-04-04 11:58:19 +00:00
|
|
|
const user = ctx.user;
|
2023-03-23 22:10:01 +00:00
|
|
|
|
2023-04-19 20:14:09 +00:00
|
|
|
const bookingWhere: Prisma.BookingTimeStatusWhereInput = {
|
2023-06-29 17:03:44 +00:00
|
|
|
teamId,
|
2023-04-19 20:14:09 +00:00
|
|
|
eventTypeId,
|
2023-04-04 11:58:19 +00:00
|
|
|
createdAt: {
|
|
|
|
gte: dayjs(startDate).startOf("day").toDate(),
|
|
|
|
lte: dayjs(endDate).endOf("day").toDate(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-06-29 17:03:44 +00:00
|
|
|
if (isAll && user.isOwnerAdminOfParentTeam) {
|
|
|
|
delete bookingWhere.teamId;
|
|
|
|
const teamsFromOrg = await ctx.prisma.team.findMany({
|
|
|
|
where: {
|
|
|
|
parentId: user?.organizationId,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const usersFromTeam = await ctx.prisma.membership.findMany({
|
|
|
|
where: {
|
|
|
|
teamId: {
|
|
|
|
in: teamsFromOrg.map((t) => t.id),
|
|
|
|
},
|
|
|
|
accepted: true,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
userId: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
bookingWhere["OR"] = [
|
|
|
|
{
|
|
|
|
teamId: {
|
|
|
|
in: teamsFromOrg.map((t) => t.id),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: {
|
|
|
|
in: usersFromTeam.map((u) => u.userId),
|
|
|
|
},
|
|
|
|
teamId: null,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (teamId && !isAll) {
|
2023-04-04 11:58:19 +00:00
|
|
|
const usersFromTeam = await ctx.prisma.membership.findMany({
|
|
|
|
where: {
|
|
|
|
teamId,
|
2023-06-29 17:03:44 +00:00
|
|
|
accepted: true,
|
2023-04-04 11:58:19 +00:00
|
|
|
},
|
|
|
|
select: {
|
|
|
|
userId: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const userIdsFromTeams = usersFromTeam.map((u) => u.userId);
|
|
|
|
bookingWhere["OR"] = [
|
|
|
|
{
|
2023-04-19 20:14:09 +00:00
|
|
|
teamId,
|
2023-04-04 11:58:19 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
userId: {
|
|
|
|
in: userIdsFromTeams,
|
|
|
|
},
|
2023-04-19 20:14:09 +00:00
|
|
|
teamId: null,
|
2023-04-04 11:58:19 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-04-19 20:14:09 +00:00
|
|
|
const bookingsFromTeam = await ctx.prisma.bookingTimeStatus.groupBy({
|
2023-03-23 22:10:01 +00:00
|
|
|
by: ["userId"],
|
2023-04-04 11:58:19 +00:00
|
|
|
where: bookingWhere,
|
2023-03-23 22:10:01 +00:00
|
|
|
_count: {
|
|
|
|
id: true,
|
|
|
|
},
|
|
|
|
orderBy: {
|
|
|
|
_count: {
|
|
|
|
id: "asc",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
take: 10,
|
|
|
|
});
|
|
|
|
|
|
|
|
const userIds = bookingsFromTeam
|
|
|
|
.filter((booking) => typeof booking.userId === "number")
|
|
|
|
.map((booking) => booking.userId);
|
2023-04-04 11:58:19 +00:00
|
|
|
if (userIds.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const usersFromTeam = await ctx.prisma.user.findMany({
|
2023-03-23 22:10:01 +00:00
|
|
|
where: {
|
|
|
|
id: {
|
2023-04-04 11:58:19 +00:00
|
|
|
in: userIds as number[],
|
2023-03-23 22:10:01 +00:00
|
|
|
},
|
|
|
|
},
|
2023-06-29 17:03:44 +00:00
|
|
|
select: UserSelect,
|
2023-03-23 22:10:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const userHashMap = new Map();
|
2023-04-04 11:58:19 +00:00
|
|
|
usersFromTeam.forEach((user) => {
|
2023-03-23 22:10:01 +00:00
|
|
|
userHashMap.set(user.id, user);
|
|
|
|
});
|
|
|
|
|
|
|
|
const result = bookingsFromTeam.map((booking) => {
|
|
|
|
return {
|
|
|
|
userId: booking.userId,
|
2023-04-04 11:58:19 +00:00
|
|
|
user: userHashMap.get(booking.userId),
|
2023-04-25 22:39:47 +00:00
|
|
|
emailMd5: md5(user?.email),
|
2023-03-23 22:10:01 +00:00
|
|
|
count: booking._count.id,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}),
|
|
|
|
teamListForUser: authedProcedure.query(async ({ ctx }) => {
|
|
|
|
const user = ctx.user;
|
|
|
|
|
2023-04-04 11:58:19 +00:00
|
|
|
// Fetch user data
|
|
|
|
const userData = await ctx.prisma.user.findUnique({
|
|
|
|
where: {
|
|
|
|
id: user.id,
|
|
|
|
},
|
2023-06-29 17:03:44 +00:00
|
|
|
select: UserSelect,
|
2023-04-04 11:58:19 +00:00
|
|
|
});
|
|
|
|
|
2023-06-29 17:03:44 +00:00
|
|
|
if (!userData) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const membershipConditional: Prisma.MembershipWhereInput = {
|
|
|
|
team: {
|
|
|
|
slug: { not: null },
|
2023-03-23 22:10:01 +00:00
|
|
|
},
|
2023-06-29 17:03:44 +00:00
|
|
|
accepted: true,
|
|
|
|
userId: user.id,
|
|
|
|
OR: [
|
|
|
|
{
|
|
|
|
role: "ADMIN",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: "OWNER",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
// Validate if user belongs to org as admin/owner
|
|
|
|
if (user.organizationId) {
|
|
|
|
const teamsFromOrg = await ctx.prisma.team.findMany({
|
|
|
|
where: {
|
|
|
|
parentId: user.organizationId,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
slug: true,
|
|
|
|
name: true,
|
|
|
|
logo: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const orgTeam = await ctx.prisma.team.findUnique({
|
|
|
|
where: {
|
|
|
|
id: user.organizationId,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
slug: true,
|
|
|
|
name: true,
|
|
|
|
logo: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (!orgTeam) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const result: IResultTeamList[] = [
|
|
|
|
{
|
|
|
|
id: orgTeam.id,
|
|
|
|
slug: orgTeam.slug,
|
|
|
|
name: orgTeam.name,
|
|
|
|
logo: orgTeam.logo,
|
|
|
|
isOrg: true,
|
|
|
|
},
|
|
|
|
...teamsFromOrg.map(
|
|
|
|
(team: Prisma.TeamGetPayload<{ select: { id: true; slug: true; name: true; logo: true } }>) => {
|
|
|
|
return {
|
|
|
|
...team,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look if user it's admin/owner in multiple teams
|
|
|
|
const belongsToTeams = await ctx.prisma.membership.findMany({
|
|
|
|
where: membershipConditional,
|
2023-03-23 22:10:01 +00:00
|
|
|
include: {
|
|
|
|
team: {
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
name: true,
|
|
|
|
logo: true,
|
|
|
|
slug: true,
|
2023-06-29 17:03:44 +00:00
|
|
|
metadata: true,
|
2023-03-23 22:10:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-06-29 17:03:44 +00:00
|
|
|
if (belongsToTeams.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const result: IResultTeamList[] = belongsToTeams.map((membership) => {
|
2023-04-04 11:58:19 +00:00
|
|
|
return { ...membership.team };
|
|
|
|
});
|
2023-06-29 17:03:44 +00:00
|
|
|
|
2023-03-23 22:10:01 +00:00
|
|
|
return result;
|
|
|
|
}),
|
|
|
|
userList: userBelongsToTeamProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
teamId: z.coerce.number().nullable(),
|
2023-06-29 17:03:44 +00:00
|
|
|
isAll: z.boolean().nullable(),
|
2023-03-23 22:10:01 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.query(async ({ ctx, input }) => {
|
|
|
|
const user = ctx.user;
|
2023-06-29 17:03:44 +00:00
|
|
|
const { teamId, isAll } = input;
|
2023-03-23 22:10:01 +00:00
|
|
|
|
2023-06-29 17:03:44 +00:00
|
|
|
if (!teamId) {
|
2023-03-23 22:10:01 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2023-06-29 17:03:44 +00:00
|
|
|
if (isAll && user.organizationId && user.isOwnerAdminOfParentTeam) {
|
|
|
|
const usersInTeam = await ctx.prisma.membership.findMany({
|
|
|
|
where: {
|
|
|
|
team: {
|
|
|
|
parentId: user.organizationId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
include: {
|
|
|
|
user: {
|
|
|
|
select: UserSelect,
|
|
|
|
},
|
|
|
|
},
|
2023-07-13 22:04:30 +00:00
|
|
|
distinct: ["userId"],
|
2023-06-29 17:03:44 +00:00
|
|
|
});
|
|
|
|
return usersInTeam.map((membership) => membership.user);
|
|
|
|
}
|
|
|
|
|
2023-03-23 22:10:01 +00:00
|
|
|
const membership = await ctx.prisma.membership.findFirst({
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
2023-06-29 17:03:44 +00:00
|
|
|
teamId,
|
|
|
|
accepted: true,
|
2023-03-23 22:10:01 +00:00
|
|
|
},
|
|
|
|
include: {
|
|
|
|
user: {
|
|
|
|
select: UserSelect,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2023-06-29 17:03:44 +00:00
|
|
|
if (!membership) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const isMember = membership && membership.role === "MEMBER";
|
2023-03-23 22:10:01 +00:00
|
|
|
// If user is not admin, return himself only
|
2023-06-29 17:03:44 +00:00
|
|
|
if (isMember) {
|
2023-03-23 22:10:01 +00:00
|
|
|
return [membership.user];
|
|
|
|
}
|
2023-06-29 17:03:44 +00:00
|
|
|
|
2023-03-23 22:10:01 +00:00
|
|
|
const usersInTeam = await ctx.prisma.membership.findMany({
|
|
|
|
where: {
|
2023-06-29 17:03:44 +00:00
|
|
|
teamId,
|
|
|
|
accepted: true,
|
2023-03-23 22:10:01 +00:00
|
|
|
},
|
|
|
|
include: {
|
|
|
|
user: {
|
|
|
|
select: UserSelect,
|
|
|
|
},
|
|
|
|
},
|
2023-07-13 22:04:30 +00:00
|
|
|
distinct: ["userId"],
|
2023-03-23 22:10:01 +00:00
|
|
|
});
|
2023-06-29 17:03:44 +00:00
|
|
|
|
2023-03-23 22:10:01 +00:00
|
|
|
return usersInTeam.map((membership) => membership.user);
|
|
|
|
}),
|
|
|
|
eventTypeList: userBelongsToTeamProcedure
|
|
|
|
.input(
|
|
|
|
z.object({
|
2023-04-04 11:58:19 +00:00
|
|
|
teamId: z.coerce.number().optional().nullable(),
|
|
|
|
userId: z.coerce.number().optional().nullable(),
|
2023-06-29 17:03:44 +00:00
|
|
|
isAll: z.boolean().optional(),
|
2023-03-23 22:10:01 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.query(async ({ ctx, input }) => {
|
2023-04-04 11:58:19 +00:00
|
|
|
const { prisma, user } = ctx;
|
2023-06-29 17:03:44 +00:00
|
|
|
const { teamId, userId, isAll } = input;
|
2023-03-23 22:10:01 +00:00
|
|
|
|
2023-04-04 11:58:19 +00:00
|
|
|
if (!teamId && !userId) {
|
2023-03-23 22:10:01 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2023-04-04 11:58:19 +00:00
|
|
|
const membershipWhereConditional: Prisma.MembershipWhereInput = {};
|
2023-06-29 17:03:44 +00:00
|
|
|
|
|
|
|
let childrenTeamIds: number[] = [];
|
|
|
|
|
|
|
|
if (isAll && teamId && user.organizationId && user.isOwnerAdminOfParentTeam) {
|
|
|
|
const childTeams = await prisma.team.findMany({
|
|
|
|
where: {
|
|
|
|
parentId: user.organizationId,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (childTeams.length > 0) {
|
|
|
|
childrenTeamIds = childTeams.map((team) => team.id);
|
|
|
|
}
|
|
|
|
membershipWhereConditional["teamId"] = {
|
|
|
|
in: [user.organizationId, ...childrenTeamIds],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (teamId && !isAll) {
|
2023-04-04 11:58:19 +00:00
|
|
|
membershipWhereConditional["teamId"] = teamId;
|
|
|
|
membershipWhereConditional["userId"] = user.id;
|
|
|
|
}
|
|
|
|
if (userId) {
|
|
|
|
membershipWhereConditional["userId"] = userId;
|
|
|
|
}
|
2023-03-23 22:10:01 +00:00
|
|
|
|
2023-04-04 11:58:19 +00:00
|
|
|
// I'm not using unique here since when userId comes from input we should look for every
|
|
|
|
// event type that user owns
|
|
|
|
const membership = await prisma.membership.findFirst({
|
|
|
|
where: membershipWhereConditional,
|
|
|
|
});
|
2023-03-23 22:10:01 +00:00
|
|
|
|
2023-06-29 17:03:44 +00:00
|
|
|
if (!membership && !user.isOwnerAdminOfParentTeam) {
|
|
|
|
throw new Error("User is not part of a team/org");
|
2023-03-23 22:10:01 +00:00
|
|
|
}
|
|
|
|
|
2023-04-04 11:58:19 +00:00
|
|
|
const eventTypeWhereConditional: Prisma.EventTypeWhereInput = {};
|
2023-06-29 17:03:44 +00:00
|
|
|
if (isAll && childrenTeamIds.length > 0 && user.organizationId && user.isOwnerAdminOfParentTeam) {
|
|
|
|
eventTypeWhereConditional["teamId"] = {
|
|
|
|
in: [user.organizationId, ...childrenTeamIds],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (teamId && !isAll) {
|
2023-04-04 11:58:19 +00:00
|
|
|
eventTypeWhereConditional["teamId"] = teamId;
|
|
|
|
}
|
|
|
|
if (userId) {
|
|
|
|
eventTypeWhereConditional["userId"] = userId;
|
|
|
|
}
|
|
|
|
let eventTypeResult: Prisma.EventTypeGetPayload<{
|
2023-03-28 23:24:57 +00:00
|
|
|
select: {
|
2023-04-04 11:58:19 +00:00
|
|
|
id: true;
|
|
|
|
slug: true;
|
|
|
|
teamId: true;
|
|
|
|
title: true;
|
2023-06-29 17:03:44 +00:00
|
|
|
team: {
|
|
|
|
select: {
|
|
|
|
name: true;
|
|
|
|
};
|
|
|
|
};
|
2023-04-04 11:58:19 +00:00
|
|
|
};
|
|
|
|
}>[] = [];
|
2023-03-23 22:10:01 +00:00
|
|
|
|
2023-06-29 17:03:44 +00:00
|
|
|
let isMember = membership?.role === "MEMBER";
|
|
|
|
if (user.isOwnerAdminOfParentTeam) {
|
|
|
|
isMember = false;
|
|
|
|
}
|
|
|
|
if (isMember) {
|
|
|
|
eventTypeWhereConditional["OR"] = [
|
|
|
|
{ userId: user.id },
|
|
|
|
{ users: { some: { id: user.id } } },
|
|
|
|
// @TODO this is not working as expected
|
|
|
|
// hosts: { some: { id: user.id } },
|
|
|
|
];
|
|
|
|
}
|
|
|
|
eventTypeResult = await prisma.eventType.findMany({
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
slug: true,
|
|
|
|
title: true,
|
|
|
|
teamId: true,
|
|
|
|
team: {
|
2023-04-04 11:58:19 +00:00
|
|
|
select: {
|
2023-06-29 17:03:44 +00:00
|
|
|
name: true,
|
2023-04-04 11:58:19 +00:00
|
|
|
},
|
2023-06-29 17:03:44 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
where: eventTypeWhereConditional,
|
|
|
|
});
|
2023-04-04 11:58:19 +00:00
|
|
|
|
|
|
|
return eventTypeResult;
|
2023-03-23 22:10:01 +00:00
|
|
|
}),
|
|
|
|
});
|