@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
1 lines • 2.52 kB
Source Map (JSON)
{"version":3,"file":"update-storage.mjs","names":[],"sources":["../../../../src/services/media/jobs/update-storage.ts"],"sourcesContent":["import { OptionsRepository } from \"../../../libs/repositories/index.js\";\nimport type { ServiceFn } from \"../../../utils/services/types.js\";\nimport getStorageUsage from \"../get-storage-usage.js\";\nimport { getMediaStorageOptionName } from \"../helpers/storage-usage-options.js\";\n\n/**\n * Recalculates and updates the media storage usage options\n */\nconst updateMediaStorage: ServiceFn<[], undefined> = async (context) => {\n\tconst Options = new OptionsRepository(context.db.client, context.config.db);\n\n\tconst [storageUsageRes, existingOptionsRes] = await Promise.all([\n\t\tgetStorageUsage(context, {\n\t\t\tgrouped: true,\n\t\t}),\n\t\tOptions.selectMediaStorageUsageOptions(),\n\t]);\n\tif (storageUsageRes.error) return storageUsageRes;\n\tif (existingOptionsRes.error) return existingOptionsRes;\n\n\tconst storageValues = new Map(\n\t\texistingOptionsRes.data.map((option) => [option.name, 0]),\n\t);\n\tstorageValues.set(\"media_storage_used\", 0);\n\n\tfor (const bucket of storageUsageRes.data.buckets ?? []) {\n\t\tstorageValues.set(\n\t\t\tgetMediaStorageOptionName(bucket.tenantKey),\n\t\t\tbucket.total,\n\t\t);\n\t}\n\n\tfor (const [name, value] of storageValues.entries()) {\n\t\tconst updateMediaStorageRes = await Options.upsertSingle({\n\t\t\tdata: {\n\t\t\t\tname,\n\t\t\t\tvalue_int: value,\n\t\t\t\tvalue_text: null,\n\t\t\t\tvalue_bool: null,\n\t\t\t},\n\t\t\treturning: [\"name\"],\n\t\t\tvalidation: {\n\t\t\t\tenabled: true,\n\t\t\t},\n\t\t});\n\t\tif (updateMediaStorageRes.error) return updateMediaStorageRes;\n\t}\n\n\treturn {\n\t\terror: undefined,\n\t\tdata: undefined,\n\t};\n};\n\nexport default updateMediaStorage;\n"],"mappings":"+KAQA,MAAM,EAA+C,KAAO,IAAY,CACvE,IAAM,EAAU,IAAI,EAAkB,EAAQ,GAAG,OAAQ,EAAQ,OAAO,EAAE,EAEpE,CAAC,EAAiB,GAAsB,MAAM,QAAQ,IAAI,CAC/D,EAAgB,EAAS,CACxB,QAAS,EACV,CAAC,EACD,EAAQ,+BAA+B,CACxC,CAAC,EACD,GAAI,EAAgB,MAAO,OAAO,EAClC,GAAI,EAAmB,MAAO,OAAO,EAErC,IAAM,EAAgB,IAAI,IACzB,EAAmB,KAAK,IAAK,GAAW,CAAC,EAAO,KAAM,CAAC,CAAC,CACzD,EACA,EAAc,IAAI,qBAAsB,CAAC,EAEzC,IAAK,IAAM,KAAU,EAAgB,KAAK,SAAW,CAAC,EACrD,EAAc,IACb,EAA0B,EAAO,SAAS,EAC1C,EAAO,KACR,EAGD,IAAK,GAAM,CAAC,EAAM,KAAU,EAAc,QAAQ,EAAG,CACpD,IAAM,EAAwB,MAAM,EAAQ,aAAa,CACxD,KAAM,CACL,OACA,UAAW,EACX,WAAY,KACZ,WAAY,IACb,EACA,UAAW,CAAC,MAAM,EAClB,WAAY,CACX,QAAS,EACV,CACD,CAAC,EACD,GAAI,EAAsB,MAAO,OAAO,CACzC,CAEA,MAAO,CACN,MAAO,IAAA,GACP,KAAM,IAAA,EACP,CACD"}