-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed a step to seed data during dock image building (#277)
- Loading branch information
1 parent
26bb22d
commit 73b4c1b
Showing
3 changed files
with
38 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,55 @@ | ||
import { ReadStream } from 'fs'; | ||
import { GraphQLError } from 'graphql'; | ||
import * as xlsx from 'xlsx'; | ||
import { ReadStream } from 'fs' | ||
import { GraphQLError } from 'graphql' | ||
import * as xlsx from 'xlsx' | ||
|
||
const EmailPattern: RegExp = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | ||
const EmailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/ | ||
|
||
type Role = 'trainee' | 'admin' | 'ttl' | 'coordinator'; | ||
type Role = 'trainee' | 'admin' | 'ttl' | 'coordinator' | ||
|
||
export async function extractFileData(file: any) { | ||
try { | ||
const { createReadStream, filename } = await file; | ||
const stream: ReadStream = createReadStream(); | ||
const { createReadStream, filename } = await file | ||
const stream: ReadStream = createReadStream() | ||
|
||
const buffer = await new Promise<Buffer>((resolve, reject) => { | ||
const chunks: any[] = []; | ||
stream.on('data', (chunk) => chunks.push(chunk)); | ||
stream.on('end', () => resolve(Buffer.concat(chunks))); | ||
stream.on('error', reject); | ||
}); | ||
const chunks: any[] = [] | ||
stream.on('data', (chunk) => chunks.push(chunk)) | ||
stream.on('end', () => resolve(Buffer.concat(chunks))) | ||
stream.on('error', reject) | ||
}) | ||
|
||
const workbook: xlsx.WorkBook = xlsx.read(buffer, { type: 'buffer' }); | ||
const invitees: { email: string; role: Role }[] = []; | ||
const invalidRows: string[] = []; | ||
const workbook: xlsx.WorkBook = xlsx.read(buffer, { type: 'buffer' }) | ||
const invitees: { email: string; role: Role }[] = [] | ||
const invalidRows: string[] = [] | ||
|
||
workbook.SheetNames.forEach((sheetName) => { | ||
const worksheet: xlsx.WorkSheet = workbook.Sheets[sheetName]; | ||
const data: any[] = xlsx.utils.sheet_to_json(worksheet); | ||
const worksheet: xlsx.WorkSheet = workbook.Sheets[sheetName] | ||
const data: any[] = xlsx.utils.sheet_to_json(worksheet) | ||
|
||
data.forEach((row) => { | ||
const email = (row as { email?: string }).email?.trim(); | ||
const role = (row as { role?: string }).role?.trim().toLowerCase(); | ||
|
||
const validRoles: Role[] = ['trainee', 'admin', 'ttl', 'coordinator']; | ||
if (email && EmailPattern.test(email) && role && validRoles.includes(role as Role)) { | ||
invitees.push({ email, role: role as Role }); | ||
const email = (row as { email?: string }).email?.trim() | ||
const role = (row as { role?: string }).role?.trim().toLowerCase() | ||
|
||
const validRoles: Role[] = ['trainee', 'admin', 'ttl', 'coordinator'] | ||
if ( | ||
email && | ||
EmailPattern.test(email) && | ||
role && | ||
validRoles.includes(role as Role) | ||
) { | ||
invitees.push({ email, role: role as Role }) | ||
} else { | ||
invalidRows.push(JSON.stringify(row)); | ||
invalidRows.push(JSON.stringify(row)) | ||
} | ||
}); | ||
}); | ||
}) | ||
}) | ||
|
||
return { invitees, invalidRows, filename }; | ||
return { invitees, invalidRows, filename } | ||
} catch (error) { | ||
throw new GraphQLError('Failed to process the file.', { | ||
extensions: { | ||
code: 'FILE_PROCESSING_ERROR', | ||
}, | ||
}); | ||
}) | ||
} | ||
} |