1
+ < << << << HEAD
1
2
const {
2
3
CollaborativeInput ,
3
4
LineInput ,
@@ -214,4 +215,150 @@ module.exports = {
214
215
updateCollaborativeLanguage,
215
216
deleteCollaborativeInput,
216
217
deleteCollaborativeLineInput,
217
- } ;
218
+ } ;
219
+ = === ===
220
+ const CollaborativeInput = require ( '../models/collaborationCodeModel' ) ;
221
+ const axios = require ( 'axios' ) ;
222
+ const config = require ( '../config/config' ) ;
223
+
224
+ const getCollaborativeInput = async ( sessionId ) => {
225
+ try {
226
+ const dataOutput = await CollaborativeInput . findOne ( { sessionId : sessionId } ) ;
227
+
228
+ console . log ( `Get collaborative input for session ${ sessionId } ` ) ;
229
+
230
+ return [ dataOutput . initTime , dataOutput . language , dataOutput . codes ] ;
231
+
232
+ } catch ( error ) {
233
+ console . log ( `Error getting collaborative input for session ${ sessionId } ` ) ;
234
+
235
+ return [ "None" , "" , "" ] ;
236
+ }
237
+ }
238
+
239
+ const getCollaborativeInputByLine = async ( sessionId , line ) => {
240
+ try {
241
+ const dataOutput = await CollaborativeInput . findOne ( { sessionId : sessionId , 'codes.line' : line } ) ;
242
+
243
+ console . log ( `Get collaborative input for session ${ sessionId } line ${ line } ` ) ;
244
+
245
+ return [ dataOutput . language , line , dataOutput . codes [ line ] . code ] ;
246
+
247
+ } catch ( error ) {
248
+ console . error ( `Error getting collaborative input by line for session ${ sessionId } ` ) ;
249
+
250
+ return [ "None" , line , "" ] ;
251
+ }
252
+ }
253
+
254
+ const initCollaborativeCode = async ( initTime , sessionId , language ) => {
255
+ try {
256
+ const input = await getCollaborativeInput ( sessionId ) ;
257
+
258
+ if ( input [ 0 ] === "None" ) {
259
+ const collaborativeInput = new CollaborativeInput (
260
+ { sessionId : sessionId , initTime : initTime , language : language , codes : [ ] } ) ;
261
+
262
+ await collaborativeInput . save ( ) ;
263
+
264
+ console . log ( `Successfully added:` , collaborativeInput ) ;
265
+
266
+ return [ collaborativeInput . initTime , language , [ ] ] ;
267
+
268
+ } else {
269
+ console . log ( `Collaborative input already exists for ${ sessionId } ` ) ;
270
+
271
+ return input ;
272
+ }
273
+
274
+ } catch ( error ) {
275
+ console . log ( `Failed to add collaborative input for ${ sessionId } ` ) ;
276
+
277
+ return [ "None" , "" ] ;
278
+ }
279
+ }
280
+
281
+ const updateCollaborativeLineInput = async ( sessionId , line , code , lastModifier ) => {
282
+ try {
283
+ let collaborativeInput = await CollaborativeInput . findOne (
284
+ { sessionId : sessionId , 'codes.line' : line } ) ;
285
+
286
+ if ( collaborativeInput ) {
287
+ await CollaborativeInput . updateOne (
288
+ { sessionId : sessionId , 'codes.line' : line } ,
289
+ { $set : { 'codes.$.code' : code , 'codes.$.lastModifier' : lastModifier } }
290
+ ) ;
291
+
292
+ } else {
293
+ await CollaborativeInput . updateOne (
294
+ { sessionId : sessionId } ,
295
+ { $push : { codes : { line : line , code : code , lastModifier : lastModifier } } }
296
+ ) ;
297
+ }
298
+
299
+ console . log ( `Successfully updated line:` , line ) ;
300
+
301
+ } catch ( error ) {
302
+ console . log ( `Failed to update collaborative input for ${ sessionId } line ${ line } ` ) ;
303
+ }
304
+ }
305
+
306
+ const updateCollaborativeInput = async ( sessionId , codes ) => {
307
+ try {
308
+ let collaborativeInput = await CollaborativeInput . findOne ( { sessionId : sessionId } ) ;
309
+ const sessionReq = await axios . get ( `${ config . matchingServiceUrl } /getSession/${ sessionId } ` ) ;
310
+
311
+ const session = sessionReq . data . session ;
312
+
313
+ if ( collaborativeInput . codes !== null ) {
314
+ collaborativeInput . codes = codes ;
315
+
316
+ } else {
317
+ collaborativeInput = new CollaborativeInput ( { sessionId : sessionId , initTime : session . initTime , language : session . language , codes : codes } ) ;
318
+ }
319
+
320
+ await collaborativeInput . save ( ) ;
321
+
322
+ console . log ( `Successfully updated:` , collaborativeInput ) ;
323
+
324
+ } catch ( error ) {
325
+ console . log ( `Failed to update collaborative input for ${ sessionId } ` ) ;
326
+ }
327
+ }
328
+
329
+ const deleteCollaborativeInput = async ( sessionId ) => {
330
+ try {
331
+ const result = await CollaborativeInput . deleteOne ( { sessionId : sessionId } ) ;
332
+
333
+ console . log ( `Successfully deleted:` , result ) ;
334
+
335
+ } catch ( error ) {
336
+ console . log ( `Failed to delete collaborative input for ${ sessionId } ` ) ;
337
+ }
338
+ }
339
+
340
+ const deleteCollaborativeLineInput = async ( sessionId , line ) => {
341
+ try {
342
+ const collaborativeInput = await CollaborativeInput . findOne ( { sessionId : sessionId } ) ;
343
+
344
+ collaborativeInput . codes . splice ( line , 1 ) ;
345
+
346
+ await collaborativeInput . save ( ) ;
347
+
348
+ console . log ( `Successfully deleted:` , collaborativeInput ) ;
349
+
350
+ } catch ( error ) {
351
+ console . log ( `Failed to delete collaborative input for ${ sessionId } line ${ line } ` ) ;
352
+ }
353
+ }
354
+
355
+ module . exports = {
356
+ getCollaborativeInput,
357
+ getCollaborativeInputByLine,
358
+ initCollaborativeCode,
359
+ updateCollaborativeLineInput,
360
+ updateCollaborativeInput,
361
+ deleteCollaborativeInput,
362
+ deleteCollaborativeLineInput
363
+ }
364
+ >>> > >>> origin / collab - fix
0 commit comments