Skip to content

Commit 7c6789d

Browse files
authored
Merge pull request #451 from TogetherCrew/handle-website-workflows
Handle website workflows
2 parents a97950e + c458351 commit 7c6789d

4 files changed

Lines changed: 152 additions & 39 deletions

File tree

src/controllers/hivemind.controller.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ const askQuestion = catchAsync(async function (req: IAuthRequest, res: Response)
99
console.debug('Body', req.body.communityId, req.body.question, req.body.chatId);
1010
req.setTimeout(6 * 60 * 1000);
1111
res.setTimeout(6 * 60 * 1000);
12-
const answer = await HivemindTemporalService.triggerWorkflow(req.body.communityId, req.body.question, false, req.body.chatId);
12+
const answer = await HivemindTemporalService.triggerWorkflow(
13+
req.body.communityId,
14+
req.body.question,
15+
false,
16+
req.body.chatId,
17+
);
1318
res.status(httpStatus.OK).send({ answer });
1419
});
1520

src/controllers/platform.controller.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,7 @@ const getPlatform = catchAsync(async function (req: IAuthRequest, res: Response)
324324
});
325325
const updatePlatform = catchAsync(async function (req: IAuthAndPlatform, res: Response) {
326326
platformService.validatePlatformUpdate(req.platform, req.body);
327-
if (req.platform.name === PlatformNames.Discord) {
328-
const discordIdentity = userService.getIdentityByProvider(req.user.identities, PlatformNames.Discord);
329-
if (discordIdentity) {
330-
await platformService.notifyDiscordUserImportComplete(req.platform.id, discordIdentity.id);
331-
}
332-
}
333-
const platform = await platformService.updatePlatform(req.platform, req.body);
327+
const platform = await platformService.updatePlatform(req.platform, req.user, req.body);
334328
res.send(platform);
335329
});
336330
const deletePlatform = catchAsync(async function (req: IAuthAndPlatform, res: Response) {

src/services/module.service.ts

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,68 @@ const updateModule = async (
6363
module: HydratedDocument<IModule>,
6464
updateBody: Partial<IModuleUpdateBody>,
6565
): Promise<HydratedDocument<IModule>> => {
66-
if (updateBody.options && updateBody.options.platforms) {
67-
if (updateBody.options.platforms[0].name == undefined) {
68-
{
69-
const globalOption = module.options?.platforms[0];
70-
if (globalOption) globalOption.metadata = updateBody.options.platforms[0].metadata;
71-
else module.options?.platforms.push(updateBody.options.platforms[0]);
72-
}
66+
if (!updateBody.options?.platforms?.length) {
67+
return module.save();
68+
}
69+
70+
if (!module.options) {
71+
module.options = { platforms: [] };
72+
} else if (!module.options.platforms) {
73+
module.options.platforms = [];
74+
}
75+
76+
const platforms = updateBody.options.platforms;
77+
78+
if (platforms[0].name === undefined) {
79+
const globalOption = module.options.platforms[0];
80+
if (globalOption) {
81+
globalOption.metadata = platforms[0].metadata;
82+
} else {
83+
module.options.platforms.push(platforms[0]);
84+
}
85+
return module.save();
86+
}
87+
88+
for (const newPlatform of platforms) {
89+
const existingPlatform = module.options.platforms.find((p) => p.name === newPlatform.name);
90+
91+
if (existingPlatform) {
92+
existingPlatform.metadata = newPlatform.metadata;
7393
} else {
74-
for (const newPlatform of updateBody.options.platforms) {
75-
const existingPlatform = module.options?.platforms.find((p) => p.name === newPlatform.name);
76-
if (existingPlatform) {
77-
existingPlatform.metadata = newPlatform.metadata;
78-
} else {
79-
module.options?.platforms.push(newPlatform);
80-
if (module.name === ModuleNames.Hivemind && newPlatform.name === PlatformNames.Website) {
81-
const scheduleId = await websiteService.coreService.createWebsiteSchedule(newPlatform.platform);
82-
const platform = await platformService.getPlatformById(newPlatform.platform);
83-
platform?.set('metadata.scheduleId', scheduleId);
84-
await platform?.save();
85-
}
86-
}
94+
module.options.platforms.push(newPlatform);
95+
if (module.name === ModuleNames.Hivemind && newPlatform.name === PlatformNames.Website) {
96+
await handleHivemindWebsiteCase(newPlatform);
8797
}
8898
}
8999
}
90-
return await module.save();
100+
return module.save();
101+
};
102+
103+
/**
104+
* Handle special case for Hivemind module with Website platform
105+
* @param {Object} platform - Platform object
106+
*/
107+
const handleHivemindWebsiteCase = async (platform: any) => {
108+
const platformDoc = await platformService.getPlatformById(platform.platform);
109+
110+
if (!platformDoc) return;
111+
112+
const isActivated = platform.metadata?.activated;
113+
const existingScheduleId = platformDoc.get('metadata.scheduleId');
114+
115+
if (isActivated === true) {
116+
if (!existingScheduleId) {
117+
const scheduleId = await websiteService.coreService.createWebsiteSchedule(platform.platform);
118+
platformDoc.set('metadata.scheduleId', scheduleId);
119+
await platformDoc.save();
120+
}
121+
} else if (isActivated === false) {
122+
if (existingScheduleId) {
123+
await websiteService.coreService.deleteWebsiteSchedule(existingScheduleId);
124+
platformDoc.set('metadata.scheduleId', null);
125+
await platformDoc.save();
126+
}
127+
}
91128
};
92129

93130
/**

src/services/platform.service.ts

Lines changed: 87 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ import { Snowflake } from 'discord.js';
22
import httpStatus from 'http-status';
33
import { FilterQuery, HydratedDocument, ObjectId, Types } from 'mongoose';
44

5-
import { IPlatform, IUser, Platform, PlatformNames } from '@togethercrew.dev/db';
5+
import { IPlatform, IUser, ModuleNames, Platform, PlatformNames } from '@togethercrew.dev/db';
66

77
import { analyzerAction, analyzerWindow } from '../config/analyzer.statics';
88
import parentLogger from '../config/logger';
99
import { IAuthAndPlatform } from '../interfaces/Request.interface';
1010
import ApiError from '../utils/ApiError';
11+
import { platformService } from './';
1112
import discourseService from './discourse';
13+
import moduleService from './module.service';
1214
import reputationScoreService from './reputationScore.service';
1315
import sagaService from './saga.service';
16+
import userService from './user.service';
17+
import websiteService from './website';
1418

1519
const logger = parentLogger.child({ module: 'PlatformService' });
1620
/**
@@ -120,8 +124,20 @@ const updatePlatformByFilter = async (
120124
*/
121125
const updatePlatform = async (
122126
platform: HydratedDocument<IPlatform>,
127+
user: HydratedDocument<IUser>,
123128
updateBody: Partial<IPlatform>,
124129
): Promise<HydratedDocument<IPlatform>> => {
130+
// Handle special cases based on platform type
131+
if (platform.name === PlatformNames.Website) {
132+
await handleWebsiteResourceChanges(platform, updateBody);
133+
}
134+
if (platform.name === PlatformNames.Discord) {
135+
const discordIdentity = userService.getIdentityByProvider(user.identities, PlatformNames.Discord);
136+
if (discordIdentity) {
137+
await platformService.notifyDiscordUserImportComplete(platform.id, discordIdentity.id);
138+
}
139+
}
140+
125141
if (updateBody.metadata) {
126142
updateBody.metadata = {
127143
...platform.metadata,
@@ -139,15 +155,7 @@ const updatePlatform = async (
139155
* @returns {Promise<HydratedDocument<IPlatform>>}
140156
*/
141157
const deletePlatform = async (platform: HydratedDocument<IPlatform>): Promise<HydratedDocument<IPlatform>> => {
142-
switch (platform.name) {
143-
case PlatformNames.Discourse: {
144-
if (platform.metadata?.scheduleId) {
145-
await discourseService.coreService.deleteDiscourseSchedule(platform.metadata.scheduleId);
146-
}
147-
}
148-
default: {
149-
}
150-
}
158+
await handlePlatformCleanup(platform);
151159
return await platform.remove();
152160
};
153161

@@ -321,6 +329,75 @@ const getReputationScore = async (platform: HydratedDocument<IPlatform>, user: H
321329
reputationScore: (await reputationScoreService.calculateReputationScoreForUser(platform, identity.id)) * 100,
322330
};
323331
};
332+
333+
/**
334+
* Handle platform-specific cleanup during deletion
335+
* @param {HydratedDocument<IPlatform>} platform - Platform document
336+
* @returns {Promise<void>}
337+
*/
338+
const handlePlatformCleanup = async (platform: HydratedDocument<IPlatform>): Promise<void> => {
339+
switch (platform.name) {
340+
case PlatformNames.Discourse: {
341+
if (platform.metadata?.scheduleId) {
342+
await discourseService.coreService.deleteDiscourseSchedule(platform.metadata.scheduleId);
343+
}
344+
break;
345+
}
346+
case PlatformNames.Website: {
347+
if (platform.metadata?.scheduleId) {
348+
await websiteService.coreService.deleteWebsiteSchedule(platform.metadata.scheduleId);
349+
}
350+
break;
351+
}
352+
default:
353+
break;
354+
}
355+
};
356+
357+
/**
358+
* Handle Website platform resource changes
359+
* @param {HydratedDocument<IPlatform>} platform - Platform document
360+
* @param {Partial<IPlatform>} updateBody - Update body
361+
* @returns {Promise<void>}
362+
*/
363+
const handleWebsiteResourceChanges = async (
364+
platform: HydratedDocument<IPlatform>,
365+
updateBody: Partial<IPlatform>,
366+
): Promise<void> => {
367+
if (!updateBody.metadata?.resources || !platform.metadata?.resources) {
368+
return;
369+
}
370+
const oldResources = JSON.stringify(platform.metadata.resources.sort());
371+
const newResources = JSON.stringify(updateBody.metadata.resources.sort());
372+
373+
if (oldResources !== newResources) {
374+
const existingScheduleId = platform.metadata.scheduleId;
375+
376+
if (existingScheduleId) {
377+
await websiteService.coreService.deleteWebsiteSchedule(existingScheduleId);
378+
updateBody.metadata.scheduleId = null;
379+
}
380+
381+
const moduleFilter = {
382+
name: ModuleNames.Hivemind,
383+
'options.platforms': {
384+
$elemMatch: {
385+
name: PlatformNames.Website,
386+
platform: platform._id,
387+
'metadata.activated': true,
388+
},
389+
},
390+
};
391+
392+
const hivemindModule = await moduleService.getModuleByFilter(moduleFilter);
393+
394+
if (hivemindModule) {
395+
const scheduleId = await websiteService.coreService.createWebsiteSchedule(platform._id);
396+
updateBody.metadata.scheduleId = scheduleId;
397+
}
398+
}
399+
};
400+
324401
export default {
325402
createPlatform,
326403
getPlatformById,

0 commit comments

Comments
 (0)