Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MPDX-7987 Merge Contacts #959

Merged
merged 14 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions pages/api/Schema/MergeContacts/mergeContacts.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ extend type Mutation {
"""
Returns the ids of the winner
"""
mergeContacts(input: MergeContactsInput!): ID!
mergeContacts(input: MergeContactsInput!): [ID!]!
}

input MergeContactsInput {
"""
The ids of the contacts to make the losers of the merge
"""
loserContactIds: [ID!]!
input WinnersAndLosers {
winnerId: ID!
loserId: ID!
}

"""
The id of the contact to make the winner of the merge
"""
winnerContactId: ID!
input MergeContactsInput {
winnersAndLosers: [WinnersAndLosers!]!
}
7 changes: 2 additions & 5 deletions pages/api/Schema/MergeContacts/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ const MergeContactsResolvers: Resolvers = {
Mutation: {
mergeContacts: (
_source,
{ input: { loserContactIds, winnerContactId } },
{ input: { winnersAndLosers } },
{ dataSources },
) => {
return dataSources.mpdxRestApi.mergeContacts(
loserContactIds,
winnerContactId,
);
return dataSources.mpdxRestApi.mergeContacts(winnersAndLosers);
},
},
};
Expand Down
15 changes: 9 additions & 6 deletions pages/api/graphql-rest.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ExportFormatEnum,
ExportLabelTypeEnum,
ExportSortEnum,
MergeContactsInput,
} from 'src/graphql/types.generated';
import schema from './Schema';
import { getAccountListAnalytics } from './Schema/AccountListAnalytics/dataHandler';
Expand Down Expand Up @@ -201,21 +202,23 @@ class MpdxRestApi extends RESTDataSource {
return `${process.env.REST_API_URL}contacts/exports${pathAddition}/${data.id}.${format}`;
}

async mergeContacts(loserContactIds: Array<string>, winnerContactId: string) {
async mergeContacts(
winnersAndLosers: MergeContactsInput['winnersAndLosers'],
) {
const response = await this.post('contacts/merges/bulk', {
data: loserContactIds.map((loserId) => ({
data: winnersAndLosers.map((contact) => ({
data: {
type: 'contacts',
attributes: {
loser_id: loserId,
winner_id: winnerContactId,
loser_id: contact.loserId,
winner_id: contact.winnerId,
},
},
})),
});

// Return the id of the winner
return response[0].data.id;
// Return the id of the winners
return response.map((contact) => contact.data.id);
}

async getAccountListAnalytics(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
mutation MassActionsMerge($loserContactIds: [ID!]!, $winnerContactId: ID!) {
mergeContacts(
input: {
loserContactIds: $loserContactIds
winnerContactId: $winnerContactId
}
)
mutation MassActionsMerge($input: MergeContactsInput!) {
mergeContacts(input: $input)
}

query GetContactsForMerging(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,14 @@ describe('MassActionsMergeModal', () => {
.filter(({ operationName }) => operationName === 'MassActionsMerge');
expect(mergeCalls).toHaveLength(1);
expect(mergeCalls[0].variables).toEqual({
loserContactIds: ['contact-2'],
winnerContactId: 'contact-1',
input: {
winnersAndLosers: [
{
loserId: 'contact-2',
winnerId: 'contact-1',
},
],
},
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,21 @@ export const MassActionsMergeModal: React.FC<MassActionsMergeModalProps> = ({
});

const mergeContacts = async () => {
const loserContactIds = ids.filter((id) => id !== primaryContactId);
const winnersAndLosers = ids
.filter((id) => id !== primaryContactId)
.map((id) => {
return { winnerId: primaryContactId, loserId: id };
});
await contactsMerge({
variables: {
loserContactIds,
winnerContactId: primaryContactId,
input: {
winnersAndLosers,
},
},
update: (cache) => {
// Delete the loser contacts and remove dangling references to them
loserContactIds.forEach((id) => {
cache.evict({ id: `Contact:${id}` });
winnersAndLosers.forEach((contact) => {
cache.evict({ id: `Contact:${contact.loserId}` });
});
cache.gc();
},
Expand Down
Loading
Loading