This commit is contained in:
Théo LAGACHE
2026-06-24 16:38:09 +02:00
parent 75dff0cc2c
commit 04c2bc8922
@@ -7,18 +7,18 @@ import {userAction} from "@/lib/safe-actions/actions";
import { db } from "@/db"; import { db } from "@/db";
import { and, eq } from "drizzle-orm"; import { and, eq } from "drizzle-orm";
import { withUpdatedAt } from "@/db/utils"; import { withUpdatedAt } from "@/db/utils";
import { import { StorageChannelFormSchema } from "@/features/channel/schemas/channel-form.schema";
StorageChannelFormSchema
} from "@/features/channel/schemas/channel-form.schema";
import { StorageChannel } from "@/db/schema/12_storage-channel"; import { StorageChannel } from "@/db/schema/12_storage-channel";
export const addStorageChannelAction = userAction
export const addStorageChannelAction = userAction.schema( .schema(
z.object({ z.object({
organizationId: z.string().optional(), organizationId: z.string().optional(),
data: StorageChannelFormSchema data: StorageChannelFormSchema,
}) }),
).action(async ({parsedInput}): Promise<ServerActionResult<StorageChannel>> => { )
.action(
async ({ parsedInput }): Promise<ServerActionResult<StorageChannel>> => {
const { organizationId, data } = parsedInput; const { organizationId, data } = parsedInput;
try { try {
@@ -29,7 +29,7 @@ export const addStorageChannelAction = userAction.schema(
name: data.name, name: data.name,
config: data.config, config: data.config,
enabled: data.enabled ?? true, enabled: data.enabled ?? true,
organizationId: organizationId ?? null organizationId: organizationId ?? null,
}) })
.returning(); .returning();
@@ -44,7 +44,7 @@ export const addStorageChannelAction = userAction.schema(
success: true, success: true,
value: { value: {
...channel, ...channel,
config: channel.config as JSON config: channel.config as JSON,
}, },
actionSuccess: { actionSuccess: {
message: "Storage channel has been successfully created.", message: "Storage channel has been successfully created.",
@@ -63,43 +63,35 @@ export const addStorageChannelAction = userAction.schema(
}, },
}; };
} }
}); },
);
export const removeStorageChannelAction = userAction.schema( export const removeStorageChannelAction = userAction
.schema(
z.object({ z.object({
organizationId: z.string().optional(), organizationId: z.string().optional(),
id: z.string(), id: z.string(),
}) }),
).action(async ({parsedInput}): Promise<ServerActionResult<StorageChannel>> => { )
.action(
async ({ parsedInput }): Promise<ServerActionResult<StorageChannel>> => {
const { organizationId, id } = parsedInput; const { organizationId, id } = parsedInput;
try { try {
const existing = await db.query.storageChannel.findFirst({
where: eq(drizzleDb.schemas.storageChannel.id, id),
});
if (!existing) {
return {
success: false,
actionError: { message: "Storage channel not found.", status: 404, messageParams: { id } },
};
}
if (existing.organizationId === null) {
return {
success: false,
actionError: { message: "System storage channels cannot be deleted.", status: 403, messageParams: { id } },
};
}
if (organizationId) { if (organizationId) {
await db await db
.delete(drizzleDb.schemas.organizationStorageChannel) .delete(drizzleDb.schemas.organizationStorageChannel)
.where( .where(
and( and(
eq(drizzleDb.schemas.organizationStorageChannel.organizationId, organizationId), eq(
eq(drizzleDb.schemas.organizationStorageChannel.storageChannelId, id) drizzleDb.schemas.organizationStorageChannel.organizationId,
) organizationId,
),
eq(
drizzleDb.schemas.organizationStorageChannel.storageChannelId,
id,
),
),
); );
} }
@@ -123,7 +115,7 @@ export const removeStorageChannelAction = userAction.schema(
success: true, success: true,
value: { value: {
...deletedChannel, ...deletedChannel,
config: deletedChannel.config as JSON config: deletedChannel.config as JSON,
}, },
actionSuccess: { actionSuccess: {
message: "Storage channel has been successfully removed.", message: "Storage channel has been successfully removed.",
@@ -142,26 +134,31 @@ export const removeStorageChannelAction = userAction.schema(
}, },
}; };
} }
}); },
);
export const updateStorageChannelAction = userAction
export const updateStorageChannelAction = userAction.schema( .schema(
z.object({ z.object({
id: z.string(), id: z.string(),
data: StorageChannelFormSchema data: StorageChannelFormSchema,
}) }),
).action(async ({parsedInput}): Promise<ServerActionResult<StorageChannel>> => { )
.action(
async ({ parsedInput }): Promise<ServerActionResult<StorageChannel>> => {
const { id, data } = parsedInput; const { id, data } = parsedInput;
try { try {
const [channel] = await db const [channel] = await db
.update(drizzleDb.schemas.storageChannel) .update(drizzleDb.schemas.storageChannel)
.set(withUpdatedAt({ .set(
withUpdatedAt({
provider: data.provider, provider: data.provider,
name: data.name, name: data.name,
config: data.config, config: data.config,
enabled: data.enabled ?? true, enabled: data.enabled ?? true,
})) }),
)
.where(eq(drizzleDb.schemas.storageChannel.id, id)) .where(eq(drizzleDb.schemas.storageChannel.id, id))
.returning(); .returning();
@@ -169,7 +166,7 @@ export const updateStorageChannelAction = userAction.schema(
success: true, success: true,
value: { value: {
...channel, ...channel,
config: channel.config as JSON config: channel.config as JSON,
}, },
actionSuccess: { actionSuccess: {
message: `Storage channel "${channel.name}" has been successfully updated.`, message: `Storage channel "${channel.name}" has been successfully updated.`,
@@ -188,4 +185,5 @@ export const updateStorageChannelAction = userAction.schema(
}, },
}; };
} }
}); },
);