fix(media): prevent attaching an asset to a target twice
Drop `position` from the `asset_usages` unique constraint so the same asset can't be attached to one target/slot more than once, and update the insert's onConflict target + lookup to match the narrower key. Guard it at the service layer too: attachAssetService now rejects an attach when the asset already has a usage for that target with a new ASSET_ALREADY_ATTACHED (409) error, backed by an assetUsageExistsForTarget query.
This commit is contained in:
@@ -17,6 +17,7 @@ import type {
|
||||
} from '@cio/utils/validation/assets';
|
||||
import type { TTranscriptResponse, TUpdateTranscript } from '@cio/utils/validation/media';
|
||||
import {
|
||||
assetUsageExistsForTarget,
|
||||
createAssetUsage,
|
||||
createHlsAssetPlaceholder,
|
||||
createOrGetAssetByStorageKey,
|
||||
@@ -503,6 +504,12 @@ export async function getAssetUsageGraphService(orgId: string, assetId: string)
|
||||
export async function attachAssetService(orgId: string, assetId: string, profileId: string, input: TAssetAttach) {
|
||||
try {
|
||||
await getAssetService(orgId, assetId);
|
||||
|
||||
const alreadyAttached = await assetUsageExistsForTarget(assetId, orgId, input.targetType, input.targetId);
|
||||
if (alreadyAttached) {
|
||||
throw new AppError('Asset is already attached to this target', ErrorCodes.ASSET_ALREADY_ATTACHED, 409);
|
||||
}
|
||||
|
||||
return await createAssetUsage({
|
||||
organizationId: orgId,
|
||||
assetId,
|
||||
|
||||
Reference in New Issue
Block a user