@@ -33,22 +33,9 @@ export interface TokensPair {
3333/**
3434 * Membership collection DB implementation
3535 */
36- export interface MembershipDBScheme {
37- /**
38- * Document id
39- */
40- _id : ObjectId ;
41-
42- /**
43- * User's workspace id
44- */
45- workspaceId : ObjectId ;
46-
47- /**
48- * Shows if member is pending
49- */
36+ export type MembershipDBScheme = Record < string , {
5037 isPending ?: boolean ;
51- }
38+ } > ;
5239
5340/**
5441 * This structure represents how user notifications are stored at the DB (in 'users' collection)
@@ -124,6 +111,11 @@ export default class UserModel extends AbstractModel<UserDBScheme> implements Us
124111 */
125112 public githubId ?: string ;
126113
114+ /**
115+ * User's workspaces
116+ */
117+ public workspaces ! : MembershipDBScheme ;
118+
127119 /**
128120 * User's original password (this field appears only after registration).
129121 * Using to send password to user after registration
@@ -155,11 +147,6 @@ export default class UserModel extends AbstractModel<UserDBScheme> implements Us
155147 */
156148 protected collection : Collection < UserDBScheme > ;
157149
158- /**
159- * Collection of user's workspaces
160- */
161- private membershipCollection : Collection < MembershipDBScheme > ;
162-
163150 /**
164151 * Model constructor
165152 * @param modelData - user data
@@ -174,7 +161,6 @@ export default class UserModel extends AbstractModel<UserDBScheme> implements Us
174161
175162 super ( modelData ) ;
176163
177- this . membershipCollection = this . dbConnection . collection ( 'membership:' + this . _id ) ;
178164 this . collection = this . dbConnection . collection < UserDBScheme > ( 'users' ) ;
179165 }
180166
@@ -339,19 +325,13 @@ export default class UserModel extends AbstractModel<UserDBScheme> implements Us
339325 * @param workspaceId - user's id to add
340326 * @param isPending - if true, mark user's membership as pending
341327 */
342- public async addWorkspace ( workspaceId : string , isPending = false ) : Promise < object > {
343- const doc : OptionalId < MembershipDBScheme > = {
344- workspaceId : new ObjectId ( workspaceId ) ,
345- } ;
346-
347- if ( isPending ) {
348- doc . isPending = isPending ;
349- }
350-
351- const documentId = ( await this . membershipCollection . insertOne ( doc ) ) . insertedId ;
328+ public async addWorkspace ( workspaceId : string , isPending = false ) : Promise < { workspaceId : string } > {
329+ await this . update (
330+ { _id : new ObjectId ( this . _id ) } ,
331+ { [ `workspaces.${ workspaceId } ` ] : { isPending } }
332+ ) ;
352333
353334 return {
354- id : documentId ,
355335 workspaceId,
356336 } ;
357337 }
@@ -361,9 +341,10 @@ export default class UserModel extends AbstractModel<UserDBScheme> implements Us
361341 * @param workspaceId - id of workspace to remove
362342 */
363343 public async removeWorkspace ( workspaceId : string ) : Promise < { workspaceId : string } > {
364- await this . membershipCollection . deleteOne ( {
365- workspaceId : new ObjectId ( workspaceId ) ,
366- } ) ;
344+ await this . collection . updateOne (
345+ { _id : new ObjectId ( this . _id ) } ,
346+ { $unset : { [ `workspaces.${ workspaceId } ` ] : '' } }
347+ ) ;
367348
368349 return {
369350 workspaceId,
@@ -375,11 +356,9 @@ export default class UserModel extends AbstractModel<UserDBScheme> implements Us
375356 * @param workspaceId - workspace id to confirm
376357 */
377358 public async confirmMembership ( workspaceId : string ) : Promise < void > {
378- await this . membershipCollection . updateOne (
379- {
380- workspaceId : new ObjectId ( workspaceId ) ,
381- } ,
382- { $unset : { isPending : '' } }
359+ await this . collection . updateOne (
360+ { _id : new ObjectId ( this . _id ) } ,
361+ { $unset : { [ `workspaces.${ workspaceId } .isPending` ] : '' } }
383362 ) ;
384363 }
385364
@@ -389,23 +368,22 @@ export default class UserModel extends AbstractModel<UserDBScheme> implements Us
389368 * @param ids - workspaces id to filter them if there are workspaces that doesn't belong to the user
390369 */
391370 public async getWorkspacesIds ( ids : ( string | ObjectId ) [ ] = [ ] ) : Promise < string [ ] > {
392- const idsAsObjectId = ids . map ( id => new ObjectId ( id ) ) ;
393- const searchQuery = ids . length ? {
394- workspaceId : {
395- $in : idsAsObjectId ,
396- } ,
397- isPending : {
398- $ne : true ,
399- } ,
400- } : {
401- isPending : {
402- $ne : true ,
403- } ,
404- } ;
371+ const res = [ ] ;
405372
406- const membershipDocuments = await this . membershipCollection . find ( searchQuery ) . toArray ( ) ;
373+ if ( ids . length === 0 ) {
374+ return Object . keys ( this . workspaces ) ;
375+ }
376+
377+ for ( const id of ids ) {
378+ const workspaceId = id . toString ( ) ;
379+ const workspace = this . workspaces [ workspaceId ] ;
380+
381+ if ( workspace && workspace . isPending !== true ) {
382+ res . push ( workspaceId ) ;
383+ }
384+ }
407385
408- return membershipDocuments . map ( doc => doc . workspaceId . toString ( ) ) ;
386+ return res ;
409387 }
410388
411389 /**
0 commit comments