added log for site settings and user settings
This commit is contained in:
1124
pnpm-lock.yaml
generated
1124
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,6 @@
|
|||||||
import { authClient } from '@/lib/auth-client';
|
import { authClient } from '@/lib/auth-client';
|
||||||
import { m } from '@/paraglide/messages';
|
import { m } from '@/paraglide/messages';
|
||||||
|
import { uploadProfileImage } from '@/service/profile.api';
|
||||||
import { ProfileInput, profileUpdateSchema } from '@/service/profile.schema';
|
import { ProfileInput, profileUpdateSchema } from '@/service/profile.schema';
|
||||||
import { UserCircleIcon } from '@phosphor-icons/react';
|
import { UserCircleIcon } from '@phosphor-icons/react';
|
||||||
import { useForm } from '@tanstack/react-form';
|
import { useForm } from '@tanstack/react-form';
|
||||||
@@ -35,9 +36,21 @@ const ProfileForm = () => {
|
|||||||
},
|
},
|
||||||
onSubmit: async ({ value }) => {
|
onSubmit: async ({ value }) => {
|
||||||
try {
|
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(
|
await authClient.updateUser(
|
||||||
{
|
{
|
||||||
name: value.name,
|
name: value.name,
|
||||||
|
image: imageKey,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
|
|||||||
@@ -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 };
|
||||||
|
});
|
||||||
|
|||||||
@@ -1,37 +1,50 @@
|
|||||||
import { prisma } from '@/db';
|
import { prisma } from '@/db';
|
||||||
import { Setting } from '@/generated/prisma/client';
|
import { Setting } from '@/generated/prisma/client';
|
||||||
import { authMiddleware } from '@/lib/middleware';
|
import { authMiddleware } from '@/lib/middleware';
|
||||||
|
import { extractDiffObjects } from '@/utils/help';
|
||||||
import { createServerFn } from '@tanstack/react-start';
|
import { createServerFn } from '@tanstack/react-start';
|
||||||
|
import { createAuditLog } from './audit.api';
|
||||||
import { settingSchema, userSettingSchema } from './setting.schema';
|
import { settingSchema, userSettingSchema } from './setting.schema';
|
||||||
|
|
||||||
type AdminSettingReturn = {
|
type AdminSettingReturn = {
|
||||||
[key: string]: Setting;
|
[key: string]: Pick<Setting, 'id' | 'key' | 'value'> | string;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Settings for admin
|
async function getAllAdminSettings(valueOnly = false) {
|
||||||
export const getAdminSettings = createServerFn({ method: 'GET' })
|
|
||||||
.middleware([authMiddleware])
|
|
||||||
.handler(async () => {
|
|
||||||
console.log('first');
|
|
||||||
const settings = await prisma.setting.findMany({
|
const settings = await prisma.setting.findMany({
|
||||||
where: {
|
where: {
|
||||||
relation: 'admin',
|
relation: 'admin',
|
||||||
},
|
},
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
key: true,
|
||||||
|
value: true,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const results: AdminSettingReturn = {};
|
const results: AdminSettingReturn = {};
|
||||||
|
|
||||||
settings.forEach((setting) => {
|
settings.forEach((setting) => {
|
||||||
results[setting.key] = setting;
|
results[setting.key] = valueOnly ? setting.value : setting;
|
||||||
});
|
});
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Settings for admin
|
||||||
|
export const getAdminSettings = createServerFn({ method: 'GET' })
|
||||||
|
.middleware([authMiddleware])
|
||||||
|
.handler(async () => {
|
||||||
|
return await getAllAdminSettings();
|
||||||
});
|
});
|
||||||
|
|
||||||
export const updateAdminSettings = createServerFn({ method: 'POST' })
|
export const updateAdminSettings = createServerFn({ method: 'POST' })
|
||||||
.inputValidator(settingSchema)
|
.inputValidator(settingSchema)
|
||||||
.middleware([authMiddleware])
|
.middleware([authMiddleware])
|
||||||
.handler(async ({ data }) => {
|
.handler(async ({ data, context }) => {
|
||||||
|
try {
|
||||||
|
const oldSetting = await getAllAdminSettings(true);
|
||||||
|
|
||||||
// Update each setting
|
// Update each setting
|
||||||
const updates = Object.entries(data).map(([key, value]) =>
|
const updates = Object.entries(data).map(([key, value]) =>
|
||||||
prisma.setting.upsert({
|
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({
|
await createAuditLog({
|
||||||
// action: 'update',
|
action: 'update',
|
||||||
// tableName: 'setting',
|
tableName: 'setting',
|
||||||
// recordId: '',
|
recordId: listId,
|
||||||
// oldValue: '',
|
oldValue: JSON.stringify(oldValue),
|
||||||
// newValue: JSON.stringify(data),
|
newValue: JSON.stringify(newValue),
|
||||||
// userId: '',
|
userId: context.user.id,
|
||||||
// });
|
});
|
||||||
|
|
||||||
return { success: true };
|
return { success: true };
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Setting for user
|
// Setting for user
|
||||||
@@ -76,10 +98,15 @@ export const getUserSettings = createServerFn({ method: 'GET' })
|
|||||||
relation: 'user',
|
relation: 'user',
|
||||||
key: context.user.id,
|
key: context.user.id,
|
||||||
},
|
},
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
key: true,
|
||||||
|
value: true,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
settings: settings as Setting,
|
settings,
|
||||||
value: JSON.parse(settings.value) as UserSetting,
|
value: JSON.parse(settings.value) as UserSetting,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -93,7 +120,17 @@ export const updateUserSettings = createServerFn({ method: 'POST' })
|
|||||||
.handler(async ({ data, context }) => {
|
.handler(async ({ data, context }) => {
|
||||||
// Update each setting
|
// Update each setting
|
||||||
try {
|
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 },
|
where: { key: context.user.id },
|
||||||
update: { value: JSON.stringify(data) },
|
update: { value: JSON.stringify(data) },
|
||||||
create: {
|
create: {
|
||||||
@@ -104,14 +141,16 @@ export const updateUserSettings = createServerFn({ method: 'POST' })
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// await createAuditLog({
|
if (oldSetting) {
|
||||||
// action: 'update',
|
await createAuditLog({
|
||||||
// tableName: 'setting',
|
action: 'update',
|
||||||
// recordId: '',
|
tableName: 'setting',
|
||||||
// oldValue: '',
|
recordId: updated.id,
|
||||||
// newValue: '',
|
oldValue: oldSetting.value,
|
||||||
// userId: '',
|
newValue: JSON.stringify(data),
|
||||||
// });
|
userId: context.user.id,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return { success: true };
|
return { success: true };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
26
src/utils/disk-storage.ts
Normal file
26
src/utils/disk-storage.ts
Normal 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'}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,3 +7,21 @@ export function jsonSupport(jsonSTR: string) {
|
|||||||
return jsonSTR;
|
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>],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user