diff --git a/.migrate b/.migrate index 566ba03..ad55368 100644 --- a/.migrate +++ b/.migrate @@ -1,9 +1,9 @@ { - "lastRun": "1738072787796-update-user-documents-for-telegram-login.ts", + "lastRun": "1738072787797-add-activated-field-to-modules.ts", "migrations": [ { - "title": "1738072787796-update-user-documents-for-telegram-login.ts", - "timestamp": 1738073593695 + "title": "1738072787797-add-activated-field-to-modules.ts", + "timestamp": 1744199444528 } ] } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 1139d00..7c3fb7a 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.3.0", + "@togethercrew.dev/db": "^3.4.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.3.0", - "resolved": "https://registry.npmjs.org/@togethercrew.dev/db/-/db-3.3.0.tgz", - "integrity": "sha512-5UYnxR0P5N8KAEsGUfIkbGT8B+wu4sU9hiKvLp5J9MeaebnPHp9U/Lie30ofjP3eOVetpE3SwO/HVuBrR4aYaw==", + "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==", "license": "ISC", "dependencies": { "discord.js": "^14.7.1", diff --git a/package.json b/package.json index 77b93b8..960981f 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.3.0", + "@togethercrew.dev/db": "^3.4.0", "@togethercrew.dev/tc-messagebroker": "^0.0.50", "@types/express-session": "^1.17.7", "@types/morgan": "^1.9.5", diff --git a/src/controllers/hivemind.controller.ts b/src/controllers/hivemind.controller.ts index bbc488d..90f52ab 100644 --- a/src/controllers/hivemind.controller.ts +++ b/src/controllers/hivemind.controller.ts @@ -6,7 +6,6 @@ import HivemindTemporalService from '../services/temporal/hivemind.service'; import { catchAsync } from '../utils'; const askQuestion = catchAsync(async function (req: IAuthRequest, res: Response) { - console.debug('Body', req.body.communityId, req.body.question, req.body.chatId); req.setTimeout(6 * 60 * 1000); res.setTimeout(6 * 60 * 1000); const answer = await HivemindTemporalService.triggerWorkflow( diff --git a/src/migrations/db/1738072787796-update-user-documents-for-telegram-login.ts b/src/migrations/db/1738072787796-update-user-documents-for-telegram-login.ts deleted file mode 100644 index 5160feb..0000000 --- a/src/migrations/db/1738072787796-update-user-documents-for-telegram-login.ts +++ /dev/null @@ -1,78 +0,0 @@ -import 'dotenv/config'; - -import mongoose from 'mongoose'; - -import { PlatformNames, User } 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(); - - // This single pipeline: - // 1) sets `identities` to an array containing { provider: 'discord', id: '$discordId' } - // 2) unsets the old `discordId` and `email` fields - const result = await User.updateMany( - { - discordId: { $exists: true }, - }, - [ - { - $set: { - identities: [ - { - provider: PlatformNames.Discord, - id: '$discordId', - }, - ], - }, - }, - { - $unset: ['discordId', 'email'], - }, - ], - ); - - logger.info(`Up migration: modified ${result.modifiedCount} user(s).`); - - await mongoose.connection.close(); -}; - -export const down = async () => { - await connectToMongoDB(); - - // We'll do a quick find() and loop if you want to revert, because the pipeline - // would need to parse out "discordId" from identities. - const users = await User.find({ - identities: { $exists: true, $ne: [] }, - }); - - for (const userDoc of users) { - // cast to any or .get('...') - const user: any = userDoc; - - const discordIdentity = user.identities.find((identity: any) => identity.provider === PlatformNames.Discord); - if (discordIdentity) { - user.discordId = discordIdentity.id; - } - user.identities = []; - - // If needed, re-add `email` from some backup, if you have it. - user.markModified('identities'); - await user.save(); - } - - logger.info('Down migration: successfully reverted to old schema.'); - await mongoose.connection.close(); -}; diff --git a/src/migrations/db/1738072787797-add-activated-field-to-modules.ts b/src/migrations/db/1738072787797-add-activated-field-to-modules.ts new file mode 100644 index 0000000..aefd1ad --- /dev/null +++ b/src/migrations/db/1738072787797-add-activated-field-to-modules.ts @@ -0,0 +1,29 @@ +import 'dotenv/config'; + +import mongoose from 'mongoose'; + +import { Module } 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(); + + const result = await Module.updateMany({}, { $set: { activated: true } }); + logger.info(`Up migration: added activated field to ${result.modifiedCount} module(s).`); + + await mongoose.connection.close(); +}; + +export const down = async () => {}; diff --git a/src/services/module.service.ts b/src/services/module.service.ts index 3f88b29..cf1d585 100644 --- a/src/services/module.service.ts +++ b/src/services/module.service.ts @@ -1,6 +1,6 @@ import { FilterQuery, HydratedDocument, ObjectId, Types } from 'mongoose'; -import { IModule, IModuleUpdateBody, Module, ModuleNames, PlatformNames } from '@togethercrew.dev/db'; +import { IModule, IModuleUpdateBody, Module } from '@togethercrew.dev/db'; import platformService from './platform.service'; import websiteService from './website'; @@ -87,15 +87,11 @@ const updateModule = async ( for (const newPlatform of platforms) { const existingPlatform = module.options.platforms.find((p) => p.name === newPlatform.name); - console.log('S4', existingPlatform); if (existingPlatform) { - console.log('A1', module.name, newPlatform.name); - if (module.name === ModuleNames.Hivemind && newPlatform.name === PlatformNames.Website) { - console.log('A2'); - - await handleHivemindWebsiteCase(newPlatform); - } + // if (module.name === ModuleNames.Hivemind && newPlatform.name === PlatformNames.Website) { + // await handleHivemindWebsiteCase(newPlatform); + // } existingPlatform.metadata = newPlatform.metadata; } else { module.options.platforms.push(newPlatform); diff --git a/src/services/platform.service.ts b/src/services/platform.service.ts index a96988f..066ac85 100644 --- a/src/services/platform.service.ts +++ b/src/services/platform.service.ts @@ -137,7 +137,6 @@ const updatePlatform = async ( await platformService.notifyDiscordUserImportComplete(platform.id, discordIdentity.id); } } - console.log('Main', updateBody); if (updateBody.metadata) { updateBody.metadata = { diff --git a/src/validations/module.validation.ts b/src/validations/module.validation.ts index 7896f6f..f8929e6 100644 --- a/src/validations/module.validation.ts +++ b/src/validations/module.validation.ts @@ -1,7 +1,10 @@ import Joi from 'joi'; import { - HivemindPlatformNames, ModuleNames, PlatformNames, ViolationDetectionPlatformNames + HivemindPlatformNames, + ModuleNames, + PlatformNames, + ViolationDetectionPlatformNames, } from '@togethercrew.dev/db'; import { objectId } from './custom.validation'; @@ -12,6 +15,7 @@ const createModule = { .required() .valid(...Object.values(ModuleNames)), community: Joi.string().custom(objectId).required(), + activated: Joi.boolean().required(), }), }; @@ -202,6 +206,7 @@ const dynamicModuleUpdate = (req: any) => { bodyOption = { body: Joi.object().required().keys({ options: hivemindOptions(), + activated: Joi.boolean(), }), }; break; @@ -209,6 +214,7 @@ const dynamicModuleUpdate = (req: any) => { bodyOption = { body: Joi.object().required().keys({ options: violationDetectionOptions(), + activated: Joi.boolean(), }), }; break; @@ -216,6 +222,7 @@ const dynamicModuleUpdate = (req: any) => { bodyOption = { body: Joi.object().required().keys({ options: dynamicNftOptions(), + activated: Joi.boolean(), }), }; break;