import { Building, Project, User, Property } from '@prisma/client' /** * Really useful to have the good profile of a resident for a project * Without that, we often have resident that have multiple properties * in multiple projects and that fucks the data for the current project... */ type ProjectInputType = Project & { buildingId: string } type UserInputType = { property: { buildingId: string } properties: { buildingId: string }[] projectsTenants: { id: string }[] projectsBoardMember: { id: string }[] } export const formatResidentToOnlyHaveProjectData = ( user: UserInputType, project: ProjectInputType ) => { if (!user) { return null } if (!project || !project?.buildingId) { return user } // Should maybe do this in the graphql query where we only get units of the person based on the building const userPropertiesForCurrentProject = user.properties.filter( (p) => p.buildingId === project.buildingId ) const formattedUser = { ...user, property: user.property && project && user.property.buildingId === project.buildingId ? user.property : null, properties: userPropertiesForCurrentProject, projectsTenants: user.projectsTenants?.filter((p) => p.id === project.id) || [], projectsBoardMember: user.projectsBoardMember?.filter((p) => p.id === project.id) || [], } return formattedUser }