mirror of
https://github.com/linkwarden/linkwarden.git
synced 2026-07-01 08:16:26 +00:00
68 lines
1.6 KiB
TypeScript
68 lines
1.6 KiB
TypeScript
import { prisma } from "@linkwarden/prisma";
|
|
import {
|
|
UpdateUserPreferenceSchemaType,
|
|
UpdateUserPreferenceSchema,
|
|
} from "@linkwarden/lib/schemaValidation";
|
|
|
|
export default async function updateUserPreference(
|
|
userId: number,
|
|
body: UpdateUserPreferenceSchemaType
|
|
) {
|
|
const dataValidation = UpdateUserPreferenceSchema.safeParse(body);
|
|
|
|
if (!dataValidation.success) {
|
|
return {
|
|
response: `Error: ${
|
|
dataValidation.error.issues[0].message
|
|
} [${dataValidation.error.issues[0].path.join(", ")}]`,
|
|
status: 400,
|
|
};
|
|
}
|
|
|
|
const data = dataValidation.data;
|
|
|
|
const updatedUser = await prisma.user.update({
|
|
where: {
|
|
id: userId,
|
|
},
|
|
data: {
|
|
theme: data.theme,
|
|
readableFontFamily: data.readableFontFamily,
|
|
readableFontSize: data.readableFontSize,
|
|
readableLineHeight: data.readableLineHeight,
|
|
readableLineWidth: data.readableLineWidth,
|
|
},
|
|
include: {
|
|
subscriptions: true,
|
|
dashboardSections: true,
|
|
parentSubscription: {
|
|
include: {
|
|
user: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!updatedUser) return { response: "User not found.", status: 404 };
|
|
|
|
const { password, subscriptions, parentSubscription, ...lessSensitiveInfo } =
|
|
updatedUser;
|
|
|
|
const response = {
|
|
...lessSensitiveInfo,
|
|
subscription: {
|
|
active: subscriptions?.active,
|
|
quantity: subscriptions?.quantity,
|
|
provider: subscriptions?.provider,
|
|
},
|
|
parentSubscription: {
|
|
active: parentSubscription?.active,
|
|
user: {
|
|
email: parentSubscription?.user.email,
|
|
},
|
|
},
|
|
};
|
|
|
|
return { response, status: 200 };
|
|
}
|