Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TYPE "Gender" ADD VALUE IF NOT EXISTS 'DIVERS';
1 change: 1 addition & 0 deletions apps/api/prisma/schema/Person.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
enum Gender {
MALE
FEMALE
DIVERS
UNSPECIFIED
}

Expand Down
17 changes: 16 additions & 1 deletion apps/api/src/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,25 @@ const prisma = new PrismaClient({
},
})

let queryLoggingEnabled = config.loggingLevel === 'debug'

if (config.loggingLevel === 'debug') {
prisma.$on('query', (e) => {
log.debug(e.query)
if (queryLoggingEnabled) {
log.debug(e.query)
}
})
}

export async function withoutQueryLogging<T>(callback: () => Promise<T>): Promise<T> {
const previousQueryLoggingEnabled = queryLoggingEnabled
queryLoggingEnabled = false

try {
return await callback()
} finally {
queryLoggingEnabled = previousQueryLoggingEnabled
}
}

export default prisma
87 changes: 66 additions & 21 deletions apps/api/src/scripts/createAccount.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,54 @@
import { input, password as passwordInput, select, search } from '@inquirer/prompts'
import { Role } from '@prisma/client'

import prisma from '../prisma.js'
import prisma, { withoutQueryLogging } from '../prisma.js'
import { getAccountCreateData } from '../services/account/schema/account.schema.js'
import logActivity from '../util/activity.js'
import { getEnumOptions, roleMapping } from '../client.js'
import { getEnumOptions, roleMapping, GenderMapping } from '../client.js'

function parseBirthday(value: string): Date | undefined {
if (!/^\d{2}-\d{2}-\d{4}$/.test(value)) {
return undefined
}

const day = Number(value.slice(0, 2))
const month = Number(value.slice(3, 5))
const year = Number(value.slice(6, 10))
const birthday = new Date(Date.UTC(year, month - 1, day))

if (
birthday.getUTCFullYear() !== year ||
birthday.getUTCMonth() !== month - 1 ||
birthday.getUTCDate() !== day
) {
return undefined
}

const today = new Date()
const todayAtMidnight = new Date(Date.UTC(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate()))
if (birthday > todayAtMidnight) {
return undefined
}

return birthday
}

async function createUser() {
const email = await input({ message: 'E-Mail' })
const firstname = await input({ message: 'Vorname' })
const lastname = await input({ message: 'Nachname' })
const password = await passwordInput({ message: 'Passwort' })

const roleId = await select({
message: 'Rolle',
choices: getEnumOptions(roleMapping).map((option) => {
return {
name: option.label,
value: option.value,
}
}),
choices: getEnumOptions(roleMapping).map((option) => ({
name: option.label,
value: option.value,
})),
})

async function selectGliederung(): Promise<string> {
return await search({
const gliederungId = await withoutQueryLogging(() =>
search({
message: 'Deine Gliederung',
source: async (term) => {
const results = await prisma.gliederung.findMany({
Expand All @@ -40,20 +66,39 @@ async function createUser() {
}))
},
})
)
const birthdayInput = await input({
message: 'Geburtsdatum (TT-MM-JJJJ)',
validate: (value) =>
parseBirthday(value) ? true : 'Bitte ein gültiges Datum im Format TT-MM-JJJJ eingeben, das nicht in der Zukunft liegt.',
})
const birthday = parseBirthday(birthdayInput)
if (!birthday) {
throw new Error('Ungültiges Geburtsdatum.')
}

const gliederungId = await selectGliederung()
const gender = await select({
message: 'Geschlecht',
choices: getEnumOptions(GenderMapping).map((option) => ({
name: option.label,
value: option.value,
})),
})

const accountData = await getAccountCreateData({
email: email,
firstname: firstname,
lastname: lastname,
password: password,
roleId: roleId,
email,
firstname,
lastname,
password,
roleId,
isActiv: true,
gliederungId,
adminInGliederungId: roleId === Role.GLIEDERUNG_ADMIN ? gliederungId : undefined,
birthday: new Date(),
gender: 'FEMALE',
adminInGliederungId:
roleId === Role.GLIEDERUNG_ADMIN
? gliederungId
: undefined,
birthday,
gender,
})

const res = await prisma.account.create({
Expand All @@ -69,7 +114,7 @@ async function createUser() {

await logActivity({
type: 'CREATE',
description: `account was created via CLI script`,
description: 'account was created via CLI script',
subjectType: 'account',
subjectId: res.id,
})
Expand All @@ -78,4 +123,4 @@ async function createUser() {
process.exit()
}

await createUser()
await createUser()
3 changes: 2 additions & 1 deletion apps/api/src/types/enums/mappings/Gender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { defineEnumMapping } from '../defineEnumMapping.js'
export const GenderMapping = defineEnumMapping<Gender>({
MALE: { human: 'Männlich' },
FEMALE: { human: 'Weiblich' },
UNSPECIFIED: { human: 'Divers' },
DIVERS: { human: 'Divers' },
UNSPECIFIED: { human: 'Keine Angabe' },
})

export { type Gender }