@@ -10,7 +10,12 @@ import { StatusReturn } from "../types";
1010
1111const logger = createLogger ( "collection.importCsv" ) ;
1212
13- async function importFile ( fileContents : string , itemId : number , academicYear : AcademicYear ) {
13+ async function importFile (
14+ fileContents : string ,
15+ fileName : string ,
16+ itemId : number ,
17+ academicYear : AcademicYear ,
18+ ) {
1419 logger . info ( "Importing file for: " , itemId ) ;
1520
1621 const iter = parse ( fileContents , {
@@ -30,6 +35,15 @@ async function importFile(fileContents: string, itemId: number, academicYear: Ac
3035
3136 const academicYearReference = academicYearDB . year ;
3237
38+ // 0.1: Create import
39+ // Name: <csv file name> DD/MM/YYYY HH:MM
40+ const importName = `${ fileName } @ ${ new Date ( ) . toLocaleString ( "en-GB" ) } ` ;
41+ const importItem = await prisma . orderItemImport . create ( {
42+ data : {
43+ name : importName ,
44+ } ,
45+ } ) ;
46+
3347 for await ( const record of iter ) {
3448 logger . debug ( `Record: ${ JSON . stringify ( record ) } ` ) ;
3549
@@ -123,6 +137,7 @@ async function importFile(fileContents: string, itemId: number, academicYear: Ac
123137 variantId : varientDB . id ,
124138 quantity : quantity ,
125139 collected : false ,
140+ importId : importItem . id ,
126141 } ,
127142 } ) ;
128143 }
@@ -135,6 +150,7 @@ export interface CSVFormValues {
135150
136151export async function importCsv (
137152 fileContents : string ,
153+ fileName : string ,
138154 productId : number ,
139155 academicYear : AcademicYear ,
140156) : Promise < StatusReturn > {
@@ -145,7 +161,7 @@ export async function importCsv(
145161 } ) ;
146162
147163 try {
148- await importFile ( fileContents , productId , academicYear ) ;
164+ await importFile ( fileContents , fileName , productId , academicYear ) ;
149165 } catch ( e : any ) {
150166 return {
151167 status : "error" ,
@@ -161,3 +177,11 @@ export async function importCsv(
161177 message : `Data imported for ${ product ?. name } in ${ academicYear } ` ,
162178 } ;
163179}
180+
181+ export async function getImportList ( ) {
182+ return prisma . orderItemImport . findMany ( {
183+ orderBy : {
184+ date : "asc" ,
185+ } ,
186+ } ) ;
187+ }
0 commit comments