From d5c1bd6365f8ddabfacb86f8e1c9cadfa3a7ebf8 Mon Sep 17 00:00:00 2001 From: Anders Borch Date: Fri, 28 Apr 2023 07:40:40 +0200 Subject: [PATCH] Handle missing account owner (#89) --- .../src/interfaces/company.interface.test.ts | 57 ++++++++++++++++--- front/src/interfaces/company.interface.ts | 33 ++++++----- front/src/pages/companies/companies-table.tsx | 12 +++- 3 files changed, 79 insertions(+), 23 deletions(-) diff --git a/front/src/interfaces/company.interface.test.ts b/front/src/interfaces/company.interface.test.ts index 765f357adc..4f71d4611c 100644 --- a/front/src/interfaces/company.interface.test.ts +++ b/front/src/interfaces/company.interface.test.ts @@ -21,12 +21,33 @@ describe('mapCompany', () => { expect(company.name).toBe('ACME'); expect(company.domain_name).toBe('exmaple.com'); expect(company.creationDate).toEqual(now); - expect(company.accountOwner.id).toBe( + expect(company.accountOwner?.id).toBe( '7af20dea-0412-4c4c-8b13-d6f0e6e09e87', ); - expect(company.accountOwner.email).toBe('john@example.com'); - expect(company.accountOwner.first_name).toBe('John'); - expect(company.accountOwner.last_name).toBe('Doe'); + expect(company.accountOwner?.email).toBe('john@example.com'); + expect(company.accountOwner?.first_name).toBe('John'); + expect(company.accountOwner?.last_name).toBe('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', + }); + 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.employees).toBe(10); expect(company.address).toBe( '1 Infinite Loop, 95014 Cupertino, California, USA', @@ -55,11 +76,33 @@ describe('mapCompany', () => { 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( + expect(company.account_owner?.id).toBe( '522d4ec4-c46b-4360-a0a7-df8df170be81', ); - expect(company.account_owner.email).toBe('john@example.com'); - expect(company.account_owner.displayName).toBe('John Doe'); + expect(company.account_owner?.email).toBe('john@example.com'); + expect(company.account_owner?.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 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.employees).toBe(10); expect(company.address).toBe( '1 Infinite Loop, 95014 Cupertino, California, USA', diff --git a/front/src/interfaces/company.interface.ts b/front/src/interfaces/company.interface.ts index fd3cee50c1..a775f9a410 100644 --- a/front/src/interfaces/company.interface.ts +++ b/front/src/interfaces/company.interface.ts @@ -12,7 +12,7 @@ export interface Company { employees: number; address: string; opportunities: Opportunity[]; - accountOwner: User; + accountOwner?: User; creationDate: Date; } @@ -26,7 +26,7 @@ export type GraphqlQueryCompany = { id: string; name: string; domain_name: string; - account_owner: GraphqlQueryAccountOwner; + account_owner?: GraphqlQueryAccountOwner; employees: number; address: string; created_at: string; @@ -36,12 +36,17 @@ export const mapCompany = (company: GraphqlQueryCompany): Company => ({ ...company, name: company.name, domain_name: company.domain_name, - accountOwner: { - id: company.account_owner.id, - email: company.account_owner.email, - first_name: company.account_owner.displayName.split(' ').shift() || '', - last_name: company.account_owner.displayName.split(' ').slice(1).join(' '), - }, + accountOwner: company.account_owner + ? { + id: company.account_owner.id, + email: company.account_owner.email, + first_name: company.account_owner.displayName.split(' ').shift() || '', + last_name: company.account_owner.displayName + .split(' ') + .slice(1) + .join(' '), + } + : undefined, creationDate: new Date(company.created_at), opportunities: [{ name: 'Sales Pipeline', icon: '' }], }); @@ -51,9 +56,11 @@ export const mapGqlCompany = (company: Company): GraphqlQueryCompany => ({ name: company.name, domain_name: company.domain_name, created_at: company.creationDate.toUTCString(), - account_owner: { - id: company.accountOwner.id, - email: company.accountOwner.email, - displayName: `${company.accountOwner.first_name} ${company.accountOwner.last_name}`, - }, + account_owner: company.accountOwner + ? { + id: company.accountOwner.id, + email: company.accountOwner.email, + displayName: `${company.accountOwner.first_name} ${company.accountOwner.last_name}`, + } + : undefined, }); diff --git a/front/src/pages/companies/companies-table.tsx b/front/src/pages/companies/companies-table.tsx index 69a6857217..c07bf12871 100644 --- a/front/src/pages/companies/companies-table.tsx +++ b/front/src/pages/companies/companies-table.tsx @@ -107,9 +107,15 @@ export const companiesColumns = [ header: () => , cell: (props) => ( - + <> + {props.row.original.accountOwner && ( + + )} + ), }),