1
+ import fs from "fs" ;
2
+ import Papa from "papaparse" ;
1
3
import { and , count , eq , sql } from "drizzle-orm" ;
2
4
import { userSchema } from "../schemas/Basic/user.schema" ;
3
5
import { permanenceSchema } from "../schemas/Basic/permanence.schema" ;
4
6
import { db } from "../database/db" ;
5
7
import { userPermanenceSchema } from "../schemas/Relational/userpermanences.schema" ;
6
8
9
+ type CsvPermanence = {
10
+ name : string ;
11
+ description : string ;
12
+ location : string ;
13
+ start_at : string ; // ISO string
14
+ end_at : string ;
15
+ capacity : string ;
16
+ is_open : string ; // 'true' or 'false'
17
+ } ;
18
+
7
19
// Classes d'erreurs personnalisées
8
20
class UnauthorizedError extends Error { }
9
21
class AlreadyRegisteredError extends Error { }
@@ -250,4 +262,30 @@ export const getAllPermanencesWithUsers = async () => {
250
262
) ;
251
263
252
264
return results ;
253
- } ;
265
+ } ;
266
+
267
+ export const importPermanencesFromCSV = async ( filePath : string ) : Promise < void > => {
268
+ const fileContent = fs . readFileSync ( filePath , "utf8" ) ;
269
+
270
+ const { data, errors } = Papa . parse < CsvPermanence > ( fileContent , {
271
+ header : true ,
272
+ skipEmptyLines : true ,
273
+ } ) ;
274
+
275
+ if ( errors . length > 0 ) {
276
+ console . error ( "CSV parsing errors:" , errors ) ;
277
+ throw new Error ( "Erreur lors du parsing du CSV." ) ;
278
+ }
279
+
280
+ const parsedData = data . map ( ( r ) => ( {
281
+ name : r . name ,
282
+ description : r . description ,
283
+ location : r . location ,
284
+ start_at : new Date ( r . start_at ) ,
285
+ end_at : new Date ( r . end_at ) ,
286
+ capacity : parseInt ( r . capacity , 10 ) ,
287
+ is_open : r . is_open ?. toLowerCase ( ) === "true" ,
288
+ } ) ) ;
289
+
290
+ await db . insert ( permanenceSchema ) . values ( parsedData ) ;
291
+ } ;
0 commit comments