class ApplicationInviteServiceImpl extends InviteService<ApplicationInvite> implements ApplicationInviteService {

    @Override
    @Transactional
    public ServiceResult<Void> createApplicationInvites(InviteOrganisationResource inviteOrganisationResource, Optional<Long> applicationId) {
        String errorField = applicationId.isPresent() ? EDIT_EMAIL_FIELD : NEW_EMAIL_FIELD;
        return validateInviteOrganisationResource(inviteOrganisationResource).andOnSuccess(() ->
                validateUniqueEmails(inviteOrganisationResource.getInviteResources(), errorField)).andOnSuccess(() ->
                findOrAssembleInviteOrganisationFromResource(inviteOrganisationResource, applicationId).andOnSuccess(inviteOrganisation -> {
                    List<ApplicationInvite> invites = saveInviteOrganisationWithInvites(inviteOrganisation, inviteOrganisationResource.getInviteResources());
                    return applicationInviteNotificationService.inviteCollaborators(invites);
                }));
    }

}