@@ -2,7 +2,7 @@ import { Collection, Db, ObjectId } from 'mongodb';
22import { ProjectDBScheme , WorkspaceDBScheme } from '@hawk.so/types' ;
33import { WorkspaceWithTariffPlan } from '../types' ;
44import HawkCatcher from '@hawk.so/nodejs' ;
5- import { CriticalError } from '../../../lib/workerErrors' ;
5+ import { CriticalError , NonCriticalError } from '../../../lib/workerErrors' ;
66
77/**
88 * Class that implements methods used for interaction between limiter and db
@@ -35,49 +35,24 @@ export class DbHelper {
3535 }
3636
3737 /**
38- * Method that returns all workspaces with their tariff plans
38+ * Method that yields all workspaces with their tariff plans
3939 */
40- public async getWorkspacesWithTariffPlans ( ) :Promise < WorkspaceWithTariffPlan [ ] > ;
40+ public getWorkspacesWithTariffPlans ( ) : AsyncGenerator < WorkspaceWithTariffPlan > ;
4141 /**
4242 * Method that returns workspace with its tariff plan by its id
4343 *
4444 * @param id - id of the workspace to fetch
4545 */
46- public async getWorkspacesWithTariffPlans ( id : string ) :Promise < WorkspaceWithTariffPlan > ;
46+ public getWorkspacesWithTariffPlans ( id : string ) : Promise < WorkspaceWithTariffPlan > ;
4747 /**
48- * Returns workspace with its tariff plan by its id
49- *
50- * @param id - workspace id
48+ * @param id - id of the workspace to fetch
5149 */
52- public async getWorkspacesWithTariffPlans ( id ?: string ) :Promise < WorkspaceWithTariffPlan [ ] | WorkspaceWithTariffPlan > {
53- /* eslint-disable-next-line */
54- const queue : any [ ] = [
55- {
56- $lookup : {
57- from : 'plans' ,
58- localField : 'tariffPlanId' ,
59- foreignField : '_id' ,
60- as : 'tariffPlan' ,
61- } ,
62- } ,
63- {
64- $unwind : {
65- path : '$tariffPlan' ,
66- } ,
67- } ,
68- ] ;
69-
50+ public getWorkspacesWithTariffPlans ( id ?: string ) : AsyncGenerator < WorkspaceWithTariffPlan > | Promise < WorkspaceWithTariffPlan > {
7051 if ( id !== undefined ) {
71- queue . unshift ( {
72- $match : {
73- _id : new ObjectId ( id ) ,
74- } ,
75- } ) ;
52+ return this . getOneWorkspaceWithTariffPlan ( id ) ;
7653 }
7754
78- const workspacesArray = await this . workspacesCollection . aggregate < WorkspaceWithTariffPlan > ( queue ) . toArray ( ) ;
79-
80- return ( id !== undefined ) ? workspacesArray [ 0 ] : workspacesArray ;
55+ return this . yieldWorkspacesWithTariffPlans ( ) ;
8156 }
8257
8358 /**
@@ -172,4 +147,72 @@ export class DbHelper {
172147
173148 return this . projectsCollection . find ( query ) . toArray ( ) ;
174149 }
150+
151+ /**
152+ * Returns a single workspace with its tariff plan by id
153+ *
154+ * @param id - workspace id
155+ */
156+ private async getOneWorkspaceWithTariffPlan ( id : string ) : Promise < WorkspaceWithTariffPlan > {
157+ const pipeline = [
158+ {
159+ $match : {
160+ _id : new ObjectId ( id ) ,
161+ } ,
162+ } ,
163+ ...this . tariffPlanLookupPipeline ( ) ,
164+ ] ;
165+
166+ const workspace = await this . workspacesCollection . aggregate < WorkspaceWithTariffPlan > ( pipeline ) . next ( ) ;
167+
168+ if ( workspace === null ) {
169+ throw new NonCriticalError ( `Workspace ${ id } not found` , {
170+ workspaceId : id ,
171+ } ) ;
172+ }
173+
174+ return workspace ;
175+ }
176+
177+ /**
178+ * Yields all workspaces with their tariff plans one by one
179+ */
180+ private async * yieldWorkspacesWithTariffPlans ( ) : AsyncGenerator < WorkspaceWithTariffPlan > {
181+ const pipeline = this . tariffPlanLookupPipeline ( ) ;
182+ const cursor = this . workspacesCollection . aggregate < WorkspaceWithTariffPlan > ( pipeline ) ;
183+
184+ for await ( const workspace of cursor ) {
185+ yield workspace ;
186+ }
187+ }
188+
189+ /* eslint-disable-next-line */
190+ private tariffPlanLookupPipeline ( ) : any [ ] {
191+ return [
192+ {
193+ $lookup : {
194+ from : 'plans' ,
195+ localField : 'tariffPlanId' ,
196+ foreignField : '_id' ,
197+ as : 'tariffPlan' ,
198+ } ,
199+ } ,
200+ {
201+ $unwind : {
202+ path : '$tariffPlan' ,
203+ } ,
204+ } ,
205+ {
206+ $project : {
207+ _id : 1 ,
208+ name : 1 ,
209+ isBlocked : 1 ,
210+ blockedDate : 1 ,
211+ lastChargeDate : 1 ,
212+ billingPeriodEventsCount : 1 ,
213+ tariffPlan : 1 ,
214+ } ,
215+ } ,
216+ ] ;
217+ }
175218}
0 commit comments