added log for site settings and user settings

This commit is contained in:
2026-01-09 22:52:00 +07:00
parent ae39cc111f
commit ac8f22204d
6 changed files with 735 additions and 602 deletions

1124
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,6 @@
import { authClient } from '@/lib/auth-client';
import { m } from '@/paraglide/messages';
import { uploadProfileImage } from '@/service/profile.api';
import { ProfileInput, profileUpdateSchema } from '@/service/profile.schema';
import { UserCircleIcon } from '@phosphor-icons/react';
import { useForm } from '@tanstack/react-form';
@@ -35,9 +36,21 @@ const ProfileForm = () => {
},
onSubmit: async ({ value }) => {
try {
let imageKey;
if (value.image) {
// upload image
const formData = new FormData();
formData.set('file', value.image);
const { imageKey: uploadedKey } = await uploadProfileImage({
data: formData,
});
imageKey = uploadedKey;
}
await authClient.updateUser(
{
name: value.name,
image: imageKey,
},
{
onSuccess: () => {

View File

@@ -0,0 +1,17 @@
import { authMiddleware } from '@/lib/middleware';
import { saveFile } from '@/utils/disk-storage';
import { createServerFn } from '@tanstack/react-start';
import z from 'zod';
export const uploadProfileImage = createServerFn({ method: 'POST' })
.middleware([authMiddleware])
.inputValidator(z.instanceof(FormData))
.handler(async ({ data: formData }) => {
const uuid = crypto.randomUUID();
const file = formData.get('file') as File;
if (!(file instanceof File)) throw new Error('File not found');
const imageKey = `${uuid}.${file.type.split('/')[1]}`;
const buffer = Buffer.from(await file.arrayBuffer());
await saveFile(imageKey, buffer);
return { imageKey };
});

View File

@@ -1,37 +1,50 @@
import { prisma } from '@/db';
import { Setting } from '@/generated/prisma/client';
import { authMiddleware } from '@/lib/middleware';
import { extractDiffObjects } from '@/utils/help';
import { createServerFn } from '@tanstack/react-start';
import { createAuditLog } from './audit.api';
import { settingSchema, userSettingSchema } from './setting.schema';
type AdminSettingReturn = {
[key: string]: Setting;
[key: string]: Pick<Setting, 'id' | 'key' | 'value'> | string;
};
// Settings for admin
export const getAdminSettings = createServerFn({ method: 'GET' })
.middleware([authMiddleware])
.handler(async () => {
console.log('first');
async function getAllAdminSettings(valueOnly = false) {
const settings = await prisma.setting.findMany({
where: {
relation: 'admin',
},
select: {
id: true,
key: true,
value: true,
},
});
const results: AdminSettingReturn = {};
settings.forEach((setting) => {
results[setting.key] = setting;
results[setting.key] = valueOnly ? setting.value : setting;
});
return results;
}
// Settings for admin
export const getAdminSettings = createServerFn({ method: 'GET' })
.middleware([authMiddleware])
.handler(async () => {
return await getAllAdminSettings();
});
export const updateAdminSettings = createServerFn({ method: 'POST' })
.inputValidator(settingSchema)
.middleware([authMiddleware])
.handler(async ({ data }) => {
.handler(async ({ data, context }) => {
try {
const oldSetting = await getAllAdminSettings(true);
// Update each setting
const updates = Object.entries(data).map(([key, value]) =>
prisma.setting.upsert({
@@ -46,20 +59,29 @@ export const updateAdminSettings = createServerFn({ method: 'POST' })
}),
);
await prisma.$transaction(updates);
const updated = await prisma.$transaction(updates);
// console.log(updates);
const [oldValue, newValue] = extractDiffObjects(oldSetting, data);
const keyEdit = Object.keys(oldValue);
const listId = updated
.filter((s) => keyEdit.includes(s.key))
.map((s) => s.id)
.join(', ');
// await createAuditLog({
// action: 'update',
// tableName: 'setting',
// recordId: '',
// oldValue: '',
// newValue: JSON.stringify(data),
// userId: '',
// });
await createAuditLog({
action: 'update',
tableName: 'setting',
recordId: listId,
oldValue: JSON.stringify(oldValue),
newValue: JSON.stringify(newValue),
userId: context.user.id,
});
return { success: true };
} catch (error) {
console.log(error);
throw error;
}
});
// Setting for user
@@ -76,10 +98,15 @@ export const getUserSettings = createServerFn({ method: 'GET' })
relation: 'user',
key: context.user.id,
},
select: {
id: true,
key: true,
value: true,
},
});
return {
settings: settings as Setting,
settings,
value: JSON.parse(settings.value) as UserSetting,
};
} catch (error) {
@@ -93,7 +120,17 @@ export const updateUserSettings = createServerFn({ method: 'POST' })
.handler(async ({ data, context }) => {
// Update each setting
try {
await prisma.setting.upsert({
const oldSetting = await prisma.setting.findUnique({
where: {
relation: 'user',
key: context.user.id,
},
select: {
value: true,
},
});
const updated = await prisma.setting.upsert({
where: { key: context.user.id },
update: { value: JSON.stringify(data) },
create: {
@@ -104,14 +141,16 @@ export const updateUserSettings = createServerFn({ method: 'POST' })
},
});
// await createAuditLog({
// action: 'update',
// tableName: 'setting',
// recordId: '',
// oldValue: '',
// newValue: '',
// userId: '',
// });
if (oldSetting) {
await createAuditLog({
action: 'update',
tableName: 'setting',
recordId: updated.id,
oldValue: oldSetting.value,
newValue: JSON.stringify(data),
userId: context.user.id,
});
}
return { success: true };
} catch (error) {

26
src/utils/disk-storage.ts Normal file
View File

@@ -0,0 +1,26 @@
import fs, { writeFile } from 'fs/promises';
import path from 'path';
const uploadDir = './data/avatar';
export async function saveFile(key: string, file: Buffer | File) {
if (!uploadDir) {
throw new Error('Upload directory not found');
}
const fileBuffer =
file instanceof File ? Buffer.from(await file.arrayBuffer()) : file;
const filePath = path.join(uploadDir, key);
try {
await fs.mkdir(uploadDir, { recursive: true });
await writeFile(filePath, fileBuffer);
return key;
} catch (error) {
console.error(`Error saving file: ${key}`, error);
throw new Error(
`Failed to save file: ${error instanceof Error ? error.message : 'Unknown error'}`,
);
}
}

View File

@@ -7,3 +7,21 @@ export function jsonSupport(jsonSTR: string) {
return jsonSTR;
}
}
type AnyRecord = Record<string, unknown>;
export function extractDiffObjects<T extends AnyRecord>(
a: T,
b: T,
): [Partial<T>, Partial<T>] {
return (Object.keys(a) as (keyof T)[]).reduce(
([accA, accB], key) => {
if (a[key] !== b[key]) {
accA[key] = a[key];
accB[key] = b[key];
}
return [accA, accB];
},
[{}, {}] as [Partial<T>, Partial<T>],
);
}