Skip to content

Commit

Permalink
feat: v1 API - add sort option on GET /bookings (calcom#15374)
Browse files Browse the repository at this point in the history
Co-authored-by: Keith Williams <keithwillcode@gmail.com>
Co-authored-by: Hariom Balhara <hariombalhara@gmail.com>
  • Loading branch information
3 people authored Jul 4, 2024
1 parent 557498b commit 785ab8b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions apps/api/v1/lib/validations/booking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export const schemaBookingCreateBodyParams = extendedBookingCreateBody.merge(sch
export const schemaBookingGetParams = z.object({
dateFrom: iso8601.optional(),
dateTo: iso8601.optional(),
order: z.enum(["asc", "desc"]).default("asc"),
sortBy: z.enum(["createdAt", "updatedAt"]).optional(),
});

const schemaBookingEditParams = z
Expand Down
26 changes: 25 additions & 1 deletion apps/api/v1/pages/api/bookings/_get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ import { schemaQuerySingleOrMultipleUserIds } from "~/lib/validations/shared/que
* type: string
* format: email
* example: [john.doe@example.com, jane.doe@example.com]
* - in: query
* name: order
* required: false
* schema:
* type: string
* enum: [asc, desc]
* - in: query
* name: sortBy
* required: false
* schema:
* type: string
* enum: [createdAt, updatedAt]
* operationId: listBookings
* tags:
* - bookings
Expand Down Expand Up @@ -177,7 +189,7 @@ export async function handler(req: NextApiRequest) {
isOrganizationOwnerOrAdmin,
pagination: { take, skip },
} = req;
const { dateFrom, dateTo } = schemaBookingGetParams.parse(req.query);
const { dateFrom, dateTo, order, sortBy } = schemaBookingGetParams.parse(req.query);

const args: Prisma.BookingFindManyArgs = {};
if (req.query.take && req.query.page) {
Expand Down Expand Up @@ -251,6 +263,18 @@ export async function handler(req: NextApiRequest) {
};
}

if (sortBy === "updatedAt") {
args.orderBy = {
updatedAt: order,
};
}

if (sortBy === "createdAt") {
args.orderBy = {
createdAt: order,
};
}

const data = await prisma.booking.findMany(args);
return { bookings: data.map((booking) => schemaBookingReadPublic.parse(booking)) };
}
Expand Down

0 comments on commit 785ab8b

Please sign in to comment.