@@ -16,7 +16,10 @@ import {
1616} from '@crowd/common'
1717import { CommonIntegrationService , getGithubInstallationToken } from '@crowd/common_services'
1818import { ICreateInsightsProject } from '@crowd/data-access-layer/src/collections'
19- import { upsertMailingLists } from '@crowd/data-access-layer/src/mailinglist'
19+ import {
20+ findMailingListsOwnedByOtherIntegration ,
21+ upsertMailingLists ,
22+ } from '@crowd/data-access-layer/src/mailinglist'
2023import {
2124 ICreateRepository ,
2225 IRepository ,
@@ -1452,6 +1455,8 @@ export default class IntegrationService {
14521455 let integration
14531456
14541457 try {
1458+ const qx = SequelizeRepository . getQueryExecutor ( { ...( options || this . options ) , transaction } )
1459+
14551460 integration = await this . createOrUpdate (
14561461 {
14571462 platform : PlatformType . MAILINGLIST ,
@@ -1462,8 +1467,16 @@ export default class IntegrationService {
14621467 options ,
14631468 )
14641469
1470+ const conflicts = await findMailingListsOwnedByOtherIntegration ( qx , integration . id , lists )
1471+ if ( conflicts . length > 0 ) {
1472+ throw new Error400 (
1473+ this . options . language ,
1474+ 'errors.mailingList.alreadyConnected' ,
1475+ conflicts . join ( ', ' ) ,
1476+ )
1477+ }
1478+
14651479 const currentSegmentId = ( options || this . options ) . currentSegments [ 0 ] . id
1466- const qx = SequelizeRepository . getQueryExecutor ( { ...( options || this . options ) , transaction } )
14671480 await upsertMailingLists ( qx , currentSegmentId , integration . id , lists )
14681481
14691482 if ( ! existingTransaction ) {
0 commit comments