Initial state for GitNexus analysis
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
'use server';
|
||||
|
||||
import { authActionClient } from '@kit/next/safe-action';
|
||||
import { getLogger } from '@kit/shared/logger';
|
||||
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||
|
||||
import { CreateModuleSchema, UpdateModuleSchema } from '../../schema/module.schema';
|
||||
import { createModuleBuilderApi } from '../api';
|
||||
|
||||
export const createModule = authActionClient
|
||||
.inputSchema(CreateModuleSchema)
|
||||
.action(async ({ parsedInput: input }) => {
|
||||
const client = getSupabaseServerClient();
|
||||
const logger = await getLogger();
|
||||
const api = createModuleBuilderApi(client);
|
||||
|
||||
logger.info({ name: 'modules.create', moduleName: input.name }, 'Creating module...');
|
||||
|
||||
const module = await api.modules.createModule(input);
|
||||
|
||||
logger.info({ name: 'modules.create', moduleId: module.id }, 'Module created');
|
||||
|
||||
return { success: true, module };
|
||||
});
|
||||
|
||||
export const updateModule = authActionClient
|
||||
.inputSchema(UpdateModuleSchema)
|
||||
.action(async ({ parsedInput: input }) => {
|
||||
const client = getSupabaseServerClient();
|
||||
const logger = await getLogger();
|
||||
const api = createModuleBuilderApi(client);
|
||||
|
||||
logger.info({ name: 'modules.update', moduleId: input.moduleId }, 'Updating module...');
|
||||
|
||||
const module = await api.modules.updateModule(input);
|
||||
|
||||
logger.info({ name: 'modules.update', moduleId: module.id }, 'Module updated');
|
||||
|
||||
return { success: true, module };
|
||||
});
|
||||
|
||||
export const deleteModule = authActionClient
|
||||
.inputSchema(UpdateModuleSchema.pick({ moduleId: true }))
|
||||
.action(async ({ parsedInput: { moduleId } }) => {
|
||||
const client = getSupabaseServerClient();
|
||||
const logger = await getLogger();
|
||||
const api = createModuleBuilderApi(client);
|
||||
|
||||
logger.info({ name: 'modules.delete', moduleId }, 'Archiving module...');
|
||||
|
||||
await api.modules.deleteModule(moduleId);
|
||||
|
||||
logger.info({ name: 'modules.delete', moduleId }, 'Module archived');
|
||||
|
||||
return { success: true };
|
||||
});
|
||||
@@ -0,0 +1,132 @@
|
||||
'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 { CreateRecordSchema, UpdateRecordSchema, DeleteRecordSchema, LockRecordSchema } from '../../schema/module-record.schema';
|
||||
import { createModuleBuilderApi } from '../api';
|
||||
import { validateRecordData } from '../services/record-validation.service';
|
||||
|
||||
export const createRecord = authActionClient
|
||||
.inputSchema(CreateRecordSchema)
|
||||
.action(async ({ parsedInput: input, ctx }) => {
|
||||
const client = getSupabaseServerClient();
|
||||
const logger = await getLogger();
|
||||
const api = createModuleBuilderApi(client);
|
||||
const userId = ctx.user.id;
|
||||
|
||||
// Get field definitions for validation
|
||||
const moduleWithFields = await api.modules.getModuleWithFields(input.moduleId);
|
||||
|
||||
if (!moduleWithFields) {
|
||||
throw new Error('Module not found');
|
||||
}
|
||||
|
||||
// Validate data against field definitions
|
||||
const fields = (moduleWithFields as unknown as { fields: Array<{ name: string; field_type: string; is_required: boolean; min_value?: number | null; max_value?: number | null; max_length?: number | null; regex_pattern?: string | null }> }).fields;
|
||||
const validation = validateRecordData(
|
||||
input.data as Record<string, unknown>,
|
||||
fields as Parameters<typeof validateRecordData>[1],
|
||||
);
|
||||
|
||||
if (!validation.success) {
|
||||
return { success: false, errors: validation.errors };
|
||||
}
|
||||
|
||||
logger.info({ name: 'records.create', moduleId: input.moduleId }, 'Creating record...');
|
||||
|
||||
const record = await api.records.createRecord(input, userId);
|
||||
|
||||
// Write audit log
|
||||
await api.audit.log({
|
||||
accountId: input.accountId,
|
||||
userId,
|
||||
tableName: 'module_records',
|
||||
recordId: record.id,
|
||||
action: 'insert',
|
||||
newData: input.data as Record<string, unknown>,
|
||||
});
|
||||
|
||||
return { success: true, record };
|
||||
});
|
||||
|
||||
export const updateRecord = authActionClient
|
||||
.inputSchema(UpdateRecordSchema.extend({ accountId: z.string().uuid() }))
|
||||
.action(async ({ parsedInput: input, ctx }) => {
|
||||
const client = getSupabaseServerClient();
|
||||
const logger = await getLogger();
|
||||
const api = createModuleBuilderApi(client);
|
||||
const userId = ctx.user.id;
|
||||
|
||||
// Get existing record for audit
|
||||
const existing = await api.records.getRecord(input.recordId);
|
||||
|
||||
logger.info({ name: 'records.update', recordId: input.recordId }, 'Updating record...');
|
||||
|
||||
const record = await api.records.updateRecord(input, userId);
|
||||
|
||||
// Write audit log
|
||||
await api.audit.log({
|
||||
accountId: input.accountId,
|
||||
userId,
|
||||
tableName: 'module_records',
|
||||
recordId: record.id,
|
||||
action: 'update',
|
||||
oldData: existing.data as Record<string, unknown>,
|
||||
newData: input.data as Record<string, unknown>,
|
||||
});
|
||||
|
||||
return { success: true, record };
|
||||
});
|
||||
|
||||
export const deleteRecord = authActionClient
|
||||
.inputSchema(DeleteRecordSchema.extend({ accountId: z.string().uuid() }))
|
||||
.action(async ({ parsedInput: input, ctx }) => {
|
||||
const client = getSupabaseServerClient();
|
||||
const logger = await getLogger();
|
||||
const api = createModuleBuilderApi(client);
|
||||
const userId = ctx.user.id;
|
||||
|
||||
// Get existing record for audit
|
||||
const existing = await api.records.getRecord(input.recordId);
|
||||
|
||||
logger.info({ name: 'records.delete', recordId: input.recordId, hard: input.hard }, 'Deleting record...');
|
||||
|
||||
await api.records.deleteRecord(input);
|
||||
|
||||
// Write audit log
|
||||
await api.audit.log({
|
||||
accountId: input.accountId,
|
||||
userId,
|
||||
tableName: 'module_records',
|
||||
recordId: input.recordId,
|
||||
action: 'delete',
|
||||
oldData: existing.data as Record<string, unknown>,
|
||||
});
|
||||
|
||||
return { success: true };
|
||||
});
|
||||
|
||||
export const lockRecord = authActionClient
|
||||
.inputSchema(LockRecordSchema.extend({ accountId: z.string().uuid() }))
|
||||
.action(async ({ parsedInput: input, ctx }) => {
|
||||
const client = getSupabaseServerClient();
|
||||
const api = createModuleBuilderApi(client);
|
||||
const userId = ctx.user.id;
|
||||
|
||||
const record = await api.records.lockRecord(input, userId);
|
||||
|
||||
await api.audit.log({
|
||||
accountId: input.accountId,
|
||||
userId,
|
||||
tableName: 'module_records',
|
||||
recordId: input.recordId,
|
||||
action: 'lock',
|
||||
newData: { locked: input.lock },
|
||||
});
|
||||
|
||||
return { success: true, record };
|
||||
});
|
||||
Reference in New Issue
Block a user