diff --git a/.migrate b/.migrate index ad55368..78be70f 100644 --- a/.migrate +++ b/.migrate @@ -1,9 +1,9 @@ { - "lastRun": "1738072787797-add-activated-field-to-modules.ts", + "lastRun": "1744651967487-add-modules.ts", "migrations": [ { - "title": "1738072787797-add-activated-field-to-modules.ts", - "timestamp": 1744199444528 + "title": "1744651967487-add-modules.ts", + "timestamp": 1744652563722 } ] -} \ No newline at end of file +} diff --git a/package-lock.json b/package-lock.json index 7c3fb7a..885005b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,7 +14,7 @@ "@notionhq/client": "^2.2.3", "@sentry/node": "^7.50.0", "@temporalio/client": "^1.11.3", - "@togethercrew.dev/db": "^3.4.0", + "@togethercrew.dev/db": "^3.5.0", "@togethercrew.dev/tc-messagebroker": "^0.0.50", "@types/express-session": "^1.17.7", "@types/morgan": "^1.9.5", @@ -3659,9 +3659,9 @@ "dev": true }, "node_modules/@togethercrew.dev/db": { - "version": "3.4.0", - "resolved": "https://registry.npmjs.org/@togethercrew.dev/db/-/db-3.4.0.tgz", - "integrity": "sha512-7XoagWGLwuh7SsY6C7awobzF7JZ7wnsex06+iovRC+gJGNM4G5ppP/iNSnEgyFHUm9j/7iBJc5ZrzW/m5RCNhA==", + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/@togethercrew.dev/db/-/db-3.5.0.tgz", + "integrity": "sha512-DfrcNd71QPZsxIqSssQJhbGuxCLKm3pPE951c/AG/bzd5qLhmPsTsgq4qTXr37eetlF5WUMS0JlnSE+CITDNuA==", "license": "ISC", "dependencies": { "discord.js": "^14.7.1", diff --git a/package.json b/package.json index 960981f..e3a7760 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "@notionhq/client": "^2.2.3", "@sentry/node": "^7.50.0", "@temporalio/client": "^1.11.3", - "@togethercrew.dev/db": "^3.4.0", + "@togethercrew.dev/db": "^3.5.0", "@togethercrew.dev/tc-messagebroker": "^0.0.50", "@types/express-session": "^1.17.7", "@types/morgan": "^1.9.5", diff --git a/src/migrations/db/1744651967487-add-modules.ts b/src/migrations/db/1744651967487-add-modules.ts new file mode 100644 index 0000000..793eacc --- /dev/null +++ b/src/migrations/db/1744651967487-add-modules.ts @@ -0,0 +1,64 @@ +import 'dotenv/config'; + +import mongoose from 'mongoose'; + +import { Community, Module, ModuleNames } from '@togethercrew.dev/db'; + +import config from '../../config'; +import logger from '../../config/logger'; + +async function connectToMongoDB() { + try { + await mongoose.connect(config.mongoose.serverURL); + logger.info('Connected to MongoDB!'); + } catch (error) { + logger.fatal('Failed to connect to MongoDB!'); + throw error; + } +} + +export const up = async () => { + await connectToMongoDB(); + + try { + const communities = await Community.find({}).exec(); + logger.info(`Found ${communities.length} communities.`); + + const newModuleNames = [ModuleNames.Announcements, ModuleNames.CommunityHealth, ModuleNames.CommunityInsights]; + + for (const community of communities) { + for (const moduleName of newModuleNames) { + const newModule = new Module({ + name: moduleName, + community: community._id, + activated: true, + options: { platforms: [] }, + }); + await newModule.save(); + logger.info(`Created module "${moduleName}" for community ${community._id}`); + } + } + } catch (error) { + logger.error('Error during migration up:', error); + throw error; + } finally { + await mongoose.connection.close(); + logger.info('MongoDB connection closed.'); + } +}; + +export const down = async () => { + await connectToMongoDB(); + + try { + const moduleNamesToDelete = [ModuleNames.Announcements, ModuleNames.CommunityHealth, ModuleNames.CommunityInsights]; + const result = await Module.deleteMany({ name: { $in: moduleNamesToDelete } }); + logger.info(`Migration down: deleted ${result.deletedCount} modules.`); + } catch (error) { + logger.error('Error during migration down:', error); + throw error; + } finally { + await mongoose.connection.close(); + logger.info('MongoDB connection closed.'); + } +};