@@ -6,6 +6,7 @@ import { parseRow as parseComponentRow } from "~repository/ComponentRepo";
6
6
import { entityNotFoundError } from "~exceptions/genericErrors" ;
7
7
import APIError from "~utils/APIError" ;
8
8
import { HTTP_BAD_REQUEST } from "~utils/http_code" ;
9
+ import { IPortfolioPayload } from "~interfaces/IPortfolioPayload" ;
9
10
10
11
/**
11
12
* ADMIN
@@ -14,11 +15,13 @@ import { HTTP_BAD_REQUEST } from "~utils/http_code";
14
15
*
15
16
* @param title - title for the portfolio
16
17
* @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
17
19
* @param description - optional descrition for the portfolio
18
20
*/
19
21
export const addPortfolio = async (
20
22
title : string ,
21
23
components : Component [ ] ,
24
+ payload : IPortfolioPayload ,
22
25
description ?: string ,
23
26
) => {
24
27
try {
@@ -28,7 +31,40 @@ export const addPortfolio = async (
28
31
. doc ( component . id ) ,
29
32
) ;
30
33
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 ) ;
32
68
33
69
await getDb ( )
34
70
. collection ( "portfolios" )
@@ -52,12 +88,14 @@ export const addPortfolio = async (
52
88
* @param portfolioId - id of the portfolio that needs to be updated
53
89
* @param title - title for the portfolio
54
90
* @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
55
92
* @param description - optional descrition for the portfolio
56
93
*/
57
94
export const updatePortfolio = async (
58
95
portfolioId : string ,
59
96
title : string ,
60
97
components : Component [ ] ,
98
+ payload : IPortfolioPayload ,
61
99
description ?: string ,
62
100
) => {
63
101
try {
@@ -85,6 +123,41 @@ export const updatePortfolio = async (
85
123
throw new APIError ( "No attributes specified for updation" , undefined , HTTP_BAD_REQUEST ) ;
86
124
}
87
125
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
+
88
161
await getDb ( )
89
162
. collection ( "portfolios" )
90
163
. doc ( portfolioId )
@@ -159,16 +232,93 @@ export const fetchPublicPortfolio = async (portfolioCode: string) => {
159
232
*
160
233
* fetch all portfolios
161
234
*
162
- * @param showComponents - boolean to indicate if the components should be fetched as well
163
235
*/
164
- export const fetchPortFolios = async ( showComponents : boolean = false ) => {
236
+ export const fetchPortFolios = async ( ) => {
165
237
try {
166
238
const portfolios = await getDb ( )
167
239
. collection ( "portfolios" )
168
240
. where ( "status" , "==" , STATUS_ACTIVE )
169
241
. get ( ) ;
170
242
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
+ } ) ;
172
322
173
323
return await Promise . all ( promises ) ;
174
324
} catch ( error ) {
0 commit comments