feat: add file upload and management features; enhance pagination and permissions handling
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
import { useState } from 'react';
|
||||
|
||||
import { Button } from '@kit/ui/button';
|
||||
|
||||
import type { CmsFieldType } from '../schema/module.schema';
|
||||
import { FieldRenderer } from './field-renderer';
|
||||
|
||||
@@ -114,13 +116,13 @@ export function ModuleForm({
|
||||
))}
|
||||
|
||||
<div className="flex justify-end gap-2 border-t pt-4">
|
||||
<button
|
||||
<Button
|
||||
type="submit"
|
||||
disabled={isLoading}
|
||||
className="bg-primary text-primary-foreground hover:bg-primary/90 inline-flex items-center justify-center rounded-md px-4 py-2 text-sm font-medium disabled:opacity-50"
|
||||
data-test="module-record-submit-btn"
|
||||
>
|
||||
{isLoading ? 'Wird gespeichert...' : 'Speichern'}
|
||||
</button>
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
'use client';
|
||||
|
||||
import { useTranslations } from 'next-intl';
|
||||
|
||||
import { formatDate } from '@kit/shared/dates';
|
||||
|
||||
import type { CmsFieldType } from '../schema/module.schema';
|
||||
@@ -56,6 +58,7 @@ export function ModuleTable({
|
||||
onSelectionChange,
|
||||
currentSort,
|
||||
}: ModuleTableProps) {
|
||||
const t = useTranslations('cms.modules');
|
||||
const visibleFields = fields
|
||||
.filter((f) => f.show_in_table)
|
||||
.sort((a, b) => a.sort_order - b.sort_order);
|
||||
@@ -137,7 +140,7 @@ export function ModuleTable({
|
||||
colSpan={visibleFields.length + (onSelectionChange ? 1 : 0)}
|
||||
className="text-muted-foreground p-8 text-center"
|
||||
>
|
||||
Keine Datensätze gefunden
|
||||
{t('noRecords')}
|
||||
</td>
|
||||
</tr>
|
||||
) : (
|
||||
@@ -183,8 +186,11 @@ export function ModuleTable({
|
||||
{pagination.totalPages > 1 && (
|
||||
<div className="flex items-center justify-between px-2">
|
||||
<span className="text-muted-foreground text-sm">
|
||||
{pagination.total} Datensätze — Seite {pagination.page} von{' '}
|
||||
{pagination.totalPages}
|
||||
{t('paginationSummary', {
|
||||
total: pagination.total,
|
||||
page: pagination.page,
|
||||
totalPages: pagination.totalPages,
|
||||
})}
|
||||
</span>
|
||||
<div className="flex gap-1">
|
||||
<button
|
||||
@@ -192,14 +198,14 @@ export function ModuleTable({
|
||||
disabled={pagination.page <= 1}
|
||||
className="hover:bg-muted rounded border px-3 py-1 text-sm disabled:opacity-50"
|
||||
>
|
||||
← Zurück
|
||||
{t('paginationPrevious')}
|
||||
</button>
|
||||
<button
|
||||
onClick={() => onPageChange(pagination.page + 1)}
|
||||
disabled={pagination.page >= pagination.totalPages}
|
||||
className="hover:bg-muted rounded border px-3 py-1 text-sm disabled:opacity-50"
|
||||
>
|
||||
Weiter →
|
||||
{t('paginationNext')}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
'use server';
|
||||
|
||||
import { z } from 'zod';
|
||||
|
||||
import { authActionClient } from '@kit/next/safe-action';
|
||||
import { getLogger } from '@kit/shared/logger';
|
||||
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||
|
||||
import { createModuleBuilderApi } from '../api';
|
||||
|
||||
export const uploadFile = authActionClient
|
||||
.inputSchema(
|
||||
z.object({
|
||||
accountId: z.string().uuid(),
|
||||
fileName: z.string().min(1),
|
||||
fileType: z.string().min(1),
|
||||
fileSize: z
|
||||
.number()
|
||||
.int()
|
||||
.min(1)
|
||||
.max(10 * 1024 * 1024), // 10MB max
|
||||
base64: z.string().min(1),
|
||||
moduleName: z.string().optional(),
|
||||
fieldName: z.string().optional(),
|
||||
recordId: z.string().uuid().optional(),
|
||||
}),
|
||||
)
|
||||
.action(async ({ parsedInput: input, ctx }) => {
|
||||
const client = getSupabaseServerClient();
|
||||
const logger = await getLogger();
|
||||
const api = createModuleBuilderApi(client);
|
||||
|
||||
logger.info(
|
||||
{ name: 'files.upload', fileName: input.fileName },
|
||||
'Uploading file...',
|
||||
);
|
||||
|
||||
const data = await api.files.uploadFile({
|
||||
accountId: input.accountId,
|
||||
userId: ctx.user.id,
|
||||
file: {
|
||||
name: input.fileName,
|
||||
type: input.fileType,
|
||||
size: input.fileSize,
|
||||
base64: input.base64,
|
||||
},
|
||||
moduleName: input.moduleName,
|
||||
fieldName: input.fieldName,
|
||||
recordId: input.recordId,
|
||||
});
|
||||
|
||||
logger.info({ name: 'files.upload' }, 'File uploaded');
|
||||
|
||||
return { success: true, data };
|
||||
});
|
||||
|
||||
export const deleteFile = authActionClient
|
||||
.inputSchema(z.object({ fileId: z.string().uuid() }))
|
||||
.action(async ({ parsedInput: { fileId } }) => {
|
||||
const client = getSupabaseServerClient();
|
||||
const logger = await getLogger();
|
||||
const api = createModuleBuilderApi(client);
|
||||
|
||||
logger.info({ name: 'files.delete', fileId }, 'Deleting file...');
|
||||
await api.files.deleteFile(fileId);
|
||||
logger.info({ name: 'files.delete' }, 'File deleted');
|
||||
|
||||
return { success: true };
|
||||
});
|
||||
@@ -1,5 +1,7 @@
|
||||
'use server';
|
||||
|
||||
import { z } from 'zod';
|
||||
|
||||
import { authActionClient } from '@kit/next/safe-action';
|
||||
import { getLogger } from '@kit/shared/logger';
|
||||
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||
@@ -69,3 +71,125 @@ export const deleteModule = authActionClient
|
||||
|
||||
return { success: true };
|
||||
});
|
||||
|
||||
// ── Permissions ──────────────────────────────────────────────────
|
||||
|
||||
export const upsertModulePermission = authActionClient
|
||||
.inputSchema(
|
||||
z.object({
|
||||
moduleId: z.string().uuid(),
|
||||
role: z.string().min(1),
|
||||
canRead: z.boolean(),
|
||||
canInsert: z.boolean(),
|
||||
canUpdate: z.boolean(),
|
||||
canDelete: z.boolean(),
|
||||
canExport: z.boolean(),
|
||||
canImport: z.boolean(),
|
||||
canLock: z.boolean(),
|
||||
canBulkEdit: z.boolean(),
|
||||
canManage: z.boolean(),
|
||||
canPrint: z.boolean(),
|
||||
}),
|
||||
)
|
||||
.action(async ({ parsedInput: input }) => {
|
||||
const client = getSupabaseServerClient();
|
||||
const logger = await getLogger();
|
||||
const api = createModuleBuilderApi(client);
|
||||
|
||||
logger.info(
|
||||
{
|
||||
name: 'modules.permissions.upsert',
|
||||
moduleId: input.moduleId,
|
||||
role: input.role,
|
||||
},
|
||||
'Upserting module permission...',
|
||||
);
|
||||
|
||||
const data = await api.modules.upsertPermission({
|
||||
moduleId: input.moduleId,
|
||||
role: input.role,
|
||||
can_read: input.canRead,
|
||||
can_insert: input.canInsert,
|
||||
can_update: input.canUpdate,
|
||||
can_delete: input.canDelete,
|
||||
can_export: input.canExport,
|
||||
can_import: input.canImport,
|
||||
can_lock: input.canLock,
|
||||
can_bulk_edit: input.canBulkEdit,
|
||||
can_manage: input.canManage,
|
||||
can_print: input.canPrint,
|
||||
});
|
||||
|
||||
logger.info(
|
||||
{
|
||||
name: 'modules.permissions.upsert',
|
||||
moduleId: input.moduleId,
|
||||
role: input.role,
|
||||
},
|
||||
'Module permission upserted',
|
||||
);
|
||||
|
||||
return { success: true, data };
|
||||
});
|
||||
|
||||
// ── Relations ────────────────────────────────────────────────────
|
||||
|
||||
export const createModuleRelation = authActionClient
|
||||
.inputSchema(
|
||||
z.object({
|
||||
sourceModuleId: z.string().uuid(),
|
||||
sourceFieldId: z.string().uuid(),
|
||||
targetModuleId: z.string().uuid(),
|
||||
targetFieldId: z.string().uuid().optional(),
|
||||
relationType: z.enum(['has_one', 'has_many', 'belongs_to']),
|
||||
}),
|
||||
)
|
||||
.action(async ({ parsedInput: input }) => {
|
||||
const client = getSupabaseServerClient();
|
||||
const logger = await getLogger();
|
||||
const api = createModuleBuilderApi(client);
|
||||
|
||||
logger.info(
|
||||
{
|
||||
name: 'modules.relations.create',
|
||||
sourceModuleId: input.sourceModuleId,
|
||||
targetModuleId: input.targetModuleId,
|
||||
},
|
||||
'Creating module relation...',
|
||||
);
|
||||
|
||||
const data = await api.modules.createRelation(input);
|
||||
|
||||
logger.info(
|
||||
{ name: 'modules.relations.create', relationId: data.id },
|
||||
'Module relation created',
|
||||
);
|
||||
|
||||
return { success: true, data };
|
||||
});
|
||||
|
||||
export const deleteModuleRelation = authActionClient
|
||||
.inputSchema(
|
||||
z.object({
|
||||
relationId: z.string().uuid(),
|
||||
}),
|
||||
)
|
||||
.action(async ({ parsedInput: { relationId } }) => {
|
||||
const client = getSupabaseServerClient();
|
||||
const logger = await getLogger();
|
||||
const api = createModuleBuilderApi(client);
|
||||
|
||||
logger.info(
|
||||
{ name: 'modules.relations.delete', relationId },
|
||||
'Deleting module relation...',
|
||||
);
|
||||
|
||||
await api.modules.deleteRelation(relationId);
|
||||
|
||||
logger.info(
|
||||
{ name: 'modules.relations.delete', relationId },
|
||||
'Module relation deleted',
|
||||
);
|
||||
|
||||
return { success: true };
|
||||
});
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { SupabaseClient } from '@supabase/supabase-js';
|
||||
import type { Database } from '@kit/supabase/database';
|
||||
|
||||
import { createAuditService } from './services/audit.service';
|
||||
import { createFileService } from './services/file.service';
|
||||
import { createModuleDefinitionService } from './services/module-definition.service';
|
||||
import { createModuleQueryService } from './services/module-query.service';
|
||||
import { createRecordCrudService } from './services/record-crud.service';
|
||||
@@ -20,5 +21,6 @@ export function createModuleBuilderApi(client: SupabaseClient<Database>) {
|
||||
query: createModuleQueryService(client),
|
||||
records: createRecordCrudService(client),
|
||||
audit: createAuditService(client),
|
||||
files: createFileService(client),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
import type { SupabaseClient } from '@supabase/supabase-js';
|
||||
|
||||
import type { Database } from '@kit/supabase/database';
|
||||
|
||||
interface UploadFileInput {
|
||||
accountId: string;
|
||||
userId: string;
|
||||
file: {
|
||||
name: string;
|
||||
type: string;
|
||||
size: number;
|
||||
base64: string;
|
||||
};
|
||||
moduleName?: string;
|
||||
fieldName?: string;
|
||||
recordId?: string;
|
||||
}
|
||||
|
||||
interface ListFilesOptions {
|
||||
moduleName?: string;
|
||||
recordId?: string;
|
||||
search?: string;
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
}
|
||||
|
||||
export function createFileService(client: SupabaseClient<Database>) {
|
||||
return {
|
||||
async uploadFile(input: UploadFileInput) {
|
||||
const path = `${input.accountId}/${Date.now()}-${input.file.name}`;
|
||||
const buffer = Buffer.from(input.file.base64, 'base64');
|
||||
|
||||
const { error: uploadError } = await client.storage
|
||||
.from('cms-files')
|
||||
.upload(path, buffer, {
|
||||
contentType: input.file.type,
|
||||
});
|
||||
|
||||
if (uploadError) throw uploadError;
|
||||
|
||||
const { data, error } = await client
|
||||
.from('cms_files')
|
||||
.insert({
|
||||
account_id: input.accountId,
|
||||
record_id: input.recordId ?? null,
|
||||
module_name: input.moduleName ?? null,
|
||||
field_name: input.fieldName ?? null,
|
||||
file_name: input.file.name,
|
||||
original_name: input.file.name,
|
||||
mime_type: input.file.type,
|
||||
file_size: input.file.size,
|
||||
storage_path: path,
|
||||
created_by: input.userId,
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (error) throw error;
|
||||
return data;
|
||||
},
|
||||
|
||||
async listFiles(accountId: string, opts?: ListFilesOptions) {
|
||||
let q = client
|
||||
.from('cms_files')
|
||||
.select('*', { count: 'exact' })
|
||||
.eq('account_id', accountId)
|
||||
.order('created_at', { ascending: false });
|
||||
|
||||
if (opts?.moduleName) {
|
||||
q = q.eq('module_name', opts.moduleName);
|
||||
}
|
||||
|
||||
if (opts?.recordId) {
|
||||
q = q.eq('record_id', opts.recordId);
|
||||
}
|
||||
|
||||
if (opts?.search) {
|
||||
q = q.ilike('file_name', `%${opts.search}%`);
|
||||
}
|
||||
|
||||
const page = opts?.page ?? 1;
|
||||
const pageSize = opts?.pageSize ?? 25;
|
||||
q = q.range((page - 1) * pageSize, page * pageSize - 1);
|
||||
|
||||
const { data, error, count } = await q;
|
||||
|
||||
if (error) throw error;
|
||||
return { data: data ?? [], total: count ?? 0, page, pageSize };
|
||||
},
|
||||
|
||||
async deleteFile(fileId: string) {
|
||||
const { data: file, error: getErr } = await client
|
||||
.from('cms_files')
|
||||
.select('storage_path')
|
||||
.eq('id', fileId)
|
||||
.single();
|
||||
|
||||
if (getErr) throw getErr;
|
||||
|
||||
await client.storage.from('cms-files').remove([file.storage_path]);
|
||||
|
||||
const { error } = await client
|
||||
.from('cms_files')
|
||||
.delete()
|
||||
.eq('id', fileId);
|
||||
|
||||
if (error) throw error;
|
||||
},
|
||||
|
||||
getPublicUrl(storagePath: string) {
|
||||
return client.storage.from('cms-files').getPublicUrl(storagePath).data
|
||||
.publicUrl;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -144,5 +144,83 @@ export function createModuleDefinitionService(
|
||||
|
||||
if (error) throw error;
|
||||
},
|
||||
|
||||
// ── Permissions ──────────────────────────────────────────────
|
||||
|
||||
async listPermissions(moduleId: string) {
|
||||
const { data, error } = await client
|
||||
.from('module_permissions')
|
||||
.select('*')
|
||||
.eq('module_id', moduleId);
|
||||
|
||||
if (error) throw error;
|
||||
return data ?? [];
|
||||
},
|
||||
|
||||
async upsertPermission(input: {
|
||||
moduleId: string;
|
||||
role: string;
|
||||
[key: string]: unknown;
|
||||
}) {
|
||||
const { moduleId, role, ...perms } = input;
|
||||
|
||||
const { data, error } = await client
|
||||
.from('module_permissions')
|
||||
.upsert(
|
||||
{ module_id: moduleId, role, ...perms },
|
||||
{ onConflict: 'module_id,role' },
|
||||
)
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (error) throw error;
|
||||
return data;
|
||||
},
|
||||
|
||||
// ── Relations ────────────────────────────────────────────────
|
||||
|
||||
async listRelations(moduleId: string) {
|
||||
const { data, error } = await client
|
||||
.from('module_relations')
|
||||
.select(
|
||||
'*, source_module:modules!module_relations_source_module_id_fkey(id, display_name), target_module:modules!module_relations_target_module_id_fkey(id, display_name), source_field:module_fields!module_relations_source_field_id_fkey(id, name, display_name)',
|
||||
)
|
||||
.or(`source_module_id.eq.${moduleId},target_module_id.eq.${moduleId}`);
|
||||
|
||||
if (error) throw error;
|
||||
return data ?? [];
|
||||
},
|
||||
|
||||
async createRelation(input: {
|
||||
sourceModuleId: string;
|
||||
sourceFieldId: string;
|
||||
targetModuleId: string;
|
||||
targetFieldId?: string;
|
||||
relationType: string;
|
||||
}) {
|
||||
const { data, error } = await client
|
||||
.from('module_relations')
|
||||
.insert({
|
||||
source_module_id: input.sourceModuleId,
|
||||
source_field_id: input.sourceFieldId,
|
||||
target_module_id: input.targetModuleId,
|
||||
target_field_id: input.targetFieldId ?? null,
|
||||
relation_type: input.relationType,
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (error) throw error;
|
||||
return data;
|
||||
},
|
||||
|
||||
async deleteRelation(relationId: string) {
|
||||
const { error } = await client
|
||||
.from('module_relations')
|
||||
.delete()
|
||||
.eq('id', relationId);
|
||||
|
||||
if (error) throw error;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user