@@ -6,6 +6,7 @@ import { parseRow as parseComponentRow } from "~repository/ComponentRepo";
66import  {  entityNotFoundError  }  from  "~exceptions/genericErrors" ; 
77import  APIError  from  "~utils/APIError" ; 
88import  {  HTTP_BAD_REQUEST  }  from  "~utils/http_code" ; 
9+ import  {  IPortfolioPayload  }  from  "~interfaces/IPortfolioPayload" ; 
910
1011/** 
1112 * ADMIN 
@@ -14,11 +15,13 @@ import { HTTP_BAD_REQUEST } from "~utils/http_code";
1415 * 
1516 * @param  title - title for the portfolio 
1617 * @param  components  - components that belong to the given portfolio 
18+  * @param  payload  - original payload so part of it can be saved in the db for future retrieval 
1719 * @param  description - optional descrition for the portfolio 
1820 */ 
1921export  const  addPortfolio  =  async  ( 
2022  title : string , 
2123  components : Component [ ] , 
24+   payload : IPortfolioPayload , 
2225  description ?: string , 
2326)  =>  { 
2427  try  { 
@@ -28,7 +31,40 @@ export const addPortfolio = async (
2831        . doc ( component . id ) , 
2932    ) ; 
3033
31-     const  insertObj  =  Portfolio . init ( title ,  componentRefs ,  description ) ; 
34+     const  {  categoryId,  componentId,  projectId,  technologyId }  =  payload ; 
35+ 
36+     const  payloadComponentRefs  =  componentId . map ( component  => 
37+       getDb ( ) 
38+         . collection ( "components" ) 
39+         . doc ( component ) , 
40+     ) ; 
41+ 
42+     const  payloadCategoryRefs  =  categoryId . map ( category  => 
43+       getDb ( ) 
44+         . collection ( "categories" ) 
45+         . doc ( category ) , 
46+     ) ; 
47+ 
48+     const  payloadProjectRefs  =  projectId . map ( project  => 
49+       getDb ( ) 
50+         . collection ( "projects" ) 
51+         . doc ( project ) , 
52+     ) ; 
53+ 
54+     const  payloadTechnologyRefs  =  technologyId . map ( technology  => 
55+       getDb ( ) 
56+         . collection ( "technologies" ) 
57+         . doc ( technology ) , 
58+     ) ; 
59+ 
60+     const  refs  =  { 
61+       category : payloadCategoryRefs , 
62+       project : payloadProjectRefs , 
63+       technology : payloadTechnologyRefs , 
64+       component : payloadComponentRefs , 
65+     } ; 
66+ 
67+     const  insertObj  =  Portfolio . init ( title ,  componentRefs ,  refs ,  description ) ; 
3268
3369    await  getDb ( ) 
3470      . collection ( "portfolios" ) 
@@ -52,12 +88,14 @@ export const addPortfolio = async (
5288 * @param  portfolioId - id of the portfolio that needs to be updated 
5389 * @param  title - title for the portfolio 
5490 * @param  components  - components that belong to the given portfolio 
91+  * @param  payload  - original payload so part of it can be saved in the db for future retrieval 
5592 * @param  description - optional descrition for the portfolio 
5693 */ 
5794export  const  updatePortfolio  =  async  ( 
5895  portfolioId : string , 
5996  title : string , 
6097  components : Component [ ] , 
98+   payload : IPortfolioPayload , 
6199  description ?: string , 
62100)  =>  { 
63101  try  { 
@@ -85,6 +123,41 @@ export const updatePortfolio = async (
85123      throw  new  APIError ( "No attributes specified for updation" ,  undefined ,  HTTP_BAD_REQUEST ) ; 
86124    } 
87125
126+     const  {  categoryId,  componentId,  projectId,  technologyId }  =  payload ; 
127+ 
128+     const  payloadComponentRefs  =  componentId . map ( component  => 
129+       getDb ( ) 
130+         . collection ( "components" ) 
131+         . doc ( component ) , 
132+     ) ; 
133+ 
134+     const  payloadCategoryRefs  =  categoryId . map ( category  => 
135+       getDb ( ) 
136+         . collection ( "categories" ) 
137+         . doc ( category ) , 
138+     ) ; 
139+ 
140+     const  payloadProjectRefs  =  projectId . map ( project  => 
141+       getDb ( ) 
142+         . collection ( "projects" ) 
143+         . doc ( project ) , 
144+     ) ; 
145+ 
146+     const  payloadTechnologyRefs  =  technologyId . map ( technology  => 
147+       getDb ( ) 
148+         . collection ( "technologies" ) 
149+         . doc ( technology ) , 
150+     ) ; 
151+ 
152+     const  refs  =  { 
153+       category : payloadCategoryRefs , 
154+       project : payloadProjectRefs , 
155+       technology : payloadTechnologyRefs , 
156+       component : payloadComponentRefs , 
157+     } ; 
158+ 
159+     obj . refs  =  refs ; 
160+ 
88161    await  getDb ( ) 
89162      . collection ( "portfolios" ) 
90163      . doc ( portfolioId ) 
@@ -159,16 +232,93 @@ export const fetchPublicPortfolio = async (portfolioCode: string) => {
159232 * 
160233 * fetch all portfolios 
161234 * 
162-  * @param  showComponents - boolean to indicate if the components should be fetched as well 
163235 */ 
164- export  const  fetchPortFolios  =  async  ( showComponents :  boolean   =   false )  =>  { 
236+ export  const  fetchPortFolios  =  async  ( )  =>  { 
165237  try  { 
166238    const  portfolios  =  await  getDb ( ) 
167239      . collection ( "portfolios" ) 
168240      . where ( "status" ,  "==" ,  STATUS_ACTIVE ) 
169241      . get ( ) ; 
170242
171-     const  promises  =  portfolios . docs . map ( project  =>  parseRow ( project . data ( ) ,  showComponents ) ) ; 
243+     const  promises  =  portfolios . docs . map ( async  item  =>  { 
244+       const  row  =  item . data ( ) ; 
245+       const  references  =  row . refs ; 
246+ 
247+       let  categoryData  =  [ ] ; 
248+       let  componentData  =  [ ] ; 
249+       let  projectData  =  [ ] ; 
250+       let  technologyData  =  [ ] ; 
251+ 
252+       if  ( references )  { 
253+         const  categoryPromises  =  row . refs . category . map ( 
254+           async  ( categoryRef : FirebaseFirestore . DocumentReference )  =>  { 
255+             const  category  =  await  categoryRef . get ( ) ; 
256+             const  categoryData  =  category . exists  ? category . data ( )  : null ; 
257+             return  { 
258+               id : categoryData . id , 
259+               name : categoryData . name , 
260+             } ; 
261+           } , 
262+         ) ; 
263+ 
264+         const  componentPromises  =  row . refs . component . map ( 
265+           async  ( componentRef : FirebaseFirestore . DocumentReference )  =>  { 
266+             const  component  =  await  componentRef . get ( ) ; 
267+             const  componentData  =  component . exists  ? component . data ( )  : null ; 
268+             return  { 
269+               id : componentData . id , 
270+               name : componentData . name , 
271+             } ; 
272+           } , 
273+         ) ; 
274+ 
275+         const  projectPromises  =  row . refs . project . map ( 
276+           async  ( projectRef : FirebaseFirestore . DocumentReference )  =>  { 
277+             const  project  =  await  projectRef . get ( ) ; 
278+             const  projectData  =  project . exists  ? project . data ( )  : null ; 
279+             return  { 
280+               id : projectData . id , 
281+               name : projectData . name , 
282+             } ; 
283+           } , 
284+         ) ; 
285+ 
286+         const  technologyPromises  =  row . refs . technology . map ( 
287+           async  ( technologyRef : FirebaseFirestore . DocumentReference )  =>  { 
288+             const  technology  =  await  technologyRef . get ( ) ; 
289+             const  technologyData  =  technology . exists  ? technology . data ( )  : null ; 
290+             return  { 
291+               id : technologyData . id , 
292+               name : technologyData . name , 
293+             } ; 
294+           } , 
295+         ) ; 
296+ 
297+         const  [ category ,  component ,  project ,  technology ]  =  await  Promise . all ( [ 
298+           Promise . all ( categoryPromises ) , 
299+           Promise . all ( componentPromises ) , 
300+           Promise . all ( projectPromises ) , 
301+           Promise . all ( technologyPromises ) , 
302+         ] ) ; 
303+ 
304+         categoryData  =  category ; 
305+         componentData  =  component ; 
306+         projectData  =  project ; 
307+         technologyData  =  technology ; 
308+       } 
309+ 
310+       return  { 
311+         id : row . id , 
312+         title : row . title , 
313+         description : row . description , 
314+         category : categoryData , 
315+         component : componentData , 
316+         project : projectData , 
317+         technology : technologyData , 
318+         createdAt : row . createdAt . toDate ( ) , 
319+         updatedAt : row . updatedAt . toDate ( ) , 
320+       } ; 
321+     } ) ; 
172322
173323    return  await  Promise . all ( promises ) ; 
174324  }  catch  ( error )  { 
0 commit comments