Skip to content

fix: replace bookingTimeStatus with the denormalized table #21028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: eunjae/cal-5420-make-dtosdedicated-tables-that-are-flattened-reporting
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions apps/web/app/api/cron/monthlyDigestEmail/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async function postHandler(request: NextRequest) {
const userIdsFromTeams = team.members.map((u) => u.userId);

// Booking Events
const whereConditional: Prisma.BookingTimeStatusWhereInput = {
const whereConditional: Prisma.BookingTimeStatusDenormalizedWhereInput = {
OR: [
{
teamId: team.id,
Expand All @@ -122,7 +122,7 @@ async function postHandler(request: NextRequest) {
EventData["Cancelled"] = countGroupedByStatus["cancelled"];

// Most Booked Event Type
const bookingWhere: Prisma.BookingTimeStatusWhereInput = {
const bookingWhere: Prisma.BookingTimeStatusDenormalizedWhereInput = {
createdAt: {
gte: dayjs(firstDateOfMonth).startOf("day").toDate(),
lte: dayjs(new Date()).endOf("day").toDate(),
Expand All @@ -141,7 +141,7 @@ async function postHandler(request: NextRequest) {
],
};

const bookingsFromSelected = await prisma.bookingTimeStatus.groupBy({
const bookingsFromSelected = await prisma.bookingTimeStatusDenormalized.groupBy({
by: ["eventTypeId"],
where: bookingWhere,
_count: {
Expand Down Expand Up @@ -234,7 +234,7 @@ async function postHandler(request: NextRequest) {
});

// Most booked members
const bookingsFromTeam = await prisma.bookingTimeStatus.groupBy({
const bookingsFromTeam = await prisma.bookingTimeStatusDenormalized.groupBy({
by: ["userId"],
where: bookingWhere,
_count: {
Expand Down
28 changes: 14 additions & 14 deletions packages/features/insights/server/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function buildSqlCondition(condition: any): string {

class EventsInsights {
static countGroupedByStatusForRanges = async (
whereConditional: Prisma.BookingTimeStatusWhereInput,
whereConditional: Prisma.BookingTimeStatusDenormalizedWhereInput,
startDate: Dayjs,
endDate: Dayjs,
timeView: "week" | "month" | "year" | "day"
Expand Down Expand Up @@ -95,9 +95,9 @@ class EventsInsights {
"timeStatus",
"noShowHost"
FROM
"BookingTimeStatus"
"BookingTimeStatusDenormalized"
JOIN
"Attendee" "a" ON "a"."bookingId" = "BookingTimeStatus"."id"
"Attendee" "a" ON "a"."bookingId" = "BookingTimeStatusDenormalized"."id"
WHERE
"createdAt" BETWEEN ${formattedStartDate}::timestamp AND ${formattedEndDate}::timestamp
AND ${Prisma.raw(whereClause)}
Expand Down Expand Up @@ -185,8 +185,8 @@ class EventsInsights {
return aggregate;
};

static getTotalNoShowGuests = async (where: Prisma.BookingTimeStatusWhereInput) => {
const bookings = await prisma.bookingTimeStatus.findMany({
static getTotalNoShowGuests = async (where: Prisma.BookingTimeStatusDenormalizedWhereInput) => {
const bookings = await prisma.bookingTimeStatusDenormalized.findMany({
where,
select: {
id: true,
Expand All @@ -206,8 +206,8 @@ class EventsInsights {
return totalNoShowGuests;
};

static countGroupedByStatus = async (where: Prisma.BookingTimeStatusWhereInput) => {
const data = await prisma.bookingTimeStatus.groupBy({
static countGroupedByStatus = async (where: Prisma.BookingTimeStatusDenormalizedWhereInput) => {
const data = await prisma.bookingTimeStatusDenormalized.groupBy({
where,
by: ["timeStatus", "noShowHost"],
_count: {
Expand Down Expand Up @@ -237,8 +237,8 @@ class EventsInsights {
);
};

static getAverageRating = async (whereConditional: Prisma.BookingTimeStatusWhereInput) => {
return await prisma.bookingTimeStatus.aggregate({
static getAverageRating = async (whereConditional: Prisma.BookingTimeStatusDenormalizedWhereInput) => {
return await prisma.bookingTimeStatusDenormalized.aggregate({
_avg: {
rating: true,
},
Expand All @@ -251,8 +251,8 @@ class EventsInsights {
});
};

static getTotalCSAT = async (whereConditional: Prisma.BookingTimeStatusWhereInput) => {
const result = await prisma.bookingTimeStatus.findMany({
static getTotalCSAT = async (whereConditional: Prisma.BookingTimeStatusDenormalizedWhereInput) => {
const result = await prisma.bookingTimeStatusDenormalized.findMany({
where: {
...whereConditional,
rating: {
Expand Down Expand Up @@ -385,11 +385,11 @@ class EventsInsights {
const limit = props.limit ?? 100; // Default batch size
const offset = props.offset ?? 0;

const totalCountPromise = prisma.bookingTimeStatus.count({
const totalCountPromise = prisma.bookingTimeStatusDenormalized.count({
where: whereConditional,
});

const csvDataPromise = prisma.bookingTimeStatus.findMany({
const csvDataPromise = prisma.bookingTimeStatusDenormalized.findMany({
select: {
id: true,
uid: true,
Expand Down Expand Up @@ -496,7 +496,7 @@ class EventsInsights {
} = props;

// Obtain the where conditional
let whereConditional: Prisma.BookingTimeStatusWhereInput = {};
let whereConditional: Prisma.BookingTimeStatusDenormalizedWhereInput = {};

if (startDate && endDate) {
whereConditional.createdAt = {
Expand Down
24 changes: 12 additions & 12 deletions packages/features/insights/server/trpc-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ const buildBaseWhereCondition = async ({
isAll,
ctx,
}: BuildBaseWhereConditionType): Promise<{
whereCondition: Prisma.BookingTimeStatusWhereInput;
whereCondition: Prisma.BookingTimeStatusDenormalizedWhereInput;
isEmptyResponse?: boolean;
}> => {
let whereCondition: Prisma.BookingTimeStatusWhereInput = {};
const ANDConditions: Prisma.BookingTimeStatusWhereInput[] = [];
let whereCondition: Prisma.BookingTimeStatusDenormalizedWhereInput = {};
const ANDConditions: Prisma.BookingTimeStatusDenormalizedWhereInput[] = [];
// EventType Filter
if (eventTypeId) ANDConditions.push({ OR: [{ eventTypeId }, { eventParentId: eventTypeId }] });
// User/Member filter
Expand Down Expand Up @@ -588,7 +588,7 @@ export const insightsRouter = router({
},
};

const bookingsFromSelected = await ctx.insightsDb.bookingTimeStatus.groupBy({
const bookingsFromSelected = await ctx.insightsDb.bookingTimeStatusDenormalized.groupBy({
by: ["eventTypeId"],
where: bookingWhere,
_count: {
Expand Down Expand Up @@ -725,7 +725,7 @@ export const insightsRouter = router({

const startOfEndOf = timeView === "year" ? "year" : timeView === "month" ? "month" : "week";

const allBookings = await ctx.insightsDb.bookingTimeStatus.findMany({
const allBookings = await ctx.insightsDb.bookingTimeStatusDenormalized.findMany({
select: {
eventLength: true,
createdAt: true,
Expand Down Expand Up @@ -795,7 +795,7 @@ export const insightsRouter = router({
status: "CANCELLED",
};

const bookingsFromTeam = await ctx.insightsDb.bookingTimeStatus.groupBy({
const bookingsFromTeam = await ctx.insightsDb.bookingTimeStatusDenormalized.groupBy({
by: ["userId"],
where: bookingWhere,
_count: {
Expand Down Expand Up @@ -877,7 +877,7 @@ export const insightsRouter = router({
},
};

const bookingsFromTeam = await ctx.insightsDb.bookingTimeStatus.groupBy({
const bookingsFromTeam = await ctx.insightsDb.bookingTimeStatusDenormalized.groupBy({
by: ["userId"],
where: bookingWhere,
_count: {
Expand Down Expand Up @@ -958,7 +958,7 @@ export const insightsRouter = router({
},
};

const bookingsFromTeam = await ctx.insightsDb.bookingTimeStatus.groupBy({
const bookingsFromTeam = await ctx.insightsDb.bookingTimeStatusDenormalized.groupBy({
by: ["userId"],
where: bookingWhere,
_count: {
Expand Down Expand Up @@ -1255,7 +1255,7 @@ export const insightsRouter = router({
];
}

const bookingsFromTeam = await ctx.insightsDb.bookingTimeStatus.findMany({
const bookingsFromTeam = await ctx.insightsDb.bookingTimeStatusDenormalized.findMany({
where: bookingWhere,
orderBy: {
endTime: "desc",
Expand Down Expand Up @@ -1334,7 +1334,7 @@ export const insightsRouter = router({
noShowHost: true,
};

const bookingsFromTeam = await ctx.insightsDb.bookingTimeStatus.groupBy({
const bookingsFromTeam = await ctx.insightsDb.bookingTimeStatusDenormalized.groupBy({
by: ["userId"],
where: bookingWhere,
_count: {
Expand Down Expand Up @@ -1413,7 +1413,7 @@ export const insightsRouter = router({
rating: { not: null },
};

const bookingsFromTeam = await ctx.insightsDb.bookingTimeStatus.groupBy({
const bookingsFromTeam = await ctx.insightsDb.bookingTimeStatusDenormalized.groupBy({
by: ["userId"],
where: bookingWhere,
_avg: {
Expand Down Expand Up @@ -1492,7 +1492,7 @@ export const insightsRouter = router({
rating: { not: null },
};

const bookingsFromTeam = await ctx.insightsDb.bookingTimeStatus.groupBy({
const bookingsFromTeam = await ctx.insightsDb.bookingTimeStatusDenormalized.groupBy({
by: ["userId"],
where: bookingWhere,
_avg: {
Expand Down