@@ -2,15 +2,19 @@ import { Snowflake } from 'discord.js';
22import httpStatus from 'http-status' ;
33import { 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
77import { analyzerAction , analyzerWindow } from '../config/analyzer.statics' ;
88import parentLogger from '../config/logger' ;
99import { IAuthAndPlatform } from '../interfaces/Request.interface' ;
1010import ApiError from '../utils/ApiError' ;
11+ import { platformService } from './' ;
1112import discourseService from './discourse' ;
13+ import moduleService from './module.service' ;
1214import reputationScoreService from './reputationScore.service' ;
1315import sagaService from './saga.service' ;
16+ import userService from './user.service' ;
17+ import websiteService from './website' ;
1418
1519const logger = parentLogger . child ( { module : 'PlatformService' } ) ;
1620/**
@@ -120,8 +124,20 @@ const updatePlatformByFilter = async (
120124 */
121125const 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 */
141157const 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+
324401export default {
325402 createPlatform,
326403 getPlatformById,
0 commit comments