Show person relation summaries

This commit is contained in:
karilint
2026-05-25 14:41:19 +03:00
parent a78f43bd1a
commit 275f2cf11f
9 changed files with 334 additions and 7 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
import { PersonDetailsType } from '../../../../frontend/src/shared/types'
export const existingPerson: Omit<PersonDetailsType, 'user'> = {
export const existingPerson: Omit<PersonDetailsType, 'user' | 'project_relations' | 'coordinator_relations'> = {
initials: 'AD',
first_name: 'adf',
surname: 'ads',
@@ -31,6 +31,37 @@ describe('Updating person works', () => {
updatedPerson = body
})
it('returns project and coordinator relations for a person', async () => {
const { body, status } = await send<PersonDetailsType>('person/ER', 'GET')
expect(status).toEqual(200)
expect(body.project_relations).toEqual(
expect.arrayContaining([
expect.objectContaining({
pid: 3,
relation: 'Member',
proj_code: 'NOW',
proj_name: 'NOW Database',
}),
])
)
expect(body.coordinator_relations).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: 2,
type: 'Region',
name: 'region 44524b6e',
}),
expect.objectContaining({
id: 7,
type: 'Taxa',
name: 'group bb181839',
details: 'Carnivora / Felidae',
}),
])
)
})
it('Contains correct data and full name is automatically updated', () => {
const { initials, first_name, surname, full_name, email, organization, country } = updatedPerson!
expect(initials).toEqual(existingPerson.initials)
+103 -6
View File
@@ -1,5 +1,10 @@
import { nowDb } from '../utils/db'
import { EditDataType, PersonDetailsType } from '../../../frontend/src/shared/types'
import {
EditDataType,
PersonCoordinatorRelation,
PersonDetailsType,
PersonProjectRelation,
} from '../../../frontend/src/shared/types'
import Prisma from '../../prisma/generated/now_test_client'
import { ValidationObject } from '../../../frontend/src/shared/validators/validator'
import { validatePerson } from '../../../frontend/src/shared/validators/person'
@@ -14,23 +19,115 @@ export const getAllPersons = async () => {
return persons.map(person => ({ ...person, user: person.user_id ? userMap.get(person.user_id) : null }))
}
const formatSpeciesCoordinatorTaxa = (
taxa: Array<{
order_name: string
family_name: string
}>
): string | undefined => {
if (taxa.length === 0) return undefined
return taxa.map(({ order_name, family_name }) => [order_name, family_name].filter(Boolean).join(' / ')).join(', ')
}
export const getPersonRelations = async (
initials: string
): Promise<{
project_relations: PersonProjectRelation[]
coordinator_relations: PersonCoordinatorRelation[]
}> => {
const [contactProjects, memberProjects, regionalCoordinators, speciesCoordinators, stratigraphyCoordinators] =
await Promise.all([
nowDb.now_proj.findMany({
where: { contact: initials },
select: { pid: true, proj_code: true, proj_name: true, proj_status: true },
orderBy: [{ proj_name: 'asc' }, { pid: 'asc' }],
}),
nowDb.now_proj_people.findMany({
where: { initials },
select: {
now_proj: { select: { pid: true, proj_code: true, proj_name: true, proj_status: true } },
},
orderBy: [{ now_proj: { proj_name: 'asc' } }, { pid: 'asc' }],
}),
nowDb.now_reg_coord_people.findMany({
where: { initials },
select: {
now_reg_coord: { select: { reg_coord_id: true, region: true } },
},
orderBy: [{ now_reg_coord: { region: 'asc' } }, { reg_coord_id: 'asc' }],
}),
nowDb.now_sp_coord_people.findMany({
where: { initials },
select: {
now_sp_coord: {
select: {
sp_coord_id: true,
tax_group: true,
now_sp_coord_taxa: {
select: { order_name: true, family_name: true },
orderBy: [{ order_name: 'asc' }, { family_name: 'asc' }],
},
},
},
},
orderBy: [{ now_sp_coord: { tax_group: 'asc' } }, { sp_coord_id: 'asc' }],
}),
nowDb.now_strat_coord_people.findMany({
where: { initials },
select: {
now_strat_coord: { select: { strat_coord_id: true, title: true } },
},
orderBy: [{ now_strat_coord: { title: 'asc' } }, { strat_coord_id: 'asc' }],
}),
])
const project_relations: PersonProjectRelation[] = [
...contactProjects.map(project => ({ ...project, relation: 'Contact' as const })),
...memberProjects.map(({ now_proj }) => ({ ...now_proj, relation: 'Member' as const })),
]
const coordinator_relations: PersonCoordinatorRelation[] = [
...regionalCoordinators.map(({ now_reg_coord }) => ({
id: now_reg_coord.reg_coord_id,
type: 'Region' as const,
name: now_reg_coord.region,
})),
...speciesCoordinators.map(({ now_sp_coord }) => ({
id: now_sp_coord.sp_coord_id,
type: 'Taxa' as const,
name: now_sp_coord.tax_group,
details: formatSpeciesCoordinatorTaxa(now_sp_coord.now_sp_coord_taxa),
})),
...stratigraphyCoordinators.map(({ now_strat_coord }) => ({
id: now_strat_coord.strat_coord_id,
type: 'Stratigraphy' as const,
name: now_strat_coord.title,
})),
]
return { project_relations, coordinator_relations }
}
export const getPersonDetails = async (id: string) => {
// TODO: Check if user has access
const person = await nowDb.com_people.findUnique({
where: { initials: id },
})
const [person, relations] = await Promise.all([
nowDb.com_people.findUnique({
where: { initials: id },
}),
getPersonRelations(id),
])
if (!person) return null
if (!person.user_id) return { ...person, user: null }
if (!person.user_id) return { ...person, user: null, now_user_group: '', ...relations }
const user = await nowDb.com_users.findUnique({
where: { user_id: person.user_id },
select: { user_id: true, user_name: true, last_login: true, now_user_group: true },
})
return { ...person, user, now_user_group: user?.now_user_group }
return { ...person, user, now_user_group: user?.now_user_group, ...relations }
}
export const validateEntirePerson = (editedFields: EditDataType<Prisma.com_people>) => {