{"version":3,"file":"sync-collections.mjs","names":["constants","formatter"],"sources":["../../../src/services/sync/sync-collections.ts"],"sourcesContent":["import constants from \"../../constants/constants.js\";\nimport formatter from \"../../libs/formatters/index.js\";\nimport logger from \"../../libs/logger/index.js\";\nimport { CollectionsRepository } from \"../../libs/repositories/index.js\";\nimport type { ServiceFn } from \"../../utils/services/types.js\";\n\n/**\n * Responsible for syncing active collections to the DB.\n * - In the case a collection exists in the DB, but not in the config: it is marked as deleted.\n */\n\nconst syncCollections: ServiceFn<[], undefined> = async (context) => {\n\tconst Collections = new CollectionsRepository(\n\t\tcontext.db.client,\n\t\tcontext.config.db,\n\t);\n\tconst activeCollections = context.config.collections.map((c) => c.key);\n\n\tconst collectionsRes = await Collections.selectMultiple({\n\t\tselect: [\"key\", \"is_deleted\"],\n\t\tvalidation: {\n\t\t\tenabled: true,\n\t\t},\n\t});\n\tif (collectionsRes.error) return collectionsRes;\n\tconst collectionsFromDB = collectionsRes.data.map(\n\t\t(collection) => collection.key,\n\t);\n\n\t//* new collections\n\tconst missingCollections = activeCollections.filter(\n\t\t(key) => !collectionsFromDB.includes(key),\n\t);\n\tif (missingCollections.length > 0) {\n\t\tlogger.debug({\n\t\t\tmessage: `Syncing new collections to the DB: ${missingCollections.join(\", \")}`,\n\t\t\tscope: constants.logScopes.sync,\n\t\t});\n\t}\n\n\t//* deleted collections\n\tconst collectionsToDelete = collectionsRes.data.filter(\n\t\t(collection) =>\n\t\t\t!activeCollections.includes(collection.key) &&\n\t\t\tformatter.formatBoolean(collection.is_deleted) === false,\n\t);\n\tconst collectionsToDeleteKeys = collectionsToDelete.map(\n\t\t(collection) => collection.key,\n\t);\n\tif (collectionsToDeleteKeys.length > 0) {\n\t\tlogger.debug({\n\t\t\tmessage: `Marking the following collections as deleted: ${collectionsToDeleteKeys.join(\", \")}`,\n\t\t\tscope: constants.logScopes.sync,\n\t\t});\n\t}\n\n\t//* previously deleted, now active\n\tconst unDeletedCollections = collectionsRes.data.filter(\n\t\t(collection) =>\n\t\t\tformatter.formatBoolean(collection.is_deleted) &&\n\t\t\tactiveCollections.includes(collection.key),\n\t);\n\tconst unDeletedCollectionKeys = unDeletedCollections.map(\n\t\t(collection) => collection.key,\n\t);\n\tif (unDeletedCollectionKeys.length > 0) {\n\t\tlogger.debug({\n\t\t\tmessage: `Restoring previously deleted collections: ${unDeletedCollectionKeys.join(\", \")}`,\n\t\t\tscope: constants.logScopes.sync,\n\t\t});\n\t}\n\n\tconst [createMultipleRes, updateDeletedRes, updateRestoredRes] =\n\t\tawait Promise.all([\n\t\t\tmissingCollections.length > 0 &&\n\t\t\t\tCollections.createMultiple({\n\t\t\t\t\tdata: missingCollections\n\t\t\t\t\t\t.map((key) => {\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tkey,\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t})\n\t\t\t\t\t\t.filter((c) => c !== null),\n\t\t\t\t}),\n\t\t\tcollectionsToDeleteKeys.length > 0 &&\n\t\t\t\tCollections.updateSingle({\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tis_deleted: true,\n\t\t\t\t\t\tis_deleted_at: new Date().toISOString(),\n\t\t\t\t\t},\n\t\t\t\t\twhere: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tkey: \"key\",\n\t\t\t\t\t\t\toperator: \"in\",\n\t\t\t\t\t\t\tvalue: collectionsToDeleteKeys,\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t}),\n\t\t\tunDeletedCollectionKeys.length > 0 &&\n\t\t\t\tCollections.updateSingle({\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tis_deleted: false,\n\t\t\t\t\t\tis_deleted_at: null,\n\t\t\t\t\t},\n\t\t\t\t\twhere: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tkey: \"key\",\n\t\t\t\t\t\t\toperator: \"in\",\n\t\t\t\t\t\t\tvalue: unDeletedCollectionKeys,\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t}),\n\t\t]);\n\tif (typeof createMultipleRes !== \"boolean\" && createMultipleRes.error)\n\t\treturn createMultipleRes;\n\tif (typeof updateDeletedRes !== \"boolean\" && updateDeletedRes.error)\n\t\treturn updateDeletedRes;\n\tif (typeof updateRestoredRes !== \"boolean\" && updateRestoredRes.error)\n\t\treturn updateRestoredRes;\n\n\treturn {\n\t\terror: undefined,\n\t\tdata: undefined,\n\t};\n};\n\nexport default syncCollections;\n"],"mappings":"8LAWA,MAAM,EAA4C,KAAO,IAAY,CACpE,IAAM,EAAc,IAAI,EACvB,EAAQ,GAAG,OACX,EAAQ,OAAO,EAChB,EACM,EAAoB,EAAQ,OAAO,YAAY,IAAK,GAAM,EAAE,GAAG,EAE/D,EAAiB,MAAM,EAAY,eAAe,CACvD,OAAQ,CAAC,MAAO,YAAY,EAC5B,WAAY,CACX,QAAS,EACV,CACD,CAAC,EACD,GAAI,EAAe,MAAO,OAAO,EACjC,IAAM,EAAoB,EAAe,KAAK,IAC5C,GAAe,EAAW,GAC5B,EAGM,EAAqB,EAAkB,OAC3C,GAAQ,CAAC,EAAkB,SAAS,CAAG,CACzC,EACI,EAAmB,OAAS,GAC/B,EAAO,MAAM,CACZ,QAAS,sCAAsC,EAAmB,KAAK,IAAI,IAC3E,MAAOA,EAAU,UAAU,IAC5B,CAAC,EASF,IAAM,EALsB,EAAe,KAAK,OAC9C,GACA,CAAC,EAAkB,SAAS,EAAW,GAAG,GAC1CC,EAAU,cAAc,EAAW,UAAU,IAAM,EAEH,CAAC,CAAC,IAClD,GAAe,EAAW,GAC5B,EACI,EAAwB,OAAS,GACpC,EAAO,MAAM,CACZ,QAAS,iDAAiD,EAAwB,KAAK,IAAI,IAC3F,MAAOD,EAAU,UAAU,IAC5B,CAAC,EASF,IAAM,EALuB,EAAe,KAAK,OAC/C,GACAC,EAAU,cAAc,EAAW,UAAU,GAC7C,EAAkB,SAAS,EAAW,GAAG,CAEQ,CAAC,CAAC,IACnD,GAAe,EAAW,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