feat: add file upload and management features; enhance pagination and permissions handling
This commit is contained in:
@@ -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 };
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user