Skip to content

Commit

Permalink
fix: fix seeding script
Browse files Browse the repository at this point in the history
* chore: adjust seeding script to have resolved translations for roles

* chore: remove console log

* docs: write documentation for seeding database

* docs: add missing subpages under dev guide in overview

* docs: add explanation how to execute seeding script

* chore: reduce num entities to 15
  • Loading branch information
qhantom authored Dec 10, 2023
1 parent 6f07393 commit 5887bea
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 42 deletions.
6 changes: 1 addition & 5 deletions packages/app/public/locales/de/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,10 @@
"EXECUTIVE": "Führungskraft",
"MANAGER": "Manager",
"CONSULTANT": "Berater",
"ADMINISTRATOR": "Administrator",
"LIAISON": "Verbindung",
"OFFICER": "Beamter",
"DEVELOPER": "Entwickler",
"AGENT": "Agent",
"SUPERVISOR": "Aufseher",
"ANALYST": "Analyst",
"DESIGNER": "Designer"
"AGENT": "Agent"
}
},
"rolesView": {
Expand Down
6 changes: 1 addition & 5 deletions packages/app/public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,10 @@
"EXECUTIVE": "Executive",
"MANAGER": "Manager",
"CONSULTANT": "Consultant",
"ADMINISTRATOR": "Administrator",
"LIAISON": "Liaison",
"OFFICER": "Officer",
"DEVELOPER": "Developer",
"AGENT": "Agent",
"SUPERVISOR": "Supervisor",
"ANALYST": "Analyst",
"DESIGNER": "Designer"
"AGENT": "Agent"
}
},
"rolesView": {
Expand Down
141 changes: 127 additions & 14 deletions packages/docs/pages/devguide.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,69 @@ Welcome developer! This guide will help you get started developing with Essenciu
href="/devguide/codestyle-and-linting"
/>

<Card
icon={
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth={1.5}
stroke="currentColor"
className="w-6 h-6"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M9.813 15.904L9 18.75l-.813-2.846a4.5 4.5 0 00-3.09-3.09L2.25 12l2.846-.813a4.5 4.5 0 003.09-3.09L9 5.25l.813 2.846a4.5 4.5 0 003.09 3.09L15.75 12l-2.846.813a4.5 4.5 0 00-3.09 3.09zM18.259 8.715L18 9.75l-.259-1.035a3.375 3.375 0 00-2.455-2.456L14.25 6l1.036-.259a3.375 3.375 0 002.455-2.456L18 2.25l.259 1.035a3.375 3.375 0 002.456 2.456L21.75 6l-1.035.259a3.375 3.375 0 00-2.456 2.456zM16.894 20.567L16.5 21.75l-.394-1.183a2.25 2.25 0 00-1.423-1.423L13.5 18.75l1.183-.394a2.25 2.25 0 001.423-1.423l.394-1.183.394 1.183a2.25 2.25 0 001.423 1.423l1.183.394-1.183.394a2.25 2.25 0 00-1.423 1.423z"
/>
</svg>
}
title="Package Responsibilities"
href="/devguide/packageResponsibilities"
/>

<Card
icon={
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth={1.5}
stroke="currentColor"
className="w-6 h-6"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M9.813 15.904L9 18.75l-.813-2.846a4.5 4.5 0 00-3.09-3.09L2.25 12l2.846-.813a4.5 4.5 0 003.09-3.09L9 5.25l.813 2.846a4.5 4.5 0 003.09 3.09L15.75 12l-2.846.813a4.5 4.5 0 00-3.09 3.09zM18.259 8.715L18 9.75l-.259-1.035a3.375 3.375 0 00-2.455-2.456L14.25 6l1.036-.259a3.375 3.375 0 002.455-2.456L18 2.25l.259 1.035a3.375 3.375 0 002.456 2.456L21.75 6l-1.035.259a3.375 3.375 0 00-2.456 2.456zM16.894 20.567L16.5 21.75l-.394-1.183a2.25 2.25 0 00-1.423-1.423L13.5 18.75l1.183-.394a2.25 2.25 0 001.423-1.423l.394-1.183.394 1.183a2.25 2.25 0 001.423 1.423l1.183.394-1.183.394a2.25 2.25 0 00-1.423 1.423z"
/>
</svg>
}
title="Dependencies"
href="/devguide/dependencies"
/>

<Card
icon={
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth={1.5}
stroke="currentColor"
className="w-6 h-6"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M9.813 15.904L9 18.75l-.813-2.846a4.5 4.5 0 00-3.09-3.09L2.25 12l2.846-.813a4.5 4.5 0 003.09-3.09L9 5.25l.813 2.846a4.5 4.5 0 003.09 3.09L15.75 12l-2.846.813a4.5 4.5 0 00-3.09 3.09zM18.259 8.715L18 9.75l-.259-1.035a3.375 3.375 0 00-2.455-2.456L14.25 6l1.036-.259a3.375 3.375 0 002.455-2.456L18 2.25l.259 1.035a3.375 3.375 0 002.456 2.456L21.75 6l-1.035.259a3.375 3.375 0 00-2.456 2.456zM16.894 20.567L16.5 21.75l-.394-1.183a2.25 2.25 0 00-1.423-1.423L13.5 18.75l1.183-.394a2.25 2.25 0 001.423-1.423l.394-1.183.394 1.183a2.25 2.25 0 001.423 1.423l1.183.394-1.183.394a2.25 2.25 0 00-1.423 1.423z"
/>
</svg>
}
title="New Page"
href="/devguide/new-page"
/>

<Card
icon={
<svg
Expand Down Expand Up @@ -75,7 +138,7 @@ Welcome developer! This guide will help you get started developing with Essenciu
</svg>
}
title="Pull requests"
href="/devguide/pull-request"
href="/devguide/pull-requests"
/>

<Card
Expand All @@ -99,17 +162,67 @@ Welcome developer! This guide will help you get started developing with Essenciu
href="/devguide/commits"
/>

<Card
icon={
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="w-6 h-6">
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M2.25 15.75l5.159-5.159a2.25 2.25 0 013.182 0l5.159 5.159m-1.5-1.5l1.409-1.409a2.25 2.25 0 013.182 0l2.909 2.909m-18 3.75h16.5a1.5 1.5 0 001.5-1.5V6a1.5 1.5 0 00-1.5-1.5H3.75A1.5 1.5 0 002.25 6v12a1.5 1.5 0 001.5 1.5zm10.5-11.25h.008v.008h-.008V8.25zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0z"
/>
</svg>
}
title="Releases"
href="/devguide/releases"
/>
<Card
icon={
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth={1.5}
stroke="currentColor"
className="w-6 h-6"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M2.25 15.75l5.159-5.159a2.25 2.25 0 013.182 0l5.159 5.159m-1.5-1.5l1.409-1.409a2.25 2.25 0 013.182 0l2.909 2.909m-18 3.75h16.5a1.5 1.5 0 001.5-1.5V6a1.5 1.5 0 00-1.5-1.5H3.75A1.5 1.5 0 002.25 6v12a1.5 1.5 0 001.5 1.5zm10.5-11.25h.008v.008h-.008V8.25zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0z"
/>
</svg>
}
title="Releases"
href="/devguide/releases"
/>

<Card
icon={
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth={1.5}
stroke="currentColor"
className="w-6 h-6"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M2.25 15.75l5.159-5.159a2.25 2.25 0 013.182 0l5.159 5.159m-1.5-1.5l1.409-1.409a2.25 2.25 0 013.182 0l2.909 2.909m-18 3.75h16.5a1.5 1.5 0 001.5-1.5V6a1.5 1.5 0 00-1.5-1.5H3.75A1.5 1.5 0 002.25 6v12a1.5 1.5 0 001.5 1.5zm10.5-11.25h.008v.008h-.008V8.25zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0z"
/>
</svg>
}
title="Types"
href="/devguide/types"
/>

<Card
icon={
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth={1.5}
stroke="currentColor"
className="w-6 h-6"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M2.25 15.75l5.159-5.159a2.25 2.25 0 013.182 0l5.159 5.159m-1.5-1.5l1.409-1.409a2.25 2.25 0 013.182 0l2.909 2.909m-18 3.75h16.5a1.5 1.5 0 001.5-1.5V6a1.5 1.5 0 00-1.5-1.5H3.75A1.5 1.5 0 002.25 6v12a1.5 1.5 0 001.5 1.5zm10.5-11.25h.008v.008h-.008V8.25zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0z"
/>
</svg>
}
title="Seeding Database"
href="/devguide/seeding-database"
/>

</Cards>
3 changes: 2 additions & 1 deletion packages/docs/pages/devguide/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"pull-requests": "Pull Requests",
"commits": "Commits",
"releases": "Releases",
"types": "Types"
"types": "Types",
"seeding-database": "Seeding Database"
}
17 changes: 17 additions & 0 deletions packages/docs/pages/devguide/seeding-database.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Seeding database

You can find a seeding script (`seed-db.ts`) on the root level of the monorepo to seed the Postgres database that is used by the Essencium backend. The seeding script only works out of the box with the Essencium Backend. When using a different backend, it must be adjusted accordingly.
You also need to pass the admin username and password into the `CONFIG` constant (in `seed-db.ts`) to authenticate. To run the script, just execute `pnpm seed-db` on root level.

The script creates by default 20 new users and roles and deletes existing roles and users beforehand except the admin user. The number of users and roles, the script should create, can be adjusted by changing the value in `CONFIG.NUM_ENTITIES`.

The output should look like this:

```
🌱 Started seeding database
➡ Setting up axios instance
➡ Authenticating
➡ Deleting existing users and roles
➡ Creating new roles and users
✅ Seeding successful
```
51 changes: 34 additions & 17 deletions scripts/seed-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,47 @@
/* eslint-disable no-console */

import { faker } from '@faker-js/faker'
import {
RightOutput,
RoleOutput,
UserOutput,
} from '@frachtwerk/essencium-types'
import { RoleOutput, UserOutput } from '@frachtwerk/essencium-types'
import axios, { AxiosError, AxiosResponse } from 'axios'

const CONFIG = {
NUM_ENTITIES: 50,
NUM_ENTITIES: 15,
AVAILABLE_LOCALES: ['en', 'de'],
ADMIN_USERNAME: '',
ADMIN_PASSWORD: '',
BASE_URL: 'https://backend.staging.essencium.dev',
ROLES_TO_KEEP: ['ADMIN', 'USER'],
}

const ROLES_TO_CREATE = [
'HR',
'ORCHESTRATOR',
'REPRESENTATIVE',
'FACILITATOR',
'TECHNICIAN',
'COORDINATOR',
'ARCHITECT',
'PLANNER',
'STRATEGIST',
'PRODUCER',
'DIRECTOR',
'SPECIALIST',
'ENGINEER',
'EXECUTIVE',
'MANAGER',
'CONSULTANT',
'LIAISON',
'OFFICER',
'DEVELOPER',
'AGENT',
] as const

async function seedDatabase(): Promise<void> {
console.log('🌱 Started seeding database')

console.log(`➡ Creating ${CONFIG.NUM_ENTITIES} users and roles`)
if (!CONFIG.ADMIN_USERNAME || !CONFIG.ADMIN_PASSWORD) {
throw new Error('❌ Please configure username and password')
}

let accessToken = ''

Expand Down Expand Up @@ -90,28 +111,24 @@ async function seedDatabase(): Promise<void> {
roles.content
.filter((role: RoleOutput) => !CONFIG.ROLES_TO_KEEP.includes(role.name))
.map((role: RoleOutput) => {
return axiosInstance.delete(`/v1/roles/${role.id}`)
return axiosInstance.delete(`/v1/roles/${role.name}`)
}),
)

// Create new roles and users
console.log('➡ Creating new roles and users')

const { data: rights } = await axiosInstance.get('/v1/rights')

const createdRolesResponse = await Promise.all(
Array(CONFIG.NUM_ENTITIES)
.fill(null)
.map(() => {
.map((_, index) => {
return axiosInstance
.post<RoleOutput>('/v1/roles', {
description: faker.name.jobTitle(),
editable: faker.datatype.boolean(),
name: faker.name.jobType().toUpperCase(),
protected: faker.datatype.boolean(),
rights: faker.helpers.arrayElements(
rights.content.map((right: RightOutput) => right.id),
),
name: ROLES_TO_CREATE[index],
protected: false,
rights: [],
})
.catch((error: AxiosError) => {
if (error?.response?.status !== 409) {
Expand Down Expand Up @@ -142,7 +159,7 @@ async function seedDatabase(): Promise<void> {
locale: faker.helpers.arrayElement(CONFIG.AVAILABLE_LOCALES),
mobile: faker.phone.number(),
role: faker.helpers.arrayElement(
createdRoles.map((role: RoleOutput) => role.id),
createdRoles.map((role: RoleOutput) => role.name),
),
})
.catch((error: AxiosError) => {
Expand Down

2 comments on commit 5887bea

@vercel
Copy link

@vercel vercel bot commented on 5887bea Dec 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 5887bea Dec 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.