|
| 1 | +import { Test, type TestingModule } from '@nestjs/testing'; |
| 2 | + |
| 3 | +import { ExceptionHandlerService } from 'src/engine/core-modules/exception-handler/exception-handler.service'; |
| 4 | +import { FieldActorSource } from 'src/engine/metadata-modules/field-metadata/composite-types/actor.composite-type'; |
| 5 | +import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager'; |
| 6 | +import { type ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity'; |
| 7 | +import { CreateCompanyAndContactService } from 'src/modules/contact-creation-manager/services/create-company-and-contact.service'; |
| 8 | +import { CreateCompanyService } from 'src/modules/contact-creation-manager/services/create-company.service'; |
| 9 | +import { CreateContactService } from 'src/modules/contact-creation-manager/services/create-contact.service'; |
| 10 | +import { type PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity'; |
| 11 | + |
| 12 | +describe('CreateCompanyAndContactService', () => { |
| 13 | + let service: CreateCompanyAndContactService; |
| 14 | + let createCompaniesService: CreateCompanyService; |
| 15 | + let createContactService: CreateContactService; |
| 16 | + |
| 17 | + const workspaceId = 'workspace-1'; |
| 18 | + |
| 19 | + const mockConnectedAccount = {} as ConnectedAccountWorkspaceEntity; |
| 20 | + |
| 21 | + beforeEach(async () => { |
| 22 | + const mockCreateCompaniesService = { |
| 23 | + createOrRestoreCompanies: jest.fn(), |
| 24 | + }; |
| 25 | + const mockCreateContactService = { |
| 26 | + restorePeople: jest.fn(), |
| 27 | + }; |
| 28 | + |
| 29 | + const module: TestingModule = await Test.createTestingModule({ |
| 30 | + providers: [ |
| 31 | + CreateCompanyAndContactService, |
| 32 | + { |
| 33 | + provide: CreateCompanyService, |
| 34 | + useValue: mockCreateCompaniesService, |
| 35 | + }, |
| 36 | + { |
| 37 | + provide: CreateContactService, |
| 38 | + useValue: mockCreateContactService, |
| 39 | + }, |
| 40 | + { |
| 41 | + provide: TwentyORMGlobalManager, |
| 42 | + useValue: {}, |
| 43 | + }, |
| 44 | + { |
| 45 | + provide: ExceptionHandlerService, |
| 46 | + useValue: {}, |
| 47 | + }, |
| 48 | + ], |
| 49 | + }).compile(); |
| 50 | + |
| 51 | + service = module.get<CreateCompanyAndContactService>( |
| 52 | + CreateCompanyAndContactService, |
| 53 | + ); |
| 54 | + createContactService = |
| 55 | + module.get<CreateContactService>(CreateContactService); |
| 56 | + createCompaniesService = |
| 57 | + module.get<CreateCompanyService>(CreateCompanyService); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('restorePeopleAndRestoreOrCreateCompanies - method', () => { |
| 61 | + it('should restore the soft deleted contact - matching primary email', async () => { |
| 62 | + await service.restorePeopleAndRestoreOrCreateCompanies( |
| 63 | + [ |
| 64 | + { |
| 65 | + id: 'contact-1', |
| 66 | + emails: { |
| 67 | + primaryEmail: '[email protected]', |
| 68 | + additionalEmails: [], |
| 69 | + }, |
| 70 | + deletedAt: new Date(), |
| 71 | + } as unknown as PersonWorkspaceEntity, |
| 72 | + ], |
| 73 | + [ |
| 74 | + { |
| 75 | + |
| 76 | + displayName: 'Contact 1', |
| 77 | + }, |
| 78 | + ], |
| 79 | + workspaceId, |
| 80 | + FieldActorSource.CALENDAR, |
| 81 | + mockConnectedAccount, |
| 82 | + ); |
| 83 | + expect(createContactService.restorePeople).toHaveBeenCalledWith( |
| 84 | + [ |
| 85 | + { |
| 86 | + id: 'contact-1', |
| 87 | + companyId: undefined, |
| 88 | + }, |
| 89 | + ], |
| 90 | + workspaceId, |
| 91 | + ); |
| 92 | + expect( |
| 93 | + createCompaniesService.createOrRestoreCompanies, |
| 94 | + ).toHaveBeenCalled(); |
| 95 | + }); |
| 96 | + it('should restore the soft deleted contact - matching primary email', async () => { |
| 97 | + await service.restorePeopleAndRestoreOrCreateCompanies( |
| 98 | + [ |
| 99 | + { |
| 100 | + id: 'contact-1', |
| 101 | + emails: { |
| 102 | + primaryEmail: '[email protected]', |
| 103 | + additionalEmails: [ |
| 104 | + |
| 105 | + ], |
| 106 | + }, |
| 107 | + deletedAt: new Date(), |
| 108 | + } as unknown as PersonWorkspaceEntity, |
| 109 | + ], |
| 110 | + [ |
| 111 | + { |
| 112 | + |
| 113 | + displayName: 'Contact 1', |
| 114 | + }, |
| 115 | + ], |
| 116 | + workspaceId, |
| 117 | + FieldActorSource.CALENDAR, |
| 118 | + mockConnectedAccount, |
| 119 | + ); |
| 120 | + expect(createContactService.restorePeople).toHaveBeenCalledWith( |
| 121 | + [ |
| 122 | + { |
| 123 | + id: 'contact-1', |
| 124 | + companyId: undefined, |
| 125 | + }, |
| 126 | + ], |
| 127 | + workspaceId, |
| 128 | + ); |
| 129 | + expect( |
| 130 | + createCompaniesService.createOrRestoreCompanies, |
| 131 | + ).toHaveBeenCalled(); |
| 132 | + }); |
| 133 | + it('should not restore any contact', async () => { |
| 134 | + await service.restorePeopleAndRestoreOrCreateCompanies( |
| 135 | + [ |
| 136 | + { |
| 137 | + id: 'contact-1', |
| 138 | + emails: { |
| 139 | + primaryEmail: '[email protected]', |
| 140 | + additionalEmails: [], |
| 141 | + }, |
| 142 | + deletedAt: new Date(), |
| 143 | + } as unknown as PersonWorkspaceEntity, |
| 144 | + ], |
| 145 | + [ |
| 146 | + { |
| 147 | + |
| 148 | + displayName: 'Contact 1', |
| 149 | + }, |
| 150 | + ], |
| 151 | + workspaceId, |
| 152 | + FieldActorSource.CALENDAR, |
| 153 | + mockConnectedAccount, |
| 154 | + ); |
| 155 | + expect(createContactService.restorePeople).not.toHaveBeenCalled(); |
| 156 | + expect( |
| 157 | + createCompaniesService.createOrRestoreCompanies, |
| 158 | + ).not.toHaveBeenCalled(); |
| 159 | + }); |
| 160 | + }); |
| 161 | +}); |
0 commit comments