class ProjectPartnerInviteServiceImpl extends BaseTransactionalService implements ProjectPartnerInviteService {

    @Override
    @Transactional
    public ServiceResult<Void> acceptInvite(long inviteId, long organisationId) {
        return find(projectPartnerInviteRepository.findById(inviteId), notFoundError(ProjectPartnerInvite.class, inviteId))
                .andOnSuccess(invite ->
                        find(organisation(organisationId))
                                .andOnSuccess((organisation) -> {
                                    Project project = invite.getProject();
                                    invite.getInviteOrganisation().setOrganisation(organisation);

                                    PartnerOrganisation partnerOrganisation = new PartnerOrganisation(project, organisation, false);

                                    if (partnerOrganisationRepository.findOneByProjectIdAndOrganisationId(project.getId(), organisation.getId()) != null) {
                                        return serviceFailure(ORGANISATION_ALREADY_EXISTS_FOR_PROJECT);
                                    }

                                    partnerOrganisation = partnerOrganisationRepository.save(partnerOrganisation);

                                    linkAddressesToOrganisation(organisation, partnerOrganisation);

                                    pendingPartnerProgressRepository.save(new PendingPartnerProgress(partnerOrganisation));

                                    ProjectUser projectUser = new ProjectUser(invite.getUser(), project, ProjectParticipantRole.PROJECT_PARTNER, organisation);
                                    projectUser = projectUserRepository.save(projectUser);
                                    projectPartnerChangeService.updateProjectWhenPartnersChange(project.getId());
                                    projectFinanceService.createProjectFinance(project.getId(),
                                            organisation.getId());



                                    eligibilityWorkflowHandler.projectCreated(partnerOrganisation, projectUser);
                                    viabilityWorkflowHandler.projectCreated(partnerOrganisation, projectUser);

                                    if (project.getApplication().getCompetition().applicantNotRequiredForViabilityChecks(organisation.getOrganisationTypeEnum())) {
                                        viabilityWorkflowHandler.viabilityNotApplicable(partnerOrganisation, null);
                                    }
                                    invite.open();

                                    activityLogService.recordActivityByProjectIdAndOrganisationIdAndAuthorId(project.getId(), organisationId, invite.getSentBy().getId(), ActivityType.ORGANISATION_ADDED);
                                    return serviceSuccess();
                                }));
    }

}