{"version":3,"file":"media-usage-work-ByEuWySG.mjs","names":[],"sources":["../src/api/handlers/media-usage-work.ts"],"sourcesContent":["import type { Kysely } from \"kysely\";\n\nimport { MediaUsageWorkRepository } from \"../../database/repositories/media-usage-work.js\";\nimport { InvalidCursorError } from \"../../database/repositories/types.js\";\nimport type { Database } from \"../../database/types.js\";\nimport { MediaUsageCollectionDeletionRepository } from \"../../media/usage/collection-deletion.js\";\nimport { ErrorCode } from \"../errors.js\";\nimport type {\n\tMediaUsageWorkListQuery,\n\tMediaUsageWorkListResponse,\n\tMediaUsageWorkRetryRequest,\n\tMediaUsageWorkRetryResponse,\n\tMediaUsageCollectionDeletionListQuery,\n\tMediaUsageCollectionDeletionListResponse,\n\tMediaUsageCollectionDeletionRetryRequest,\n\tMediaUsageCollectionDeletionRetryResponse,\n} from \"../schemas/media-usage.js\";\nimport type { ApiResult } from \"../types.js\";\n\nexport type {\n\tMediaUsageWorkItem,\n\tMediaUsageWorkListQuery,\n\tMediaUsageWorkListResponse,\n\tMediaUsageWorkRetryRequest,\n\tMediaUsageWorkRetryResponse,\n} from \"../schemas/media-usage.js\";\n\nexport async function handleMediaUsageWorkList(\n\tdb: Kysely<Database>,\n\tquery: MediaUsageWorkListQuery,\n): Promise<ApiResult<MediaUsageWorkListResponse>> {\n\ttry {\n\t\tconst page = await new MediaUsageWorkRepository(db).findOperatorPage({\n\t\t\tcollectionSlug: query.collection,\n\t\t\tstate: query.state,\n\t\t\tcursor: query.cursor,\n\t\t\tlimit: query.limit,\n\t\t});\n\t\tif (!page) {\n\t\t\treturn {\n\t\t\t\tsuccess: false,\n\t\t\t\terror: {\n\t\t\t\t\tcode: ErrorCode.COLLECTION_NOT_FOUND,\n\t\t\t\t\tmessage: \"Collection not found\",\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\t\treturn { success: true, data: page };\n\t} catch (error) {\n\t\tif (error instanceof InvalidCursorError) {\n\t\t\treturn {\n\t\t\t\tsuccess: false,\n\t\t\t\terror: {\n\t\t\t\t\tcode: ErrorCode.INVALID_CURSOR,\n\t\t\t\t\tmessage: \"Invalid media usage work cursor\",\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\t\tconsole.error(\"[media-usage-work] list failed:\", error);\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\terror: {\n\t\t\t\tcode: ErrorCode.MEDIA_USAGE_WORK_LIST_ERROR,\n\t\t\t\tmessage: \"Failed to list media usage work\",\n\t\t\t},\n\t\t};\n\t}\n}\n\nexport async function handleMediaUsageCollectionDeletionList(\n\tdb: Kysely<Database>,\n\tquery: MediaUsageCollectionDeletionListQuery,\n): Promise<ApiResult<MediaUsageCollectionDeletionListResponse>> {\n\ttry {\n\t\treturn {\n\t\t\tsuccess: true,\n\t\t\tdata: await new MediaUsageCollectionDeletionRepository(db).findOperatorPage(query),\n\t\t};\n\t} catch (error) {\n\t\tif (error instanceof InvalidCursorError) {\n\t\t\treturn {\n\t\t\t\tsuccess: false,\n\t\t\t\terror: { code: ErrorCode.INVALID_CURSOR, message: \"Invalid cursor\" },\n\t\t\t};\n\t\t}\n\t\tconsole.error(\"[media-usage:collection-deletion] list failed:\", error);\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\terror: {\n\t\t\t\tcode: ErrorCode.MEDIA_USAGE_COLLECTION_DELETION_LIST_ERROR,\n\t\t\t\tmessage: \"Failed to list collection deletions\",\n\t\t\t},\n\t\t};\n\t}\n}\n\nexport async function handleMediaUsageCollectionDeletionRetry(\n\tdb: Kysely<Database>,\n\tinput: MediaUsageCollectionDeletionRetryRequest,\n): Promise<ApiResult<MediaUsageCollectionDeletionRetryResponse>> {\n\ttry {\n\t\tconst result = await new MediaUsageCollectionDeletionRepository(db).retryOperatorDeletion(\n\t\t\tinput,\n\t\t);\n\t\tif (result.outcome === \"pending\") {\n\t\t\treturn { success: true, data: { changed: result.changed, item: result.item } };\n\t\t}\n\t\tif (result.outcome === \"lease_active\") {\n\t\t\treturn {\n\t\t\t\tsuccess: false,\n\t\t\t\terror: {\n\t\t\t\t\tcode: ErrorCode.WORK_LEASE_ACTIVE,\n\t\t\t\t\tmessage: \"Collection deletion is currently leased\",\n\t\t\t\t\tdetails: { leaseExpiresAt: result.leaseExpiresAt },\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\terror: {\n\t\t\t\tcode: result.outcome === \"not_found\" ? ErrorCode.NOT_FOUND : ErrorCode.WORK_CHANGED,\n\t\t\t\tmessage:\n\t\t\t\t\tresult.outcome === \"not_found\"\n\t\t\t\t\t\t? \"Collection deletion not found\"\n\t\t\t\t\t\t: \"Collection deletion changed\",\n\t\t\t},\n\t\t};\n\t} catch (error) {\n\t\tconsole.error(\"[media-usage:collection-deletion] retry failed:\", error);\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\terror: {\n\t\t\t\tcode: ErrorCode.MEDIA_USAGE_COLLECTION_DELETION_RETRY_ERROR,\n\t\t\t\tmessage: \"Failed to retry collection deletion\",\n\t\t\t},\n\t\t};\n\t}\n}\n\nexport async function handleMediaUsageWorkRetry(\n\tdb: Kysely<Database>,\n\tinput: MediaUsageWorkRetryRequest,\n): Promise<ApiResult<MediaUsageWorkRetryResponse>> {\n\ttry {\n\t\tconst result = await new MediaUsageWorkRepository(db).retryOperatorWork(input);\n\t\tswitch (result.outcome) {\n\t\t\tcase \"pending\":\n\t\t\t\treturn {\n\t\t\t\t\tsuccess: true,\n\t\t\t\t\tdata: { changed: result.changed, item: result.work },\n\t\t\t\t};\n\t\t\tcase \"lease_active\":\n\t\t\t\treturn {\n\t\t\t\t\tsuccess: false,\n\t\t\t\t\terror: {\n\t\t\t\t\t\tcode: ErrorCode.WORK_LEASE_ACTIVE,\n\t\t\t\t\t\tmessage: \"Media usage work is currently leased\",\n\t\t\t\t\t\tdetails: { leaseExpiresAt: result.leaseExpiresAt },\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\tcase \"collection_not_found\":\n\t\t\t\treturn {\n\t\t\t\t\tsuccess: false,\n\t\t\t\t\terror: {\n\t\t\t\t\t\tcode: ErrorCode.COLLECTION_NOT_FOUND,\n\t\t\t\t\t\tmessage: \"Collection not found\",\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\tcase \"conflict\":\n\t\t\t\treturn {\n\t\t\t\t\tsuccess: false,\n\t\t\t\t\terror: {\n\t\t\t\t\t\tcode: ErrorCode.WORK_CHANGED,\n\t\t\t\t\t\tmessage: \"Media usage work changed; retry the request\",\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t}\n\t} catch (error) {\n\t\tconsole.error(\"[media-usage-work] retry failed:\", error);\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\terror: {\n\t\t\t\tcode: ErrorCode.MEDIA_USAGE_WORK_RETRY_ERROR,\n\t\t\t\tmessage: \"Failed to retry media usage work\",\n\t\t\t},\n\t\t};\n\t}\n}\n"],"mappings":";;;;;;AA2BA,eAAsB,yBACrB,IACA,OACiD;AACjD,KAAI;EACH,MAAM,OAAO,MAAM,IAAI,yBAAyB,GAAG,CAAC,iBAAiB;GACpE,gBAAgB,MAAM;GACtB,OAAO,MAAM;GACb,QAAQ,MAAM;GACd,OAAO,MAAM;GACb,CAAC;AACF,MAAI,CAAC,KACJ,QAAO;GACN,SAAS;GACT,OAAO;IACN,MAAM,UAAU;IAChB,SAAS;IACT;GACD;AAEF,SAAO;GAAE,SAAS;GAAM,MAAM;GAAM;UAC5B,OAAO;AACf,MAAI,iBAAiB,mBACpB,QAAO;GACN,SAAS;GACT,OAAO;IACN,MAAM,UAAU;IAChB,SAAS;IACT;GACD;AAEF,UAAQ,MAAM,mCAAmC,MAAM;AACvD,SAAO;GACN,SAAS;GACT,OAAO;IACN,MAAM,UAAU;IAChB,SAAS;IACT;GACD;;;AAIH,eAAsB,uCACrB,IACA,OAC+D;AAC/D,KAAI;AACH,SAAO;GACN,SAAS;GACT,MAAM,MAAM,IAAI,uCAAuC,GAAG,CAAC,iBAAiB,MAAM;GAClF;UACO,OAA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