2026-01-12 21:16:52 +01:00
|
|
|
"use server";
|
2026-01-13 22:22:01 +01:00
|
|
|
|
|
|
|
|
import {eq} from 'drizzle-orm';
|
|
|
|
|
import * as drizzleDb from '@/db';
|
|
|
|
|
import {db} from '@/db';
|
|
|
|
|
import type {StorageInput, StorageProviderKind, StorageResult,} from './types';
|
|
|
|
|
import {dispatchViaProvider} from "@/features/storages/providers";
|
2026-01-12 21:16:52 +01:00
|
|
|
|
|
|
|
|
export async function dispatchStorage(
|
2026-01-13 22:22:01 +01:00
|
|
|
input: StorageInput,
|
2026-01-12 21:16:52 +01:00
|
|
|
policyId?: string,
|
|
|
|
|
channelId?: string,
|
|
|
|
|
organizationId?: string
|
2026-01-13 22:22:01 +01:00
|
|
|
): Promise<StorageResult> {
|
2026-01-12 21:16:52 +01:00
|
|
|
try {
|
2026-01-13 22:22:01 +01:00
|
|
|
if (!channelId) {
|
2026-01-12 21:16:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
provider: null,
|
2026-01-13 22:22:01 +01:00
|
|
|
error: 'No storage channel provided',
|
2026-01-12 21:16:52 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-13 22:22:01 +01:00
|
|
|
const channel = await db.query.storageChannel.findFirst({
|
|
|
|
|
where: eq(drizzleDb.schemas.storageChannel.id, channelId),
|
|
|
|
|
});
|
2026-01-12 21:16:52 +01:00
|
|
|
|
2026-01-13 22:22:01 +01:00
|
|
|
if (!channel || !channel.enabled) {
|
2026-01-12 21:16:52 +01:00
|
|
|
return {
|
|
|
|
|
success: false,
|
2026-01-13 22:22:01 +01:00
|
|
|
provider: channel?.provider as StorageProviderKind,
|
|
|
|
|
error: 'Storage channel not found or disabled',
|
2026-01-12 21:16:52 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-13 22:22:01 +01:00
|
|
|
return await dispatchViaProvider(
|
|
|
|
|
channel.provider as StorageProviderKind,
|
2026-01-12 21:16:52 +01:00
|
|
|
channel.config,
|
2026-01-13 22:22:01 +01:00
|
|
|
input
|
2026-01-12 21:16:52 +01:00
|
|
|
);
|
|
|
|
|
} catch (err: any) {
|
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
provider: null,
|
2026-01-13 22:22:01 +01:00
|
|
|
error: err.message || 'Unexpected storage dispatch error',
|
2026-01-12 21:16:52 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|