Manually reorder event types (#1142)
* Add event type reordering * Add migration for position field * hack on a hack * can edit * fix ordering * Remove console.log Co-authored-by: Alex Johansson <[email protected]> Co-authored-by: KATT <[email protected]>
This commit is contained in:
1 parent
6fa980f801
commit
6b171a6f87
6 files changed
+184
-7
No files matched your search
+119
-1
@@ -1,4 +1,5 @@
|
||||
import { BookingStatus, Prisma } from "@prisma/client";
|
||||
import _ from "lodash";
|
||||
import { z } from "zod";
|
||||
|
||||
import { checkPremiumUsername } from "@ee/lib/core/checkPremiumUsername";
|
||||
@@ -85,6 +86,7 @@ const loggedInViewerRouter = createProtectedRouter()
|
||||
hidden: true,
|
||||
price: true,
|
||||
currency: true,
|
||||
position: true,
|
||||
users: {
|
||||
select: {
|
||||
id: true,
|
||||
@@ -126,6 +128,14 @@ const loggedInViewerRouter = createProtectedRouter()
|
||||
},
|
||||
eventTypes: {
|
||||
select: eventTypeSelect,
|
||||
orderBy: [
|
||||
{
|
||||
position: "desc",
|
||||
},
|
||||
{
|
||||
id: "asc",
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -136,6 +146,14 @@ const loggedInViewerRouter = createProtectedRouter()
|
||||
team: null,
|
||||
},
|
||||
select: eventTypeSelect,
|
||||
orderBy: [
|
||||
{
|
||||
position: "desc",
|
||||
},
|
||||
{
|
||||
id: "asc",
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
});
|
||||
@@ -150,6 +168,14 @@ const loggedInViewerRouter = createProtectedRouter()
|
||||
userId: ctx.user.id,
|
||||
},
|
||||
select: eventTypeSelect,
|
||||
orderBy: [
|
||||
{
|
||||
position: "desc",
|
||||
},
|
||||
{
|
||||
id: "asc",
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
type EventTypeGroup = {
|
||||
@@ -184,7 +210,7 @@ const loggedInViewerRouter = createProtectedRouter()
|
||||
name: user.name,
|
||||
image: user.avatar,
|
||||
},
|
||||
eventTypes: mergedEventTypes,
|
||||
eventTypes: _.orderBy(mergedEventTypes, ["position", "id"], ["desc", "asc"]),
|
||||
metadata: {
|
||||
membershipCount: 1,
|
||||
readOnly: false,
|
||||
@@ -449,6 +475,98 @@ const loggedInViewerRouter = createProtectedRouter()
|
||||
data,
|
||||
});
|
||||
},
|
||||
})
|
||||
.mutation("eventTypeOrder", {
|
||||
input: z.object({
|
||||
ids: z.array(z.number()),
|
||||
}),
|
||||
async resolve({ input, ctx }) {
|
||||
const { prisma, user } = ctx;
|
||||
const allEventTypes = await ctx.prisma.eventType.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
where: {
|
||||
id: {
|
||||
in: input.ids,
|
||||
},
|
||||
OR: [
|
||||
{
|
||||
userId: user.id,
|
||||
},
|
||||
{
|
||||
users: {
|
||||
some: {
|
||||
id: user.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
team: {
|
||||
members: {
|
||||
some: {
|
||||
userId: user.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
const allEventTypeIds = new Set(allEventTypes.map((type) => type.id));
|
||||
if (input.ids.some((id) => !allEventTypeIds.has(id))) {
|
||||
throw new TRPCError({
|
||||
code: "UNAUTHORIZED",
|
||||
});
|
||||
}
|
||||
await Promise.all(
|
||||
_.reverse(input.ids).map((id, position) => {
|
||||
return prisma.eventType.update({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
data: {
|
||||
position,
|
||||
},
|
||||
});
|
||||
})
|
||||
);
|
||||
},
|
||||
})
|
||||
.mutation("eventTypePosition", {
|
||||
input: z.object({
|
||||
eventType: z.number(),
|
||||
action: z.string(),
|
||||
}),
|
||||
async resolve({ input, ctx }) {
|
||||
// This mutation is for the user to be able to order their event types by incrementing or decrementing the position number
|
||||
const { prisma } = ctx;
|
||||
if (input.eventType && input.action == "increment") {
|
||||
await prisma.eventType.update({
|
||||
where: {
|
||||
id: input.eventType,
|
||||
},
|
||||
data: {
|
||||
position: {
|
||||
increment: 1,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (input.eventType && input.action == "decrement") {
|
||||
await prisma.eventType.update({
|
||||
where: {
|
||||
id: input.eventType,
|
||||
},
|
||||
data: {
|
||||
position: {
|
||||
decrement: 1,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
export const viewerRouter = createRouter()
|
||||
|
||||
Reference in new issue
Block a user