-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodule.service.ts
More file actions
174 lines (149 loc) · 5.21 KB
/
module.service.ts
File metadata and controls
174 lines (149 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { FilterQuery, HydratedDocument, ObjectId, Types } from 'mongoose';
import {
IModule, IModuleUpdateBody, Module, ModuleNames, PlatformNames
} from '@togethercrew.dev/db';
import platformService from './platform.service';
import temporalMediaWiki from './temporal/mediaWiki.service';
import websiteService from './website';
/**
* Create a module
* @param {IModule} ModuleBody
* @returns {Promise<HydratedDocument<IModule>>}
*/
const createModule = async (ModuleBody: IModule): Promise<HydratedDocument<IModule>> => {
return Module.create(ModuleBody);
};
/**
* Query for modules
* @param {Object} filter - Mongo filter
* @param {Object} options - Query options
* @param {string} [options.sortBy] - Sort option in the format: sortField:(desc|asc)
* @param {number} [options.limit] - Maximum number of results per page (default = 10)
* @param {number} [options.page] - Current page (default = 1)
*/
const queryModules = async (filter: object, options: object) => {
return Module.paginate(filter, options);
};
/**
* Get module by filter
* @param {Object} filter - Mongo filter
* @returns {Promise<HydratedDocument<IModule> | null>}
*/
const getModuleByFilter = async (filter: FilterQuery<IModule>): Promise<HydratedDocument<IModule> | null> => {
return Module.findOne(filter);
};
/**
* Get modules by filter
* @param {Object} filter - Mongo filter
* @returns {Promise<HydratedDocument<IModule> | null>}
*/
const getModulesByFilter = async (filter: FilterQuery<IModule>): Promise<HydratedDocument<IModule>[]> => {
return Module.find(filter);
};
/**
* Get module by id
* @param {Types.ObjectId} id
* @returns {Promise<HydratedDocument<IModule> | null>}
*/
const getModuleById = async (id: Types.ObjectId): Promise<HydratedDocument<IModule> | null> => {
return Module.findById(id);
};
/**
* Update module
* @param {HydratedDocument<IModule>} module - module doc
* @param {Partial<IModule>} updateBody
* @returns {Promise<HydratedDocument<IModule>>}
*/
const updateModule = async (
module: HydratedDocument<IModule>,
updateBody: Partial<IModuleUpdateBody>,
): Promise<HydratedDocument<IModule>> => {
if (!updateBody.options?.platforms?.length) {
Object.assign(module, updateBody);
return await module.save();
}
if (!module.options) {
module.options = { platforms: [] };
} else if (!module.options.platforms) {
module.options.platforms = [];
}
const platforms = updateBody.options.platforms;
if (platforms[0].name === undefined) {
const globalOption = module.options.platforms[0];
if (globalOption) {
globalOption.metadata = platforms[0].metadata;
} else {
module.options.platforms.push(platforms[0]);
}
return module.save();
}
for (const newPlatform of platforms) {
const existingPlatform = module.options.platforms.find((p) => p.name === newPlatform.name);
if (existingPlatform) {
if (module.name === ModuleNames.Hivemind && newPlatform.name === PlatformNames.Website) {
await handleHivemindWebsiteCase(newPlatform);
}
if (module.name === ModuleNames.Hivemind && newPlatform.name === PlatformNames.MediaWiki) {
await handleHivemindMediaWikiCase(newPlatform);
}
existingPlatform.metadata = newPlatform.metadata;
} else {
module.options.platforms.push(newPlatform);
}
}
return module.save();
};
/**
* Handle special case for Hivemind module with Website platform
* @param {Object} platform - Platform object
*/
const handleHivemindWebsiteCase = async (platform: any) => {
const platformDoc = await platformService.getPlatformById(platform.platform);
if (!platformDoc) return;
const isActivated = platform.metadata?.activated;
const existingScheduleId = platformDoc.get('metadata.scheduleId');
if (isActivated === true) {
if (!existingScheduleId) {
const scheduleId = await websiteService.coreService.createWebsiteSchedule(platform.platform);
platformDoc.set('metadata.scheduleId', scheduleId);
await platformDoc.save();
}
} else if (isActivated === false) {
if (existingScheduleId) {
await websiteService.coreService.deleteWebsiteSchedule(existingScheduleId);
await websiteService.coreService.terminateWebsiteWorkflow(platformDoc.community.toString());
platformDoc.set('metadata.scheduleId', null);
await platformDoc.save();
}
}
};
/**
* Handle special case for Hivemind module with MediaWiki platform
* @param {Object} platform - Platform object
*/
const handleHivemindMediaWikiCase = async (platform: any) => {
const platformDoc = await platformService.getPlatformById(platform.platform);
if (!platformDoc) return;
const isActivated = platform.metadata?.activated;
if (isActivated === true) {
temporalMediaWiki.executeWorkflow(platformDoc.id);
} else if (isActivated === false) {
temporalMediaWiki.terminateWorkflow(platformDoc.id);
}
};
/**
* Delete module
* @param {HydratedDocument<IModule>} module - module doc
* @returns {Promise<HydratedDocument<IModule>>}
*/
const deleteModule = async (module: HydratedDocument<IModule>): Promise<HydratedDocument<IModule>> => {
return await module.remove();
};
export default {
createModule,
queryModules,
getModuleByFilter,
getModuleById,
deleteModule,
updateModule,
};