feat: add new ACTOR field type and createdBy standard fields (#6324)

This pull request introduces a new `FieldMetadataType` called `ACTOR`.
The primary objective of this new type is to add an extra column to the
following objects: `person`, `company`, `opportunity`, `note`, `task`,
and all custom objects.

This composite type contains three properties:

- `source`
    ```typescript
    export enum FieldActorSource {
      EMAIL = 'EMAIL',
      CALENDAR = 'CALENDAR',
      API = 'API',
      IMPORT = 'IMPORT',
      MANUAL = 'MANUAL',
    }
    ```
- `workspaceMemberId`
- This property can be `undefined` in some cases and refers to the
member who created the record.
- `name`
- Serves as a fallback if the `workspaceMember` is deleted and is used
for other source types like `API`.

### Functionality

The pre-hook system has been updated to allow real-time argument
updates. When a record is created, a pre-hook can now compute and update
the arguments accordingly. This enhancement enables the `createdBy`
field to be populated with the correct values based on the
`authContext`.

The `authContext` now includes:
- An optional User entity
- An optional ApiKey entity
- The workspace entity

This provides access to the necessary data for the `createdBy` field.

In the GraphQL API, only the `source` can be specified in the
`createdBy` input. This allows the front-end to specify the source when
creating records from a CSV file.

### Front-End Handling

On the front-end, `orderBy` and `filter` are only applied to the name
property of the `ACTOR` composite type. Currently, we are unable to
apply these operations to the workspace member relation. This means that
if a workspace member changes their first name or last name, there may
be a mismatch because the name will differ from the new one. The name
displayed on the screen is based on the workspace member entity when
available.

### Missing Components

Currently, this PR does not include a `createdBy` value for the `MAIL`
and `CALENDAR` sources. These records are created in a job, and at
present, we only have access to the workspaceId within the job. To
address this, we should use a function similar to
`loadServiceWithContext`, which was recently removed from `TwentyORM`.
This function would allow us to pass the `authContext` to the jobs
without disrupting existing jobs.
Another PR will be created to handle these cases.

### Related Issues

Fixes issue #5155.

### Additional Notes

This PR doesn't include the migrations of the current records and views.
Everything works properly when the database is reset but this part is
still missing for now. We'll add that in another PR.

- There is a minor issue: front-end tests are broken since this commit:
[80c0fc7ff1).

---------

Co-authored-by: Lucas Bordeau <bordeau.lucas@gmail.com>
Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
Jérémy M
2024-08-03 15:43:31 +02:00
committed by GitHub
parent 9cf08d912a
commit 6432ad39b9
152 changed files with 24425 additions and 14968 deletions

View File

@ -18,6 +18,9 @@ export const companyPrefillData = async (
'addressAddressCountry',
'employees',
'position',
'createdBySource',
'createdByWorkspaceMemberId',
'createdByName',
])
.orIgnore()
.values([
@ -32,6 +35,9 @@ export const companyPrefillData = async (
addressAddressCountry: 'United States',
employees: 5000,
position: 1,
createdBySource: 'MANUAL',
createdByWorkspaceMemberId: null,
createdByName: 'System',
},
{
name: 'Qonto',
@ -44,6 +50,9 @@ export const companyPrefillData = async (
addressAddressCountry: 'France',
employees: 800,
position: 2,
createdBySource: 'MANUAL',
createdByWorkspaceMemberId: null,
createdByName: 'System',
},
{
name: 'Stripe',
@ -56,6 +65,9 @@ export const companyPrefillData = async (
addressAddressCountry: 'Ireland',
employees: 8000,
position: 3,
createdBySource: 'MANUAL',
createdByWorkspaceMemberId: null,
createdByName: 'System',
},
{
name: 'Figma',
@ -68,6 +80,9 @@ export const companyPrefillData = async (
addressAddressCountry: 'United States',
employees: 800,
position: 4,
createdBySource: 'MANUAL',
createdByWorkspaceMemberId: null,
createdByName: 'System',
},
{
name: 'Notion',
@ -80,6 +95,9 @@ export const companyPrefillData = async (
addressAddressCountry: 'United States',
employees: 400,
position: 5,
createdBySource: 'MANUAL',
createdByWorkspaceMemberId: null,
createdByName: 'System',
},
])
.returning('*')

View File

@ -1,5 +1,6 @@
import { EntityManager } from 'typeorm';
// FixMe: Is this file a duplicate of src/database/typeorm-seeds/workspace/people.ts
export const personPrefillData = async (
entityManager: EntityManager,
schemaName: string,
@ -14,6 +15,9 @@ export const personPrefillData = async (
'email',
'avatarUrl',
'position',
'createdBySource',
'createdByWorkspaceMemberId',
'createdByName',
])
.orIgnore()
.values([
@ -25,6 +29,9 @@ export const personPrefillData = async (
avatarUrl:
'https://twentyhq.github.io/placeholder-images/people/image-3.png',
position: 1,
createdBySource: 'MANUAL',
createdByWorkspaceMemberId: null,
createdByName: 'System',
},
{
nameFirstName: 'Alexandre',
@ -34,6 +41,9 @@ export const personPrefillData = async (
avatarUrl:
'https://twentyhq.github.io/placeholder-images/people/image-89.png',
position: 2,
createdBySource: 'MANUAL',
createdByWorkspaceMemberId: null,
createdByName: 'System',
},
{
nameFirstName: 'Patrick',
@ -43,6 +53,9 @@ export const personPrefillData = async (
avatarUrl:
'https://twentyhq.github.io/placeholder-images/people/image-47.png',
position: 3,
createdBySource: 'MANUAL',
createdByWorkspaceMemberId: null,
createdByName: 'System',
},
{
nameFirstName: 'Dylan',
@ -52,6 +65,9 @@ export const personPrefillData = async (
avatarUrl:
'https://twentyhq.github.io/placeholder-images/people/image-40.png',
position: 4,
createdBySource: 'MANUAL',
createdByWorkspaceMemberId: null,
createdByName: 'System',
},
{
nameFirstName: 'Ivan',
@ -61,6 +77,9 @@ export const personPrefillData = async (
avatarUrl:
'https://twentyhq.github.io/placeholder-images/people/image-68.png',
position: 5,
createdBySource: 'MANUAL',
createdByWorkspaceMemberId: null,
createdByName: 'System',
},
])
.returning('*')

View File

@ -39,7 +39,7 @@ export const companiesAllView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.company].fields[
COMPANY_STANDARD_FIELD_IDS.accountOwner
COMPANY_STANDARD_FIELD_IDS.createdBy
],
position: 2,
isVisible: true,
@ -48,7 +48,7 @@ export const companiesAllView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.company].fields[
BASE_OBJECT_STANDARD_FIELD_IDS.createdAt
COMPANY_STANDARD_FIELD_IDS.accountOwner
],
position: 3,
isVisible: true,
@ -57,7 +57,7 @@ export const companiesAllView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.company].fields[
COMPANY_STANDARD_FIELD_IDS.employees
BASE_OBJECT_STANDARD_FIELD_IDS.createdAt
],
position: 4,
isVisible: true,
@ -66,10 +66,19 @@ export const companiesAllView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.company].fields[
COMPANY_STANDARD_FIELD_IDS.linkedinLink
COMPANY_STANDARD_FIELD_IDS.employees
],
position: 5,
isVisible: true,
size: 150,
},
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.company].fields[
COMPANY_STANDARD_FIELD_IDS.linkedinLink
],
position: 6,
isVisible: true,
size: 170,
},
{
@ -77,7 +86,7 @@ export const companiesAllView = async (
objectMetadataMap[STANDARD_OBJECT_IDS.company].fields[
COMPANY_STANDARD_FIELD_IDS.address
],
position: 6,
position: 7,
isVisible: true,
size: 170,
},

View File

@ -32,7 +32,16 @@ export const notesAllView = async (
objectMetadataMap[STANDARD_OBJECT_IDS.note].fields[
NOTE_STANDARD_FIELD_IDS.body
],
position: 0,
position: 1,
isVisible: true,
size: 150,
},
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.note].fields[
NOTE_STANDARD_FIELD_IDS.createdBy
],
position: 2,
isVisible: true,
size: 150,
},
@ -41,7 +50,7 @@ export const notesAllView = async (
objectMetadataMap[STANDARD_OBJECT_IDS.note].fields[
BASE_OBJECT_STANDARD_FIELD_IDS.createdAt
],
position: 0,
position: 3,
isVisible: true,
size: 150,
},

View File

@ -36,7 +36,7 @@ export const opportunitiesAllView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.opportunity].fields[
OPPORTUNITY_STANDARD_FIELD_IDS.closeDate
OPPORTUNITY_STANDARD_FIELD_IDS.createdBy
],
position: 2,
isVisible: true,
@ -45,12 +45,21 @@ export const opportunitiesAllView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.opportunity].fields[
OPPORTUNITY_STANDARD_FIELD_IDS.company
OPPORTUNITY_STANDARD_FIELD_IDS.closeDate
],
position: 3,
isVisible: true,
size: 150,
},
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.opportunity].fields[
OPPORTUNITY_STANDARD_FIELD_IDS.company
],
position: 4,
isVisible: true,
size: 150,
},
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.opportunity].fields[

View File

@ -39,7 +39,7 @@ export const opportunitiesByStageView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.opportunity].fields[
OPPORTUNITY_STANDARD_FIELD_IDS.closeDate
OPPORTUNITY_STANDARD_FIELD_IDS.createdBy
],
position: 2,
isVisible: true,
@ -48,12 +48,21 @@ export const opportunitiesByStageView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.opportunity].fields[
OPPORTUNITY_STANDARD_FIELD_IDS.company
OPPORTUNITY_STANDARD_FIELD_IDS.closeDate
],
position: 3,
isVisible: true,
size: 150,
},
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.opportunity].fields[
OPPORTUNITY_STANDARD_FIELD_IDS.company
],
position: 4,
isVisible: true,
size: 150,
},
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.opportunity].fields[

View File

@ -39,7 +39,7 @@ export const peopleAllView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.person].fields[
PERSON_STANDARD_FIELD_IDS.company
PERSON_STANDARD_FIELD_IDS.createdBy
],
position: 2,
isVisible: true,
@ -48,7 +48,7 @@ export const peopleAllView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.person].fields[
PERSON_STANDARD_FIELD_IDS.phone
PERSON_STANDARD_FIELD_IDS.company
],
position: 3,
isVisible: true,
@ -57,7 +57,7 @@ export const peopleAllView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.person].fields[
BASE_OBJECT_STANDARD_FIELD_IDS.createdAt
PERSON_STANDARD_FIELD_IDS.phone
],
position: 4,
isVisible: true,
@ -66,7 +66,7 @@ export const peopleAllView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.person].fields[
PERSON_STANDARD_FIELD_IDS.city
BASE_OBJECT_STANDARD_FIELD_IDS.createdAt
],
position: 5,
isVisible: true,
@ -75,7 +75,7 @@ export const peopleAllView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.person].fields[
PERSON_STANDARD_FIELD_IDS.jobTitle
PERSON_STANDARD_FIELD_IDS.city
],
position: 6,
isVisible: true,
@ -84,7 +84,7 @@ export const peopleAllView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.person].fields[
PERSON_STANDARD_FIELD_IDS.linkedinLink
PERSON_STANDARD_FIELD_IDS.jobTitle
],
position: 7,
isVisible: true,
@ -93,12 +93,21 @@ export const peopleAllView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.person].fields[
PERSON_STANDARD_FIELD_IDS.xLink
PERSON_STANDARD_FIELD_IDS.linkedinLink
],
position: 8,
isVisible: true,
size: 150,
},
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.person].fields[
PERSON_STANDARD_FIELD_IDS.xLink
],
position: 9,
isVisible: true,
size: 150,
},
],
};
};

View File

@ -49,7 +49,7 @@ export const tasksAllView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.task].fields[
TASK_STANDARD_FIELD_IDS.dueAt
TASK_STANDARD_FIELD_IDS.createdBy
],
position: 3,
isVisible: true,
@ -58,7 +58,7 @@ export const tasksAllView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.task].fields[
TASK_STANDARD_FIELD_IDS.assignee
TASK_STANDARD_FIELD_IDS.dueAt
],
position: 4,
isVisible: true,
@ -67,7 +67,7 @@ export const tasksAllView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.task].fields[
TASK_STANDARD_FIELD_IDS.body
TASK_STANDARD_FIELD_IDS.assignee
],
position: 5,
isVisible: true,
@ -76,12 +76,21 @@ export const tasksAllView = async (
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.task].fields[
BASE_OBJECT_STANDARD_FIELD_IDS.createdAt
TASK_STANDARD_FIELD_IDS.body
],
position: 6,
isVisible: true,
size: 150,
},
{
fieldMetadataId:
objectMetadataMap[STANDARD_OBJECT_IDS.task].fields[
BASE_OBJECT_STANDARD_FIELD_IDS.createdAt
],
position: 7,
isVisible: true,
size: 150,
},
/*
TODO: Add later, since we don't have real-time it probably doesn't work well?
{