feat: organization booking hooks (#20611)
* docs: request auth headers * feat: useOrganizationsBookings hook * feat: useOrganizationUserBookings hook * rename hooks * query params * fix: organizations users bookings extract user from userId not auth * fix: passing array from hook * docs * correct header doc --------- Co-authored-by: Anik Dhabal Babu <[email protected]>
This commit is contained in:
1 parent
15d2c0f858
commit
c89a1877ae
14 files changed
+373
-45
No files matched your search
@@ -11,6 +11,8 @@ import type { GetBookingsInput_2024_08_13 } from "@calcom/platform-types";
|
||||
|
||||
import http from "../../lib/http";
|
||||
|
||||
const QUERY_KEY = "use-bookings";
|
||||
|
||||
export const useBookings = (input: GetBookingsInput_2024_08_13) => {
|
||||
const pathname = `/${V2_ENDPOINTS.bookings}`;
|
||||
const headers = {
|
||||
@@ -18,23 +20,7 @@ export const useBookings = (input: GetBookingsInput_2024_08_13) => {
|
||||
};
|
||||
|
||||
const bookingsQuery = useQuery({
|
||||
queryKey: [
|
||||
"use-bookings",
|
||||
input.status,
|
||||
input.attendeeEmail,
|
||||
input.attendeeName,
|
||||
input.eventTypeIds,
|
||||
input.eventTypeId,
|
||||
input.teamsIds,
|
||||
input.teamId,
|
||||
input.afterStart,
|
||||
input.beforeEnd,
|
||||
input.sortStart,
|
||||
input.sortEnd,
|
||||
input.sortCreated,
|
||||
input.take,
|
||||
input.skip,
|
||||
],
|
||||
queryKey: [QUERY_KEY, ...extractBookingsQueryKeys(input)],
|
||||
queryFn: async () => {
|
||||
return http
|
||||
.get<GetBookingsOutput_2024_08_13>(pathname, {
|
||||
@@ -52,3 +38,27 @@ export const useBookings = (input: GetBookingsInput_2024_08_13) => {
|
||||
|
||||
return bookingsQuery;
|
||||
};
|
||||
|
||||
export function extractBookingsQueryKeys(query: GetBookingsInput_2024_08_13) {
|
||||
return [
|
||||
query.status,
|
||||
query.attendeeEmail,
|
||||
query.attendeeName,
|
||||
query.eventTypeIds,
|
||||
query.eventTypeId,
|
||||
query.teamsIds,
|
||||
query.teamId,
|
||||
query.afterStart,
|
||||
query.beforeEnd,
|
||||
query.afterCreatedAt,
|
||||
query.beforeCreatedAt,
|
||||
query.afterUpdatedAt,
|
||||
query.beforeUpdatedAt,
|
||||
query.sortStart,
|
||||
query.sortEnd,
|
||||
query.sortCreated,
|
||||
query.sortUpdatedAt,
|
||||
query.take,
|
||||
query.skip,
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
|
||||
import { SUCCESS_STATUS } from "@calcom/platform-constants";
|
||||
import type { GetBookingsOutput_2024_08_13, GetOrganizationsBookingsInput } from "@calcom/platform-types";
|
||||
|
||||
import http from "../../../lib/http";
|
||||
import { extractBookingsQueryKeys } from "../../bookings/useBookings";
|
||||
import { useAtomsContext } from "../../useAtomsContext";
|
||||
|
||||
export const QUERY_KEY = "use-organization-bookings";
|
||||
|
||||
export const useOrganizationBookings = (query: GetOrganizationsBookingsInput) => {
|
||||
const { organizationId } = useAtomsContext();
|
||||
|
||||
const pathname = `/organizations/${organizationId}/bookings`;
|
||||
|
||||
const organizationBookingsQuery = useQuery({
|
||||
queryKey: [QUERY_KEY, ...extractOrganizationBookingsQueryKeys(query)],
|
||||
queryFn: async () => {
|
||||
return http
|
||||
.get<GetBookingsOutput_2024_08_13>(pathname, {
|
||||
params: query,
|
||||
})
|
||||
.then((res) => {
|
||||
if (res.data.status === SUCCESS_STATUS) {
|
||||
return res.data.data;
|
||||
}
|
||||
throw new Error(res.data?.error?.message);
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
return organizationBookingsQuery;
|
||||
};
|
||||
|
||||
function extractOrganizationBookingsQueryKeys(query: GetOrganizationsBookingsInput) {
|
||||
return [...extractBookingsQueryKeys(query), query.userIds];
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
|
||||
import { SUCCESS_STATUS } from "@calcom/platform-constants";
|
||||
import type { GetBookingsInput_2024_08_13, GetBookingsOutput_2024_08_13 } from "@calcom/platform-types";
|
||||
|
||||
import http from "../../../lib/http";
|
||||
import { extractBookingsQueryKeys } from "../../bookings/useBookings";
|
||||
import { useAtomsContext } from "../../useAtomsContext";
|
||||
|
||||
export const QUERY_KEY = "use-organization-user-bookings";
|
||||
|
||||
export const useOrganizationUserBookings = (userId: number, query: GetBookingsInput_2024_08_13) => {
|
||||
const { organizationId } = useAtomsContext();
|
||||
|
||||
const pathname = `/organizations/${organizationId}/users/${userId}/bookings`;
|
||||
|
||||
const organizationUserBookingsQuery = useQuery({
|
||||
queryKey: [QUERY_KEY, userId, ...extractBookingsQueryKeys(query)],
|
||||
queryFn: async () => {
|
||||
return http
|
||||
.get<GetBookingsOutput_2024_08_13>(pathname, {
|
||||
params: query,
|
||||
})
|
||||
.then((res) => {
|
||||
if (res.data.status === SUCCESS_STATUS) {
|
||||
return res.data.data;
|
||||
}
|
||||
throw new Error(res.data?.error?.message);
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
return organizationUserBookingsQuery;
|
||||
};
|
||||
@@ -34,3 +34,6 @@ export { PaymentForm } from "./event-types/payments/PaymentForm";
|
||||
|
||||
export { useCreateEventType } from "./hooks/event-types/private/useCreateEventType";
|
||||
export { useCreateTeamEventType } from "./hooks/event-types/private/useCreateTeamEventType";
|
||||
|
||||
export { useOrganizationBookings } from "./hooks/organizations/bookings/useOrganizationBookings";
|
||||
export { useOrganizationUserBookings } from "./hooks/organizations/bookings/useOrganizationUserBookings";
|
||||
@@ -86,6 +86,9 @@ export class GetBookingsInput_2024_08_13 {
|
||||
if (typeof value === "string") {
|
||||
return value.split(",").map((eventTypeId: string) => parseInt(eventTypeId));
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
return value.map((eventTypeId: string | number) => +eventTypeId);
|
||||
}
|
||||
return value;
|
||||
})
|
||||
@IsArray()
|
||||
@@ -116,6 +119,9 @@ export class GetBookingsInput_2024_08_13 {
|
||||
if (typeof value === "string") {
|
||||
return value.split(",").map((teamId: string) => parseInt(teamId));
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
return value.map((teamId: string | number) => +teamId);
|
||||
}
|
||||
return value;
|
||||
})
|
||||
@IsArray()
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { Transform } from "class-transformer";
|
||||
import { ArrayMinSize, IsArray, IsNumber, IsOptional } from "class-validator";
|
||||
|
||||
import { GetBookingsInput_2024_08_13 } from "@calcom/platform-types";
|
||||
|
||||
export class GetOrganizationsBookingsInput extends GetBookingsInput_2024_08_13 {
|
||||
@IsArray()
|
||||
@IsOptional()
|
||||
@Transform(({ value }) => {
|
||||
if (typeof value === "string") {
|
||||
return value.split(",").map((userId: string) => parseInt(userId));
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
return value.map((userId: string | number) => +userId);
|
||||
}
|
||||
return value;
|
||||
})
|
||||
@IsNumber({}, { each: true })
|
||||
@ArrayMinSize(1, { message: "userIds must contain at least 1 user id" })
|
||||
@ApiProperty({
|
||||
type: String,
|
||||
required: false,
|
||||
description: "Filter bookings by ids of users within your organization.",
|
||||
example: "?userIds=100,200",
|
||||
})
|
||||
userIds?: number[];
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export * from "./get-org-bookings.input";
|
||||
@@ -1 +1,2 @@
|
||||
export * from "./teams";
|
||||
export * from "./bookings";
|
||||
Reference in new issue
Block a user