-
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-2017] Resync user on active campaign #1269
Merged
Merged
Changes from all commits
Commits
Show all changes
14 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 2fc6b3d
Update packages/active-campaign-client/src/handlers/resyncUserHandler.ts
tommaso1 dab5b50
Update packages/active-campaign-client/src/handlers/resyncUserHandler.ts
tommaso1 d607a42
pr changes
petros-double-test1 599d42c
pr changes
petros-double-test1 0bbe156
Merge branch 'main' into dev-2017
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 |
82 changes: 82 additions & 0 deletions
82
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,82 @@ | ||
import { deleteContact } from '../helpers/deleteContact'; | ||
import { getUserFromCognitoUsername } from '../helpers/getUserFromCognito'; | ||
import { getSubscribedWebinars } from '../helpers/getSubscribedWebinars'; | ||
import { addContact } from '../helpers/addContact'; | ||
import { APIGatewayProxyResult, SQSEvent } from 'aws-lambda'; | ||
import { addContactToList } from '../helpers/manageListSubscription'; | ||
import { queueEventParser } from '../helpers/queueEventParser'; | ||
|
||
export async function resyncUserHandler(event: { | ||
readonly Records: SQSEvent['Records']; | ||
}): Promise<APIGatewayProxyResult> { | ||
try { | ||
const queueEvent = queueEventParser(event); | ||
const cognitoUsername = queueEvent.detail.additionalEventData.sub; | ||
const deletionResult = await deleteContact(cognitoUsername); | ||
if ( | ||
deletionResult.statusCode !== 200 && | ||
deletionResult.statusCode !== 404 | ||
) { | ||
// eslint-disable-next-line functional/no-throw-statements | ||
throw new Error('Error adding contact'); | ||
} | ||
|
||
const user = await getUserFromCognitoUsername(cognitoUsername); | ||
|
||
if (!user) { | ||
console.log( | ||
`User: ${cognitoUsername} not present on Cognito, sync done.` | ||
); | ||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({ | ||
message: 'User not present on Cognito, sync done.', | ||
}), | ||
}; | ||
} | ||
|
||
const userWebinarsSubscriptions = await getSubscribedWebinars( | ||
cognitoUsername | ||
); | ||
|
||
const webinarIds = JSON.parse(userWebinarsSubscriptions.body) | ||
.map( | ||
(webinar: { readonly webinarId: { readonly S: string } }) => | ||
webinar?.webinarId?.S | ||
) | ||
.filter(Boolean); | ||
|
||
const res = await addContact(user); | ||
if (res.statusCode !== 200) { | ||
// eslint-disable-next-line functional/no-throw-statements | ||
throw new Error('Error adding contact'); | ||
} | ||
|
||
await webinarIds.reduce( | ||
async ( | ||
prevPromise: Promise<APIGatewayProxyResult>, | ||
webinarId: string | ||
) => { | ||
await prevPromise; | ||
try { | ||
const result = await addContactToList(cognitoUsername, webinarId); | ||
console.log('Add contact to list result:', result, webinarId); // TODO: Remove after testing | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); // wait 1 sec to avoid rate limiting | ||
} catch (e) { | ||
console.error('Error adding contact to list', e); // TODO: Remove after testing | ||
} | ||
}, | ||
Promise.resolve() | ||
); | ||
|
||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({ message: 'User resynced' }), | ||
}; | ||
} catch (error) { | ||
return { | ||
statusCode: 500, | ||
body: JSON.stringify({ message: error }), | ||
}; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/active-campaign-client/src/helpers/getSubscribedWebinars.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 getSubscribedWebinars( | ||
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); | ||
return { | ||
statusCode: 200, | ||
body: JSON.stringify(response.Items), | ||
}; | ||
} catch (error) { | ||
console.error('Error querying items by username:', error); | ||
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); | ||
} |
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.
I think you should remove the TODOs