-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #145 from codeanker/development
1.7.0
- Loading branch information
Showing
7 changed files
with
274 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,188 @@ | ||
import * as fs from 'fs' | ||
import * as path from 'path' | ||
|
||
import { Role } from '@prisma/client' | ||
import * as csv from 'fast-csv' | ||
import type { Middleware } from 'koa' | ||
|
||
import { logger } from '../logger' | ||
import { getEntityIdFromHeader } from '../authentication' | ||
import prisma from '../prisma' | ||
import { inputSchema as anmeldungCreateSchema } from '../services/anmeldung/anmeldungPublicCreate' | ||
import { getPersonCreateData } from '../services/person/schema/person.schema' | ||
import { customFieldValuesCreateMany } from '../types/defineCustomFieldValues' | ||
|
||
import { customParseFormat, dayjs } from '@codeanker/helpers' | ||
|
||
export const importAnmeldungen: Middleware = async function (ctx) { | ||
for (const file of Object.keys(ctx.request.files!)) { | ||
// TODO: Process file | ||
logger.info(`processing file ${file}`) | ||
dayjs.extend(customParseFormat) | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
export const importAnmeldungen: Middleware = async function (ctx, next) { | ||
let accountId | ||
try { | ||
accountId = await getEntityIdFromHeader(ctx.request.header.authorization) | ||
} catch (e) { | ||
ctx.response.status = 401 | ||
ctx.response.message = 'Token not valid' | ||
return | ||
} | ||
|
||
ctx.response.status = 200 | ||
ctx.response.body = { | ||
ok: true, | ||
if (accountId == null || !ctx.request.files || !ctx.request.body.unterveranstaltungId) { | ||
ctx.response.status = 400 | ||
ctx.response.message = 'Es wurden keine Dateien oder Unterveranstaltung übergeben' | ||
return | ||
} | ||
|
||
const account = await prisma.account.findUnique({ | ||
where: { | ||
id: parseInt(accountId), | ||
}, | ||
select: { | ||
role: true, | ||
person: { | ||
select: { | ||
firstname: true, | ||
lastname: true, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
if (account == null) { | ||
ctx.res.statusCode = 401 | ||
ctx.res.end() | ||
return | ||
} | ||
|
||
if (account.role !== Role.ADMIN) { | ||
ctx.response.status = 401 | ||
ctx.response.message = 'Account not authorized' | ||
return | ||
} | ||
|
||
let files: any = [] | ||
if (Array.isArray(ctx.request.files.files)) { | ||
files = ctx.request.files.files | ||
} else { | ||
files.push(ctx.request.files.files) | ||
} | ||
|
||
files.filter((file: any) => { | ||
if (file.mimetype !== 'text/csv') { | ||
ctx.response.status = 406 | ||
ctx.response.message = 'Datei muss vom Typ CSV sein' | ||
return | ||
} | ||
}) | ||
|
||
const unterveranstaltung = await findUnterveranstaltung(parseInt(ctx.request.body.unterveranstaltungId)) | ||
if (!unterveranstaltung) { | ||
ctx.response.status = 400 | ||
ctx.response.message = 'Unterveranstaltung nicht gefunden' | ||
return | ||
} | ||
|
||
files.forEach((file) => { | ||
fs.createReadStream(path.resolve(file.filepath)) | ||
.pipe(csv.parse({ headers: true, delimiter: ';', ignoreEmpty: true })) | ||
.on('error', (error) => console.error(error)) | ||
.on('data', (row) => createAnmeldung(row, unterveranstaltung)) | ||
// eslint-disable-next-line no-console | ||
.on('end', (rowCount: number) => console.log(`Parsed ${rowCount} rows`)) | ||
|
||
ctx.response.status = 200 | ||
ctx.response.body = { | ||
ok: true, | ||
} | ||
}) | ||
} | ||
|
||
/** | ||
* Erstelle Anmeldung in der Datenbank | ||
* @param row | ||
* @param unterveranstaltungId | ||
*/ | ||
async function createAnmeldung(row: any, unterveranstaltung) { | ||
// console.log(unterveranstaltungId, row) | ||
|
||
try { | ||
const mappedRow = { | ||
data: { | ||
gliederungId: unterveranstaltung.gliederungId, | ||
unterveranstaltungId: unterveranstaltung.id, | ||
//Person Schema | ||
firstname: row.vorname, | ||
lastname: row.nachname, | ||
birthday: dayjs(row.geburtstag, 'DD.MM.YY').toDate(), | ||
gender: row.geschlecht, | ||
email: row.email, | ||
telefon: row.telefon, | ||
address: { | ||
street: row.strasse, | ||
number: row.hausnummer, | ||
zip: row.plz, | ||
city: row.ort, | ||
}, | ||
essgewohnheit: row.essgewohnheit, | ||
nahrungsmittelIntoleranzen: formatNahrungsmittelIntoleranzen(row.nahrungsmittelIntoleranzen), | ||
weitereIntoleranzen: formatNahrungsmittelIntoleranzen(row.weitereIntoleranzen), | ||
notfallkontaktPersonen: [ | ||
{ | ||
firstname: row.notfallkontaktVorname, | ||
lastname: row.notfallkontaktNachname, | ||
telefon: row.notfallkontaktTelefon, | ||
istErziehungsberechtigt: row.istErziehungsberechtigt === 'Ja' ? true : false, | ||
}, | ||
], | ||
}, | ||
customFieldValues: [ | ||
{ | ||
fieldId: parseInt(row.customField1Id), | ||
value: row.customField1Value, | ||
}, | ||
], | ||
} | ||
|
||
const validatedData = anmeldungCreateSchema.parse(mappedRow) | ||
|
||
const personData = await getPersonCreateData(validatedData.data) | ||
|
||
await prisma.person.create({ | ||
data: { | ||
...personData, | ||
anmeldungen: { | ||
create: { | ||
unterveranstaltungId: unterveranstaltung.id, | ||
comment: validatedData.data.comment, | ||
createdAt: new Date(), | ||
customFieldValues: { | ||
createMany: customFieldValuesCreateMany(validatedData.customFieldValues), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
} catch (e) { | ||
throw new Error('Anmeldung konnte nicht erstellt werden') | ||
} | ||
} | ||
/** | ||
* Suche nach der passenden Unterveranstaltung anhand der ID | ||
* @param unterveranstaltungId | ||
* @returns | ||
*/ | ||
async function findUnterveranstaltung(unterveranstaltungId: number) { | ||
return await prisma.unterveranstaltung.findUnique({ | ||
where: { | ||
id: unterveranstaltungId, | ||
}, | ||
select: { | ||
id: true, | ||
gliederungId: true, | ||
}, | ||
}) | ||
} | ||
|
||
function formatNahrungsmittelIntoleranzen(nahrungsmittelIntoleranzen: string) { | ||
if (!nahrungsmittelIntoleranzen) return [] | ||
return nahrungsmittelIntoleranzen.split(',').map((item) => item.trim()) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.