-
Notifications
You must be signed in to change notification settings - Fork 2
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
[DEV-1614] Add bulk import of contacts #1273
Merged
Merged
Changes from 17 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
4984a2a
sync user wip
petros-double-test1 33802ef
sync user
petros-double-test1 c432ad4
changeset
petros-double-test1 fd01f78
Merge branch 'main' into dev-2017
MarcoPonchia 5865dc1
Update .changeset/cuddly-cats-retire.md
tommaso1 b1b8180
Update packages/active-campaign-client/src/index.ts
tommaso1 6ec2fc2
Update packages/active-campaign-client/src/index.ts
tommaso1 b102553
Update packages/active-campaign-client/src/helpers/resyncUser.ts
tommaso1 3a7569d
pr comments
petros-double-test1 941f3a7
bulk add contact
petros-double-test1 cdbf4cc
bulk add contact
petros-double-test1 210f144
resync user
petros-double-test1 53f3ea1
changeset
petros-double-test1 49f2320
Update packages/active-campaign-client/src/clients/activeCampaignClie…
tommaso1 29e7407
Update packages/active-campaign-client/src/handlers/resyncUserHandler.ts
tommaso1 2e93e4c
pr changes
petros-double-test1 997e3ad
Merge branch 'dev-1614' of github.com:pagopa/developer-portal into de…
petros-double-test1 e1949e6
Update packages/active-campaign-client/src/helpers/fetchSubscribedWeb…
tommaso1 a082a7b
Update packages/active-campaign-client/.env.example
tommaso1 696fab5
Update packages/active-campaign-client/src/handlers/resyncUserHandler.ts
tommaso1 2874681
pr changes
petros-double-test1 3808be8
Merge remote-tracking branch 'origin/main' into dev-1614
petros-double-test1 f3dd9a4
Merge branch 'main' into dev-1614
MarcoPonchia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"active-campaign-client": minor | ||
--- | ||
|
||
Add resync user handler | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"active-campaign-client": patch | ||
--- | ||
|
||
Add bulk import of contacts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/active-campaign-client/src/__tests__/helpers/bulkAddContactToList.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { bulkAddContactToList } from '../../helpers/bulkAddContactsToLists'; | ||
import { User } from '../../types/user'; | ||
|
||
const user: User = { | ||
username: '466e0280-9061-7007-c3e0-beb6be672f68', | ||
email: `test@example${new Date().getTime()}e.com`, | ||
given_name: 'Giovanni', | ||
family_name: 'Doe', | ||
'custom:mailinglist_accepted': 'true', | ||
'custom:company_type': 'Test Co', | ||
'custom:job_role': 'Developer', | ||
}; | ||
|
||
describe.skip('Active campaign integration contact flow', () => { | ||
it('should bulk add contacts to a list', async () => { | ||
const response = await bulkAddContactToList( | ||
[user], | ||
[[Number(process.env.TEST_AC_LIST_ID)]] | ||
); | ||
expect(response.statusCode).toBe(200); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
packages/active-campaign-client/src/handlers/resyncUserHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { deleteContact } from '../helpers/deleteContact'; | ||
import { getUserFromCognitoByUsername } from '../helpers/getUserFromCognito'; | ||
import { fetchSubscribedWebinarsFromDynamo } from '../helpers/fetchSubscribedWebinarsFromDynamo'; | ||
import { addContact } from '../helpers/addContact'; | ||
import { APIGatewayProxyResult, SQSEvent } from 'aws-lambda'; | ||
import { addContactToList } from '../helpers/manageListSubscription'; | ||
import { queueEventParser } from '../helpers/queueEventParser'; | ||
import { acClient } from '../clients/activeCampaignClient'; | ||
import { bulkAddContactToList } from '../helpers/bulkAddContactsToLists'; | ||
|
||
export async function resyncUserHandler(event: { | ||
readonly Records: SQSEvent['Records']; | ||
}): Promise<APIGatewayProxyResult> { | ||
try { | ||
const queueEvent = queueEventParser(event); | ||
const cognitoId = queueEvent.detail.additionalEventData.sub; | ||
const deletionResult = await deleteContact(cognitoId); | ||
if (deletionResult.statusCode != 200 && deletionResult.statusCode != 404) { | ||
// eslint-disable-next-line functional/no-throw-statements | ||
throw new Error('Error adding contact'); | ||
} | ||
|
||
const user = await getUserFromCognitoByUsername(cognitoId); | ||
|
||
if (!user) { | ||
console.log(`User: ${cognitoId} not present on Cognito, sync done.`); | ||
tommaso1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({ | ||
message: 'User not present on Cognito, sync done.', | ||
}), | ||
}; | ||
} | ||
|
||
const userWebinarsSubscriptions = await fetchSubscribedWebinarsFromDynamo( | ||
cognitoId | ||
); | ||
|
||
const webinarIds = JSON.parse(userWebinarsSubscriptions.body) | ||
.map( | ||
(webinar: { readonly webinarId: { readonly S: string } }) => | ||
webinar?.webinarId?.S | ||
) | ||
.filter(Boolean); | ||
|
||
console.log('Webinar IDs:', webinarIds); // TODO: Remove after testing | ||
|
||
const webinarListIds = await Promise.all( | ||
webinarIds.map(async (webinarId: string) => { | ||
const listId = await acClient.getListIdByName(webinarId); | ||
return listId; | ||
}) | ||
MarcoPonchia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
|
||
const res = await bulkAddContactToList([user], [webinarListIds]); | ||
|
||
|
||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({ message: 'User resynced' }), | ||
}; | ||
} catch (error) { | ||
return { | ||
statusCode: 500, | ||
body: JSON.stringify({ message: error }), | ||
}; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
packages/active-campaign-client/src/helpers/bulkAddContactsToLists.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { APIGatewayProxyResult } from 'aws-lambda'; | ||
import { acClient } from '../clients/activeCampaignClient'; | ||
import { ContactPayload } from '../types/contactPayload'; | ||
import { User } from '../types/user'; | ||
|
||
export async function bulkAddContactToList( | ||
users: readonly User[], | ||
listIds: readonly (readonly number[])[] | ||
): Promise<APIGatewayProxyResult> { | ||
try { | ||
// Transform to AC payload | ||
const acPayload: readonly (ContactPayload & { | ||
readonly listIds: readonly number[]; | ||
})[] = users.map((user, index) => ({ | ||
contact: { | ||
email: user.email, | ||
firstName: user.given_name, | ||
lastName: user.family_name, | ||
phone: `cognito:${user.username}`, | ||
fieldValues: [ | ||
{ | ||
field: '2', | ||
value: user['custom:company_type'], | ||
}, | ||
{ | ||
field: '1', | ||
value: user['custom:job_role'], | ||
}, | ||
{ | ||
field: '3', | ||
value: | ||
user['custom:mailinglist_accepted'] === 'true' ? 'TRUE' : 'FALSE', | ||
}, | ||
], | ||
}, | ||
listIds: listIds[index], | ||
})); | ||
|
||
const response = await acClient.bulkAddContactToList(acPayload); | ||
|
||
return { | ||
statusCode: 200, | ||
body: JSON.stringify(response), | ||
}; | ||
} catch (error) { | ||
console.error('Error:', error); | ||
return { | ||
statusCode: 500, | ||
body: JSON.stringify({ message: 'Internal server error' }), | ||
}; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/active-campaign-client/src/helpers/fetchSubscribedWebinarsFromDynamo.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { DynamoDBClient, QueryCommand } from '@aws-sdk/client-dynamodb'; | ||
import { APIGatewayProxyResult } from 'aws-lambda'; | ||
|
||
export async function fetchSubscribedWebinarsFromDynamo( | ||
username: string | ||
): Promise<APIGatewayProxyResult> { | ||
try { | ||
const dynamoClient = new DynamoDBClient({ region: process.env.AWS_REGION }); | ||
const command = new QueryCommand({ | ||
TableName: process.env.DYNAMO_WEBINARS_TABLE_NAME, | ||
KeyConditionExpression: 'username = :username', | ||
ExpressionAttributeValues: { | ||
':username': { S: username }, | ||
}, | ||
}); | ||
|
||
const response = await dynamoClient.send(command); | ||
console.log('getWebinarSubscriptions', response); | ||
marcobottaro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return { | ||
statusCode: 200, | ||
body: JSON.stringify(response.Items), | ||
}; | ||
} catch (error) { | ||
console.error('Error querying items by username:', error); | ||
tommaso1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return { | ||
statusCode: 500, | ||
body: JSON.stringify({ message: 'Internal server error' }), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
import { SQSEvent } from 'aws-lambda'; | ||
import { sqsQueueHandler } from './handlers/sqsQueueHandler'; | ||
import { resyncUserHandler } from './handlers/resyncUserHandler'; | ||
|
||
export async function sqsQueue(event: { | ||
readonly Records: SQSEvent['Records']; | ||
}) { | ||
return await sqsQueueHandler(event); | ||
} | ||
|
||
export async function resyncQueue(event: { | ||
readonly Records: SQSEvent['Records']; | ||
}) { | ||
return await resyncUserHandler(event); | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/active-campaign-client/src/types/bulkAddContactPayload.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export type BulkAddContactPayload = { | ||
readonly contacts: readonly { | ||
readonly email: string; | ||
readonly first_name: string; | ||
readonly last_name: string; | ||
readonly phone: string | undefined; | ||
readonly customer_acct_name: string; | ||
readonly fields: readonly { readonly id: number; readonly value: string }[]; | ||
readonly subscribe: readonly { readonly listid: number }[]; | ||
}[]; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changeset file is duplicated from previous PR should be deleted