22 * project service
33 */
44
5- import { factories } from ' @strapi/strapi' ;
6- import * as fs from 'fs' ;
7- import * as csv from ' csv-parse/sync' ;
8- import * as stringify from ' csv-stringify/sync' ;
5+ import { factories } from " @strapi/strapi" ;
6+ import * as fs from "fs" ;
7+ import * as csv from " csv-parse/sync" ;
8+ import * as stringify from " csv-stringify/sync" ;
99
1010interface ProjectRow {
1111 countries : string ;
@@ -15,10 +15,13 @@ interface ProjectRow {
1515}
1616
1717async function findProjectStatusIdByName ( name : string ) : Promise < number > {
18- const result : any = await strapi . entityService . findMany ( 'api::project-status.project-status' , {
19- filters : { name } ,
20- fields : [ 'id' ] ,
21- } ) ;
18+ const result : any = await strapi . entityService . findMany (
19+ "api::project-status.project-status" ,
20+ {
21+ filters : { name } ,
22+ fields : [ "id" ] ,
23+ }
24+ ) ;
2225
2326 if ( result . length === 0 ) {
2427 throw new Error ( `No project status found with the name "${ name } "` ) ;
@@ -28,10 +31,13 @@ async function findProjectStatusIdByName(name: string): Promise<number> {
2831}
2932
3033async function findObjectiveIdByType ( type : string ) : Promise < number > {
31- const result : any = await strapi . entityService . findMany ( 'api::objective.objective' , {
32- filters : { type } ,
33- fields : [ 'id' ] ,
34- } ) ;
34+ const result : any = await strapi . entityService . findMany (
35+ "api::objective.objective" ,
36+ {
37+ filters : { type } ,
38+ fields : [ "id" ] ,
39+ }
40+ ) ;
3541
3642 if ( result . length === 0 ) {
3743 throw new Error ( `No objective found with the name "${ type } "` ) ;
@@ -41,10 +47,13 @@ async function findObjectiveIdByType(type: string): Promise<number> {
4147}
4248
4349async function findOrganizationTypeIdByName ( name : string ) : Promise < number > {
44- const result : any = await strapi . entityService . findMany ( 'api::organization-type.organization-type' , {
45- filters : { name } ,
46- fields : [ 'id' ] ,
47- } ) ;
50+ const result : any = await strapi . entityService . findMany (
51+ "api::organization-type.organization-type" ,
52+ {
53+ filters : { name } ,
54+ fields : [ "id" ] ,
55+ }
56+ ) ;
4857
4958 if ( result . length === 0 ) {
5059 throw new Error ( `No organization type found with the name "${ name } "` ) ;
@@ -54,10 +63,13 @@ async function findOrganizationTypeIdByName(name: string): Promise<number> {
5463}
5564
5665async function findFundingIdByName ( name : string ) : Promise < number > {
57- const result : any = await strapi . entityService . findMany ( 'api::organization-type.organization-type' , {
58- filters : { name } ,
59- fields : [ 'id' ] ,
60- } ) ;
66+ const result : any = await strapi . entityService . findMany (
67+ "api::organization-type.organization-type" ,
68+ {
69+ filters : { name } ,
70+ fields : [ "id" ] ,
71+ }
72+ ) ;
6173
6274 if ( result . length === 0 ) {
6375 throw new Error ( `No funding found with the name "${ name } "` ) ;
@@ -67,10 +79,13 @@ async function findFundingIdByName(name: string): Promise<number> {
6779}
6880
6981async function findWorldCountryIdByName ( name : string ) : Promise < number > {
70- const result : any = await strapi . entityService . findMany ( 'api::world-country.world-country' , {
71- filters : { name } ,
72- fields : [ 'id' ] ,
73- } ) ;
82+ const result : any = await strapi . entityService . findMany (
83+ "api::world-country.world-country" ,
84+ {
85+ filters : { name } ,
86+ fields : [ "id" ] ,
87+ }
88+ ) ;
7489
7590 if ( result . length === 0 ) {
7691 throw new Error ( `No world country found with the name "${ name } "` ) ;
@@ -79,13 +94,14 @@ async function findWorldCountryIdByName(name: string): Promise<number> {
7994 return result [ 0 ] . id ;
8095}
8196
82-
83-
8497async function findCountryIdByName ( name : string ) : Promise < number > {
85- const result : any = await strapi . entityService . findMany ( 'api::country.country' , {
86- filters : { name } ,
87- fields : [ 'id' ] ,
88- } ) ;
98+ const result : any = await strapi . entityService . findMany (
99+ "api::country.country" ,
100+ {
101+ filters : { name } ,
102+ fields : [ "id" ] ,
103+ }
104+ ) ;
89105
90106 if ( result . length === 0 ) {
91107 throw new Error ( `No country found with the name "${ name } "` ) ;
@@ -95,9 +111,9 @@ async function findCountryIdByName(name: string): Promise<number> {
95111}
96112
97113async function findSdgIdByName ( name : string ) : Promise < number > {
98- const result : any = await strapi . entityService . findMany ( ' api::sdg.sdg' , {
114+ const result : any = await strapi . entityService . findMany ( " api::sdg.sdg" , {
99115 filters : { name } ,
100- fields : [ 'id' ] ,
116+ fields : [ "id" ] ,
101117 } ) ;
102118
103119 if ( result . length === 0 ) {
@@ -108,10 +124,13 @@ async function findSdgIdByName(name: string): Promise<number> {
108124}
109125
110126async function findPillarIdByName ( name : string ) : Promise < number > {
111- const result : any = await strapi . entityService . findMany ( 'api::pillar.pillar' , {
112- filters : { name } ,
113- fields : [ 'id' ] ,
114- } ) ;
127+ const result : any = await strapi . entityService . findMany (
128+ "api::pillar.pillar" ,
129+ {
130+ filters : { name } ,
131+ fields : [ "id" ] ,
132+ }
133+ ) ;
115134
116135 if ( result . length === 0 ) {
117136 throw new Error ( `No pillar found with the name "${ name } "` ) ;
@@ -120,70 +139,109 @@ async function findPillarIdByName(name: string): Promise<number> {
120139 return result [ 0 ] . id ;
121140}
122141
123- export default factories . createCoreService ( ' api::project.project' , {
142+ export default factories . createCoreService ( " api::project.project" , {
124143 async parseAndReplaceIds ( file , author = null ) {
125144 // Read and parse the CSV file
126- const fileContent = fs . readFileSync ( file . path , 'utf8' ) ;
127-
128- const allowedColumns = [ 'name' , 'highlight' , 'status' , 'objective' , 'amount' , 'countries' , 'source_country' , 'sdgs' , 'pillar' , 'organization_type' , 'info' , 'funding' ] ;
145+ const fileContent = fs . readFileSync ( file . path , "utf8" ) ;
146+
147+ const allowedColumns = [
148+ "name" ,
149+ "highlight" ,
150+ "status" ,
151+ "objective" ,
152+ "amount" ,
153+ "countries" ,
154+ "source_country" ,
155+ "sdgs" ,
156+ "sector" ,
157+ "organization_type" ,
158+ "info" ,
159+ "funding" ,
160+ ] ;
129161
130162 const records : ProjectRow [ ] = csv . parse ( fileContent , {
131163 columns : true ,
132164 skip_empty_lines : true ,
133165 } ) ;
134166
135167 if ( records . length === 0 ) {
136- throw new Error ( ' CSV file is empty' ) ;
168+ throw new Error ( " CSV file is empty" ) ;
137169 }
138170
139171 const csvColumns = Object . keys ( records [ 0 ] ) ;
140172
141- const invalidColumns = csvColumns . filter ( col => ! allowedColumns . includes ( col ) ) ;
173+ const invalidColumns = csvColumns . filter (
174+ ( col ) => ! allowedColumns . includes ( col )
175+ ) ;
142176 if ( invalidColumns . length > 0 ) {
143- throw new Error ( `Invalid columns detected: ${ invalidColumns . join ( ', ' ) } ` ) ;
177+ throw new Error ( `Invalid columns detected: ${ invalidColumns . join ( ", " ) } ` ) ;
144178 }
145179
146- const missingColumns = allowedColumns . filter ( col => ! csvColumns . includes ( col ) ) ;
180+ const missingColumns = allowedColumns . filter (
181+ ( col ) => ! csvColumns . includes ( col )
182+ ) ;
183+
147184 if ( missingColumns . length > 0 ) {
148- throw new Error ( `Missing required columns: ${ missingColumns . join ( ', ' ) } ` ) ;
185+ throw new Error ( `Missing required columns: ${ missingColumns . join ( ", " ) } ` ) ;
149186 }
150187
151188 // Process each row
152- const updatedRecords = await Promise . all ( records . map ( async ( row : ProjectRow ) => {
153- const countryNames = row . countries . split ( ';' ) . map ( name => name . trim ( ) ) ;
154- const sdgNames = row . sdgs . split ( ';' ) . map ( name => name . trim ( ) ) ;
155- const pillarNames = row . pillar . split ( ';' ) . map ( name => name . trim ( ) ) ;
156- const status = row . status ? [ await findProjectStatusIdByName ( row . status ) ] : [ ] ;
157- const sourceCountry = row . source_country ? [ await findWorldCountryIdByName ( row . source_country ) ] : [ ] ;
158- const objective = row . objective ? [ await findObjectiveIdByType ( row . objective ) ] : [ ] ;
159- const organizationType = row . organization_type ? [ await findOrganizationTypeIdByName ( row . organization_type ) ] : [ ]
160- const funding = row . funding ? [ await findFundingIdByName ( row . funding ) ] : [ ] ;
161-
162- const countryIds = await Promise . all ( countryNames . map ( name => findCountryIdByName ( name ) ) ) ;
163- const sdgIds = await Promise . all ( sdgNames . map ( name => findSdgIdByName ( name ) ) ) ;
164- const pillarIds = await Promise . all ( pillarNames . map ( name => findPillarIdByName ( name ) ) ) ;
165- const publishedAt = new Date ( ) . toISOString ( ) ;
166-
167- const updatedRow : any = {
168- ...row ,
169- countries : countryIds ,
170- sdgs : sdgIds ,
171- pillar : pillarIds ,
172- status,
173- source_country : sourceCountry ,
174- objective,
175- organization_type : organizationType ,
176- funding,
177- publishedAt,
178- } ;
179-
180- // Add author relation if provided
181- if ( author ) {
182- updatedRow . author = [ author ] ;
183- }
184-
185- return updatedRow ;
186- } ) ) ;
189+ const updatedRecords = await Promise . all (
190+ records . map ( async ( row : ProjectRow ) => {
191+ const countryNames = row . countries
192+ . split ( ";" )
193+ . map ( ( name ) => name . trim ( ) ) ;
194+ const sdgNames = row . sdgs . split ( ";" ) . map ( ( name ) => name . trim ( ) ) ;
195+ const pillarNames = row . sector . split ( ";" ) . map ( ( name ) => name . trim ( ) ) ;
196+ const status = row . status
197+ ? [ await findProjectStatusIdByName ( row . status ) ]
198+ : [ ] ;
199+ const sourceCountry = row . source_country
200+ ? [ await findWorldCountryIdByName ( row . source_country ) ]
201+ : [ ] ;
202+ const objective = row . objective
203+ ? [ await findObjectiveIdByType ( row . objective ) ]
204+ : [ ] ;
205+ const organizationType = row . organization_type
206+ ? [ await findOrganizationTypeIdByName ( row . organization_type ) ]
207+ : [ ] ;
208+ const funding = row . funding
209+ ? [ await findFundingIdByName ( row . funding ) ]
210+ : [ ] ;
211+
212+ const countryIds = await Promise . all (
213+ countryNames . map ( ( name ) => findCountryIdByName ( name ) )
214+ ) ;
215+ const sdgIds = await Promise . all (
216+ sdgNames . map ( ( name ) => findSdgIdByName ( name ) )
217+ ) ;
218+ const pillarIds = await Promise . all (
219+ pillarNames . map ( ( name ) => findPillarIdByName ( name ) )
220+ ) ;
221+ const publishedAt = new Date ( ) . toISOString ( ) ;
222+
223+ const { sector, ...rowWithoutSector } = row ;
224+ const updatedRow : any = {
225+ ...rowWithoutSector ,
226+ countries : countryIds ,
227+ sdgs : sdgIds ,
228+ pillar : pillarIds ,
229+ status,
230+ source_country : sourceCountry ,
231+ objective,
232+ organization_type : organizationType ,
233+ funding,
234+ publishedAt,
235+ } ;
236+
237+ // Add author relation if provided
238+ if ( author ) {
239+ updatedRow . author = [ author ] ;
240+ }
241+
242+ return updatedRow ;
243+ } )
244+ ) ;
187245
188246 const updatedCSV = stringify . stringify ( updatedRecords , { header : true } ) ;
189247
0 commit comments