1- import lodash , { chunk , uniq } from 'lodash'
1+ import lodash , { uniq } from 'lodash'
22import Sequelize , { QueryTypes } from 'sequelize'
33
44import {
@@ -29,7 +29,7 @@ import {
2929} from '@crowd/data-access-layer'
3030import { findManyLfxMemberships } from '@crowd/data-access-layer/src/lfx_memberships'
3131import { findMaintainerRoles } from '@crowd/data-access-layer/src/maintainers'
32- import { addMemberNoMerge , removeMemberToMerge } from '@crowd/data-access-layer/src/member_merge'
32+ import { insertMemberNoMerge , removeMemberToMerge } from '@crowd/data-access-layer/src/member_merge'
3333import {
3434 deleteMemberSegmentAffiliations ,
3535 findMemberAffiliations ,
@@ -86,7 +86,7 @@ import MemberAttributeSettingsRepository from './memberAttributeSettingsReposito
8686import SegmentRepository from './segmentRepository'
8787import SequelizeRepository from './sequelizeRepository'
8888import TenantRepository from './tenantRepository'
89- import { IMemberMergeSuggestion , mapUsernameToIdentities } from './types/memberTypes'
89+ import { mapUsernameToIdentities } from './types/memberTypes'
9090
9191const { Op } = Sequelize
9292
@@ -581,72 +581,6 @@ class MemberRepository {
581581 }
582582 }
583583
584- static async addToMerge (
585- suggestions : IMemberMergeSuggestion [ ] ,
586- options : IRepositoryOptions ,
587- ) : Promise < void > {
588- const transaction = SequelizeRepository . getTransaction ( options )
589- const seq = SequelizeRepository . getSequelize ( options )
590-
591- // Remove possible duplicates
592- suggestions = lodash . uniqWith ( suggestions , ( a , b ) =>
593- lodash . isEqual ( lodash . sortBy ( a . members ) , lodash . sortBy ( b . members ) ) ,
594- )
595-
596- // Process suggestions in chunks of 100 or less
597- const suggestionChunks = chunk ( suggestions , 100 )
598-
599- const insertValues = (
600- memberId : string ,
601- toMergeId : string ,
602- similarity : number | null ,
603- index : number ,
604- ) => {
605- const idPlaceholder = ( key : string ) => `${ key } ${ index } `
606- return {
607- query : `(:${ idPlaceholder ( 'memberId' ) } , :${ idPlaceholder ( 'toMergeId' ) } , :${ idPlaceholder (
608- 'similarity' ,
609- ) } , NOW(), NOW())`,
610- replacements : {
611- [ idPlaceholder ( 'memberId' ) ] : memberId ,
612- [ idPlaceholder ( 'toMergeId' ) ] : toMergeId ,
613- [ idPlaceholder ( 'similarity' ) ] : similarity === null ? null : similarity ,
614- } ,
615- }
616- }
617-
618- for ( const suggestionChunk of suggestionChunks ) {
619- const placeholders : string [ ] = [ ]
620- let replacements : Record < string , unknown > = { }
621-
622- suggestionChunk . forEach ( ( suggestion , index ) => {
623- const { query, replacements : chunkReplacements } = insertValues (
624- suggestion . members [ 0 ] ,
625- suggestion . members [ 1 ] ,
626- suggestion . similarity ,
627- index ,
628- )
629- placeholders . push ( query )
630- replacements = { ...replacements , ...chunkReplacements }
631- } )
632-
633- const query = `
634- INSERT INTO "memberToMerge" ("memberId", "toMergeId", "similarity", "createdAt", "updatedAt")
635- VALUES ${ placeholders . join ( ', ' ) } on conflict do nothing;
636- `
637- try {
638- await seq . query ( query , {
639- replacements,
640- type : QueryTypes . INSERT ,
641- transaction,
642- } )
643- } catch ( error ) {
644- options . log . error ( 'error adding members to merge' , error )
645- throw error
646- }
647- }
648- }
649-
650584 static async removeToMerge ( id , toMergeId , options : IRepositoryOptions ) {
651585 const qx = SequelizeRepository . getQueryExecutor ( options )
652586
@@ -656,7 +590,7 @@ class MemberRepository {
656590 static async addNoMerge ( id , toMergeId , options : IRepositoryOptions ) {
657591 const qx = SequelizeRepository . getQueryExecutor ( options )
658592
659- await addMemberNoMerge ( qx , id , toMergeId )
593+ await insertMemberNoMerge ( qx , id , toMergeId )
660594 }
661595
662596 static async memberExists (
0 commit comments