mirror of
https://github.com/lukevella/rallly.git
synced 2025-08-03 16:38:34 +02:00
♻️ Fetch data from space (#1779)
This commit is contained in:
parent
2fe17e7f32
commit
dd9bdbcfc4
9 changed files with 112 additions and 118 deletions
|
@ -2,7 +2,7 @@ import type { PollStatus, Prisma } from "@rallly/database";
|
|||
import { prisma } from "@rallly/database";
|
||||
|
||||
type PollFilters = {
|
||||
userId: string;
|
||||
spaceId: string;
|
||||
status?: PollStatus;
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
|
@ -10,7 +10,7 @@ type PollFilters = {
|
|||
};
|
||||
|
||||
export async function getPolls({
|
||||
userId,
|
||||
spaceId,
|
||||
status,
|
||||
page = 1,
|
||||
pageSize = 10,
|
||||
|
@ -18,7 +18,7 @@ export async function getPolls({
|
|||
}: PollFilters) {
|
||||
// Build the where clause based on filters
|
||||
const where: Prisma.PollWhereInput = {
|
||||
userId,
|
||||
spaceId,
|
||||
status,
|
||||
deleted: false,
|
||||
};
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
import { getActiveSpace } from "@/auth/queries";
|
||||
import type { Prisma } from "@rallly/database";
|
||||
import { prisma } from "@rallly/database";
|
||||
import dayjs from "dayjs";
|
||||
import timezone from "dayjs/plugin/timezone";
|
||||
import utc from "dayjs/plugin/utc";
|
||||
|
||||
import type { Status } from "../schema";
|
||||
import { cache } from "react";
|
||||
import type { Status } from "./schema";
|
||||
|
||||
dayjs.extend(utc);
|
||||
dayjs.extend(timezone);
|
||||
|
||||
const mapStatus = {
|
||||
upcoming: "confirmed",
|
||||
|
@ -16,26 +15,22 @@ const mapStatus = {
|
|||
canceled: "canceled",
|
||||
} as const;
|
||||
|
||||
export async function getScheduledEvents({
|
||||
userId,
|
||||
function getEventsWhereInput({
|
||||
spaceId,
|
||||
status,
|
||||
search,
|
||||
page = 1,
|
||||
pageSize = 10,
|
||||
}: {
|
||||
userId: string;
|
||||
spaceId: string;
|
||||
status: Status;
|
||||
search?: string;
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
}) {
|
||||
const now = new Date();
|
||||
|
||||
const todayStart = dayjs().startOf("day").toDate();
|
||||
const todayEnd = dayjs().endOf("day").toDate();
|
||||
const todayStart = dayjs().startOf("day").utc().toDate();
|
||||
const todayEnd = dayjs().endOf("day").utc().toDate();
|
||||
|
||||
const where: Prisma.ScheduledEventWhereInput = {
|
||||
userId,
|
||||
spaceId,
|
||||
deletedAt: null,
|
||||
...(status === "upcoming" && {
|
||||
OR: [
|
||||
|
@ -53,6 +48,28 @@ export async function getScheduledEvents({
|
|||
status: mapStatus[status],
|
||||
};
|
||||
|
||||
return where;
|
||||
}
|
||||
|
||||
export async function getScheduledEvents({
|
||||
spaceId,
|
||||
status,
|
||||
search,
|
||||
page = 1,
|
||||
pageSize = 10,
|
||||
}: {
|
||||
spaceId: string;
|
||||
status: Status;
|
||||
search?: string;
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
}) {
|
||||
const where = getEventsWhereInput({
|
||||
spaceId,
|
||||
status,
|
||||
search,
|
||||
});
|
||||
|
||||
const [rawEvents, totalCount] = await Promise.all([
|
||||
prisma.scheduledEvent.findMany({
|
||||
where,
|
||||
|
@ -101,3 +118,13 @@ export async function getScheduledEvents({
|
|||
|
||||
return { events, totalCount, totalPages, hasNextPage };
|
||||
}
|
||||
|
||||
export const getUpcomingEventsCount = cache(async () => {
|
||||
const space = await getActiveSpace();
|
||||
return prisma.scheduledEvent.count({
|
||||
where: getEventsWhereInput({
|
||||
spaceId: space.id,
|
||||
status: "upcoming",
|
||||
}),
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue