Make all fields optional on entities (#121)

* Make all fields optional on entities

* Rewrite tests

* Add test on TableHeader Cancel button
This commit is contained in:
Charles Bochet
2023-05-17 14:50:49 +02:00
committed by GitHub
parent 2facb383a2
commit bc49815ff0
31 changed files with 541 additions and 419 deletions

View File

@ -0,0 +1,84 @@
import {
mapToGqlCompany,
mapToCompany,
Company,
GraphqlMutationCompany,
GraphqlQueryCompany,
} from '../company.interface';
describe('Company mappers', () => {
it('should map GraphQl Company to Company', () => {
const now = new Date();
now.setMilliseconds(0);
const graphQLCompany = {
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6b',
name: 'ACME',
domain_name: 'exmaple.com',
created_at: now.toUTCString(),
employees: '10',
address: '1 Infinite Loop, 95014 Cupertino, California, USA',
account_owner: {
id: '7af20dea-0412-4c4c-8b13-d6f0e6e09e87',
email: 'john@example.com',
display_name: 'John Doe',
__typename: 'User',
},
pipes: [
{
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6c',
name: 'Pipe 1',
icon: '!',
__typename: 'Pipe',
},
],
__typename: 'companies',
} satisfies GraphqlQueryCompany;
const company = mapToCompany(graphQLCompany);
expect(company).toStrictEqual({
id: graphQLCompany.id,
name: graphQLCompany.name,
domainName: graphQLCompany.domain_name,
creationDate: new Date(now.toUTCString()),
employees: graphQLCompany.employees,
address: graphQLCompany.address,
accountOwner: {
id: '7af20dea-0412-4c4c-8b13-d6f0e6e09e87',
email: 'john@example.com',
displayName: 'John Doe',
workspaceMember: undefined,
},
pipes: [],
} satisfies Company);
});
it('should map Company to GraphQLCompany', () => {
const now = new Date();
now.setMilliseconds(0);
const company = {
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6b',
name: 'ACME',
domainName: 'example.com',
employees: '10',
address: '1 Infinite Loop, 95014 Cupertino, California, USA',
pipes: [],
accountOwner: {
id: '522d4ec4-c46b-4360-a0a7-df8df170be81',
email: 'john@example.com',
displayName: 'John Doe',
},
creationDate: now,
};
const graphQLCompany = mapToGqlCompany(company);
expect(graphQLCompany).toStrictEqual({
id: company.id,
name: company.name,
domain_name: company.domainName,
created_at: now.toUTCString(),
employees: company.employees,
address: company.address,
account_owner_id: '522d4ec4-c46b-4360-a0a7-df8df170be81',
__typename: 'companies',
} satisfies GraphqlMutationCompany);
});
});

View File

@ -0,0 +1,80 @@
import {
mapToGqlPerson,
mapToPerson,
Person,
GraphqlMutationPerson,
GraphqlQueryPerson,
} from '../person.interface';
describe('Person mappers', () => {
it('should map GraphqlPerson to Person', () => {
const now = new Date();
now.setMilliseconds(0);
const graphQLPerson = {
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6b',
firstname: 'John',
lastname: 'Doe',
created_at: now.toUTCString(),
email: 'john.doe@gmail.com',
phone: '+1 (555) 123-4567',
city: 'Paris',
company: {
id: '7af20dea-0412-4c4c-8b13-d6f0e6e09e87',
name: 'John Doe',
__typename: 'Company',
},
__typename: 'people',
} satisfies GraphqlQueryPerson;
const person = mapToPerson(graphQLPerson);
expect(person).toStrictEqual({
id: graphQLPerson.id,
firstname: graphQLPerson.firstname,
lastname: graphQLPerson.lastname,
creationDate: new Date(now.toUTCString()),
email: graphQLPerson.email,
city: graphQLPerson.city,
phone: graphQLPerson.phone,
company: {
id: '7af20dea-0412-4c4c-8b13-d6f0e6e09e87',
accountOwner: undefined,
address: undefined,
creationDate: undefined,
domainName: undefined,
employees: undefined,
name: 'John Doe',
pipes: [],
},
} satisfies Person);
});
it('should map Person to GraphQlPerson', () => {
const now = new Date();
now.setMilliseconds(0);
const person = {
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6b',
firstname: 'John',
lastname: 'Doe',
creationDate: new Date(now.toUTCString()),
email: 'john.doe@gmail.com',
phone: '+1 (555) 123-4567',
city: 'Paris',
company: {
id: '7af20dea-0412-4c4c-8b13-d6f0e6e09e87',
},
} satisfies Person;
const graphQLPerson = mapToGqlPerson(person);
expect(graphQLPerson).toStrictEqual({
id: person.id,
firstname: person.firstname,
lastname: person.lastname,
created_at: now.toUTCString(),
email: person.email,
city: person.city,
phone: person.phone,
company_id: '7af20dea-0412-4c4c-8b13-d6f0e6e09e87',
__typename: 'people',
} satisfies GraphqlMutationPerson);
});
});

View File

@ -0,0 +1,71 @@
import {
mapToGqlUser,
mapToUser,
User,
GraphqlMutationUser,
GraphqlQueryUser,
} from '../user.interface';
describe('User mappers', () => {
it('should map GraphqlUser to User', () => {
const now = new Date();
now.setMilliseconds(0);
const graphQLUser = {
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6b',
display_name: 'John Doe',
email: 'john.doe@gmail.com',
workspace_member: {
id: '7af20dea-0412-4c4c-8b13-d6f0e6e09e88',
workspace: {
id: '7af20dea-0412-4c4c-8b13-d6f0e6e09e89',
display_name: 'John Doe',
__typename: 'workspace',
},
__typename: 'workspace_members',
},
__typename: 'users',
} satisfies GraphqlQueryUser;
const User = mapToUser(graphQLUser);
expect(User).toStrictEqual({
id: graphQLUser.id,
displayName: graphQLUser.display_name,
email: graphQLUser.email,
workspaceMember: {
id: graphQLUser.workspace_member.id,
workspace: {
id: graphQLUser.workspace_member.workspace.id,
displayName: graphQLUser.workspace_member.workspace.display_name,
domainName: undefined,
logo: undefined,
},
},
} satisfies User);
});
it('should map User to GraphQlUser', () => {
const now = new Date();
now.setMilliseconds(0);
const user = {
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6b',
displayName: 'John Doe',
email: 'john.doe@gmail.com',
workspaceMember: {
id: '7af20dea-0412-4c4c-8b13-d6f0e6e09e88',
workspace: {
id: '7af20dea-0412-4c4c-8b13-d6f0e6e09e89',
displayName: 'John Doe',
},
},
} satisfies User;
const graphQLUser = mapToGqlUser(user);
expect(graphQLUser).toStrictEqual({
id: user.id,
display_name: user.displayName,
email: user.email,
workspace_member_id: user.workspaceMember.id,
__typename: 'users',
} satisfies GraphqlMutationUser);
});
});

View File

@ -1,112 +0,0 @@
import { mapGqlCompany, mapCompany } from './company.interface';
describe('mapCompany', () => {
it('should map company', () => {
const now = new Date();
now.setMilliseconds(0);
const company = mapCompany({
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6b',
name: 'ACME',
domain_name: 'exmaple.com',
created_at: now.toUTCString(),
account_owner: {
id: '7af20dea-0412-4c4c-8b13-d6f0e6e09e87',
email: 'john@example.com',
displayName: 'John Doe',
__typename: 'User',
},
employees: 10,
address: '1 Infinite Loop, 95014 Cupertino, California, USA',
__typename: 'Company',
});
expect(company.id).toBe('7dfbc3f7-6e5e-4128-957e-8d86808cdf6b');
expect(company.name).toBe('ACME');
expect(company.domain_name).toBe('exmaple.com');
expect(company.creationDate).toEqual(now);
expect(company.accountOwner?.id).toBe(
'7af20dea-0412-4c4c-8b13-d6f0e6e09e87',
);
expect(company.accountOwner?.email).toBe('john@example.com');
expect(company.accountOwner?.displayName).toBe('John Doe');
expect(company.employees).toBe(10);
expect(company.address).toBe(
'1 Infinite Loop, 95014 Cupertino, California, USA',
);
});
it('should map company with no account owner', () => {
const now = new Date();
now.setMilliseconds(0);
const company = mapCompany({
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6b',
name: 'ACME',
domain_name: 'exmaple.com',
created_at: now.toUTCString(),
employees: 10,
address: '1 Infinite Loop, 95014 Cupertino, California, USA',
__typename: 'Company',
});
expect(company.id).toBe('7dfbc3f7-6e5e-4128-957e-8d86808cdf6b');
expect(company.name).toBe('ACME');
expect(company.domain_name).toBe('exmaple.com');
expect(company.creationDate).toEqual(now);
expect(company.accountOwner).toBeNull();
expect(company.employees).toBe(10);
expect(company.address).toBe(
'1 Infinite Loop, 95014 Cupertino, California, USA',
);
});
it('should map company back', () => {
const now = new Date();
now.setMilliseconds(0);
const company = mapGqlCompany({
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6b',
name: 'ACME',
domain_name: 'exmaple.com',
employees: 10,
address: '1 Infinite Loop, 95014 Cupertino, California, USA',
opportunities: [],
accountOwner: {
id: '522d4ec4-c46b-4360-a0a7-df8df170be81',
email: 'john@example.com',
displayName: 'John Doe',
},
creationDate: now,
});
expect(company.id).toBe('7dfbc3f7-6e5e-4128-957e-8d86808cdf6b');
expect(company.name).toBe('ACME');
expect(company.domain_name).toBe('exmaple.com');
expect(company.created_at).toEqual(now.toUTCString());
expect(company.account_owner_id).toBe(
'522d4ec4-c46b-4360-a0a7-df8df170be81',
);
expect(company.employees).toBe(10);
expect(company.address).toBe(
'1 Infinite Loop, 95014 Cupertino, California, USA',
);
});
it('should map company with no account owner back', () => {
const now = new Date();
now.setMilliseconds(0);
const company = mapGqlCompany({
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6b',
name: 'ACME',
domain_name: 'exmaple.com',
employees: 10,
address: '1 Infinite Loop, 95014 Cupertino, California, USA',
opportunities: [],
creationDate: now,
});
expect(company.id).toBe('7dfbc3f7-6e5e-4128-957e-8d86808cdf6b');
expect(company.name).toBe('ACME');
expect(company.domain_name).toBe('exmaple.com');
expect(company.created_at).toEqual(now.toUTCString());
expect(company.account_owner_id).toBeUndefined();
expect(company.employees).toBe(10);
expect(company.address).toBe(
'1 Infinite Loop, 95014 Cupertino, California, USA',
);
});
});

View File

@ -1,75 +1,72 @@
import { GraphqlQueryUser, User } from './user.interface';
export interface Opportunity {
id: string;
name: string;
icon: string;
}
import { Pipe } from 'stream';
import { GraphqlQueryUser, User, mapToUser } from './user.interface';
import { GraphqlQueryPipe } from './pipe.interface';
export type Company = {
id: string;
name: string;
domain_name: string;
employees: number;
address: string;
opportunities: Opportunity[];
name?: string;
domainName?: string;
employees?: string;
address?: string;
creationDate?: Date;
pipes?: Pipe[];
accountOwner?: User | null;
creationDate: Date;
};
export type GraphqlQueryCompany = {
id: string;
name: string;
domain_name: string;
name?: string;
domain_name?: string;
employees?: string;
address?: string;
created_at?: string;
account_owner?: GraphqlQueryUser | null;
employees: number;
address: string;
created_at: string;
pipes?: GraphqlQueryPipe[] | null;
__typename: string;
};
export type GraphqlMutationCompany = {
id: string;
name: string;
domain_name: string;
name?: string;
domain_name?: string;
employees?: string;
address?: string;
created_at?: string;
account_owner_id?: string;
employees: number;
address: string;
created_at: string;
account_owner?: GraphqlQueryUser | null;
__typename: string;
};
export const mapCompany = (company: GraphqlQueryCompany): Company => ({
export const mapToCompany = (company: GraphqlQueryCompany): Company => ({
id: company.id,
employees: company.employees,
name: company.name,
address: company.address,
domain_name: company.domain_name,
domainName: company.domain_name,
creationDate: company.created_at ? new Date(company.created_at) : undefined,
accountOwner: company.account_owner
? {
id: company.account_owner.id,
email: company.account_owner.email,
displayName: company.account_owner.displayName,
}
: null,
creationDate: new Date(company.created_at),
opportunities: [],
? mapToUser(company.account_owner)
: company.account_owner,
pipes: [],
});
export const mapGqlCompany = (company: Company): GraphqlMutationCompany => ({
export const mapToGqlCompany = (company: Company): GraphqlMutationCompany => ({
id: company.id,
name: company.name,
domain_name: company.domain_name,
created_at: company.creationDate.toUTCString(),
account_owner_id: company.accountOwner?.id,
domain_name: company.domainName,
address: company.address,
employees: company.employees,
id: company.id,
account_owner: company.accountOwner
? {
id: company.accountOwner?.id,
email: company.accountOwner?.email,
displayName: company.accountOwner?.displayName,
__typename: 'users',
}
: null,
created_at: company.creationDate
? company.creationDate.toUTCString()
: undefined,
account_owner_id: company.accountOwner?.id,
__typename: 'companies',
});

View File

@ -1,54 +0,0 @@
import { mapGqlPerson, mapPerson } from './person.interface';
describe('mapPerson', () => {
it('should map person', () => {
const person = mapPerson({
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6b',
firstname: 'John',
lastname: 'Doe',
email: '',
phone: '',
city: '',
created_at: '',
company: {
__typename: '',
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6b',
name: '',
domain_name: '',
employees: 0,
address: '',
created_at: '',
account_owner: null,
},
__typename: '',
});
expect(person.firstname).toBe('John');
});
it('should map person back', () => {
const person = mapGqlPerson({
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6b',
firstname: 'John',
lastname: 'Doe',
email: '',
phone: '',
city: '',
company: {
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6b',
name: 'Test',
domain_name: '',
opportunities: [],
employees: 0,
address: '',
creationDate: new Date(),
},
creationDate: new Date(),
pipe: {
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6c',
name: '',
icon: '',
},
});
expect(person.firstname).toBe('John');
});
});

View File

@ -1,64 +1,77 @@
import { Company, GraphqlQueryCompany, mapCompany } from './company.interface';
import {
Company,
GraphqlQueryCompany,
mapToCompany,
} from './company.interface';
import { Pipe } from './pipe.interface';
export type Person = {
id: string;
firstname: string;
lastname: string;
picture?: string;
email: string;
company: Company | null;
phone: string;
creationDate: Date;
pipe: Pipe | null;
city: string;
firstname?: string;
lastname?: string;
picture?: string | null;
email?: string;
phone?: string;
city?: string;
creationDate?: Date;
company?: Company | null;
pipes?: Pipe[] | null;
};
export type GraphqlQueryPerson = {
city: string;
company: GraphqlQueryCompany | null;
created_at: string;
email: string;
firstname: string;
id: string;
lastname: string;
phone: string;
firstname?: string;
lastname?: string;
city?: string;
email?: string;
phone?: string;
created_at?: string;
company?: GraphqlQueryCompany | null;
__typename: string;
};
export type GraphqlMutationPerson = {
city: string;
company_id?: string;
created_at: string;
email: string;
firstname: string;
id: string;
lastname: string;
phone: string;
firstname?: string;
lastname?: string;
email?: string;
phone?: string;
city?: string;
created_at?: string;
company_id?: string;
__typename: string;
};
export const mapPerson = (person: GraphqlQueryPerson): Person => ({
export const mapToPerson = (person: GraphqlQueryPerson): Person => ({
id: person.id,
firstname: person.firstname,
lastname: person.lastname,
email: person.email,
phone: person.phone,
city: person.city,
firstname: person.firstname,
lastname: person.lastname,
creationDate: new Date(person.created_at),
pipe: {
name: 'coucou',
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6b',
icon: '💰',
},
company: person.company ? mapCompany(person.company) : null,
creationDate: person.created_at ? new Date(person.created_at) : undefined,
company: person.company ? mapToCompany(person.company) : null,
});
export const mapGqlPerson = (person: Person): GraphqlMutationPerson => ({
...(person as Omit<Person, 'company'>),
export const mapToGqlPerson = (person: Person): GraphqlMutationPerson => ({
id: person.id,
firstname: person.firstname,
lastname: person.lastname,
created_at: person.creationDate.toUTCString(),
email: person.email,
phone: person.phone,
city: person.city,
created_at: person.creationDate
? person.creationDate.toUTCString()
: undefined,
company_id: person.company?.id,
__typename: 'People',
__typename: 'people',
});

View File

@ -1,5 +1,12 @@
export interface Pipe {
id: string;
name: string;
icon: string;
name?: string;
icon?: string | null;
}
export interface GraphqlQueryPipe {
id: string;
name?: string;
icon?: string | null;
__typename: string;
}

File diff suppressed because one or more lines are too long

View File

@ -1,42 +1,45 @@
import {
GraphqlQueryWorkspaceMember,
WorkspaceMember,
} from './workspace.interface';
mapToWorkspaceMember,
} from './workspace_member.interface';
export interface User {
id: string;
email?: string;
displayName?: string;
workspaceMember?: WorkspaceMember;
}
export type GraphqlQueryUser = {
id: string;
email: string;
displayName: string;
email?: string;
display_name?: string;
workspace_member?: GraphqlQueryWorkspaceMember;
__typename: string;
};
export interface User {
export type GraphqlMutationUser = {
id: string;
email: string;
displayName: string;
workspace_member?: WorkspaceMember;
}
export type PartialUser = Partial<User> &
Pick<User, 'id' | 'displayName' | 'email'>;
export const mapUser = (user: GraphqlQueryUser): User => {
const mappedUser = {
id: user.id,
email: user.email,
displayName: user.displayName,
} as User;
if (user.workspace_member) {
mappedUser['workspace_member'] = {
workspace: {
id: user.workspace_member.workspace.id,
displayName: user.workspace_member.workspace.display_name,
domainName: user.workspace_member.workspace.domain_name,
logo: user.workspace_member.workspace.logo,
},
};
}
return mappedUser;
email?: string;
display_name?: string;
workspace_member_id?: string;
__typename: string;
};
export const mapToUser = (user: GraphqlQueryUser): User => ({
id: user.id,
email: user.email,
displayName: user.display_name,
workspaceMember: user.workspace_member
? mapToWorkspaceMember(user.workspace_member)
: user.workspace_member,
});
export const mapToGqlUser = (user: User): GraphqlMutationUser => ({
id: user.id,
email: user.email,
display_name: user.displayName,
workspace_member_id: user.workspaceMember?.id,
__typename: 'users',
});

View File

@ -1,22 +1,41 @@
export interface Workspace {
id: string;
domainName: string;
displayName: string;
logo: string;
}
export interface WorkspaceMember {
workspace: Workspace;
domainName?: string;
displayName?: string;
logo?: string | null;
}
export type GraphqlQueryWorkspace = {
id: string;
display_name: string;
domain_name: string;
logo: string;
display_name?: string;
domain_name?: string;
logo?: string | null;
__typename: string;
};
export type GraphqlQueryWorkspaceMember = {
workspace: GraphqlQueryWorkspace;
export type GraphqlMutationWorkspace = {
id: string;
display_name?: string;
domain_name?: string;
logo?: string | null;
__typename: string;
};
export const mapToWorkspace = (
workspace: GraphqlQueryWorkspace,
): Workspace => ({
id: workspace.id,
domainName: workspace.domain_name,
displayName: workspace.display_name,
logo: workspace.logo,
});
export const mapToGqlWorkspace = (
workspace: Workspace,
): GraphqlMutationWorkspace => ({
id: workspace.id,
domain_name: workspace.domainName,
display_name: workspace.displayName,
logo: workspace.logo,
__typename: 'workspaces',
});

View File

@ -0,0 +1,39 @@
import {
Workspace,
GraphqlQueryWorkspace,
mapToWorkspace,
} from './workspace.interface';
export interface WorkspaceMember {
id: string;
workspace: Workspace;
}
export type GraphqlQueryWorkspaceMember = {
id: string;
workspace: GraphqlQueryWorkspace;
__typename: string;
};
export type GraphqlMutationWorkspaceMember = {
id: string;
workspace_id: string;
__typename: string;
};
export const mapToWorkspaceMember = (
workspaceMember: GraphqlQueryWorkspaceMember,
): WorkspaceMember => ({
id: workspaceMember.id,
workspace: workspaceMember.workspace
? mapToWorkspace(workspaceMember.workspace)
: workspaceMember.workspace,
});
export const mapToGqlWorkspaceMember = (
workspaceMember: WorkspaceMember,
): GraphqlMutationWorkspaceMember => ({
id: workspaceMember.id,
workspace_id: workspaceMember.workspace?.id,
__typename: 'workspace_members',
});