refactor: remove mappers (#326)
* refactor: remove mappers * chore: generate graphql types * lint: remove useless import * Remove preset-react-create-app from storybook addons * test: remove old tests * Upgrade storybook version * Remove sb preset-cra and add sb svgr loader * chore: remove figma image url from storybook --------- Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
@ -1,94 +0,0 @@
|
||||
import {
|
||||
Company,
|
||||
GraphqlMutationCompany,
|
||||
GraphqlQueryCompany,
|
||||
mapToCompany,
|
||||
mapToGqlCompany,
|
||||
} 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',
|
||||
domainName: 'exmaple.com',
|
||||
createdAt: now.toUTCString(),
|
||||
employees: 10,
|
||||
address: '1 Infinite Loop, 95014 Cupertino, California, USA',
|
||||
_commentCount: 1,
|
||||
accountOwner: {
|
||||
id: '7af20dea-0412-4c4c-8b13-d6f0e6e09e87',
|
||||
email: 'john@example.com',
|
||||
displayName: 'John Doe',
|
||||
avatarUrl: 'https://example.com/avatar.png',
|
||||
__typename: 'User',
|
||||
},
|
||||
pipes: [
|
||||
{
|
||||
id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6c',
|
||||
name: 'Pipe 1',
|
||||
icon: '!',
|
||||
__typename: 'Pipe',
|
||||
},
|
||||
],
|
||||
__typename: 'Company',
|
||||
} satisfies GraphqlQueryCompany;
|
||||
|
||||
const company = mapToCompany(graphQLCompany);
|
||||
expect(company).toStrictEqual({
|
||||
__typename: 'Company',
|
||||
id: graphQLCompany.id,
|
||||
name: graphQLCompany.name,
|
||||
domainName: graphQLCompany.domainName,
|
||||
createdAt: new Date(now.toUTCString()),
|
||||
employees: graphQLCompany.employees,
|
||||
address: graphQLCompany.address,
|
||||
_commentCount: 1,
|
||||
accountOwner: {
|
||||
__typename: 'users',
|
||||
id: '7af20dea-0412-4c4c-8b13-d6f0e6e09e87',
|
||||
email: 'john@example.com',
|
||||
avatarUrl: 'https://example.com/avatar.png',
|
||||
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: [],
|
||||
_commentCount: 1,
|
||||
accountOwner: {
|
||||
id: '522d4ec4-c46b-4360-a0a7-df8df170be81',
|
||||
email: 'john@example.com',
|
||||
avatarUrl: 'https://example.com/avatar.png',
|
||||
displayName: 'John Doe',
|
||||
__typename: 'users',
|
||||
},
|
||||
createdAt: now,
|
||||
__typename: 'Company',
|
||||
} satisfies Company;
|
||||
const graphQLCompany = mapToGqlCompany(company);
|
||||
expect(graphQLCompany).toStrictEqual({
|
||||
id: company.id,
|
||||
name: company.name,
|
||||
domainName: company.domainName,
|
||||
createdAt: now.toUTCString(),
|
||||
employees: company.employees,
|
||||
address: company.address,
|
||||
accountOwnerId: '522d4ec4-c46b-4360-a0a7-df8df170be81',
|
||||
__typename: 'Company',
|
||||
} satisfies GraphqlMutationCompany);
|
||||
});
|
||||
});
|
||||
@ -1,84 +1,13 @@
|
||||
import {
|
||||
GraphqlQueryPipeline,
|
||||
Pipeline,
|
||||
} from '../../pipelines/interfaces/pipeline.interface';
|
||||
import {
|
||||
GraphqlQueryUser,
|
||||
mapToUser,
|
||||
User,
|
||||
} from '../../users/interfaces/user.interface';
|
||||
import { Company as GQLCompany } from '../../../generated/graphql';
|
||||
import { DeepPartial } from '../../utils/utils';
|
||||
|
||||
export type Company = {
|
||||
__typename: 'Company';
|
||||
id: string;
|
||||
name?: string;
|
||||
domainName?: string;
|
||||
employees?: number | null;
|
||||
address?: string;
|
||||
export type Company = DeepPartial<GQLCompany> & { id: string };
|
||||
|
||||
createdAt?: Date;
|
||||
export type GraphqlQueryCompany = Company;
|
||||
|
||||
pipes?: Pipeline[];
|
||||
accountOwner?: User | null;
|
||||
export type GraphqlMutationCompany = Company;
|
||||
|
||||
_commentCount?: number;
|
||||
};
|
||||
export const mapToCompany = (company: GraphqlQueryCompany): Company => company;
|
||||
|
||||
export type GraphqlQueryCompany = {
|
||||
id: string;
|
||||
name?: string;
|
||||
domainName?: string;
|
||||
employees?: number | null;
|
||||
address?: string;
|
||||
|
||||
createdAt?: string;
|
||||
|
||||
accountOwner?: GraphqlQueryUser | null;
|
||||
pipes?: GraphqlQueryPipeline[] | null;
|
||||
__typename?: string;
|
||||
|
||||
_commentCount?: number;
|
||||
};
|
||||
|
||||
export type GraphqlMutationCompany = {
|
||||
id: string;
|
||||
name?: string;
|
||||
domainName?: string;
|
||||
employees?: number | null;
|
||||
address?: string;
|
||||
|
||||
createdAt?: string;
|
||||
|
||||
accountOwnerId?: string;
|
||||
__typename?: string;
|
||||
};
|
||||
|
||||
export const mapToCompany = (company: GraphqlQueryCompany): Company => ({
|
||||
__typename: 'Company',
|
||||
id: company.id,
|
||||
employees: company.employees,
|
||||
name: company.name,
|
||||
address: company.address,
|
||||
domainName: company.domainName,
|
||||
createdAt: company.createdAt ? new Date(company.createdAt) : undefined,
|
||||
|
||||
accountOwner: company.accountOwner
|
||||
? mapToUser(company.accountOwner)
|
||||
: company.accountOwner,
|
||||
pipes: [],
|
||||
|
||||
_commentCount: company._commentCount,
|
||||
});
|
||||
|
||||
export const mapToGqlCompany = (company: Company): GraphqlMutationCompany => ({
|
||||
id: company.id,
|
||||
name: company.name,
|
||||
domainName: company.domainName,
|
||||
address: company.address,
|
||||
employees: company.employees,
|
||||
|
||||
createdAt: company.createdAt ? company.createdAt.toUTCString() : undefined,
|
||||
|
||||
accountOwnerId: company.accountOwner?.id,
|
||||
__typename: 'Company',
|
||||
});
|
||||
export const mapToGqlCompany = (company: Company): GraphqlMutationCompany =>
|
||||
company;
|
||||
|
||||
@ -2,6 +2,7 @@ import { FetchResult, gql } from '@apollo/client';
|
||||
|
||||
import { apiClient } from '~/apollo';
|
||||
|
||||
import { UpdateCompanyMutationVariables } from '../../../generated/graphql';
|
||||
import { Company, mapToGqlCompany } from '../interfaces/company.interface';
|
||||
|
||||
export const UPDATE_COMPANY = gql`
|
||||
@ -78,11 +79,11 @@ export const DELETE_COMPANIES = gql`
|
||||
`;
|
||||
|
||||
export async function updateCompany(
|
||||
company: Company,
|
||||
company: UpdateCompanyMutationVariables,
|
||||
): Promise<FetchResult<Company>> {
|
||||
const result = await apiClient.mutate({
|
||||
mutation: UPDATE_COMPANY,
|
||||
variables: mapToGqlCompany(company),
|
||||
variables: company,
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user