@@ -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
@@ -44,6 +44,9 @@ export class DbHelper {
4444 * @param id - id of the workspace to fetch
4545 */
4646 public getWorkspacesWithTariffPlans ( id : string ) : Promise < WorkspaceWithTariffPlan > ;
47+ /**
48+ * @param id - id of the workspace to fetch
49+ */
4750 public getWorkspacesWithTariffPlans ( id ?: string ) : AsyncGenerator < WorkspaceWithTariffPlan > | Promise < WorkspaceWithTariffPlan > {
4851 if ( id !== undefined ) {
4952 return this . getOneWorkspaceWithTariffPlan ( id ) ;
@@ -52,66 +55,6 @@ export class DbHelper {
5255 return this . yieldWorkspacesWithTariffPlans ( ) ;
5356 }
5457
55- /**
56- * Returns a single workspace with its tariff plan by id
57- *
58- * @param id - workspace id
59- */
60- private async getOneWorkspaceWithTariffPlan ( id : string ) : Promise < WorkspaceWithTariffPlan > {
61- const pipeline = [
62- {
63- $match : {
64- _id : new ObjectId ( id ) ,
65- } ,
66- } ,
67- ...this . tariffPlanLookupPipeline ( ) ,
68- ] ;
69-
70- return this . workspacesCollection . aggregate < WorkspaceWithTariffPlan > ( pipeline ) . next ( ) ;
71- }
72-
73- /**
74- * Yields all workspaces with their tariff plans one by one
75- */
76- private async * yieldWorkspacesWithTariffPlans ( ) : AsyncGenerator < WorkspaceWithTariffPlan > {
77- const pipeline = this . tariffPlanLookupPipeline ( ) ;
78- const cursor = this . workspacesCollection . aggregate < WorkspaceWithTariffPlan > ( pipeline ) ;
79-
80- for await ( const workspace of cursor ) {
81- yield workspace ;
82- }
83- }
84-
85- /* eslint-disable-next-line */
86- private tariffPlanLookupPipeline ( ) : any [ ] {
87- return [
88- {
89- $lookup : {
90- from : 'plans' ,
91- localField : 'tariffPlanId' ,
92- foreignField : '_id' ,
93- as : 'tariffPlan' ,
94- } ,
95- } ,
96- {
97- $unwind : {
98- path : '$tariffPlan' ,
99- } ,
100- } ,
101- {
102- $project : {
103- _id : 1 ,
104- name : 1 ,
105- isBlocked : 1 ,
106- blockedDate : 1 ,
107- lastChargeDate : 1 ,
108- billingPeriodEventsCount : 1 ,
109- tariffPlan : 1 ,
110- } ,
111- } ,
112- ] ;
113- }
114-
11558 /**
11659 * Updates workspaces data in Database
11760 *
@@ -204,4 +147,72 @@ export class DbHelper {
204147
205148 return this . projectsCollection . find ( query ) . toArray ( ) ;
206149 }
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 = 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+ }
207218}
0 commit comments