mirror of
https://github.com/lukevella/rallly.git
synced 2025-06-27 14:57:13 +02:00
⬆️ v3.0.0 (#704)
This commit is contained in:
parent
735056f25f
commit
c22b3abc4d
385 changed files with 19912 additions and 5250 deletions
74
packages/backend/trpc/routers/user-preferences.ts
Normal file
74
packages/backend/trpc/routers/user-preferences.ts
Normal file
|
@ -0,0 +1,74 @@
|
|||
import { prisma } from "@rallly/database";
|
||||
import z from "zod";
|
||||
|
||||
import { publicProcedure, router } from "../trpc";
|
||||
|
||||
export const userPreferences = router({
|
||||
get: publicProcedure.query(async ({ ctx }) => {
|
||||
if (ctx.user.isGuest) {
|
||||
return ctx.user.preferences
|
||||
? {
|
||||
timeZone: ctx.user.preferences.timeZone ?? null,
|
||||
timeFormat: ctx.user.preferences.timeFormat ?? null,
|
||||
weekStart: ctx.user.preferences.weekStart ?? null,
|
||||
}
|
||||
: null;
|
||||
} else {
|
||||
return await prisma.userPreferences.findUnique({
|
||||
where: {
|
||||
userId: ctx.user.id,
|
||||
},
|
||||
select: {
|
||||
timeZone: true,
|
||||
weekStart: true,
|
||||
timeFormat: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
}),
|
||||
update: publicProcedure
|
||||
.input(
|
||||
z.object({
|
||||
timeZone: z.string().optional(),
|
||||
weekStart: z.number().min(0).max(6).optional(),
|
||||
timeFormat: z.enum(["hours12", "hours24"]).optional(),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
if (ctx.user.isGuest === false) {
|
||||
await prisma.userPreferences.upsert({
|
||||
where: {
|
||||
userId: ctx.user.id,
|
||||
},
|
||||
create: {
|
||||
userId: ctx.user.id,
|
||||
...input,
|
||||
},
|
||||
update: {
|
||||
...input,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
ctx.session.user = {
|
||||
...ctx.user,
|
||||
preferences: { ...ctx.user.preferences, ...input },
|
||||
};
|
||||
await ctx.session.save();
|
||||
}
|
||||
}),
|
||||
delete: publicProcedure.mutation(async ({ ctx }) => {
|
||||
if (ctx.user.isGuest) {
|
||||
ctx.session.user = {
|
||||
...ctx.user,
|
||||
preferences: undefined,
|
||||
};
|
||||
await ctx.session.save();
|
||||
} else {
|
||||
await prisma.userPreferences.delete({
|
||||
where: {
|
||||
userId: ctx.user.id,
|
||||
},
|
||||
});
|
||||
}
|
||||
}),
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue